Changeset View
Changeset View
Standalone View
Standalone View
swh/storage/storage.py
Show First 20 Lines • Show All 1,340 Lines • ▼ Show 20 Lines | |||||
origin_keys = ['id', 'type', 'url'] | origin_keys = ['id', 'type', 'url'] | ||||
@db_transaction(statement_timeout=500) | @db_transaction(statement_timeout=500) | ||||
def origin_get(self, origins, db=None, cur=None): | def origin_get(self, origins, db=None, cur=None): | ||||
"""Return origins, either all identified by their ids or all | """Return origins, either all identified by their ids or all | ||||
identified by tuples (type, url). | identified by tuples (type, url). | ||||
If the url is given and the type is omitted, one of the origins with | |||||
that url is returned. | |||||
Args: | Args: | ||||
origin: a list of dictionaries representing the individual | origin: a list of dictionaries representing the individual | ||||
origins to find. | origins to find. | ||||
These dicts have either the keys type and url: | These dicts have either the key url (and optionally type): | ||||
- type (FIXME: enum TBD): the origin type ('git', 'wget', ...) | - type (FIXME: enum TBD): the origin type ('git', 'wget', ...) | ||||
- url (bytes): the url the origin points to | - url (bytes): the url the origin points to | ||||
or the id: | or the id: | ||||
- id: the origin id | - id: the origin id | ||||
Show All 22 Lines | def origin_get(self, origins, db=None, cur=None): | ||||
for origin in origins] | for origin in origins] | ||||
if any(origin_ids): | if any(origin_ids): | ||||
# Lookup per ID | # Lookup per ID | ||||
if all(origin_ids): | if all(origin_ids): | ||||
results = db.origin_get(origin_ids, cur) | results = db.origin_get(origin_ids, cur) | ||||
else: | else: | ||||
raise ValueError( | raise ValueError( | ||||
'Either all origins or none at all should have an "id".') | 'Either all origins or none at all should have an "id".') | ||||
elif any(type_ and url for (type_, url) in origin_types_and_urls): | elif any(url for (type_, url) in origin_types_and_urls): | ||||
# Lookup per type + URL | # Lookup per type + URL | ||||
if all(type_ and url for (type_, url) in origin_types_and_urls): | if all(url for (type_, url) in origin_types_and_urls): | ||||
results = db.origin_get_with(origin_types_and_urls, cur) | results = db.origin_get_with(origin_types_and_urls, cur) | ||||
else: | else: | ||||
raise ValueError( | raise ValueError( | ||||
'Either all origins or none at all should have a ' | 'Either all origins or none at all should have ' | ||||
'"type" and an "url".') | 'an "url" key.') | ||||
else: # unsupported lookup | else: # unsupported lookup | ||||
raise ValueError('Origin must have either id or (type and url).') | raise ValueError('Origin must have either id or url.') | ||||
results = [dict(zip(self.origin_keys, result)) | results = [dict(zip(self.origin_keys, result)) | ||||
for result in results] | for result in results] | ||||
if return_single: | if return_single: | ||||
assert len(results) == 1 | assert len(results) == 1 | ||||
if results[0]['id'] is not None: | if results[0]['id'] is not None: | ||||
return results[0] | return results[0] | ||||
else: | else: | ||||
▲ Show 20 Lines • Show All 392 Lines • Show Last 20 Lines |