MCPcopy Create free account
hub / github.com/aboutcode-org/scancode-toolkit / build_pypi_index

Function build_pypi_index

etc/scripts/gen_pypi_simple.py:177–258  ·  view source on GitHub ↗

Create the a PyPI simple directory index using a ``directory`` directory of wheels and sdists in the direvctory at ``directory``/simple/ populated with the proper PyPI simple index directory structure crafted using symlinks. WARNING: The ``directory``/simple/ directory is removed i

(directory, base_url="https://thirdparty.aboutcode.org/pypi")

Source from the content-addressed store, hash-verified

175
176
177def build_pypi_index(directory, base_url="https://thirdparty.aboutcode.org/pypi"):
178 """
179 Create the a PyPI simple directory index using a ``directory`` directory of wheels and sdists in
180 the direvctory at ``directory``/simple/ populated with the proper PyPI simple index directory
181 structure crafted using symlinks.
182
183 WARNING: The ``directory``/simple/ directory is removed if it exists. NOTE: in addition to the a
184 PyPI simple index.html there is also a links.html index file generated which is suitable to use
185 with pip's --find-links
186 """
187
188 directory = Path(directory)
189
190 index_dir = directory / "simple"
191 if index_dir.exists():
192 shutil.rmtree(str(index_dir), ignore_errors=True)
193
194 index_dir.mkdir(parents=True)
195 packages_by_package_name = defaultdict(list)
196
197 # generate the main simple index.html
198 simple_html_index = [
199 "<!DOCTYPE html>",
200 "<html><head><title>PyPI Simple Index</title>",
201 '<meta charset="UTF-8"><meta name="api-version" value="2" /></head><body>',
202 ]
203
204 for pkg_file in directory.iterdir():
205 pkg_filename = pkg_file.name
206
207 if (
208 not pkg_file.is_file()
209 or not pkg_filename.endswith(dist_exts)
210 or pkg_filename.startswith(".")
211 ):
212 continue
213
214 pkg_name = get_package_name_from_filename(
215 filename=pkg_filename,
216 )
217 pkg_index_dir = index_dir / pkg_name
218 pkg_index_dir.mkdir(parents=True, exist_ok=True)
219 pkg_indexed_file = pkg_index_dir / pkg_filename
220
221 link_target = Path("../..") / pkg_filename
222 pkg_indexed_file.symlink_to(link_target)
223
224 if pkg_name not in packages_by_package_name:
225 esc_name = escape(pkg_name)
226 simple_html_index.append(f'<a href="{esc_name}/">{esc_name}</a><br/>')
227
228 packages_by_package_name[pkg_name].append(
229 Package.from_file(
230 name=pkg_name,
231 index_dir=pkg_index_dir,
232 archive_file=pkg_file,
233 )
234 )

Callers 1

gen_pypi_simple.pyFile · 0.85

Calls 7

build_per_package_indexFunction · 0.85
existsMethod · 0.45
appendMethod · 0.45
from_fileMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected