summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorOlof Johansson <olof.johansson@axis.com>2014-01-20 12:03:22 +0100
committerRichard Purdie <richard.purdie@linuxfoundation.org>2014-03-24 17:54:46 +0000
commitaca2d14e9350fde58aaaa95cdeff5b051c1d38d3 (patch)
tree503fbfed656119128b0632b59fdbfc66fb7095d9
parent64fdd3abbbe9ab99c3bc5083d4172ea7bc17bae3 (diff)
downloadpoky-aca2d14e9350fde58aaaa95cdeff5b051c1d38d3.tar.gz
bitbake: fetch2.URI: add support for query parameters
This change introduces the .query property of the URI class. It is a read/write dict of the parameters supplied in the query string of the URI. E.g.: http://example.com/?foo=bar => .query = {'foo': 'bar'} (Bitbake rev: 1cb2b3c458c8c5521591d2c8f2e0058143fc77bb) Signed-off-by: Olof Johansson <olof.johansson@axis.com> Signed-off-by: Richard Purdie <richard.purdie@linuxfoundation.org>
-rw-r--r--bitbake/lib/bb/fetch2/__init__.py38
-rw-r--r--bitbake/lib/bb/tests/fetch.py30
2 files changed, 53 insertions, 15 deletions
diff --git a/bitbake/lib/bb/fetch2/__init__.py b/bitbake/lib/bb/fetch2/__init__.py
index 5c6180bbb7..d9d193ab12 100644
--- a/bitbake/lib/bb/fetch2/__init__.py
+++ b/bitbake/lib/bb/fetch2/__init__.py
@@ -162,6 +162,7 @@ class URI(object):
162 * path_quoted (read/write) 162 * path_quoted (read/write)
163 A URI quoted version of path 163 A URI quoted version of path
164 * params (dict) (read/write) 164 * params (dict) (read/write)
165 * query (dict) (read/write)
165 * relative (bool) (read only) 166 * relative (bool) (read only)
166 True if this is a "relative URI", (e.g. file:foo.diff) 167 True if this is a "relative URI", (e.g. file:foo.diff)
167 168
@@ -201,6 +202,7 @@ class URI(object):
201 self.port = None 202 self.port = None
202 self._path = '' 203 self._path = ''
203 self.params = {} 204 self.params = {}
205 self.query = {}
204 self.relative = False 206 self.relative = False
205 207
206 if not uri: 208 if not uri:
@@ -253,36 +255,42 @@ class URI(object):
253 self.path = urllib.unquote(path) 255 self.path = urllib.unquote(path)
254 256
255 if param_str: 257 if param_str:
256 self.params = self._param_dict(param_str) 258 self.params = self._param_str_split(param_str, ";")
259 if urlp.query:
260 self.query = self._param_str_split(urlp.query, "&")
257 261
258 def __str__(self): 262 def __str__(self):
259 userinfo = self.userinfo 263 userinfo = self.userinfo
260 if userinfo: 264 if userinfo:
261 userinfo += '@' 265 userinfo += '@'
262 266
263 return "%s:%s%s%s%s%s" % ( 267 return "%s:%s%s%s%s%s%s" % (
264 self.scheme, 268 self.scheme,
265 '' if self.relative else '//', 269 '' if self.relative else '//',
266 userinfo, 270 userinfo,
267 self.hostport, 271 self.hostport,
268 self.path_quoted, 272 self.path_quoted,
269 self._param_str) 273 self._query_str(),
274 self._param_str())
270 275
271 @property
272 def _param_str(self): 276 def _param_str(self):
273 ret = '' 277 return (
274 for key, val in self.params.items(): 278 ''.join([';', self._param_str_join(self.params, ";")])
275 ret += ";%s=%s" % (key, val) 279 if self.params else '')
280
281 def _query_str(self):
282 return (
283 ''.join(['?', self._param_str_join(self.query, "&")])
284 if self.query else '')
285
286 def _param_str_split(self, string, elmdelim, kvdelim="="):
287 ret = {}
288 for k, v in [x.split(kvdelim, 1) for x in string.split(elmdelim)]:
289 ret[k] = v
276 return ret 290 return ret
277 291
278 def _param_dict(self, param_str): 292 def _param_str_join(self, dict_, elmdelim, kvdelim="="):
279 parm = {} 293 return elmdelim.join([kvdelim.join([k, v]) for k, v in dict_.items()])
280
281 for keyval in param_str.split(";"):
282 key, val = keyval.split("=", 1)
283 parm[key] = val
284
285 return parm
286 294
287 @property 295 @property
288 def hostport(self): 296 def hostport(self):
diff --git a/bitbake/lib/bb/tests/fetch.py b/bitbake/lib/bb/tests/fetch.py
index f93a636586..15fe0ab2f2 100644
--- a/bitbake/lib/bb/tests/fetch.py
+++ b/bitbake/lib/bb/tests/fetch.py
@@ -39,6 +39,7 @@ class URITest(unittest.TestCase):
39 'username': '', 39 'username': '',
40 'password': '', 40 'password': '',
41 'params': {}, 41 'params': {},
42 'query': {},
42 'relative': False 43 'relative': False
43 }, 44 },
44 "http://www.google.com/index.html;param1=value1" : { 45 "http://www.google.com/index.html;param1=value1" : {
@@ -54,6 +55,23 @@ class URITest(unittest.TestCase):
54 'params': { 55 'params': {
55 'param1': 'value1' 56 'param1': 'value1'
56 }, 57 },
58 'query': {},
59 'relative': False
60 },
61 "http://www.example.org/index.html?param1=value1" : {
62 'uri': 'http://www.example.org/index.html?param1=value1',
63 'scheme': 'http',
64 'hostname': 'www.example.org',
65 'port': None,
66 'hostport': 'www.example.org',
67 'path': '/index.html',
68 'userinfo': '',
69 'username': '',
70 'password': '',
71 'params': {},
72 'query': {
73 'param1': 'value1'
74 },
57 'relative': False 75 'relative': False
58 }, 76 },
59 "http://www.example.com:8080/index.html" : { 77 "http://www.example.com:8080/index.html" : {
@@ -67,6 +85,7 @@ class URITest(unittest.TestCase):
67 'username': '', 85 'username': '',
68 'password': '', 86 'password': '',
69 'params': {}, 87 'params': {},
88 'query': {},
70 'relative': False 89 'relative': False
71 }, 90 },
72 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : { 91 "cvs://anoncvs@cvs.handhelds.org/cvs;module=familiar/dist/ipkg" : {
@@ -82,6 +101,7 @@ class URITest(unittest.TestCase):
82 'params': { 101 'params': {
83 'module': 'familiar/dist/ipkg' 102 'module': 'familiar/dist/ipkg'
84 }, 103 },
104 'query': {},
85 'relative': False 105 'relative': False
86 }, 106 },
87 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": { 107 "cvs://anoncvs:anonymous@cvs.handhelds.org/cvs;tag=V0-99-81;module=familiar/dist/ipkg": {
@@ -98,6 +118,7 @@ class URITest(unittest.TestCase):
98 'tag': 'V0-99-81', 118 'tag': 'V0-99-81',
99 'module': 'familiar/dist/ipkg' 119 'module': 'familiar/dist/ipkg'
100 }, 120 },
121 'query': {},
101 'relative': False 122 'relative': False
102 }, 123 },
103 "file://example.diff": { # NOTE: Not RFC compliant! 124 "file://example.diff": { # NOTE: Not RFC compliant!
@@ -111,6 +132,7 @@ class URITest(unittest.TestCase):
111 'username': '', 132 'username': '',
112 'password': '', 133 'password': '',
113 'params': {}, 134 'params': {},
135 'query': {},
114 'relative': True 136 'relative': True
115 }, 137 },
116 "file:example.diff": { # NOTE: RFC compliant version of the former 138 "file:example.diff": { # NOTE: RFC compliant version of the former
@@ -125,6 +147,7 @@ class URITest(unittest.TestCase):
125 'username': '', 147 'username': '',
126 'password': '', 148 'password': '',
127 'params': {}, 149 'params': {},
150 'query': {},
128 'relative': True 151 'relative': True
129 }, 152 },
130 "file:///tmp/example.diff": { 153 "file:///tmp/example.diff": {
@@ -139,6 +162,7 @@ class URITest(unittest.TestCase):
139 'username': '', 162 'username': '',
140 'password': '', 163 'password': '',
141 'params': {}, 164 'params': {},
165 'query': {},
142 'relative': False 166 'relative': False
143 }, 167 },
144 "git:///path/example.git": { 168 "git:///path/example.git": {
@@ -153,6 +177,7 @@ class URITest(unittest.TestCase):
153 'username': '', 177 'username': '',
154 'password': '', 178 'password': '',
155 'params': {}, 179 'params': {},
180 'query': {},
156 'relative': False 181 'relative': False
157 }, 182 },
158 "git:path/example.git": { 183 "git:path/example.git": {
@@ -167,6 +192,7 @@ class URITest(unittest.TestCase):
167 'username': '', 192 'username': '',
168 'password': '', 193 'password': '',
169 'params': {}, 194 'params': {},
195 'query': {},
170 'relative': True 196 'relative': True
171 }, 197 },
172 "git://example.net/path/example.git": { 198 "git://example.net/path/example.git": {
@@ -181,6 +207,7 @@ class URITest(unittest.TestCase):
181 'username': '', 207 'username': '',
182 'password': '', 208 'password': '',
183 'params': {}, 209 'params': {},
210 'query': {},
184 'relative': False 211 'relative': False
185 } 212 }
186 } 213 }
@@ -243,6 +270,9 @@ class URITest(unittest.TestCase):
243 uri.params = test['params'] 270 uri.params = test['params']
244 self.assertEqual(uri.params, test['params']) 271 self.assertEqual(uri.params, test['params'])
245 272
273 uri.query = test['query']
274 self.assertEqual(uri.query, test['query'])
275
246 self.assertEqual(str(uri), test['uri']) 276 self.assertEqual(str(uri), test['uri'])
247 277
248 uri.params = {} 278 uri.params = {}