Merge lp:~dooferlad/linaro-image-tools/add-directory-option into lp:linaro-image-tools/11.11

Proposed by James Tunnicliffe
Status: Superseded
Proposed branch: lp:~dooferlad/linaro-image-tools/add-directory-option
Merge into: lp:linaro-image-tools/11.11
Diff against target: 305 lines (+158/-32)
7 files modified
.bzrignore (+1/-0)
linaro-media-create (+37/-25)
linaro_image_tools/media_create/__init__.py (+11/-6)
linaro_image_tools/media_create/boards.py (+28/-0)
linaro_image_tools/media_create/partitions.py (+1/-0)
linaro_image_tools/tests/test_utils.py (+44/-1)
linaro_image_tools/utils.py (+36/-0)
To merge this branch: bzr merge lp:~dooferlad/linaro-image-tools/add-directory-option
Reviewer Review Type Date Requested Status
linaro-image-tools maintainers Pending
Review via email: mp+102331@code.launchpad.net

This proposal supersedes a proposal from 2012-04-16.

This proposal has been superseded by a proposal from 2012-04-18.

Description of the change

Slightly modified patch from https://bugs.launchpad.net/linaro-image-tools/+bug/962147

Updated:
--directory can now be specified at the same time as --image-file. If --directory and --image-file are combined, a file, whose name is specified by --image-file, will be created in --directory. If --image-file is not specified, a sensible default file name will be used and printed for the user to see.

--directory and --mmc are incompatable.

--directory and specifying a full path to a file with --image-file are incompatable.

To post a comment you must log in.
Revision history for this message
Loïc Minier (lool) wrote : Posted in a previous version of this proposal

On Mon, Apr 16, 2012, James Tunnicliffe wrote:
[...]
> + if args.directory is not None:
> + if not board_config.outputs_directory:
> + raise BoardAbilityNotMet("The board '%s' does not support the"
> + "--directory option." % args.board)

 I find the per board attribute kinda ugly and I'm not sure we want to
 suffer from supporting both a single file output or a directory with
 multiple files as output.

 How about we just default to always outputting to a directory, default
 to ./ as the target directory, and allow overriding that, but keep the
 --image-file as the default filename for the main image? We could
 document for each boardconfig what it outputs as to display it at the
 end of the build or in the help output.

 There are alternate approaches such as output a zip/tgz file with all
 the assets and naming that after --image-file, but that's probably a
 bit heavy. We could also abuse --image-file by outputting a warning
 and creating a directory there instead (e.g. --image-file foo.img would
 create ./foo.img/ and output the assets below that).

--
Loïc Minier

Revision history for this message
James Tunnicliffe (dooferlad) wrote : Posted in a previous version of this proposal

On 16 April 2012 17:40, Loïc Minier <email address hidden> wrote:
> On Mon, Apr 16, 2012, James Tunnicliffe wrote:
> [...]
>> +    if args.directory is not None:
>> +        if not board_config.outputs_directory:
>> +            raise BoardAbilityNotMet("The board '%s' does not support the"
>> +                                     "--directory option." % args.board)
>
>  I find the per board attribute kinda ugly and I'm not sure we want to
>  suffer from supporting both a single file output or a directory with
>  multiple files as output.
>
>  How about we just default to always outputting to a directory, default
>  to ./ as the target directory, and allow overriding that, but keep the
>  --image-file as the default filename for the main image?  We could
>  document for each boardconfig what it outputs as to display it at the
>  end of the build or in the help output.

I like this approach since it gracefully reverts back to the old
behaviour. Will make some changes.

--
James Tunnicliffe

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

Oops, forgot to delete the outputs_directory flag. Hang on...

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

OK. Done.

511. By James Tunnicliffe

Removed outputs_directory flag

Revision history for this message
Loïc Minier (lool) wrote :

On Tue, Apr 17, 2012, James Tunnicliffe wrote:
> + IncompatableOptions,

 IncompatibleOptions

