Skip to content

Instantly share code, notes, and snippets.

@jflemer-ndp
Last active July 13, 2019 06:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jflemer-ndp/335ac1c114b8b7d3b09614b2669ec843 to your computer and use it in GitHub Desktop.
Save jflemer-ndp/335ac1c114b8b7d3b09614b2669ec843 to your computer and use it in GitHub Desktop.
Make a PEP503 simple python pypi repo
#!/usr/bin/env python
# Copyright 2019, NDP LLC
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
# this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from this
# software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# Make a PEP503 simple python pypi repo
from __future__ import print_function
import os, sys
import re
import collections
import textwrap
from stat import *
INDEXDIR = 'simple'
def pep503normalize(name):
# https://www.python.org/dev/peps/pep-0503/#normalized-names
return re.sub(r"[-_.]+", "-", name).lower()
def find_packages(repodir):
repodata = collections.OrderedDict()
split_bits = re.compile(r"^(.*?)-(\d.*)$")
oldcwd = os.getcwd()
try:
os.chdir(repodir)
for root, dirs, files in os.walk('.'):
root = os.path.normpath(root)
if root.startswith(INDEXDIR + os.sep) or root == INDEXDIR:
continue
for filename in files:
if filename == 'index.html':
continue
sb = os.stat(os.path.join(root, filename))
if S_ISREG(sb.st_mode) and not S_ISLNK(sb.st_mode):
m = split_bits.match(filename)
if m is not None:
pkg = pep503normalize(m.group(1))
repodata.setdefault(pkg, []).append([root, filename])
finally:
try:
os.chdir(oldcwd)
except:
pass
return repodata
def elem_sort_key(elem):
return [elem[0]] + [int(x) if x.isdigit() else x for x in elem[1].split('.')]
def make_index(repodir, pkg, elems):
try:
os.mkdir(os.path.join(repodir, INDEXDIR, pkg))
except:
pass
with open(os.path.join(repodir, INDEXDIR, pkg, 'index.html'), 'w') as html:
html.write(textwrap.dedent('''\
<!DOCTYPE html>
<html>
<head>
<title>Links for {0}</title>
</head>
<body>
<h1>Links for {0}</h1>
''').format(pkg))
for elem in sorted(elems, key=lambda x: elem_sort_key(x)):
linkref = os.path.normpath(os.path.join('..', '..', elem[0], elem[1]))
html.write(" <a href=\"{1}\">{0}</a><br/>\n".format(elem[1], linkref))
index = os.path.normpath(os.path.join(INDEXDIR, pkg, elem[1]))
print(index + " -> " + linkref)
html.write(textwrap.dedent('''\
</body>
</html>'''))
def main():
try:
repodir = sys.argv[1]
except:
repodir = "."
for pkg, elems in find_packages(repodir).items():
make_index(repodir, pkg, elems)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment