Merge lp:~milo/linaro-image-tools/bug1059579 into lp:linaro-image-tools/11.11

Proposed by Milo Casagrande
Status: Merged
Merged at revision: 585
Proposed branch: lp:~milo/linaro-image-tools/bug1059579
Merge into: lp:linaro-image-tools/11.11
Diff against target: 507 lines (+82/-104)
10 files modified
linaro-hwpack-convert (+4/-22)
linaro-hwpack-create (+8/-18)
linaro-hwpack-replace (+4/-17)
linaro-media-create (+17/-22)
linaro_image_tools/hwpack/__init__.py (+2/-1)
linaro_image_tools/hwpack/hwpack_convert.py (+1/-1)
linaro_image_tools/media_create/__init__.py (+1/-0)
linaro_image_tools/media_create/boards.py (+2/-12)
linaro_image_tools/media_create/partitions.py (+2/-2)
linaro_image_tools/utils.py (+41/-9)
To merge this branch: bzr merge lp:~milo/linaro-image-tools/bug1059579
Reviewer Review Type Date Requested Status
Данило Шеган (community) Approve
Fathi Boudra Pending
Review via email: mp+130736@code.launchpad.net

Description of the change

Re-using the same branch: we reapply the logger refactoring, and fix the regression found - it was missing the global declaration when initializing the variable.

To post a comment you must log in.
Revision history for this message
Данило Шеган (danilo) wrote :

Based on the fact that this was already reviewed, I'll just review the simple change in 586.

Is there any simple way you can add a test that exercises this code path? If not, land as-is, though I'd very much prefer a test.

review: Approve
Revision history for this message
Milo Casagrande (milo) wrote :

On Wed, Oct 24, 2012 at 1:46 PM, Данило Шеган <email address hidden> wrote:
> Review: Approve
>
> Based on the fact that this was already reviewed, I'll just review the simple change in 586.
>
> Is there any simple way you can add a test that exercises this code path? If not, land as-is, though I'd very much prefer a test.

I looked into that, but it is not that easy. It should be possible to
use the already mocking infrastructure to fake hwpack and deb files in
this case, but the problem is that all this code is inside a user
script (it is linaro-hwpack-replace), and we cannot really import
anything from it to test it out and use it as a module. It would
really call to refactor all the code of that script (something that
would be cool to do).