> + parser.add_argument(
> + '--directory', dest='directory',
> + help='Directory where image and accessories should be written to.')

 --output-directory?

> + outputs_directory = False

 not too nice as property; perhaps outputs_multiple_artifacts = False?
 Or expected_artifacts = 1?

> +def prep_media_path(args, board_config):
> +
> + # If args.device isn't set, generate a sensible default file name and
> + # tell the user
> + if not args.device:
> + args.device = board_config.board + ".img"
> + print "Setting destination file name to", args.device

 I think it would be more stable to just call it mmc.img or sd.img.

> + if args.directory is not None:
> + # If args.device is a path to a device (/dev/) then this is an error
> + if "--mmc" in sys.argv:
> + raise IncompatableOptions("--directory option incompatable with "
> + "option --mmc")
> +
> + # If directory is used as well as having a full path (rather than just
> + # a file name or relative path) in args.device, this is an error.
> + if re.search(r"^/", args.device):
> + raise IncompatableOptions("--directory option incompatable with "

 IncompatibleOptions + "incompatible"

 We could actually support absolute --image-file with --directory and
 just output the image where the user wants it, but I don't really care
 strongly about the behavior in that particular use case.

 Generally ok with the design apart of the minor comments here, but
 I have one objection: I think the argument validation / mangling should
 happen when parsing the cmdline rather than in boards code.

 Could you just include prep_media_path logic as part of the cmdline
 args validation? This would also remove the need for a special
 exception down the road.

 Otherwise good, thanks!

--
Loïc Minier

Revision history for this message
Ricardo Salveti (rsalveti) wrote :

On Tue, Apr 17, 2012 at 3:37 PM, Loïc Minier <email address hidden> wrote:
>> +def prep_media_path(args, board_config):
>> +
>> +    # If args.device isn't set, generate a sensible default file name and
>> +    # tell the user
>> +    if not args.device:
>> +        args.device = board_config.board + ".img"
>> +        print "Setting destination file name to", args.device
>
>  I think it would be more stable to just call it mmc.img or sd.img.

+1, that would help scripting the output later on.

>> +    if args.directory is not None:
>> +        # If args.device is a path to a device (/dev/) then this is an error
>> +        if "--mmc" in sys.argv:
>> +            raise IncompatableOptions("--directory option incompatable with "
>> +                                      "option --mmc")
>> +
>> +        # If directory is used as well as having a full path (rather than just
>> +        # a file name or relative path) in args.device, this is an error.
>> +        if re.search(r"^/", args.device):
>> +            raise IncompatableOptions("--directory option incompatable with "
>
>  IncompatibleOptions + "incompatible"
>
>  We could actually support absolute --image-file with --directory and
>  just output the image where the user wants it, but I don't really care
>  strongly about the behavior in that particular use case.
>
>  Generally ok with the design apart of the minor comments here, but
>  I have one objection: I think the argument validation / mangling should
>  happen when parsing the cmdline rather than in boards code.

+1

>  Otherwise good, thanks!

+1

512. By James Tunnicliffe

Fixed spelling of IncompatibleOptions

513. By James Tunnicliffe

Renamed --directory option to --output-directory. Set default file name for an image to sd.img

514. By James Tunnicliffe

Moved defaulting of args.device into ArgumentParser set up.

515. By James Tunnicliffe

Moved additional option checks into their own function (additional_option_checks).

516. By James Tunnicliffe

remove .idea ignore

517. By James Tunnicliffe

Removed unused directory variable.

Unmerged revisions

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file '.bzrignore'
2--- .bzrignore 2010-12-06 23:58:41 +0000
3+++ .bzrignore 2012-04-18 13:29:18 +0000
4@@ -1,2 +1,3 @@
5 .testrepository
6 _trial_temp
7+.idea
8
9=== modified file 'linaro-media-create'
10--- linaro-media-create 2012-01-17 14:50:47 +0000
11+++ linaro-media-create 2012-04-18 13:29:18 +0000
12@@ -48,6 +48,9 @@
13 is_arm_host,
14 check_file_integrity_and_log_errors,
15 path_in_tarfile_exists,
16+ IncompatibleOptions,
17+ prep_media_path,
18+ additional_option_checks,
19 )
20
21 # Just define the global variables
22@@ -97,6 +100,37 @@
23 parser = get_args_parser()
24 args = parser.parse_args()
25
26+ try:
27+ additional_option_checks(args)
28+ except IncompatibleOptions as e:
29+ parser.print_help()
30+ print >> sys.stderr, "\nError:", e.value
31+ sys.exit(1)
32+
33+ board_config = board_configs[args.board]
34+ board_config.set_metadata(args.hwpacks)
35+ board_config.set_board(args.board)
36+ board_config.add_boot_args(args.extra_boot_args)
37+ board_config.add_boot_args_from_file(args.extra_boot_args_file)
38+
39+ media = Media(prep_media_path(args))
40+
41+ if media.is_block_device:
42+ if not board_config.supports_writing_to_mmc:
43+ print "The board '%s' does not support the --mmc option. Please use "\
44+ "--image_file to create an image file for this board." % args.board
45+ sys.exit(1)
46+ if not confirm_device_selection_and_ensure_it_is_ready(args.device,
47+ args.nocheck_mmc):
48+ sys.exit(1)
49+ elif not args.should_format_rootfs or not args.should_format_bootfs:
50+ print ("Do not use --no-boot or --no-part in conjunction with "
51+ "--image_file.")
52+ sys.exit(1)
53+ else:
54+ # All good, move on.
55+ pass
56+
57 ch = logging.StreamHandler()
58 ch.setLevel(logging.INFO)
59 formatter = logging.Formatter("%(message)s")
60@@ -116,12 +150,6 @@
61 BOOT_DISK = os.path.join(TMP_DIR, 'boot-disc')
62 ROOT_DISK = os.path.join(TMP_DIR, 'root-disc')
63
64- board_config = board_configs[args.board]
65- board_config.set_metadata(args.hwpacks)
66- board_config.set_board(args.board)
67- board_config.add_boot_args(args.extra_boot_args)
68- board_config.add_boot_args_from_file(args.extra_boot_args_file)
69-
70 ensure_required_commands(args)
71
72 sig_file_list = args.hwpacksigs[:]
73@@ -134,24 +162,8 @@
74 sig_file_list, args.binary, args.hwpacks)
75 if not files_ok:
76 sys.exit(1)
77-
78+
79 atexit.register(cleanup_tempdir)
80- media = Media(args.device)
81- if media.is_block_device:
82- if not board_config.supports_writing_to_mmc:
83- print "The board '%s' does not support the --mmc option. Please use " \
84- "--image_file to create an image file for this board." % args.board
85- sys.exit(1)
86- if not confirm_device_selection_and_ensure_it_is_ready(args.device,
87- args.nocheck_mmc):
88- sys.exit(1)
89- elif not args.should_format_rootfs or not args.should_format_bootfs:
90- print ("Do not use --no-boot or --no-part in conjunction with "
91- "--image_file.")
92- sys.exit(1)
93- else:
94- # All good, move on.
95- pass
96
97 unpack_binary_tarball(args.binary, TMP_DIR)
98
99@@ -174,7 +186,7 @@
100
101 if args.should_format_bootfs:
102 board_config.populate_boot(
103- ROOTFS_DIR, rootfs_uuid, boot_partition, BOOT_DISK, args.device,
104+ ROOTFS_DIR, rootfs_uuid, boot_partition, BOOT_DISK, media.path,
105 args.is_live, args.is_lowmem, args.consoles)
106
107 if args.should_format_rootfs:
108@@ -185,4 +197,4 @@
109 rootfs_uuid, create_swap, str(args.swap_file),
110 board_config.mmc_part_offset, board_config)
111
112- print "Done creating Linaro image on %s" % args.device
113+ print "Done creating Linaro image on %s" % media.path
114
115=== modified file 'linaro_image_tools/media_create/__init__.py'
116--- linaro_image_tools/media_create/__init__.py 2012-01-19 06:31:50 +0000
117+++ linaro_image_tools/media_create/__init__.py 2012-04-18 13:29:18 +0000
118@@ -77,12 +77,17 @@
119 def get_args_parser():
120 """Get the ArgumentParser for the arguments given on the command line."""
121 parser = argparse.ArgumentParser(version='%(prog)s ' + get_version())
122- group = parser.add_mutually_exclusive_group(required=True)
123- group.add_argument(
124- '--mmc', dest='device', help='The storage device to use.')
125- group.add_argument(
126- '--image-file', '--image_file', dest='device',
127- help='File where we should write the QEMU image.')
128+ group = parser.add_mutually_exclusive_group()
129+ group.add_argument(
130+ '--mmc', dest='device', default="sd.img",
131+ help='The storage device to use.')
132+ group.add_argument(
133+ '--image-file', '--image_file', dest='device', default="sd.img",
134+ help='File where we should write an image file (defaults to sd.img '
135+ 'if neither --image-file or --mmc are specified.)')
136+ parser.add_argument(
137+ '--output-directory', dest='directory',
138+ help='Directory where image and accessories should be written to.')
139 parser.add_argument(
140 '--dev', required=True, dest='board', choices=KNOWN_BOARDS,
141 help='Generate an SD card or image for the given board.')
142
143=== modified file 'linaro_image_tools/media_create/boards.py'
144--- linaro_image_tools/media_create/boards.py 2012-04-17 16:22:09 +0000
145+++ linaro_image_tools/media_create/boards.py 2012-04-18 13:29:18 +0000
146@@ -1337,6 +1337,33 @@
147 pass
148
149
150+class FastModelConfig(BoardConfig):
151+ supports_writing_to_mmc = False
152+
153+ @classmethod
154+ def _get_bootcmd(cls, d_img_data):
155+ """Get the bootcmd for FastModel.
156+
157+ We override this as we don't do uboot.
158+ """
159+ return ""
160+
161+ @classmethod
162+ def _make_boot_files_v2(cls, boot_env, chroot_dir, boot_dir,
163+ boot_device_or_file, k_img_data, i_img_data,
164+ d_img_data):
165+ logger = logging.getLogger("linaro_image_tools")
166+ logger.info("WTF=%s." % boot_device_or_file )
167+ output_dir=os.path.dirname(boot_device_or_file)
168+ cmd = [ "cp", "-v", _get_file_matching("%s/boot/img.axf" % chroot_dir), output_dir ]
169+ proc = cmd_runner.run(cmd, as_root=True)
170+ proc.wait()
171+ cmd = [ "cp", "-v", k_img_data, i_img_data, d_img_data, output_dir ]
172+ proc = cmd_runner.run(cmd, as_root=True)
173+ proc.wait()
174+ return
175+
176+
177 class SamsungConfig(BoardConfig):
178 @classproperty
179 def extra_serial_opts(cls):
180@@ -1542,6 +1569,7 @@
181 'panda': PandaConfig,
182 'vexpress': VexpressConfig,
183 'vexpress-a9': VexpressA9Config,
184+ 'fastmodel': FastModelConfig,
185 'ux500': Ux500Config,
186 'snowball_sd': SnowballSdConfig,
187 'snowball_emmc': SnowballEmmcConfig,
188
189=== modified file 'linaro_image_tools/media_create/partitions.py'
190--- linaro_image_tools/media_create/partitions.py 2012-01-17 14:19:19 +0000
191+++ linaro_image_tools/media_create/partitions.py 2012-04-18 13:29:18 +0000
192@@ -590,4 +590,5 @@
193
194 def __init__(self, path):
195 self.path = path
196+ self.directory = None
197 self.is_block_device = path.startswith('/dev/')
198
199=== modified file 'linaro_image_tools/tests/test_utils.py'
200--- linaro_image_tools/tests/test_utils.py 2011-10-12 09:54:29 +0000
201+++ linaro_image_tools/tests/test_utils.py 2012-04-18 13:29:18 +0000
202@@ -41,9 +41,11 @@
203 verify_file_integrity,
204 check_file_integrity_and_log_errors,
205 path_in_tarfile_exists,
206+ IncompatibleOptions,
207+ prep_media_path,
208+ additional_option_checks,
209 )
210
211-
212 sudo_args = " ".join(cmd_runner.SUDO_ARGS)
213
214
215@@ -260,3 +262,44 @@
216 UnableToFindPackageProvidingCommand,
217 install_package_providing, 'mkfs.lean')
218
219+
220+class Args():
221+ def __init__(self, directory, device, board):
222+ self.directory = directory
223+ self.device = device
224+ self.board = board
225+
226+
227+class TestPrepMediaPath(TestCaseWithFixtures):
228+
229+ def test_prep_media_path(self):
230+ self.useFixture(MockSomethingFixture(os.path, 'abspath', lambda x: x))
231+ self.useFixture(MockSomethingFixture(os, "makedirs", lambda x: x))
232+
233+ self.assertEqual("testdevice",
234+ prep_media_path(Args(directory=None,
235+ device="testdevice",
236+ board="testboard")))
237+
238+ self.assertEqual("/foo/bar/testdevice",
239+ prep_media_path(Args(directory="/foo/bar",
240+ device="testdevice",
241+ board="testboard")))
242+
243+class TestPrepMediaPath(TestCaseWithFixtures):
244+
245+ def test_additional_option_checks(self):
246+ self.useFixture(MockSomethingFixture(os.path, 'abspath', lambda x: x))
247+ self.useFixture(MockSomethingFixture(os, "makedirs", lambda x: x))
248+
249+ self.assertRaises(IncompatibleOptions, additional_option_checks,
250+ Args(directory="/foo/bar",
251+ device="/testdevice",
252+ board="testboard"))
253+
254+ sys.argv.append("--mmc")
255+ self.assertRaises(IncompatibleOptions, additional_option_checks,
256+ Args(directory="/foo/bar",
257+ device="testdevice",
258+ board="testboard"))
259+ sys.argv.remove("--mmc")
260
261=== modified file 'linaro_image_tools/utils.py'
262--- linaro_image_tools/utils.py 2011-11-28 12:49:14 +0000
263+++ linaro_image_tools/utils.py 2012-04-18 13:29:18 +0000
264@@ -247,5 +247,41 @@
265 return prefer_dir
266
267
268+def prep_media_path(args):
269+ if args.directory is not None:
270+ loc = os.path.abspath(args.directory)
271+ try:
272+ os.makedirs(loc)
273+ except OSError:
274+ pass # Directory exists.
275+
276+ path = os.path.join(loc, args.device)
277+ else:
278+ path = args.device
279+
280+ return path
281+
282+
283 class UnableToFindPackageProvidingCommand(Exception):
284 """We can't find a package which provides the given command."""
285+
286+
287+class IncompatibleOptions(Exception):
288+ def __init__(self, value):
289+ self.value = value
290+
291+ def __str__(self):
292+ return repr(self.value)
293+
294+def additional_option_checks(args):
295+ if args.directory is not None:
296+ # If args.device is a path to a device (/dev/) then this is an error
297+ if "--mmc" in sys.argv:
298+ raise IncompatibleOptions("--directory option incompatable with "
299+ "option --mmc")
300+
301+ # If directory is used as well as having a full path (rather than just
302+ # a file name or relative path) in args.device, this is an error.
303+ if re.search(r"^/", args.device):
304+ raise IncompatibleOptions("--directory option incompatable with "
305+ "a full path in --image-file")

Subscribers

People subscribed via source and target branches