Gentoo Archives: gentoo-catalyst

From: Mike Frysinger <vapier@g.o>
To: gentoo-catalyst@l.g.o
Subject: [gentoo-catalyst] [PATCH 3/3] main: convert to new logging module
Date: Fri, 09 Oct 2015 05:57:40
Message-Id: 1444370248-13159-3-git-send-email-vapier@gentoo.org
In Reply to: [gentoo-catalyst] [PATCH 1/3] main: group related command line flags by Mike Frysinger
1 ---
2 catalyst/main.py | 104 +++++++++++++++++++++++--------------------------------
3 1 file changed, 43 insertions(+), 61 deletions(-)
4
5 diff --git a/catalyst/main.py b/catalyst/main.py
6 index c9a2219..bc77c59 100644
7 --- a/catalyst/main.py
8 +++ b/catalyst/main.py
9 @@ -7,6 +7,7 @@
10 # $Id$
11
12 import argparse
13 +import datetime
14 import os
15 import sys
16
17 @@ -31,36 +32,36 @@ conf_values={}
18
19
20 def version():
21 - print get_version()
22 - print "Copyright 2003-2008 Gentoo Foundation"
23 - print "Copyright 2008-2012 various authors"
24 - print "Distributed under the GNU General Public License version 2.1\n"
25 + log.info(get_version())
26 + log.info('Copyright 2003-%s Gentoo Foundation', datetime.datetime.now().year)
27 + log.info('Copyright 2008-2012 various authors')
28 + log.info('Distributed under the GNU General Public License version 2.1')
29
30 def parse_config(myconfig):
31 # search a couple of different areas for the main config file
32 myconf={}
33 config_file=""
34 + default_config_file = '/etc/catalyst/catalyst.conf'
35
36 # first, try the one passed (presumably from the cmdline)
37 if myconfig:
38 if os.path.exists(myconfig):
39 - print "Using command line specified Catalyst configuration file, "+myconfig
40 + log.notice('Using command line specified Catalyst configuration file: %s',
41 + myconfig)
42 config_file=myconfig
43
44 else:
45 - print "!!! catalyst: Could not use specified configuration file "+\
46 - myconfig
47 - sys.exit(1)
48 + log.critical('Specified configuration file does not exist: %s', myconfig)
49
50 # next, try the default location
51 - elif os.path.exists("/etc/catalyst/catalyst.conf"):
52 - print "Using default Catalyst configuration file, /etc/catalyst/catalyst.conf"
53 - config_file="/etc/catalyst/catalyst.conf"
54 + elif os.path.exists(default_config_file):
55 + log.notice('Using default Catalyst configuration file: %s',
56 + default_config_file)
57 + config_file = default_config_file
58
59 # can't find a config file (we are screwed), so bail out
60 else:
61 - print "!!! catalyst: Could not find a suitable configuration file"
62 - sys.exit(1)
63 + log.critical('Could not find a suitable configuration file')
64
65 # now, try and parse the config file "config_file"
66 try:
67 @@ -69,8 +70,7 @@ def parse_config(myconfig):
68 myconf.update(myconfig.get_values())
69
70 except Exception:
71 - print "!!! catalyst: Unable to parse configuration file, "+myconfig
72 - sys.exit(1)
73 + log.critical('Could not find parse configuration file: %s', myconfig)
74
75 # now, load up the values into conf_values so that we can use them
76 for x in list(confdefaults):
77 @@ -90,7 +90,7 @@ def parse_config(myconfig):
78 # print out any options messages
79 for opt in conf_values['options']:
80 if opt in option_messages:
81 - print option_messages[opt]
82 + log.info(option_messages[opt])
83
84 for key in ["digests", "envscript", "var_tmpfs_portage", "port_logdir",
85 "local_overlay"]:
86 @@ -102,7 +102,7 @@ def parse_config(myconfig):
87 conf_values["contents"] = myconf["contents"].replace("-", '_')
88
89 if "envscript" in myconf:
90 - print "Envscript support enabled."
91 + log.info('Envscript support enabled.')
92
93 # take care of any variable substitutions that may be left
94 for x in list(conf_values):
95 @@ -118,11 +118,8 @@ def import_module(target):
96 try:
97 mod_name = "catalyst.targets." + target
98 module = __import__(mod_name, [],[], ["not empty"])
99 - except ImportError as e:
100 - print "!!! catalyst: Python module import error: %s " % target + \
101 - "in catalyst/targets/ ... exiting."
102 - print "ERROR was: ", e
103 - sys.exit(1)
104 + except ImportError:
105 + log.critical('Python module import error: %s', target, exc_info=True)
106 return module
107
108
109 @@ -278,7 +275,7 @@ def main():
110 parse_config(myconfig)
111
112 conf_values["options"].update(options)
113 - #print "MAIN: conf_values['options'] =", conf_values["options"]
114 + log.debug('conf_values[options] = %s', conf_values['options'])
115
116 # initialize our contents generator
117 contents_map = ContentsMap(CONTENTS_DEFINITIONS)
118 @@ -308,14 +305,13 @@ def main():
119
120 # First validate all the requested digests are valid keys.
121 if digests - valid_digests:
122 - print
123 - print "These are not a valid digest entries:"
124 - print ', '.join(digests - valid_digests)
125 - print "Valid digest entries:"
126 - print ', '.join(sorted(valid_digests))
127 - print
128 - print "Catalyst aborting...."
129 - sys.exit(2)
130 + log.critical(
131 + 'These are not valid digest entries:\n'
132 + '%s\n'
133 + 'Valid digest entries:\n'
134 + '%s',
135 + ', '.join(digests - valid_digests),
136 + ', '.join(sorted(valid_digests)))
137
138 # Then check for any programs that the hash func requires.
139 for digest in digests:
140 @@ -326,37 +322,28 @@ def main():
141 if skip_missing:
142 digests.remove(digest)
143 continue
144 - print
145 - print "digest=" + digest
146 - print "\tThe " + hash_map.hash_map[digest].cmd + \
147 - " binary was not found. It needs to be in your system path"
148 - print
149 - print "Catalyst aborting...."
150 - sys.exit(2)
151 + log.critical(
152 + 'The "%s" binary needed by digest "%s" was not found. '
153 + 'It needs to be in your system path.',
154 + hash_map.hash_map[digest].cmd, digest)
155
156 # Now reload the config with our updated value.
157 conf_values['digests'] = ' '.join(digests)
158
159 if "hash_function" in conf_values:
160 if conf_values["hash_function"] not in HASH_DEFINITIONS:
161 - print
162 - print conf_values["hash_function"]+\
163 - " is not a valid hash_function entry"
164 - print "Valid hash_function entries:"
165 - print HASH_DEFINITIONS.keys()
166 - print
167 - print "Catalyst aborting...."
168 - sys.exit(2)
169 + log.critical(
170 + '%s is not a valid hash_function entry\n'
171 + 'Valid hash_function entries:\n'
172 + '%s', HASH_DEFINITIONS.keys())
173 try:
174 process.find_binary(hash_map.hash_map[conf_values["hash_function"]].cmd)
175 except process.CommandNotFound:
176 - print
177 - print "hash_function="+conf_values["hash_function"]
178 - print "\tThe "+hash_map.hash_map[conf_values["hash_function"]].cmd + \
179 - " binary was not found. It needs to be in your system path"
180 - print
181 - print "Catalyst aborting...."
182 - sys.exit(2)
183 + log.critical(
184 + 'The "%s" binary needed by hash_function "%s" was not found. '
185 + 'It needs to be in your system path.',
186 + hash_map.hash_map[conf_values['hash_function']].cmd,
187 + conf_values['hash_function'])
188
189 addlargs={}
190
191 @@ -370,25 +357,20 @@ def main():
192 cmdline.parse_lines(mycmdline)
193 addlargs.update(cmdline.get_values())
194 except CatalystError:
195 - print "!!! catalyst: Could not parse commandline, exiting."
196 - sys.exit(1)
197 + log.critical('Could not parse commandline')
198
199 if "target" not in addlargs:
200 raise CatalystError("Required value \"target\" not specified.")
201
202 if os.getuid() != 0:
203 # catalyst cannot be run as a normal user due to chroots, mounts, etc
204 - print "!!! catalyst: This script requires root privileges to operate"
205 - sys.exit(2)
206 + log.critical('This script requires root privileges to operate')
207
208 # everything is setup, so the build is a go
209 try:
210 success = build_target(addlargs)
211 except KeyboardInterrupt:
212 - print "\nCatalyst build aborted due to user interrupt ( Ctrl-C )"
213 - print
214 - print "Catalyst aborting...."
215 - sys.exit(2)
216 + log.critical('Catalyst build aborted due to user interrupt (Ctrl-C)')
217 if not success:
218 sys.exit(2)
219 sys.exit(0)
220 --
221 2.5.2

Replies