Merge lp:~james-w/linaro-image-tools/hwpack-create-config into lp:linaro-image-tools/11.11

Proposed by James Westby
Status: Merged
Merged at revision: 34
Proposed branch: lp:~james-w/linaro-image-tools/hwpack-create-config
Merge into: lp:linaro-image-tools/11.11
Diff against target: 402 lines (+375/-0)
5 files modified
.bzrignore (+1/-0)
.testr.conf (+3/-0)
hwpack/config.py (+167/-0)
hwpack/tests/__init__.py (+8/-0)
hwpack/tests/test_config.py (+196/-0)
To merge this branch: bzr merge lp:~james-w/linaro-image-tools/hwpack-create-config
Reviewer Review Type Date Requested Status
Alexander Sack (community) Approve
Linaro Infrastructure Pending
Review via email: mp+33952@code.launchpad.net

Description of the change

Hi,

Here is a small branch to add the skeleton of the hwpack module with tests, and the start of
a hwpack.config for reading the configuration files specifying what to put in a hwpack.

You can read the description of the format at https://wiki.linaro.org/Platform/UserPlatforms/Specs/10.11/HardwarePacks#hwpack-create

Thanks,

James

To post a comment you must log in.
46. By James Westby

Add a testsuite function to run the tests.

47. By James Westby

Add a .testr.conf for the project.

48. By James Westby

Use a method to avoid hardcoding StringIO everywhere.

49. By James Westby

Use a helper assertion to reduce code duplication.

Revision history for this message
Alexander Sack (asac) wrote :

looks like a good start. merged.

review: Approve
Revision history for this message
Michael Hudson-Doyle (mwhudson) wrote :

I know this is late, but:

1) I would have appreciated some documentation on what the various properties on the Config option meant.

2) I'm sure there's a place for a _get_main_option helper -- origin, main and support at least are very similar.

