forked from pulp/pulp_python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsteps.py
More file actions
217 lines (183 loc) · 9.14 KB
/
Copy pathsteps.py
File metadata and controls
217 lines (183 loc) · 9.14 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import collections
import errno
from gettext import gettext as _
import json
import logging
import os
from xml.etree import cElementTree as ElementTree
from pulp.plugins.util.publish_step import AtomicDirectoryPublishStep, PluginStep
from pulp_python.common import constants
from pulp_python.plugins import models
from pulp_python.plugins.distributors import configuration
logger = logging.getLogger(__name__)
class PublishContentStep(PluginStep):
"""
Publish Content
"""
def __init__(self):
"""
Initialize the PublishContentStep.
"""
super(PublishContentStep, self).__init__(constants.PUBLISH_STEP_CONTENT)
self.context = None
self.redirect_context = None
self.description = _('Publishing Python Content.')
def process_main(self):
"""
Publish all the python files themselves by creating the symlinks to the storage paths.
"""
repo_id = self.get_conduit().repo_id
for package in models.Package.objects.packages_in_repo(repo_id):
symlink_path = os.path.join(self.parent.web_working_dir, 'packages', package.src_path)
try:
os.makedirs(os.path.dirname(symlink_path))
except OSError, e:
if e.errno != errno.EEXIST:
raise
os.symlink(package.storage_path, symlink_path)
class PublishMetadataStep(PluginStep):
"""
Publish Metadata (refs, branch heads, etc)
"""
def __init__(self):
"""
Initialize the PublishMetadataStep.
"""
super(PublishMetadataStep, self).__init__(constants.PUBLISH_STEP_METADATA)
self.context = None
self.redirect_context = None
self.description = _('Publishing Python Metadata.')
def process_main(self):
"""
Publish all the python metadata.
"""
repo_id = self.get_conduit().repo_id
projects = models.Package.objects.packages_by_project(repo_id)
self.write_simple_api(projects)
self.write_json_api(projects)
def write_simple_api(self, projects):
"""
Create a master index that contains references to the index of each project.
The structure of the data is designed to mimic the simple api of PyPI. The data will be
for all Python projects in the repository.
More information about this API can be found here: https://wiki.python.org/moin/PyPISimple
:param projects: keys are project names, values are packages of that name
:type projects: dict
"""
simple_path = os.path.join(self.parent.web_working_dir, 'simple')
simple_index_path = os.path.join(simple_path, 'index.html')
os.makedirs(simple_path)
with open(simple_index_path, 'w') as index:
html = ElementTree.Element('html')
head = ElementTree.SubElement(html, 'head')
title = ElementTree.SubElement(head, 'title')
title.text = 'Simple Index'
ElementTree.SubElement(head, 'meta', {'name': 'api-version', 'value': '2'})
body = ElementTree.SubElement(html, 'body')
# Create a reference in index.html that points to the index for each project.
for project_name, packages in projects.items():
element = ElementTree.SubElement(body, 'a', {'href': project_name})
element.text = project_name
ElementTree.SubElement(body, 'br')
PublishMetadataStep._create_project_index(project_name, simple_path, packages)
index.write(ElementTree.tostring(html, 'utf8'))
@staticmethod
def _create_project_index(project_name, simple_path, packages):
"""
Create a project index.html in a project subdirectory that contains references to each
package of the project.
:param project_name: The name of the project
:type project_name: basestring
:param simple_path: The path to the simple publish directory
:type simple_path: basestring
:param packages: A list of packages
:type packages: list of pulp_python.plugins.models.Package
"""
project_path = os.path.join(simple_path, project_name)
os.makedirs(project_path)
package_index_path = os.path.join(project_path, 'index.html')
with open(package_index_path, 'w') as package_index:
html = ElementTree.Element('html')
head = ElementTree.SubElement(html, 'head')
title = ElementTree.SubElement(head, 'title')
title.text = 'Links for %s' % project_name
ElementTree.SubElement(head, 'meta', {'name': 'api-version', 'value': '2'})
body = ElementTree.SubElement(html, 'body')
heading = ElementTree.SubElement(body, 'h1')
heading.text = title.text
for package in packages:
href = '../../packages/%s' % package.checksum_path
node = ElementTree.SubElement(body, 'a', {'href': href, 'rel': 'internal'})
node.text = package['filename']
ElementTree.SubElement(body, 'br')
package_index.write(ElementTree.tostring(html, 'utf8'))
def write_json_api(self, projects):
"""
Create a json file for each project in the repository.
The structure of the data is designed to mimic the json api of PyPI. The data will be
for a single Python project (eg. SciPy). The inner dictionary 'info' specifies details of
the project that should be applicable for all packages. The inner dictionary 'releases'
contains keys for each version and the value is a list of dictionaries, each representing
the metadata for a single package of that version.
More information on the PyPI API can be found here: https://wiki.python.org/moin/PyPIJSON
:param projects: keys are project names, values are lists of packages of that project
:type projects: dict
"""
api_path = os.path.join(self.parent.web_working_dir, 'pypi')
for project_name, packages in projects.items():
project_metadata_path = os.path.join(api_path, project_name, 'json')
os.makedirs(project_metadata_path)
project_index_metadata_path = os.path.join(project_metadata_path, 'index.json')
with open(project_index_metadata_path, 'w') as project_json:
data = PublishMetadataStep._create_project_metadata(project_name, packages)
json.dump(data, project_json, sort_keys=True, indent=4, separators=(',', ': '))
@staticmethod
def _create_project_metadata(name, packages):
"""
Generate metadata for the project and each of its packages.
:param name: Name of the project
:type name: basestring
:param packages: list of package to include
:type packages: list of pulp_python.plugins.models.Package
:return: metadata for the project and all of its packages
:rtype: dict
"""
releases = collections.defaultdict(list)
latest_version = None
for package in packages:
releases[package.version].append(package.package_specific_metadata)
# For all versions, version > None is True
if package.parsed_version > latest_version:
# Project level metadata is populated by the latest package
info = package.project_metadata
latest_version = package.parsed_version
return {'info': info, 'releases': releases}
class PythonPublisher(PluginStep):
"""
Publisher class that is responsible for the actual publishing
of a repository via a web server.
"""
def __init__(self, repo, publish_conduit, config):
"""
:param repo: Pulp managed Python repository
:type repo: pulp.plugins.model.Repository
:param publish_conduit: Conduit providing access to relative Pulp functionality
:type publish_conduit: pulp.plugins.conduits.repo_publish.RepoPublishConduit
:param config: Pulp configuration for the distributor
:type config: pulp.plugins.config.PluginCallConfiguration
"""
super(PythonPublisher, self).__init__(constants.PUBLISH_STEP_PUBLISHER,
repo, publish_conduit, config)
publish_dir = configuration.get_web_publish_dir(repo, config)
if not os.path.exists(self.get_working_dir()):
os.makedirs(self.get_working_dir())
self.web_working_dir = os.path.join(self.get_working_dir(), repo.id)
master_publish_dir = configuration.get_master_publish_dir(repo, config)
atomic_publish_step = AtomicDirectoryPublishStep(self.get_working_dir(),
[(repo.id, publish_dir)],
master_publish_dir,
step_type=constants.PUBLISH_STEP_OVER_HTTP)
atomic_publish_step.description = _('Making files available via web.')
self.add_child(PublishMetadataStep())
self.add_child(PublishContentStep())
self.add_child(atomic_publish_step)