Gentoo Archives: gentoo-catalyst

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