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

Proposed by Milo Casagrande
Status: Merged
Approved by: James Tunnicliffe
Approved revision: 544
Merged at revision: 546
Proposed branch: lp:~milo/linaro-image-tools/hwpack-read
Merge into: lp:linaro-image-tools/11.11
Diff against target: 459 lines (+360/-9)
6 files modified
linaro-media-create (+30/-7)
linaro_image_tools/hwpack/hwpack_reader.py (+192/-0)
linaro_image_tools/hwpack/tests/__init__.py (+1/-0)
linaro_image_tools/hwpack/tests/test_hwpack_reader.py (+114/-0)
linaro_image_tools/media_create/__init__.py (+6/-2)
linaro_image_tools/utils.py (+17/-0)
To merge this branch: bzr merge lp:~milo/linaro-image-tools/hwpack-read
Reviewer Review Type Date Requested Status
James Tunnicliffe (community) Approve
Linaro Infrastructure Pending
Review via email: mp+116606@code.launchpad.net

Description of the change

With this merge, there is the initial support for hwpack metadata reading, before creating an image with l-m-c.

What has been added here is:
- A new class for reading hwpack, that makes use of the HwpackHandler to open the tarball files
- New tests for the class
- New command line option --read-hwpack that reads the hwpack metadata and prints out information about the supported boards and bootlaoders

I also fixed a couple of PEP8 issues.
Notes:
- There is one test that will fail, and is related to pyflakes, looks like it is bug 812958 (I used getter and setter)
- Some of the required command line options have been set to False, since if we want to just read the hwpack and see what is supported, we should not require all the information. Since there is still work in progress on this, the bits handling the necessary command line options is still missing.

To post a comment you must log in.
542. By Milo Casagrande

Added option check.

543. By Milo Casagrande

Fixed PEP8 warning.

544. By Milo Casagrande

Fixed tests and imports.

Revision history for this message
James Tunnicliffe (dooferlad) wrote :

Looks fine now. Thanks for the changes.

review: Approve
545. By Milo Casagrande

Fixed annoying pyflakes warnings.

546. By Milo Casagrande

