forked from ammaritiz/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquerysets.py
More file actions
56 lines (45 loc) · 1.87 KB
/
querysets.py
File metadata and controls
56 lines (45 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import itertools
import json
from pulp.server.controllers import repository as repo_controller
from pulp.server.db.querysets import QuerySetPreventCache
class PythonPackageQuerySet(QuerySetPreventCache):
"""
Custom querysets for Python packages.
"""
def packages_in_repo(self, repo_id):
"""
Query for all Python packages in a repository.
:param repo_id: Identifies the repository from which to retrieve packages
:type repo_id: basestring
:return: QuerySet containing each package in the repo
:rtype: mongoengine.queryset.QuerySet
"""
unit_qs = repo_controller.get_unit_model_querysets(repo_id, self._document)
return itertools.chain(*unit_qs)
def packages_by_project(self, repo_id):
"""
Query for all Python packages, organized by project.
:param repo_id: Identifies the repository from which to retrieve packages
:type repo_id: basestring
:return: Dictionary containing packages in the repo, organized by project name
:rtype: dict
"""
packages = self.packages_in_repo(repo_id)
projects = {}
for pkg in packages:
projects.setdefault(pkg.name, []).append(pkg)
return projects
def from_metadata(self, metadata):
"""
Create all the packages described in the project's json metadata.
:param metadata: Project metadata in JSON format that describes each available package.
:type metadata: basestring
:return: list of Python Packages
:rtype: list of models.Package objects
"""
metadata = json.loads(metadata)
units = []
for version, packages in metadata['releases'].items():
for package in packages:
units.append(self._document.from_json(package, version, metadata['info']))
return units