--
Milo Casagrande
Infrastructure Engineer
Linaro.org <www.linaro.org> │ Open source software for ARM SoCs

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'linaro-hwpack-convert'
2--- linaro-hwpack-convert 2012-10-20 06:17:46 +0000
3+++ linaro-hwpack-convert 2012-10-22 07:04:22 +0000
4@@ -22,35 +22,17 @@
5
6
7 import argparse
8-import logging
9 import sys
10-import os
11
12 from linaro_image_tools.hwpack.hwpack_convert import (
13 HwpackConverter,
14 HwpackConverterException,
15 check_and_validate_args,
16 )
17+from linaro_image_tools.utils import get_logger
18 from linaro_image_tools.__version__ import __version__
19
20
21-def get_logger(debug=False):
22- ch = logging.StreamHandler()
23- logger = logging.getLogger("linaro_hwpack_converter")
24-
25- if debug:
26- ch.setLevel(logging.DEBUG)
27- formatter = logging.Formatter(
28- "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
29- ch.setFormatter(formatter)
30- logger.setLevel(logging.DEBUG)
31- else:
32- ch.setLevel(logging.INFO)
33- formatter = logging.Formatter("%(message)s")
34- ch.setFormatter(formatter)
35- logger.setLevel(logging.INFO)
36- logger.addHandler(ch)
37-
38 if __name__ == '__main__':
39 parser = argparse.ArgumentParser(version='%(prog)s ' + __version__)
40 parser.add_argument("CONFIG_FILE",
41@@ -64,10 +46,10 @@
42 logger = get_logger(debug=args.debug)
43 try:
44 input_file, output_file = check_and_validate_args(args)
45- print "Converting '%s' into new YAML format..." % (input_file)
46+ logger.info("Converting '%s' into new YAML format..." % input_file)
47 converter = HwpackConverter(input_file, output_file)
48 except HwpackConverterException, e:
49- sys.stderr.write(str(e) + "\n")
50+ logger.error(str(e))
51 sys.exit(1)
52 converter.convert()
53- print "File '%s' converted in '%s'." % (input_file, output_file)
54+ logger.info("File '%s' converted in '%s'." % (input_file, output_file))
55
56=== modified file 'linaro-hwpack-create'
57--- linaro-hwpack-create 2012-10-20 06:17:46 +0000
58+++ linaro-hwpack-create 2012-10-22 07:04:22 +0000
59@@ -21,14 +21,14 @@
60 # USA.
61
62 import argparse
63-import logging
64 import sys
65
66 from linaro_image_tools.hwpack.builder import (
67 ConfigFileMissing, HardwarePackBuilder)
68-
69+from linaro_image_tools.utils import get_logger
70 from linaro_image_tools.__version__ import __version__
71
72+
73 if __name__ == '__main__':
74 parser = argparse.ArgumentParser(version='%(prog)s ' + __version__)
75 parser.add_argument(
76@@ -44,24 +44,14 @@
77 "version than a package that would be otherwise installed. "
78 "Can be used more than once."))
79 parser.add_argument("--debug", action="store_true")
80+
81 args = parser.parse_args()
82- ch = logging.StreamHandler()
83- ch.setLevel(logging.INFO)
84- formatter = logging.Formatter("%(message)s")
85- ch.setFormatter(formatter)
86- logger = logging.getLogger("linaro_image_tools")
87- logger.setLevel(logging.INFO)
88- logger.addHandler(ch)
89- if args.debug:
90- ch.setLevel(logging.DEBUG)
91- formatter = logging.Formatter(
92- "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
93- ch.setFormatter(formatter)
94- logger.setLevel(logging.DEBUG)
95+ logger = get_logger(debug=args.debug)
96+
97 try:
98- builder = HardwarePackBuilder(
99- args.CONFIG_FILE, args.VERSION, args.local_debs)
100+ builder = HardwarePackBuilder(args.CONFIG_FILE,
101+ args.VERSION, args.local_debs)
102 except ConfigFileMissing, e:
103- sys.stderr.write(str(e) + "\n")
104+ logger.error(str(e))
105 sys.exit(1)
106 builder.build()
107
108=== modified file 'linaro-hwpack-replace'
109--- linaro-hwpack-replace 2012-10-20 06:17:46 +0000
110+++ linaro-hwpack-replace 2012-10-22 07:04:22 +0000
111@@ -26,7 +26,6 @@
112 import sys
113 import shutil
114 import glob
115-import logging
116 import tarfile
117 import tempfile
118 import argparse
119@@ -35,6 +34,7 @@
120 from debian.deb822 import Packages
121 from linaro_image_tools.hwpack.packages import get_packages_file
122 from linaro_image_tools.hwpack.packages import FetchedPackage
123+from linaro_image_tools.utils import get_logger
124
125
126 parser = argparse.ArgumentParser()
127@@ -53,7 +53,7 @@
128 parser.add_argument("-d", "--debug-output", action="store_true", dest="debug",
129 help="Verbose messages are displayed when specified")
130
131-logger = logging.getLogger("linaro-hwpack-replace")
132+logger = None
133
134
135 class DummyStanza(object):
136@@ -65,20 +65,6 @@
137 fd.write(get_packages_file([self.info]))
138
139
140-def set_logging_param(args):
141- ch = logging.StreamHandler()
142- ch.setLevel(logging.INFO)
143- formatter = logging.Formatter("%(message)s")
144- ch.setFormatter(formatter)
145- logger.setLevel(logging.INFO)
146- logger.addHandler(ch)
147- if args.debug:
148- ch.setLevel(logging.DEBUG)
149- formatter = logging.Formatter(
150- "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
151- ch.setFormatter(formatter)
152- logger.setLevel(logging.DEBUG)
153-
154
155 def get_hwpack_name(old_hwpack, build_number):
156 # The build_number would be the job build number.
157@@ -178,7 +164,8 @@
158 "and the debian package information\n")
159 return 1
160
161- set_logging_param(args)
162+ global logger
163+ logger = get_logger(debug=args.debug)
164
165 old_hwpack = args.hwpack_name
166 new_deb_file_to_copy = args.deb_pack
167
168=== modified file 'linaro-media-create'
169--- linaro-media-create 2012-10-20 06:17:46 +0000
170+++ linaro-media-create 2012-10-22 07:04:22 +0000
171@@ -22,7 +22,6 @@
172 import os
173 import sys
174 import tempfile
175-import logging
176
177 from linaro_image_tools import cmd_runner
178
179@@ -57,6 +56,7 @@
180 MissingRequiredOption,
181 path_in_tarfile_exists,
182 prep_media_path,
183+ get_logger,
184 )
185
186 # Just define the global variables
187@@ -106,35 +106,29 @@
188 parser = get_args_parser()
189 args = parser.parse_args()
190
191- ch = logging.StreamHandler()
192- ch.setLevel(logging.INFO)
193- formatter = logging.Formatter("%(message)s")
194- ch.setFormatter(formatter)
195- logger = logging.getLogger("linaro_image_tools")
196- logger.setLevel(logging.INFO)
197- logger.addHandler(ch)
198+ logger = get_logger(debug=args.debug)
199
200 try:
201 additional_option_checks(args)
202 except IncompatibleOptions as e:
203 parser.print_help()
204- print >> sys.stderr, "\nError:", e.value
205+ logger.error(e.value)
206 sys.exit(1)
207
208 if args.readhwpack:
209 try:
210 reader = HwpackReader(args.hwpacks)
211- print reader.get_supported_boards()
212+ logger.info(reader.get_supported_boards())
213 sys.exit(0)
214 except HwpackReaderError as e:
215- print >> sys.stderr, "\nError:", e.value
216+ logger.error(e.value)
217 sys.exit(1)
218
219 try:
220 check_required_args(args)
221 except MissingRequiredOption as e:
222 parser.print_help()
223- print >> sys.stderr, "\nError:", e.value
224+ logger.error(e.value)
225 sys.exit(1)
226
227 board_config = board_configs[args.dev]
228@@ -147,16 +141,16 @@
229
230 if media.is_block_device:
231 if not board_config.supports_writing_to_mmc:
232- print ("The board '%s' does not support the --mmc option. "
233- "Please use --image_file to create an image file for this "
234- "board." % args.dev)
235+ logger.error("The board '%s' does not support the --mmc option. "
236+ "Please use --image_file to create an image file for "
237+ "this board." % args.dev)
238 sys.exit(1)
239 if not confirm_device_selection_and_ensure_it_is_ready(
240 args.device, args.nocheck_mmc):
241 sys.exit(1)
242 elif not args.should_format_rootfs or not args.should_format_bootfs:
243- print ("Do not use --no-boot or --no-part in conjunction with "
244- "--image_file.")
245+ logger.error("Do not use --no-boot or --no-part in conjunction with "
246+ "--image_file.")
247 sys.exit(1)
248 else:
249 # All good, move on.
250@@ -170,6 +164,7 @@
251 BIN_DIR = os.path.join(TMP_DIR, 'rootfs')
252 os.mkdir(BIN_DIR)
253
254+ logger.info('Searching correct rootfs path')
255 # Identify the correct path for the rootfs
256 filesystem_dir = ''
257 if path_in_tarfile_exists('binary/etc', args.binary):
258@@ -212,12 +207,12 @@
259
260 if args.rootfs == 'btrfs':
261 if not extract_kpkgs:
262- print ("Desired rootfs type is 'btrfs', trying to auto-install "
263- "the 'btrfs-tools' package")
264+ logger.info("Desired rootfs type is 'btrfs', trying to "
265+ "auto-install the 'btrfs-tools' package")
266 install_packages(ROOTFS_DIR, TMP_DIR, "btrfs-tools")
267 else:
268- print ("Desired rootfs type is 'btrfs', please make sure the "
269- "rootfs also includes 'btrfs-tools'")
270+ logger.info("Desired rootfs type is 'btrfs', please make sure the "
271+ "rootfs also includes 'btrfs-tools'")
272
273 boot_partition, root_partition = setup_partitions(
274 board_config, media, args.image_size, args.boot_label, args.rfs_label,
275@@ -248,4 +243,4 @@
276 board_config.mmc_device_id, board_config.mmc_part_offset,
277 board_config)
278
279- print "Done creating Linaro image on %s" % media.path
280+ logger.info("Done creating Linaro image on %s" % media.path)
281
282=== modified file 'linaro_image_tools/hwpack/__init__.py'
283--- linaro_image_tools/hwpack/__init__.py 2012-10-20 06:17:46 +0000
284+++ linaro_image_tools/hwpack/__init__.py 2012-10-22 07:04:22 +0000
285@@ -20,6 +20,7 @@
286 # USA.
287
288 import logging
289+from linaro_image_tools.utils import DEFAULT_LOGGER_NAME
290
291
292 class NullHandler(logging.Handler):
293@@ -28,4 +29,4 @@
294
295
296 h = NullHandler()
297-logging.getLogger(__name__).addHandler(h)
298+logging.getLogger(DEFAULT_LOGGER_NAME).addHandler(h)
299
300=== modified file 'linaro_image_tools/hwpack/hwpack_convert.py'
301--- linaro_image_tools/hwpack/hwpack_convert.py 2012-10-20 06:17:46 +0000
302+++ linaro_image_tools/hwpack/hwpack_convert.py 2012-10-22 07:04:22 +0000
303@@ -85,7 +85,7 @@
304 # The default name used for renaming dtb file
305 DEFAULT_DTB_NAME = 'board.dtb'
306
307-logger = logging.getLogger("linaro_hwpack_converter")
308+logger = logging.getLogger(__name__)
309
310
311 class HwpackConverterException(Exception):
312
313=== modified file 'linaro_image_tools/media_create/__init__.py'
314--- linaro_image_tools/media_create/__init__.py 2012-10-20 06:17:46 +0000
315+++ linaro_image_tools/media_create/__init__.py 2012-10-22 07:04:22 +0000
316@@ -173,6 +173,7 @@
317 help="Select a bootloader from a hardware pack that contains more "
318 "than one. If not specified, it will default to '%s'." %
319 DEFAULT_BOOTLOADER)
320+ parser.add_argument("--debug", action="store_true")
321
322 add_common_options(parser)
323 return parser
324
325=== modified file 'linaro_image_tools/media_create/boards.py'
326--- linaro_image_tools/media_create/boards.py 2012-10-20 06:17:46 +0000
327+++ linaro_image_tools/media_create/boards.py 2012-10-22 07:04:22 +0000
328@@ -47,6 +47,8 @@
329 partition_mounted, SECTOR_SIZE, register_loopback)
330 from StringIO import StringIO
331
332+logger = logging.getLogger(__name__)
333+
334 KERNEL_GLOB = 'vmlinuz-*-%(kernel_flavor)s'
335 INITRD_GLOB = 'initrd.img-*-%(kernel_flavor)s'
336 DTB_GLOB = 'dt-*-%(kernel_flavor)s/%(dtb_name)s'
337@@ -457,14 +459,6 @@
338
339 hardwarepack_handler = None
340
341- @staticmethod
342- def _get_logger():
343- """
344- Gets the logger instance.
345- :return: The logger instance
346- """
347- return logging.getLogger('linaro_image_tools')
348-
349 @classmethod
350 def get_metadata_field(cls, field_name):
351 """ Return the metadata value for field_name if it can be found.
352@@ -877,7 +871,6 @@
353 :param dest_dir: The directory where to copy each dtb file.
354 :param search_dir: The directory where to search for the real file.
355 """
356- logger = logging.getLogger("linaro_image_tools")
357 logger.info("Copying dtb files")
358 for dtb_file in dtb_files:
359 if dtb_file:
360@@ -922,7 +915,6 @@
361 if max_size is not None:
362 assert os.path.getsize(from_file) <= max_size, (
363 "'%s' is larger than %s" % (from_file, max_size))
364- logger = logging.getLogger("linaro_image_tools")
365 logger.info("Writing '%s' to '%s' at %s." % (from_file, to_file, seek))
366 _dd(from_file, to_file, seek=seek)
367
368@@ -945,7 +937,6 @@
369 if cls.spl_in_boot_part:
370 assert spl_file is not None, (
371 "SPL binary could not be found")
372- logger = logging.getLogger("linaro_image_tools")
373 logger.info(
374 "Copying spl '%s' to boot partition." % spl_file)
375 cmd_runner.run(["cp", "-v", spl_file, boot_dir],
376@@ -1105,7 +1096,6 @@
377 @classmethod
378 def _get_kflavor_files_v2(cls, path):
379 kernel = initrd = dtb = None
380- logger = logging.getLogger("linaro_image_tools")
381
382 if cls.vmlinuz:
383 kernel = _get_file_matching(os.path.join(path, cls.vmlinuz))
384
385=== modified file 'linaro_image_tools/media_create/partitions.py'
386--- linaro_image_tools/media_create/partitions.py 2012-10-20 06:17:46 +0000
387+++ linaro_image_tools/media_create/partitions.py 2012-10-22 07:04:22 +0000
388@@ -36,6 +36,8 @@
389
390 from linaro_image_tools import cmd_runner
391
392+logger = logging.getLogger(__name__)
393+
394 HEADS = 128
395 SECTORS = 32
396 SECTOR_SIZE = 512 # bytes
397@@ -205,7 +207,6 @@
398 try:
399 umount(path)
400 except cmd_runner.SubcommandNonZeroReturnValue, e:
401- logger = logging.getLogger("linaro_image_tools")
402 logger.warn("Failed to umount %s, but ignoring it because of a "
403 "previous error" % path)
404 logger.warn(e)
405@@ -586,7 +587,6 @@
406
407 :param media: A setup_partitions.Media object to partition.
408 """
409- logger = logging.getLogger("linaro_image_tools")
410 tts = 1
411 while (tts > 0) and (tts <= MAX_TTS):
412 try:
413
414=== modified file 'linaro_image_tools/utils.py'
415--- linaro_image_tools/utils.py 2012-10-20 06:17:46 +0000
416+++ linaro_image_tools/utils.py 2012-10-22 07:04:22 +0000
417@@ -28,6 +28,8 @@
418
419 from linaro_image_tools import cmd_runner
420
421+DEFAULT_LOGGER_NAME = 'linaro_image_tools'
422+
423
424 # try_import was copied from python-testtools 0.9.12 and was originally
425 # licensed under a MIT-style license but relicensed under the GPL in Linaro
426@@ -76,13 +78,15 @@
427
428
429 def path_in_tarfile_exists(path, tar_file):
430- tarinfo = tarfile.open(tar_file, 'r:gz')
431+ exists = True
432 try:
433+ tarinfo = tarfile.open(tar_file, 'r:gz')
434 tarinfo.getmember(path)
435- return True
436+ tarinfo.close()
437 except KeyError:
438- return False
439- tarinfo.close()
440+ exists = False
441+ finally:
442+ return exists
443
444
445 def verify_file_integrity(sig_file_list):
446@@ -145,24 +149,24 @@
447
448 # Check the outputs from verify_file_integrity
449 # Abort if anything fails.
450+ logger = logging.getLogger(__name__)
451 if len(sig_file_list):
452 if not gpg_sig_pass:
453- logging.error("GPG signature verification failed.")
454+ logger.error("GPG signature verification failed.")
455 return False, []
456
457 if not os.path.basename(binary) in verified_files:
458- logging.error("OS Binary verification failed")
459+ logger.error("OS Binary verification failed")
460 return False, []
461
462 for hwpack in hwpacks:
463 if not os.path.basename(hwpack) in verified_files:
464- logging.error("Hwpack {0} verification failed".format(hwpack))
465+ logger.error("Hwpack {0} verification failed".format(hwpack))
466 return False, []
467
468 for verified_file in verified_files:
469- logging.info('Hash verification of file {0} OK.'.format(
470+ logger.info('Hash verification of file {0} OK.'.format(
471 verified_file))
472-
473 return True, verified_files
474
475
476@@ -348,3 +352,31 @@
477 raise MissingRequiredOption("--dev option is required")
478 if args.binary is None:
479 raise MissingRequiredOption("--binary option is required")
480+
481+
482+def get_logger(name=DEFAULT_LOGGER_NAME, debug=False):
483+ """
484+ Retrieves a named logger. Default name is set in the variable
485+ DEFAULT_LOG_NAME. Debug is set to False by default.
486+
487+ :param name: The name of the logger.
488+ :param debug: If debug level should be turned on
489+ :return: A logger instance.
490+ """
491+ logger = logging.getLogger(name)
492+ ch = logging.StreamHandler()
493+
494+ if debug:
495+ ch.setLevel(logging.DEBUG)
496+ formatter = logging.Formatter(
497+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
498+ ch.setFormatter(formatter)
499+ logger.setLevel(logging.DEBUG)
500+ else:
501+ ch.setLevel(logging.INFO)
502+ formatter = logging.Formatter("%(message)s")
503+ ch.setFormatter(formatter)
504+ logger.setLevel(logging.INFO)
505+
506+ logger.addHandler(ch)
507+ return logger

Subscribers

People subscribed via source and target branches