Merge lp:~jml/pkgme-devportal/remove-package-database into lp:pkgme-devportal
- remove-package-database
- Merge into trunk
Proposed by
Jonathan Lange
Status: | Merged |
---|---|
Merged at revision: | 146 |
Proposed branch: | lp:~jml/pkgme-devportal/remove-package-database |
Merge into: | lp:pkgme-devportal |
Prerequisite: | lp:~jml/pkgme-devportal/remove-database-code |
Diff against target: |
451 lines (+7/-363) 4 files modified
devportalbinary/database.py (+0/-122) devportalbinary/testing.py (+3/-139) devportalbinary/tests/test_database.py (+2/-102) setup.py (+2/-0) |
To merge this branch: | bzr merge lp:~jml/pkgme-devportal/remove-package-database |
Related bugs: |
Reviewer | Review Type | Date Requested | Status |
---|---|---|---|
pkgme binary committers | Pending | ||
Review via email: mp+134102@code.launchpad.net |
Commit message
Remove PackageDatabase and a bunch of test code.
Description of the change
Slowly killing things.
To post a comment you must log in.
- 178. By Jonathan Lange
-
More things that we don't need.
- 179. By Jonathan Lange
-
Merge changes from trunk.
Preview Diff
[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1 | === modified file 'devportalbinary/database.py' |
2 | --- devportalbinary/database.py 2012-11-13 14:35:23 +0000 |
3 | +++ devportalbinary/database.py 2012-11-13 14:35:23 +0000 |
4 | @@ -1,135 +1,13 @@ |
5 | # Copyright 2011 Canonical Ltd. This software is licensed under the |
6 | # GNU Affero General Public License version 3 (see the file LICENSE). |
7 | |
8 | -from storm.expr import And, Column, Select, Table |
9 | -from storm.locals import create_database, Store |
10 | -from storm.uri import URI as StormURI |
11 | - |
12 | from .configuration import ( |
13 | - CONF_FILE_ENV_VAR, |
14 | - get_config_file_path, |
15 | load_configuration, |
16 | ) |
17 | |
18 | from libdep_service_client.client import Client |
19 | |
20 | |
21 | -class URI(StormURI): |
22 | - """A stand-in for Storm's URI class. |
23 | - |
24 | - This class implements the same interface as `storm.uri.URI`, except |
25 | - that the constructor has a different signature. Storm's version takes |
26 | - a string and parses it, this version can be used when you already |
27 | - have a parsed version and just need to create the object. |
28 | - """ |
29 | - |
30 | - # XXX: Only used by PackageDatabase, which is flagged for deletion. |
31 | - |
32 | - def __init__(self, scheme=None, host=None, port=None, username=None, |
33 | - password=None, database=None, options=None): |
34 | - self.scheme = scheme |
35 | - self.host = host |
36 | - self.port = port |
37 | - self.username = username |
38 | - self.password = password |
39 | - self.database = database |
40 | - self.options = options |
41 | - if self.options is None: |
42 | - self.options = dict() |
43 | - |
44 | - |
45 | -class PackageDatabase(object): |
46 | - |
47 | - # XXX: No longer used within pkgme-devportal |
48 | - |
49 | - SQLITE = 'sqlite' |
50 | - POSTGRES = 'postgres' |
51 | - |
52 | - def __init__(self, store): |
53 | - self._store = store |
54 | - |
55 | - @classmethod |
56 | - def _get_storm_sqlite_connection_uri(cls, opts): |
57 | - raise ValueError( |
58 | - "SQLite is no longer supported, you must migrate to postgresql.") |
59 | - |
60 | - @classmethod |
61 | - def _get_storm_postgres_connection_uri(cls, opts): |
62 | - if not getattr(opts, 'database_db_name', None): |
63 | - raise ValueError( |
64 | - "Can't create database, no connection info available. " |
65 | - "You must specify %s. Looked in %s. " |
66 | - "Perhaps %s is set incorrectly?" % ( |
67 | - 'db_name', get_config_file_path(), CONF_FILE_ENV_VAR)) |
68 | - return URI(scheme=opts.database_db_type, |
69 | - username=opts.database_username, |
70 | - password=opts.database_password, |
71 | - host=opts.database_host, |
72 | - port=opts.database_port, |
73 | - database=opts.database_db_name) |
74 | - |
75 | - @classmethod |
76 | - def _get_storm_connection_uri(cls, opts): |
77 | - if opts.database_db_type == cls.POSTGRES: |
78 | - return cls._get_storm_postgres_connection_uri(opts) |
79 | - elif opts.database_db_type == cls.SQLITE: |
80 | - return cls._get_storm_sqlite_connection_uri(opts) |
81 | - else: |
82 | - raise AssertionError( |
83 | - "Unsupported database: %s" % opts.database_db_type) |
84 | - |
85 | - @classmethod |
86 | - def get_db_info_from_config(cls, opts): |
87 | - return cls._get_storm_connection_uri(opts) |
88 | - |
89 | - @classmethod |
90 | - def get_store_from_config(cls, opts): |
91 | - """Create a storm store based on a config file. |
92 | - |
93 | - This method will create a storm store based |
94 | - on the information in ``~/.config/pkgme-binary/conf`` |
95 | - |
96 | - :return: a tuple of (store, store_type), where store_type |
97 | - is one of cls.SQLITE or cls.POSTGRES, indicating what |
98 | - is at the other end of the store. |
99 | - """ |
100 | - connection_info = cls.get_db_info_from_config(opts) |
101 | - database = create_database(connection_info) |
102 | - return Store(database) |
103 | - |
104 | - @classmethod |
105 | - def from_options(cls, options): |
106 | - return cls(cls.get_store_from_config(options)) |
107 | - |
108 | - def _get_query(self, library_names, arch): |
109 | - return Select( |
110 | - [Column('library'), Column('dependency')], |
111 | - And(Column('architecture') == arch, |
112 | - Column('library').is_in(map(unicode, library_names))), |
113 | - Table('libdep')) |
114 | - |
115 | - def get_multiple_dependencies(self, library_names, arch): |
116 | - """Get the binary packages that provide libraries. |
117 | - |
118 | - :return: (deps, missing), where ``deps`` is a dict mapping library |
119 | - names to sets of packages that provide them, and ``missing`` is a |
120 | - set of library names for which no dependencies could be found. |
121 | - """ |
122 | - arch = unicode(arch) |
123 | - result = self._store.execute(self._get_query(library_names, arch)) |
124 | - found = {} |
125 | - for row in result: |
126 | - [lib, dependency] = row |
127 | - if lib in found: |
128 | - found[lib].add(dependency) |
129 | - else: |
130 | - found[lib] = set([dependency]) |
131 | - return found |
132 | - |
133 | - def close(self): |
134 | - self._store.close() |
135 | - |
136 | - |
137 | class LibdepServiceClient(object): |
138 | """Implements the read part of PackageDatabase's interface.""" |
139 | |
140 | |
141 | === modified file 'devportalbinary/testing.py' |
142 | --- devportalbinary/testing.py 2012-11-13 11:12:08 +0000 |
143 | +++ devportalbinary/testing.py 2012-11-13 14:35:23 +0000 |
144 | @@ -1,7 +1,6 @@ |
145 | # Copyright 2011-2012 Canonical Ltd. This software is licensed under the |
146 | # GNU Affero General Public License version 3 (see the file LICENSE). |
147 | |
148 | -from contextlib import closing |
149 | import json |
150 | import os |
151 | import random |
152 | @@ -21,11 +20,6 @@ |
153 | Fixture, |
154 | TempDir, |
155 | ) |
156 | -from postgresfixture import ClusterFixture |
157 | -from storm.locals import create_database, Store |
158 | -from testresources import ( |
159 | - FixtureResource as _FixtureResource, |
160 | - ) |
161 | from testtools import TestCase |
162 | from treeshape import ( |
163 | from_rough_spec, |
164 | @@ -33,11 +27,7 @@ |
165 | ) |
166 | |
167 | from devportalbinary.binary import MetadataBackend |
168 | -from devportalbinary.database import ( |
169 | - LibdepServiceClient, |
170 | - PackageDatabase, |
171 | - URI, |
172 | - ) |
173 | +from devportalbinary.database import LibdepServiceClient |
174 | |
175 | from devportalbinary.configuration import CONF_FILE_ENV_VAR |
176 | |
177 | @@ -90,133 +80,6 @@ |
178 | im.size[0], im.size[1], im.format)) |
179 | |
180 | |
181 | -def get_db_schema_file_path(name): |
182 | - return os.path.join(os.path.dirname( |
183 | - os.path.abspath(__file__)), 'db', name) |
184 | - |
185 | - |
186 | -def get_db_schema_queries(filenames): |
187 | - for filename in filenames: |
188 | - path = get_db_schema_file_path(filename) |
189 | - with open(path) as f: |
190 | - yield f.read() |
191 | - |
192 | - |
193 | -class PostgresDatabaseFixture(Fixture): |
194 | - |
195 | - def __init__(self): |
196 | - super(PostgresDatabaseFixture, self).__init__() |
197 | - self.db_name = "libdep" |
198 | - |
199 | - def drop_db(self): |
200 | - # stub suggests that dropping all tables would be quicker than |
201 | - # dropping the db when the number of tables is small. |
202 | - # select quote_ident(table_schema) || '.' || |
203 | - # quote_ident(table_name) from information_schema.tables |
204 | - # WHERE table_schema = 'public'; |
205 | - self.cluster.dropdb(self.db_name) |
206 | - |
207 | - def create_db(self): |
208 | - self.cluster.createdb(self.db_name) |
209 | - queries = [ |
210 | - 'postgres_schema.sql', |
211 | - 'patch-00001.sql', |
212 | - 'patch-00002.sql', |
213 | - ] |
214 | - for patch in get_db_schema_queries(queries): |
215 | - self._execute(patch) |
216 | - |
217 | - def _execute(self, query): |
218 | - with closing(self.cluster.connect(self.db_name)) as conn: |
219 | - cur = conn.cursor() |
220 | - cur.execute(query) |
221 | - conn.commit() |
222 | - |
223 | - def close_connection(self): |
224 | - self.conn.close() |
225 | - |
226 | - def open_connection(self): |
227 | - db = create_database(URI(scheme='postgres', |
228 | - host=self.cluster.datadir, database=self.db_name)) |
229 | - self.conn = Store(db) |
230 | - self.addCleanup(self.close_connection) |
231 | - |
232 | - def reset(self): |
233 | - self.close_connection() |
234 | - self.drop_db() |
235 | - self.create_db() |
236 | - self.open_connection() |
237 | - |
238 | - def setUp(self): |
239 | - super(PostgresDatabaseFixture, self).setUp() |
240 | - self.tempdir = self.useFixture(TempDir()) |
241 | - self.cluster = self.useFixture(ClusterFixture(self.tempdir.path)) |
242 | - self.create_db() |
243 | - self.open_connection() |
244 | - |
245 | - |
246 | -class FixtureResource(_FixtureResource): |
247 | - """The built in FixtureResource doesn't get properly dirtied.""" |
248 | - # XXX: workaround for bug 1023423 |
249 | - |
250 | - def _get_dirty(self): |
251 | - return True |
252 | - |
253 | - def _set_dirty(self, new_val): |
254 | - pass |
255 | - |
256 | - _dirty = property(_get_dirty, _set_dirty) |
257 | - |
258 | - |
259 | -class PostgresDatabaseResource(FixtureResource): |
260 | - |
261 | - def __init__(self): |
262 | - fixture = PostgresDatabaseFixture() |
263 | - super(PostgresDatabaseResource, self).__init__(fixture) |
264 | - |
265 | - def reset(self, resource, result=None): |
266 | - resource.reset() |
267 | - return resource |
268 | - |
269 | - |
270 | -postgres_db_resource = PostgresDatabaseResource() |
271 | - |
272 | - |
273 | -class DatabaseConfig(Fixture): |
274 | - |
275 | - def __init__(self, db_fixture): |
276 | - super(DatabaseConfig, self).__init__() |
277 | - self.db_fixture = db_fixture |
278 | - |
279 | - def setUp(self): |
280 | - super(DatabaseConfig, self).setUp() |
281 | - self.useFixture( |
282 | - ConfigSettings( |
283 | - ('database', {'db_type': 'postgres', |
284 | - 'host': self.db_fixture.cluster.datadir, |
285 | - 'db_name': self.db_fixture.db_name, |
286 | - }))) |
287 | - |
288 | - |
289 | -class DatabaseFixture(Fixture): |
290 | - """Create a temporary database and make it the default. |
291 | - |
292 | - Don't use this twice within a test, otherwise you'll get confused. |
293 | - """ |
294 | - |
295 | - def setUp(self): |
296 | - super(DatabaseFixture, self).setUp() |
297 | - pg_db = self.useFixture(PostgresDatabaseFixture()) |
298 | - self.useFixture(DatabaseConfig(pg_db)) |
299 | - self.db = PackageDatabase(pg_db.conn) |
300 | - self.addCleanup(self.db.close) |
301 | - |
302 | - |
303 | -def ConfigFileFixture(location): |
304 | - """Use a different configuration file.""" |
305 | - return EnvironmentVariableFixture(CONF_FILE_ENV_VAR, location) |
306 | - |
307 | - |
308 | class ConfigSettings(Fixture): |
309 | """Use a configuration file with different settings.""" |
310 | |
311 | @@ -239,7 +102,8 @@ |
312 | tempdir = self.useFixture(TempDir()) |
313 | config_file_path = os.path.join(tempdir.path, 'test.cfg') |
314 | write_config_file(config_file_path, self._settings) |
315 | - self.useFixture(ConfigFileFixture(config_file_path)) |
316 | + self.useFixture( |
317 | + EnvironmentVariableFixture(CONF_FILE_ENV_VAR, config_file_path)) |
318 | |
319 | |
320 | class LibdepFixture(Fixture): |
321 | |
322 | === modified file 'devportalbinary/tests/test_database.py' |
323 | --- devportalbinary/tests/test_database.py 2012-11-13 14:35:23 +0000 |
324 | +++ devportalbinary/tests/test_database.py 2012-11-13 14:35:23 +0000 |
325 | @@ -1,112 +1,12 @@ |
326 | -import os |
327 | |
328 | -from fixtures import TempDir |
329 | -from testresources import ResourcedTestCase |
330 | from testtools import TestCase |
331 | -from testtools.matchers import ( |
332 | - Equals, |
333 | - Matcher, |
334 | - ) |
335 | |
336 | -from devportalbinary.database import ( |
337 | - get_dependency_database, |
338 | - LibdepServiceClient, |
339 | - load_configuration, |
340 | - PackageDatabase, |
341 | - ) |
342 | -from devportalbinary.testing import ( |
343 | - ConfigFileFixture, |
344 | - ConfigSettings, |
345 | - get_libdep_service_client, |
346 | - postgres_db_resource, |
347 | - ) |
348 | +from devportalbinary.database import LibdepServiceClient |
349 | +from devportalbinary.testing import get_libdep_service_client |
350 | |
351 | from libdep_service_client.client import Client |
352 | |
353 | |
354 | -class ResultsIn(Matcher): |
355 | - |
356 | - def __init__(self, db, rows): |
357 | - self._db = db |
358 | - self._rows = rows |
359 | - |
360 | - def match(self, query): |
361 | - # XXX: Abstraction violation |
362 | - results = self._db._store.execute(query) |
363 | - return Equals(self._rows).match(list(results)) |
364 | - |
365 | - |
366 | -class TestDatabase(TestCase, ResourcedTestCase): |
367 | - |
368 | - resources = [ |
369 | - ('db_fixture', postgres_db_resource), |
370 | - ] |
371 | - |
372 | - def get_package_db(self): |
373 | - db = PackageDatabase(self.db_fixture.conn) |
374 | - self.addCleanup(db.close) |
375 | - return db |
376 | - |
377 | - def test_unknown_library(self): |
378 | - db = self.get_package_db() |
379 | - deps = db.get_multiple_dependencies(['libfoo.so.0'], 'i386') |
380 | - self.assertEqual(deps, {}) |
381 | - |
382 | - def test_close(self): |
383 | - # Test that we can close the package db. |
384 | - db = PackageDatabase(self.db_fixture.conn) |
385 | - db.close() |
386 | - |
387 | - def test_close_twice(self): |
388 | - # Test that we can close the package db twice with no exception. |
389 | - db = PackageDatabase(self.db_fixture.conn) |
390 | - db.close() |
391 | - db.close() |
392 | - |
393 | - |
394 | -class TestDatabaseConfiguration(TestCase): |
395 | - |
396 | - def use_database_config(self, **db_settings): |
397 | - return self.useFixture(ConfigSettings(('database', db_settings))) |
398 | - |
399 | - def test_get_db_info_from_config_sqlite(self): |
400 | - other_tempdir = self.useFixture(TempDir()) |
401 | - expected_db_path = os.path.join(other_tempdir.path, 'db') |
402 | - self.use_database_config(db_type='sqlite', path=expected_db_path) |
403 | - options = load_configuration() |
404 | - self.assertRaises(ValueError, PackageDatabase.get_db_info_from_config, |
405 | - options) |
406 | - |
407 | - def test_remote_service(self): |
408 | - base_url = 'http://example.com/libdep-service/' |
409 | - self.use_database_config(db_type='libdep-service', base_url=base_url) |
410 | - db = get_dependency_database() |
411 | - self.assertIsInstance(db, LibdepServiceClient) |
412 | - self.assertEqual(base_url, db._client.base_url) |
413 | - |
414 | - def test_get_db_info_from_config_postgres(self): |
415 | - expected_username = self.getUniqueString() |
416 | - expected_password = self.getUniqueString() |
417 | - expected_host = self.getUniqueString() |
418 | - expected_port = self.getUniqueInteger() |
419 | - expected_db_name = self.getUniqueString() |
420 | - |
421 | - self.use_database_config( |
422 | - db_type='postgres', |
423 | - username=expected_username, |
424 | - password=expected_password, |
425 | - host=expected_host, |
426 | - port=expected_port, |
427 | - db_name=expected_db_name) |
428 | - options = load_configuration() |
429 | - uri = PackageDatabase.get_db_info_from_config(options) |
430 | - self.assertEqual(expected_db_name, uri.database) |
431 | - self.assertEqual(expected_port, uri.port) |
432 | - self.assertEqual(expected_host, uri.host) |
433 | - self.assertEqual(expected_password, uri.password) |
434 | - self.assertEqual(expected_username, uri.username) |
435 | - |
436 | - |
437 | class TestLibdepServiceClient(TestCase): |
438 | |
439 | TEST_DATA = [('libfoo', {'i386': {'libfoo': 'libfoo-bin'}})] |
440 | |
441 | === modified file 'setup.py' |
442 | --- setup.py 2012-11-13 14:35:23 +0000 |
443 | +++ setup.py 2012-11-13 14:35:23 +0000 |
444 | @@ -18,6 +18,8 @@ |
445 | ) |
446 | from setuptools import setup, find_packages |
447 | |
448 | +# XXX: Need to do a trawl to see if we are depending on things that we are no |
449 | +# longer using. |
450 | |
451 | __version__ = get_version('devportalbinary/__init__.py') |
452 |