Cheers,
mwh

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
=== added file '.bzrignore'
--- .bzrignore 1970-01-01 00:00:00 +0000
+++ .bzrignore 2010-08-27 20:28:42 +0000
@@ -0,0 +1,1 @@
1.testrepository
02
=== added file '.testr.conf'
--- .testr.conf 1970-01-01 00:00:00 +0000
+++ .testr.conf 2010-08-27 20:28:42 +0000
@@ -0,0 +1,3 @@
1[DEFAULT]
2test_command=python -m subunit.run $IDLIST
3test_id_list_default=hwpack.tests.test_suite
04
=== added directory 'hwpack'
=== added file 'hwpack/__init__.py'
=== added file 'hwpack/config.py'
--- hwpack/config.py 1970-01-01 00:00:00 +0000
+++ hwpack/config.py 2010-08-27 20:28:42 +0000
@@ -0,0 +1,167 @@
1import ConfigParser
2import re
3
4class HwpackConfigError(Exception):
5 pass
6
7
8class Config(object):
9 """Encapsulation of a hwpack-create configuration."""
10
11 MAIN_SECTION = "hwpack"
12 NAME_KEY = "name"
13 NAME_REGEX = "[a-z0-9][a-z0-9+0.]+"
14 INCLUDE_DEBS_KEY = "include-debs"
15 SUPPORT_KEY = "support"
16 SOURCES_ENTRY_KEY = "sources-entry"
17 PACKAGES_KEY = "packages"
18 PACKAGE_REGEX = NAME_REGEX
19 ORIGIN_KEY = "origin"
20 MAINTAINER_KEY = "maintainer"
21
22 def __init__(self, fp):
23 """Create a Config.
24
25 :param fp: a file-like object containing the configuration.
26 """
27 self.parser = ConfigParser.RawConfigParser()
28 self.parser.readfp(fp)
29
30 def validate(self):
31 """Check that this configuration follows the schema.
32
33 :raises HwpackConfigError: if it does not.
34 """
35 if not self.parser.has_section(self.MAIN_SECTION):
36 raise HwpackConfigError("No [%s] section" % self.MAIN_SECTION)
37 self._validate_name()
38 self._validate_include_debs()
39 self._validate_support()
40 self._validate_sections()
41
42 @property
43 def name(self):
44 return self.parser.get(self.MAIN_SECTION, self.NAME_KEY)
45
46 @property
47 def include_debs(self):
48 try:
49 if not self.parser.get(
50 self.MAIN_SECTION, self.INCLUDE_DEBS_KEY):
51 return True
52 return self.parser.getboolean(
53 self.MAIN_SECTION, self.INCLUDE_DEBS_KEY)
54 except ConfigParser.NoOptionError:
55 return True
56
57 @property
58 def origin(self):
59 try:
60 origin = self.parser.get(self.MAIN_SECTION, self.ORIGIN_KEY)
61 if not origin:
62 return None
63 return origin
64 except ConfigParser.NoOptionError:
65 return None
66
67 @property
68 def maintainer(self):
69 try:
70 maintainer = self.parser.get(
71 self.MAIN_SECTION, self.MAINTAINER_KEY)
72 if not maintainer:
73 return None
74 return maintainer
75 except ConfigParser.NoOptionError:
76 return None
77
78 @property
79 def support(self):
80 try:
81 support = self.parser.get(self.MAIN_SECTION, self.SUPPORT_KEY)
82 if not support:
83 return None
84 return support
85 except ConfigParser.NoOptionError:
86 return None
87
88 def _validate_name(self):
89 try:
90 name = self.name
91 if not name:
92 raise HwpackConfigError("Empty value for name")
93 if re.match(self.NAME_REGEX, name) is None:
94 raise HwpackConfigError("Invalid name: %s" % name)
95 except ConfigParser.NoOptionError:
96 raise HwpackConfigError(
97 "No name in the [%s] section" % self.MAIN_SECTION)
98
99 def _validate_include_debs(self):
100 try:
101 self.include_debs
102 except ValueError:
103 raise HwpackConfigError(
104 "Invalid value for include-debs: %s"
105 % self.parser.get("hwpack", "include-debs"))
106
107 def _validate_support(self):
108 support = self.support
109 if support not in (None, "supported", "unsupported"):
110 raise HwpackConfigError(
111 "Invalid value for support: %s" % support)
112
113 def _validate_section_sources_entry(self, section_name):
114 try:
115 sources_entry = self.parser.get(
116 section_name, self.SOURCES_ENTRY_KEY)
117 if not sources_entry:
118 raise HwpackConfigError(
119 "The %s in the [%s] section is missing the URI"
120 % (self.SOURCES_ENTRY_KEY, section_name))
121 if len(sources_entry.split(" ", 1)) < 2:
122 raise HwpackConfigError(
123 "The %s in the [%s] section is missing the distribution"
124 % (self.SOURCES_ENTRY_KEY, section_name))
125 if sources_entry.startswith("deb"):
126 raise HwpackConfigError(
127 "The %s in the [%s] section shouldn't start with 'deb'"
128 % (self.SOURCES_ENTRY_KEY, section_name))
129 except ConfigParser.NoOptionError:
130 raise HwpackConfigError(
131 "No %s in the [%s] section"
132 % (self.SOURCES_ENTRY_KEY, section_name))
133
134 def _validate_section_packages(self, section_name):
135 try:
136 packages = self.parser.get(section_name, self.PACKAGES_KEY)
137 if not packages:
138 raise HwpackConfigError(
139 "The %s in the [%s] section is empty"
140 % (self.PACKAGES_KEY, section_name))
141 for package in packages.split(" "):
142 if not package:
143 continue
144 if re.match(self.PACKAGE_REGEX, package) is None:
145 raise HwpackConfigError(
146 "Invalid value in %s in the [%s] section: %s"
147 % (self.PACKAGES_KEY, section_name, package))
148 except ConfigParser.NoOptionError:
149 raise HwpackConfigError(
150 "No %s in the [%s] section"
151 % (self.PACKAGES_KEY, section_name))
152
153 def _validate_section(self, section_name):
154 self._validate_section_sources_entry(section_name)
155 self._validate_section_packages(section_name)
156
157 def _validate_sections(self):
158 sections = self.parser.sections()
159 found = False
160 for section_name in sections:
161 if section_name == self.MAIN_SECTION:
162 continue
163 self._validate_section(section_name)
164 found = True
165 if not found:
166 raise HwpackConfigError(
167 "No sections other than [%s]" % self.MAIN_SECTION)
0168
=== added directory 'hwpack/tests'
=== added file 'hwpack/tests/__init__.py'
--- hwpack/tests/__init__.py 1970-01-01 00:00:00 +0000
+++ hwpack/tests/__init__.py 2010-08-27 20:28:42 +0000
@@ -0,0 +1,8 @@
1import unittest
2
3def test_suite():
4 module_names = ['hwpack.tests.test_config',
5 ]
6 loader = unittest.TestLoader()
7 suite = loader.loadTestsFromNames(module_names)
8 return suite
09
=== added file 'hwpack/tests/test_config.py'
--- hwpack/tests/test_config.py 1970-01-01 00:00:00 +0000
+++ hwpack/tests/test_config.py 2010-08-27 20:28:42 +0000
@@ -0,0 +1,196 @@
1from StringIO import StringIO
2
3from testtools import TestCase
4
5from hwpack.config import Config, HwpackConfigError
6
7
8class ConfigTests(TestCase):
9
10 def test_create(self):
11 config = Config(StringIO())
12
13 def get_config(self, contents):
14 return Config(StringIO(contents))
15
16 def assertConfigError(self, contents, f, *args, **kwargs):
17 e = self.assertRaises(HwpackConfigError, f, *args, **kwargs)
18 self.assertEqual(contents, str(e))
19
20 def assertValidationError(self, contents, config):
21 self.assertConfigError(contents, config.validate)
22
23 def test_validate_no_hwpack_section(self):
24 config = self.get_config("")
25 self.assertValidationError("No [hwpack] section", config)
26
27 def test_validate_no_name(self):
28 config = self.get_config("[hwpack]\n")
29 self.assertValidationError("No name in the [hwpack] section", config)
30
31 def test_validate_empty_name(self):
32 config = self.get_config("[hwpack]\nname = \n")
33 self.assertValidationError("Empty value for name", config)
34
35 def test_validate_invalid_name(self):
36 config = self.get_config("[hwpack]\nname = ~~\n")
37 self.assertValidationError("Invalid name: ~~", config)
38
39 def test_validate_invalid_include_debs(self):
40 config = self.get_config(
41 "[hwpack]\nname = ahwpack\n"
42 "include-debs = if you don't mind\n")
43 self.assertValidationError(
44 "Invalid value for include-debs: if you don't mind", config)
45
46 def test_validate_invalid_supported(self):
47 config = self.get_config(
48 "[hwpack]\nname = ahwpack\nsupport = if you pay us\n")
49 self.assertValidationError(
50 "Invalid value for support: if you pay us", config)
51
52 def test_validate_no_other_sections(self):
53 config = self.get_config("[hwpack]\nname = ahwpack\n")
54 self.assertValidationError(
55 "No sections other than [hwpack]", config)
56
57 def test_validate_other_section_no_sources_entry(self):
58 config = self.get_config(
59 "[hwpack]\nname = ahwpack\n\n[ubuntu]\n")
60 self.assertValidationError(
61 "No sources-entry in the [ubuntu] section", config)
62
63 def test_validate_other_section_empty_sources_entry(self):
64 config = self.get_config(
65 "[hwpack]\nname = ahwpack\n\n"
66 "[ubuntu]\nsources-entry = \n")
67 self.assertValidationError(
68 "The sources-entry in the [ubuntu] section is missing the URI",
69 config)
70
71 def test_validate_other_section_only_uri_in_sources_entry(self):
72 config = self.get_config(
73 "[hwpack]\nname = ahwpack\n\n"
74 "[ubuntu]\nsources-entry = foo\n")
75 self.assertValidationError(
76 "The sources-entry in the [ubuntu] section is missing the "
77 "distribution", config)
78
79 def test_validate_other_section_sources_entry_starting_with_deb(self):
80 config = self.get_config(
81 "[hwpack]\nname = ahwpack\n\n"
82 "[ubuntu]\nsources-entry = deb http://example.org/ "
83 "foo main\n")
84 self.assertValidationError(
85 "The sources-entry in the [ubuntu] section shouldn't start "
86 "with 'deb'", config)
87
88 def test_validate_other_section_sources_entry_starting_with_deb_src(self):
89 config = self.get_config(
90 "[hwpack]\nname = ahwpack\n\n"
91 "[ubuntu]\nsources-entry = deb-src http://example.org/ "
92 "foo main\n")
93 self.assertValidationError(
94 "The sources-entry in the [ubuntu] section shouldn't start "
95 "with 'deb'", config)
96
97 def test_validate_other_section_no_packages(self):
98 config = self.get_config(
99 "[hwpack]\nname = ahwpack\n\n"
100 "[ubuntu]\nsources-entry = foo bar\n")
101 self.assertValidationError(
102 "No packages in the [ubuntu] section", config)
103
104 def test_validate_other_section_empty_packages(self):
105 config = self.get_config(
106 "[hwpack]\nname = ahwpack\n\n"
107 "[ubuntu]\nsources-entry = foo bar\npackages = \n")
108 self.assertValidationError(
109 "The packages in the [ubuntu] section is empty",
110 config)
111
112 def test_validate_other_section_invalid_package_name(self):
113 config = self.get_config(
114 "[hwpack]\nname = ahwpack\n\n"
115 "[ubuntu]\nsources-entry = foo bar\n"
116 "packages = foo ~~ bar\n")
117 self.assertValidationError(
118 "Invalid value in packages in the [ubuntu] section: ~~",
119 config)
120
121 def test_validate_valid_config(self):
122 config = self.get_config(
123 "[hwpack]\nname = ahwpack\n\n"
124 "[ubuntu]\nsources-entry = foo bar\n"
125 "packages = foo bar\n")
126 self.assertEqual(None, config.validate())
127
128 def test_name(self):
129 config = self.get_config("[hwpack]\nname = ahwpack\n")
130 self.assertEqual("ahwpack", config.name)
131
132 def test_include_debs(self):
133 config = self.get_config(
134 "[hwpack]\nname = ahwpack\ninclude-debs = false\n")
135 self.assertEqual(False, config.include_debs)
136
137 def test_include_debs_defaults_true(self):
138 config = self.get_config(
139 "[hwpack]\nname = ahwpack\n")
140 self.assertEqual(True, config.include_debs)
141
142 def test_include_debs_defaults_true_on_empty(self):
143 config = self.get_config(
144 "[hwpack]\nname = ahwpack\ninclude-debs = \n")
145 self.assertEqual(True, config.include_debs)
146
147 def test_origin(self):
148 config = self.get_config(
149 "[hwpack]\nname = ahwpack\norigin = linaro\n")
150 self.assertEqual("linaro", config.origin)
151
152 def test_origin_default_None(self):
153 config = self.get_config(
154 "[hwpack]\nname = ahwpack\n")
155 self.assertEqual(None, config.origin)
156
157 def test_origin_None_on_empty(self):
158 config = self.get_config(
159 "[hwpack]\nname = ahwpack\norigin = \n")
160 self.assertEqual(None, config.origin)
161
162 def test_maintainer(self):
163 maintainer = "Linaro Developers <linaro-dev@lists.linaro.org>"
164 config = self.get_config(
165 "[hwpack]\nname = ahwpack\nmaintainer = %s\n" % maintainer)
166 self.assertEqual(maintainer, config.maintainer)
167
168 def test_maintainer_default_None(self):
169 config = self.get_config(
170 "[hwpack]\nname = ahwpack\n")
171 self.assertEqual(None, config.maintainer)
172
173 def test_maintainer_None_on_empty(self):
174 config = self.get_config(
175 "[hwpack]\nname = ahwpack\nmaintainer = \n")
176 self.assertEqual(None, config.maintainer)
177
178 def test_support_supported(self):
179 config = self.get_config(
180 "[hwpack]\nname = ahwpack\nsupport = supported\n")
181 self.assertEqual("supported", config.support)
182
183 def test_support_unsupported(self):
184 config = self.get_config(
185 "[hwpack]\nname = ahwpack\nsupport = unsupported\n")
186 self.assertEqual("unsupported", config.support)
187
188 def test_support_default_None(self):
189 config = self.get_config(
190 "[hwpack]\nname = ahwpack\n")
191 self.assertEqual(None, config.support)
192
193 def test_support_None_on_empty(self):
194 config = self.get_config(
195 "[hwpack]\nname = ahwpack\nsupport = \n")
196 self.assertEqual(None, config.support)

Subscribers

People subscribed via source and target branches