Merge lp:~james-w/linaro-image-tools/add-script into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 72
Proposed branch: lp:~james-w/linaro-image-tools/add-script
Merge into: lp:linaro-image-tools/11.11
Prerequisite: lp:~james-w/linaro-image-tools/hwpack-builder
Diff against target: 128 lines (+108/-0)
3 files modified
hwpack/tests/__init__.py (+1/-0)
hwpack/tests/test_script.py (+84/-0)
linaro-hwpack-create (+23/-0)
To merge this branch: bzr merge lp:~james-w/linaro-image-tools/add-script
Reviewer Review Type Date Requested Status
Zygmunt Krynicki (community) Approve
Review via email: mp+35130@code.launchpad.net

Description of the change

Hi,

The final piece of the puzzle, a script that you can run to build
a set of hardware packs.

Should be straightforward given the pieces that came before.

Thanks,

James

To post a comment you must log in.
Revision history for this message
Zygmunt Krynicki (zyga) wrote :

Hi James

104 +from optparse import OptionParser

110 +if __name__ == '__main__':
111 + parser = OptionParser()
112 + opts, args = parser.parse_args()
113 + if len(args) < 2:
114 + sys.stderr.write("Requires arguments CONFIG_FILE VERSION\n")
115 + sys.exit(1)
116 + try:

I'd use argparse as it seems to do exactly what you need and is in general superior to optparse.

parser = argparse.ArgumentParser()
parser.add_argument("CONFIG_FILE")
parser.add_argument("VERSION")
args = parser.parse_args()
HardwarePackBuilder(args.CONFIG_FILE, args.VERSION)

You could even use type=argparse.FileType("r") to get various error handlers that understand the act of opening file for reading. This would require changing your API a bit since it returns an open file, not a pathname

Revision history for this message
James Westby (james-w) wrote :

Done, thanks.

James

128. By James Westby

Use argparse. Thanks Zygmunt.

Revision history for this message
Zygmunt Krynicki (zyga) wrote :

Diff looks good, tests passed.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'hwpack/tests/__init__.py'
2--- hwpack/tests/__init__.py 2010-09-13 19:16:50 +0000
3+++ hwpack/tests/__init__.py 2010-09-13 19:16:50 +0000
4@@ -6,6 +6,7 @@
5 'hwpack.tests.test_builder',
6 'hwpack.tests.test_hardwarepack',
7 'hwpack.tests.test_packages',
8+ 'hwpack.tests.test_script',
9 'hwpack.tests.test_tarfile_matchers',
10 ]
11 loader = unittest.TestLoader()
12
13=== added file 'hwpack/tests/test_script.py'
14--- hwpack/tests/test_script.py 1970-01-01 00:00:00 +0000
15+++ hwpack/tests/test_script.py 2010-09-13 19:16:50 +0000
16@@ -0,0 +1,84 @@
17+import os
18+import subprocess
19+
20+from hwpack.hardwarepack import Metadata
21+from hwpack.testing import (
22+ AptSourceFixture,
23+ ChdirToTempdirFixture,
24+ ConfigFileFixture,
25+ DummyFetchedPackage,
26+ IsHardwarePack,
27+ TestCaseWithFixtures,
28+ )
29+
30+
31+class ScriptTests(TestCaseWithFixtures):
32+ """Tests that execute the linaro-hwpack-create script."""
33+
34+ def setUp(self):
35+ super(ScriptTests, self).setUp()
36+ self.script_path = self.find_script()
37+ self.useFixture(ChdirToTempdirFixture())
38+
39+ def find_script(self):
40+ script_name = "linaro-hwpack-create"
41+ this_path = os.path.abspath(__file__)
42+ parent_path = this_path
43+ for i in range(3):
44+ parent_path = os.path.dirname(parent_path)
45+ possible_paths = [
46+ os.path.join(parent_path, script_name),
47+ os.path.join("usr", "local", "bin", script_name),
48+ os.path.join("usr", "bin", script_name),
49+ ]
50+ for script_path in possible_paths:
51+ if os.path.exists(script_path):
52+ return script_path
53+ raise AssertionError(
54+ "Could not find linaro-hwpack-create script to test.")
55+
56+ def run_script(self, args, expected_returncode=0):
57+ cmdline = [self.script_path] + args
58+ proc = subprocess.Popen(
59+ cmdline, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
60+ (stdout, stderr) = proc.communicate()
61+ self.assertEqual(
62+ expected_returncode, proc.returncode,
63+ "%s exited with code %d. stdout: %s\nstderr: %s\n"
64+ % (str(cmdline), proc.returncode, stdout, stderr))
65+ return stdout, stderr
66+
67+ def test_errors_on_missing_configfile(self):
68+ stdout, stderr = self.run_script(
69+ ["nonexistant", "1.0"], expected_returncode=1)
70+ self.assertEqual("No such config file: 'nonexistant'\n", stderr)
71+ self.assertEqual("", stdout)
72+
73+ def test_errors_on_missing_configfile_argument(self):
74+ stdout, stderr = self.run_script([], expected_returncode=2)
75+ self.assertEqual(
76+ "usage: linaro-hwpack-create [-h] CONFIG_FILE VERSION\n"
77+ "linaro-hwpack-create: error: too few arguments\n", stderr)
78+ self.assertEqual("", stdout)
79+
80+ def test_errors_on_missing_version_argument(self):
81+ stdout, stderr = self.run_script(["somefile"], expected_returncode=2)
82+ self.assertEqual(
83+ "usage: linaro-hwpack-create [-h] CONFIG_FILE VERSION\n"
84+ "linaro-hwpack-create: error: too few arguments\n", stderr)
85+ self.assertEqual("", stdout)
86+
87+ def test_builds_a_hwpack(self):
88+ available_package = DummyFetchedPackage(
89+ "foo", "1.1", architecture="armel")
90+ source = self.useFixture(AptSourceFixture([available_package]))
91+ config = self.useFixture(ConfigFileFixture(
92+ '[hwpack]\nname=ahwpack\npackages=foo\narchitectures=armel\n'
93+ '\n[ubuntu]\nsources-entry=%s\n' % source.sources_entry))
94+ stdout, stderr = self.run_script([config.filename, "1.0"])
95+ metadata = Metadata("ahwpack", "1.0", "armel")
96+ self.assertThat(
97+ "hwpack_ahwpack_1.0_armel.tar.gz",
98+ IsHardwarePack(
99+ metadata, [available_package],
100+ {"ubuntu": source.sources_entry}))
101
102=== added file 'linaro-hwpack-create'
103--- linaro-hwpack-create 1970-01-01 00:00:00 +0000
104+++ linaro-hwpack-create 2010-09-13 19:16:50 +0000
105@@ -0,0 +1,23 @@
106+#!/usr/bin/python
107+
108+import argparse
109+import sys
110+
111+from hwpack.builder import ConfigFileMissing, HardwarePackBuilder
112+
113+
114+if __name__ == '__main__':
115+ parser = argparse.ArgumentParser()
116+ parser.add_argument(
117+ "CONFIG_FILE",
118+ help="The configuration file to take the hardware pack information "
119+ "from.")
120+ parser.add_argument(
121+ "VERSION", help="The version of the hardware pack to create.")
122+ args = parser.parse_args()
123+ try:
124+ builder = HardwarePackBuilder(args.CONFIG_FILE, args.VERSION)
125+ except ConfigFileMissing, e:
126+ sys.stderr.write(str(e) + "\n")
127+ sys.exit(1)
128+ builder.build()

Subscribers

People subscribed via source and target branches