Fixed assertEqual tests.

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== modified file 'linaro-media-create'
--- linaro-media-create 2012-04-19 01:12:10 +0000
+++ linaro-media-create 2012-07-25 13:08:18 +0000
@@ -33,6 +33,10 @@
33 install_hwpacks,33 install_hwpacks,
34 install_packages,34 install_packages,
35 )35 )
36from linaro_image_tools.hwpack.hwpack_reader import (
37 HwpackReader,
38 HwpackReaderError,
39 )
36from linaro_image_tools.media_create.partitions import (40from linaro_image_tools.media_create.partitions import (
37 Media,41 Media,
38 setup_partitions,42 setup_partitions,
@@ -44,13 +48,15 @@
44 )48 )
45from linaro_image_tools.media_create import get_args_parser49from linaro_image_tools.media_create import get_args_parser
46from linaro_image_tools.utils import (50from linaro_image_tools.utils import (
51 additional_option_checks,
52 check_file_integrity_and_log_errors,
53 check_required_args,
47 ensure_command,54 ensure_command,
55 IncompatibleOptions,
48 is_arm_host,56 is_arm_host,
49 check_file_integrity_and_log_errors,57 MissingRequiredOption,
50 path_in_tarfile_exists,58 path_in_tarfile_exists,
51 IncompatibleOptions,
52 prep_media_path,59 prep_media_path,
53 additional_option_checks,
54 )60 )
5561
56# Just define the global variables62# Just define the global variables
@@ -107,6 +113,22 @@
107 print >> sys.stderr, "\nError:", e.value113 print >> sys.stderr, "\nError:", e.value
108 sys.exit(1)114 sys.exit(1)
109115
116 if args.readhwpack:
117 try:
118 reader = HwpackReader(args.hwpacks)
119 print reader.get_supported_boards()
120 sys.exit(0)
121 except HwpackReaderError as e:
122 print >> sys.stderr, "\nError:", e.value
123 sys.exit(1)
124
125 try:
126 check_required_args(args)
127 except MissingRequiredOption as e:
128 parser.print_help()
129 print >> sys.stderr, "\nError:", e.value
130 sys.exit(1)
131
110 board_config = board_configs[args.board]132 board_config = board_configs[args.board]
111 board_config.set_metadata(args.hwpacks)133 board_config.set_metadata(args.hwpacks)
112 board_config.set_board(args.board)134 board_config.set_board(args.board)
@@ -117,11 +139,12 @@
117139
118 if media.is_block_device:140 if media.is_block_device:
119 if not board_config.supports_writing_to_mmc:141 if not board_config.supports_writing_to_mmc:
120 print "The board '%s' does not support the --mmc option. Please use "\142 print ("The board '%s' does not support the --mmc option. "
121 "--image_file to create an image file for this board." % args.board143 "Please use --image_file to create an image file for this "
144 "board." % args.board)
122 sys.exit(1)145 sys.exit(1)
123 if not confirm_device_selection_and_ensure_it_is_ready(args.device,146 if not confirm_device_selection_and_ensure_it_is_ready(
124 args.nocheck_mmc):147 args.device, args.nocheck_mmc):
125 sys.exit(1)148 sys.exit(1)
126 elif not args.should_format_rootfs or not args.should_format_bootfs:149 elif not args.should_format_rootfs or not args.should_format_bootfs:
127 print ("Do not use --no-boot or --no-part in conjunction with "150 print ("Do not use --no-boot or --no-part in conjunction with "
128151
=== added file 'linaro_image_tools/hwpack/hwpack_reader.py'
--- linaro_image_tools/hwpack/hwpack_reader.py 1970-01-01 00:00:00 +0000
+++ linaro_image_tools/hwpack/hwpack_reader.py 2012-07-25 13:08:18 +0000
@@ -0,0 +1,192 @@
1# Copyright (C) 2012 Linaro
2#
3# Author: Milo Casagrande <milo.casagrande@linaro.org>
4#
5# This file is part of Linaro Image Tools.
6#
7# Linaro Image Tools is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Linaro Image Tools is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Linaro Image Tools. If not, see <http://www.gnu.org/licenses/>.
19
20from linaro_image_tools.media_create.boards import HardwarepackHandler
21from linaro_image_tools.hwpack.hwpack_fields import (
22 FORMAT_FIELD,
23 NAME_FIELD,
24 BOARDS_FIELD,
25 BOOTLOADERS_FIELD,
26)
27
28from os import linesep as LINE_SEP
29
30# Fields necessary for the string representation of the hardware pack supported
31# boards and bootlaoders.
32HALF_SEPARATOR = '+--------------------------------------'
33ENDING = '+'
34SEPARATOR = HALF_SEPARATOR * 2 + ENDING
35FORMAT = '{:<80}'
36CENTER_ALIGN = '{:^80}'
37ELEMENT_FORMAT = '{:<39}| {:<39}'
38
39
40class HwpackReaderError(Exception):
41 """General error raised by HwpackReader."""
42 def __init__(self, value):
43 self.value = value
44
45 def __str__(self):
46 return repr(self.value)
47
48
49class Hwpack(object):
50 """A simple representation of an hardware pack and its value."""
51 def __init__(self):
52 self._hwpack = None
53 self._boards = None
54 self._bootloaders = None
55 self._name = None
56
57 @property
58 def hwpack(self):
59 """The hardware pack it refers to."""
60 return self._hwpack
61
62 def sethwpack(self, value):
63 """Sets the hwpack field."""
64 self._hwpack = value
65
66 @property
67 def boards(self):
68 """The boards field of this hardware pack."""
69 return self._boards
70
71 def setboards(self, value):
72 """Sets the boards field."""
73 self._boards = value
74
75 @property
76 def name(self):
77 """The name field of this hardware pack."""
78 return self._name
79
80 def setname(self, value):
81 """Sets the name field."""
82 self._name = value
83
84 @property
85 def bootloaders(self):
86 """The bootlaoders field of this hardware pack."""
87 return self._bootloaders
88
89 def setbootloaders(self, value):
90 """Sets the bootlaoders field."""
91 self._bootloaders = value
92
93 def __eq__(self, other):
94 """Equality method."""
95 equal = False
96 if isinstance(other, Hwpack):
97 equal = (self.name == other.name and
98 self.boards == other.boards and
99 self.hwpack == other.hwpack and
100 self.bootloaders == other.bootloaders)
101 return equal
102
103 def __hash__(self):
104 return hash(frozenset(self.bootloaders), frozenset(self.boards),
105 self.name, self.hwpack)
106
107 def __str__(self):
108 """String representation of this hwapack supported elements."""
109 string = FORMAT.format("Read hardware pack: %s" % self.hwpack)
110 string += LINE_SEP
111 string += FORMAT.format(SEPARATOR)
112 string += LINE_SEP
113 string += ELEMENT_FORMAT.format("Supported boards",
114 "Supported bootloaders")
115 string += LINE_SEP
116 string += FORMAT.format(SEPARATOR)
117 string += LINE_SEP
118
119 if self.boards:
120 for key, value in self.boards.iteritems():
121 if value.get(BOOTLOADERS_FIELD, None) is not None:
122 bootloaders = value.get(BOOTLOADERS_FIELD)
123 supported_bootloaders = bootloaders.keys()
124 else:
125 supported_bootloaders = self.bootloaders.keys()
126 string += ELEMENT_FORMAT.format(key,
127 ",".join(supported_bootloaders))
128 string += LINE_SEP
129 else:
130 # If we pass a converted file with just a single board, we do not
131 # have the boards section, and we default to the name of the hwpack
132 if self.bootloaders:
133 supported_bootloaders = self.bootloaders.keys()
134 string += ELEMENT_FORMAT.format(self.name,
135 ",".join(supported_bootloaders))
136 string += LINE_SEP
137 else:
138 string += CENTER_ALIGN.format("No supported boards and "
139 "bootloaders")
140 string += LINE_SEP
141 string += FORMAT.format(SEPARATOR)
142 return string + LINE_SEP
143
144
145class HwpackReader(object):
146 """Reads the information contained in a hwpack """
147 def __init__(self, hwpacks):
148 """Create a new instance.
149
150 :param hwpacks: The list of hardware packs to read from."""
151 self.hwpacks = hwpacks
152 # Where we store all the info from the hwpack.
153 self._supported_elements = []
154
155 @property
156 def supported_elements(self):
157 """Gets the supported elements of by all the hardwapare packs."""
158 return self._supported_elements
159
160 def _read_hwpacks_metadata(self):
161 """Reads the hardware pack metadata file, and prints information about
162 the supported boards and bootloaders."""
163 for tarball in self.hwpacks:
164 with HardwarepackHandler([tarball]) as handler:
165 hwpack_format = handler.get_field(FORMAT_FIELD)[0]
166 if hwpack_format.format_as_string == "3.0":
167 local_hwpack = Hwpack()
168 local_hwpack.sethwpack(tarball)
169 local_hwpack.setname(handler.get_field(NAME_FIELD)[0])
170 local_hwpack.setboards(handler.get_field(BOARDS_FIELD)[0])
171 local_hwpack.setbootloaders(
172 handler.get_field(BOOTLOADERS_FIELD)[0])
173 self.supported_elements.append(local_hwpack)
174 else:
175 raise HwpackReaderError("Hardwarepack '%s' cannot be "
176 "read, unsupported format." %
177 (tarball))
178
179 def get_supported_boards(self):
180 """Prints the necessary information.
181
182 :return A string representation of the information."""
183 self._read_hwpacks_metadata()
184 return str(self)
185
186 def __str__(self):
187 """The string representation of this reader. It is a printable
188 representation of the necessary information."""
189 hwpack_reader = ""
190 for element in self.supported_elements:
191 hwpack_reader += str(element)
192 return hwpack_reader
0193
=== modified file 'linaro_image_tools/hwpack/tests/__init__.py'
--- linaro_image_tools/hwpack/tests/__init__.py 2012-07-19 17:57:24 +0000
+++ linaro_image_tools/hwpack/tests/__init__.py 2012-07-25 13:08:18 +0000
@@ -30,6 +30,7 @@
30 'linaro_image_tools.hwpack.tests.test_config_v3',30 'linaro_image_tools.hwpack.tests.test_config_v3',
31 'linaro_image_tools.hwpack.tests.test_hardwarepack',31 'linaro_image_tools.hwpack.tests.test_hardwarepack',
32 'linaro_image_tools.hwpack.tests.test_hwpack_converter',32 'linaro_image_tools.hwpack.tests.test_hwpack_converter',
33 'linaro_image_tools.hwpack.tests.test_hwpack_reader',
33 'linaro_image_tools.hwpack.tests.test_packages',34 'linaro_image_tools.hwpack.tests.test_packages',
34 'linaro_image_tools.hwpack.tests.test_script',35 'linaro_image_tools.hwpack.tests.test_script',
35 'linaro_image_tools.hwpack.tests.test_tarfile_matchers',36 'linaro_image_tools.hwpack.tests.test_tarfile_matchers',
3637
=== added file 'linaro_image_tools/hwpack/tests/test_hwpack_reader.py'
--- linaro_image_tools/hwpack/tests/test_hwpack_reader.py 1970-01-01 00:00:00 +0000
+++ linaro_image_tools/hwpack/tests/test_hwpack_reader.py 2012-07-25 13:08:18 +0000
@@ -0,0 +1,114 @@
1# Copyright (C) 2012 Linaro
2#
3# Author: Milo Casagrande <milo.casagrande@linaro.org>
4#
5# This file is part of Linaro Image Tools.
6#
7# Linaro Image Tools is free software: you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation, either version 3 of the License, or
10# (at your option) any later version.
11#
12# Linaro Image Tools is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with Linaro Image Tools. If not, see <http://www.gnu.org/licenses/>.
19
20import tarfile
21from StringIO import StringIO
22from linaro_image_tools.testing import TestCaseWithFixtures
23from linaro_image_tools.tests.fixtures import (
24 CreateTempDirFixture,
25 )
26
27from linaro_image_tools.media_create.tests.fixtures import (
28 CreateTarballFixture,
29 )
30
31from linaro_image_tools.hwpack.hwpack_reader import (
32 Hwpack,
33 HwpackReader,
34 HwpackReaderError,
35 )
36
37
38class HwpackReaderTests(TestCaseWithFixtures):
39 """Test class for the hwpack reader."""
40
41 def setUp(self):
42 super(HwpackReaderTests, self).setUp()
43 self.metadata = ("format: 3.0\nversion: '1'\nname: test-hwpack\n"
44 "architecture: armel\norigin: Linaro")
45 self.hwpack = Hwpack()
46 self.hwpack.setname('test-hwpack')
47 self.tar_dir_fixture = CreateTempDirFixture()
48 self.useFixture(self.tar_dir_fixture)
49 self.tarball_fixture = CreateTarballFixture(
50 self.tar_dir_fixture.get_temp_dir())
51 self.useFixture(self.tarball_fixture)
52
53 def tearDown(self):
54 super(HwpackReaderTests, self).tearDown()
55 self.hwpack = None
56 self.metadata = ""
57
58 def add_to_tarball(self, files, tarball=None):
59 if tarball is None:
60 tarball = self.tarball_fixture.get_tarball()
61 tar_file = tarfile.open(tarball, mode='w:gz')
62 for filename, data in files:
63 tarinfo = tarfile.TarInfo(filename)
64 tarinfo.size = len(data)
65 tar_file.addfile(tarinfo, StringIO(data))
66 tar_file.close()
67 return tarball
68
69 def test_hwpack_class(self):
70 hwpack = Hwpack()
71 hwpack.setname('test-hwpack')
72 hwpack.sethwpack('a_hwpack')
73 self.hwpack.sethwpack('a_hwpack')
74 self.assertEqual(self.hwpack, hwpack)
75
76 def test_hwpack_class_not_equal(self):
77 hwpack = Hwpack()
78 hwpack.setname('test-hwpack')
79 hwpack.sethwpack('a_hwpack')
80 self.hwpack.sethwpack('b_hwpack')
81 self.assertNotEqual(self.hwpack, hwpack)
82
83 def test_hwpack_metadata_read(self):
84 tarball = self.add_to_tarball([('metadata', self.metadata)])
85 reader = HwpackReader([tarball])
86 reader._read_hwpacks_metadata()
87 self.hwpack.sethwpack(tarball)
88 self.assertEqual(self.hwpack, reader.supported_elements[0])
89
90 def test_raise_exception(self):
91 new_metadata = ("format=2.0\nversion=4")
92 tarball = self.add_to_tarball([('metadata', new_metadata)])
93 reader = HwpackReader([tarball])
94 self.assertRaises(HwpackReaderError, reader._read_hwpacks_metadata)
95
96 def test_hwpack_metadata_read_with_boards(self):
97 self.metadata += "\nboards:\n panda:\n support: supported\n"
98 tarball = self.add_to_tarball([('metadata', self.metadata)])
99 reader = HwpackReader([tarball])
100 reader._read_hwpacks_metadata()
101 self.hwpack.sethwpack(tarball)
102 self.hwpack.setboards({'panda': {'support': 'supported'}})
103 self.assertEqual(self.hwpack, reader.supported_elements[0])
104
105 def test_hwpack_metadata_read_with_bootloaders(self):
106 self.metadata += ("\nboards:\n panda:\n support: supported\n "
107 "bootloaders:\n u_boot:\n file: a_file\n")
108 tarball = self.add_to_tarball([('metadata', self.metadata)])
109 reader = HwpackReader([tarball])
110 reader._read_hwpacks_metadata()
111 self.hwpack.sethwpack(tarball)
112 self.hwpack.setboards({'panda': {'support': 'supported', 'bootloaders':
113 {'u_boot': {'file': 'a_file'}}}})
114 self.assertEqual(self.hwpack, reader.supported_elements[0])
0115
=== modified file 'linaro_image_tools/media_create/__init__.py'
--- linaro_image_tools/media_create/__init__.py 2012-06-13 14:43:59 +0000
+++ linaro_image_tools/media_create/__init__.py 2012-07-25 13:08:18 +0000
@@ -92,7 +92,11 @@
92 '--output-directory', dest='directory',92 '--output-directory', dest='directory',
93 help='Directory where image and accessories should be written to.')93 help='Directory where image and accessories should be written to.')
94 parser.add_argument(94 parser.add_argument(
95 '--dev', required=True, dest='board', choices=KNOWN_BOARDS,95 '--read-hwpack', dest='readhwpack', action='store_true',
96 help=('Read the hardware pack and print information about the '
97 'supported boards and bootloaders.'))
98 parser.add_argument(
99 '--dev', dest='board', choices=KNOWN_BOARDS,
96 help='Generate an SD card or image for the given board.')100 help='Generate an SD card or image for the given board.')
97 parser.add_argument(101 parser.add_argument(
98 '--rootfs', default='ext4', choices=['ext2', 'ext3', 'ext4', 'btrfs'],102 '--rootfs', default='ext4', choices=['ext2', 'ext3', 'ext4', 'btrfs'],
@@ -137,7 +141,7 @@
137 help=('The image size, specified in mega/giga bytes (e.g. 3000M or '141 help=('The image size, specified in mega/giga bytes (e.g. 3000M or '
138 '3G); use with --image_file only'))142 '3G); use with --image_file only'))
139 parser.add_argument(143 parser.add_argument(
140 '--binary', default='binary-tar.tar.gz', required=True,144 '--binary', default='binary-tar.tar.gz', required=False,
141 help=('The tarball containing the rootfs used to create the bootable '145 help=('The tarball containing the rootfs used to create the bootable '
142 'system.'))146 'system.'))
143 parser.add_argument(147 parser.add_argument(
144148
=== modified file 'linaro_image_tools/utils.py'
--- linaro_image_tools/utils.py 2012-06-13 14:55:34 +0000
+++ linaro_image_tools/utils.py 2012-07-25 13:08:18 +0000
@@ -306,6 +306,15 @@
306 """The hwpack parameter is not a regular file."""306 """The hwpack parameter is not a regular file."""
307307
308308
309class MissingRequiredOption(Exception):
310 """A required option from the command line is missing."""
311 def __init__(self, value):
312 self.value = value
313
314 def __str__(self):
315 return repr(self.value)
316
317
309class IncompatibleOptions(Exception):318class IncompatibleOptions(Exception):
310 def __init__(self, value):319 def __init__(self, value):
311 self.value = value320 self.value = value
@@ -331,3 +340,11 @@
331 if not os.path.isfile(hwpack):340 if not os.path.isfile(hwpack):
332 raise InvalidHwpackFile(341 raise InvalidHwpackFile(
333 "--hwpack argument (%s) is not a regular file" % hwpack)342 "--hwpack argument (%s) is not a regular file" % hwpack)
343
344
345def check_required_args(args):
346 """Check that the required args are passed."""
347 if args.board is None:
348 raise MissingRequiredOption("--dev option is required")
349 if args.binary is None:
350 raise MissingRequiredOption("--binary option is required")

Subscribers

People subscribed via source and target branches