-
-
Notifications
You must be signed in to change notification settings - Fork 792
Expand file tree
/
Copy pathgen_pypi_simple.py
More file actions
220 lines (169 loc) · 6.84 KB
/
Copy pathgen_pypi_simple.py
File metadata and controls
220 lines (169 loc) · 6.84 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
218
219
220
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# SPDX-License-Identifier: BSD-2-Clause-Views AND MIT
# Copyright (c) 2010 David Wolever <david@wolever.net>. All rights reserved.
# originally from https://github.com/wolever/pip2pi
import os
import re
import shutil
from html import escape
from pathlib import Path
"""
name: pip compatibility tags
version: 20.3.1
download_url: https://github.com/pypa/pip/blob/20.3.1/src/pip/_internal/models/wheel.py
copyright: Copyright (c) 2008-2020 The pip developers (see AUTHORS.txt file)
license_expression: mit
notes: the weel name regex is copied from pip-20.3.1 pip/_internal/models/wheel.py
Copyright (c) 2008-2020 The pip developers (see AUTHORS.txt file)
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
"""
get_wheel_from_filename = re.compile(
r"""^(?P<namever>(?P<name>.+?)-(?P<version>.*?))
((-(?P<build>\d[^-]*?))?-(?P<pyvers>.+?)-(?P<abis>.+?)-(?P<plats>.+?)
\.whl)$""",
re.VERBOSE,
).match
sdist_exts = (
".tar.gz",
".tar.bz2",
".zip",
".tar.xz",
)
wheel_ext = ".whl"
app_ext = ".pyz"
dist_exts = sdist_exts + (wheel_ext, app_ext)
class InvalidDistributionFilename(Exception):
pass
def get_package_name_from_filename(filename, normalize=True):
"""
Return the package name extracted from a package ``filename``.
Optionally ``normalize`` the name according to distribution name rules.
Raise an ``InvalidDistributionFilename`` if the ``filename`` is invalid::
>>> get_package_name_from_filename("aboutcode_toolkit-5.1.0-py2.py3-none-any.whl")
'aboutcode-toolkit'
>>> get_package_name_from_filename("boolean.py-3.7-py2.py3-none-any.whl")
'boolean-py'
>>> get_package_name_from_filename("boolean.py-3.7.tar.gz")
'boolean-py'
>>> get_package_name_from_filename("foo-1.2.3_rc1.tar.gz")
'foo'
>>> get_package_name_from_filename("foo_bar-1.2-py27-none-any.whl")
'foo-bar'
>>> get_package_name_from_filename("foo.py-1.2-py27-none-any.whl")
'foo-py'
>>> get_package_name_from_filename("Cython-0.17.2-cp26-none-linux_x86_64.whl")
'cython'
>>> get_package_name_from_filename("python_ldap-2.4.19-cp27-none-macosx_10_10_x86_64.whl")
'python-ldap'
"""
if not filename or not filename.endswith(dist_exts):
raise InvalidDistributionFilename(filename)
filename = os.path.basename(filename)
if filename.endswith(sdist_exts):
name_ver = None
extension = None
for ext in sdist_exts:
if filename.endswith(ext):
name_ver, extension, _ = filename.rpartition(ext)
break
if not extension or not name_ver:
raise InvalidDistributionFilename(filename)
name, _, version = name_ver.rpartition("-")
if not (name and version):
raise InvalidDistributionFilename(filename)
elif filename.endswith(wheel_ext):
wheel_info = get_wheel_from_filename(filename)
if not wheel_info:
raise InvalidDistributionFilename(filename)
name = wheel_info.group("name")
version = wheel_info.group("version")
if not (name and version):
raise InvalidDistributionFilename(filename)
elif filename.endswith(app_ext):
name_ver, extension, _ = filename.rpartition(".pyz")
if "-" in filename:
name, _, version = name_ver.rpartition("-")
else:
name = name_ver
if not name:
raise InvalidDistributionFilename(filename)
if normalize:
name = normalize_name(name)
return name
def normalize_name(name):
"""
Return a normalized package name per PEP503, and copied from
https://www.python.org/dev/peps/pep-0503/#id4
"""
return name and re.sub(r"[-_.]+", "-", name).lower() or name
def normalize_name_plain(name):
"""
Return a normalized package name, but do not replace dots
"""
return name and re.sub(r"[-_]+", "-", name).lower() or name
def build_pypi_index(directory):
"""
Using a ``directory`` directory of wheels and sdists, create the a PyPI
simple directory index at ``directory``/simple/ populated with the proper
PyPI simple index directory structure crafted using symlinks.
WARNING: The ``directory``/simple/ directory is removed if it exists.
"""
directory = Path(directory)
index_dir = directory / "simple"
if index_dir.exists():
shutil.rmtree(str(index_dir), ignore_errors=True)
index_dir.mkdir(parents=True)
simple_html_index = [
"<html>"
"<head>"
"<title>PyPI Simple Index</title>",
'<meta charset="UTF-8">'
'<meta name="api-version" value="2" />'
"</head>"
"<body>",
]
package_names = set()
for pkg_file in directory.iterdir():
pkg_filename = pkg_file.name
if (
not pkg_file.is_file()
or not pkg_filename.endswith(dist_exts)
or pkg_filename.startswith(".")
):
continue
original_name = get_package_name_from_filename(pkg_filename, normalize=False)
pkg_dir_name = normalize_name(original_name)
pkg_link_name = normalize_name_plain(original_name)
pkg_index_dir = index_dir / pkg_dir_name
pkg_index_dir.mkdir(parents=True, exist_ok=True)
pkg_indexed_file = pkg_index_dir / pkg_filename
link_target = Path("../..") / pkg_filename
pkg_indexed_file.symlink_to(link_target)
if pkg_link_name not in package_names:
esc_dir = escape(pkg_dir_name)
esc_link = escape(pkg_link_name)
simple_html_index.append(f'<a href="{esc_dir}/">{esc_link}</a><br/>')
package_names.add(pkg_link_name)
simple_html_index.append("</body></html>")
index_html = index_dir / "index.html"
index_html.write_text("\n".join(simple_html_index))
if __name__ == "__main__":
import sys
pkg_dir = sys.argv[1]
build_pypi_index(pkg_dir)