| 1 |
#!/usr/bin/env python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
""" |
| 4 |
Utility script to create a pypi-like directory structure (localpi) |
| 5 |
from a number of Python packages in a directory of the local filesystem. |
| 6 |
|
| 7 |
DIRECTORY STRUCTURE (before): |
| 8 |
+-- downloads/ |
| 9 |
+-- alice-1.0.zip |
| 10 |
+-- alice-1.0.tar.gz |
| 11 |
+-- bob-1.3.0.tar.gz |
| 12 |
+-- bob-1.4.2.tar.gz |
| 13 |
+-- charly-1.0.tar.bz2 |
| 14 |
|
| 15 |
DIRECTORY STRUCTURE (afterwards): |
| 16 |
+-- downloads/ |
| 17 |
+-- simple/ |
| 18 |
| +-- alice/index.html --> ../../alice-*.* |
| 19 |
| +-- bob/index.html --> ../../bob-*.* |
| 20 |
| +-- charly/index.html --> ../../charly-*.* |
| 21 |
| +-- index.html --> alice/, bob/, ... |
| 22 |
+-- alice-1.0.zip |
| 23 |
+-- alice-1.0.tar.gz |
| 24 |
+-- bob-1.3.0.tar.gz |
| 25 |
+-- bob-1.4.2.tar.gz |
| 26 |
+-- charly-1.0.tar.bz2 |
| 27 |
|
| 28 |
USAGE EXAMPLE: |
| 29 |
|
| 30 |
mkdir -p /tmp/downloads |
| 31 |
pip install --download=/tmp/downloads argparse Jinja2 |
| 32 |
make_localpi.py /tmp/downloads |
| 33 |
pip install --index-url=file:///tmp/downloads/simple argparse Jinja2 |
| 34 |
|
| 35 |
ALTERNATIVE: |
| 36 |
|
| 37 |
pip install --download=/tmp/downloads argparse Jinja2 |
| 38 |
pip install --find-links=/tmp/downloads --no-index argparse Jinja2 |
| 39 |
""" |
| 40 |
|
| 41 |
from __future__ import absolute_import, print_function, with_statement |
| 42 |
from fnmatch import fnmatch |
| 43 |
import os.path |
| 44 |
import shutil |
| 45 |
import sys |
| 46 |
|
| 47 |
|
| 48 |
__author__ = "Jens Engel" |
| 49 |
__version__ = "0.2" |
| 50 |
__license__ = "BSD" |
| 51 |
__copyright__ = "(c) 2013 by Jens Engel" |
| 52 |
|
| 53 |
|
| 54 |
class Package(object): |
| 55 |
""" |
| 56 |
Package entity that keeps track of: |
| 57 |
* one or more versions of this package |
| 58 |
* one or more archive types |
| 59 |
""" |
| 60 |
PATTERNS = [ |
| 61 |
"*.egg", "*.exe", "*.whl", "*.zip", "*.tar.gz", "*.tar.bz2", "*.7z" |
| 62 |
] |
| 63 |
|
| 64 |
def __init__(self, filename, name=None): |
| 65 |
if not name and filename: |
| 66 |
name = self.get_pkgname(filename) |
| 67 |
self.name = name |
| 68 |
self.files = [] |
| 69 |
if filename: |
| 70 |
self.files.append(filename) |
| 71 |
|
| 72 |
@property |
| 73 |
def versions(self): |
| 74 |
versions_info = [ self.get_pkgversion(p) for p in self.files ] |
| 75 |
return versions_info |
| 76 |
|
| 77 |
@classmethod |
| 78 |
def split_pkgname_parts(cls, filename): |
| 79 |
basename = cls.splitext(os.path.basename(filename)) |
| 80 |
if basename.startswith("http") and r"%2F" in basename: |
| 81 |
# -- PIP DOWNLOAD-CACHE PACKAGE FILE NAME SCHEMA: |
| 82 |
pos = basename.rfind("%2F") |
| 83 |
basename = basename[pos+3:] |
| 84 |
|
| 85 |
version_part_index = 0 |
| 86 |
parts = basename.split("-") |
| 87 |
for index, part in enumerate(parts): |
| 88 |
if index == 0: |
| 89 |
continue |
| 90 |
elif part and part[0].isdigit() and len(part) >= 3: |
| 91 |
version_part_index = index |
| 92 |
break |
| 93 |
name = "-".join(parts[:version_part_index]) |
| 94 |
version = "0.0" |
| 95 |
remainder = None |
| 96 |
if version_part_index > 0: |
| 97 |
version = parts[version_part_index] |
| 98 |
if version_part_index+1 < len(parts): |
| 99 |
remainder = "-".join(parts[version_part_index+1:]) |
| 100 |
assert name, "OOPS: basename=%s, name='%s'" % (basename, name) |
| 101 |
return (name, version, remainder) |
| 102 |
|
| 103 |
|
| 104 |
@classmethod |
| 105 |
def get_pkgname(cls, filename): |
| 106 |
return cls.split_pkgname_parts(filename)[0] |
| 107 |
|
| 108 |
@classmethod |
| 109 |
def get_pkgversion(cls, filename): |
| 110 |
return cls.split_pkgname_parts(filename)[1] |
| 111 |
|
| 112 |
@classmethod |
| 113 |
def make_pkgname_with_version(cls, filename): |
| 114 |
pkg_name = cls.get_pkgname(filename) |
| 115 |
pkg_version = cls.get_pkgversion(filename) |
| 116 |
return "%s-%s" % (pkg_name, pkg_version) |
| 117 |
|
| 118 |
@staticmethod |
| 119 |
def splitext(filename): |
| 120 |
fname = os.path.splitext(filename)[0] |
| 121 |
if fname.endswith(".tar"): |
| 122 |
fname = os.path.splitext(fname)[0] |
| 123 |
return fname |
| 124 |
|
| 125 |
@classmethod |
| 126 |
def isa(cls, filename): |
| 127 |
basename = os.path.basename(filename) |
| 128 |
if basename.startswith("."): |
| 129 |
return False |
| 130 |
for pattern in cls.PATTERNS: |
| 131 |
if fnmatch(filename, pattern): |
| 132 |
return True |
| 133 |
return False |
| 134 |
|
| 135 |
def collect_packages(package_dir, package_map=None): |
| 136 |
if package_map is None: |
| 137 |
package_map = {} |
| 138 |
packages = [] |
| 139 |
for filename in sorted(os.listdir(package_dir)): |
| 140 |
if not Package.isa(filename): |
| 141 |
continue |
| 142 |
pkg_filepath = os.path.join(package_dir, filename) |
| 143 |
package_name = Package.get_pkgname(pkg_filepath) |
| 144 |
package = package_map.get(package_name, None) |
| 145 |
if not package: |
| 146 |
# -- NEW PACKAGE DETECTED: Store/register package. |
| 147 |
package = Package(pkg_filepath) |
| 148 |
package_map[package.name] = package |
| 149 |
packages.append(package) |
| 150 |
else: |
| 151 |
# -- SAME PACKAGE: Collect other variant/version. |
| 152 |
package.files.append(pkg_filepath) |
| 153 |
return packages |
| 154 |
|
| 155 |
def make_index_for(package, index_dir, verbose=True): |
| 156 |
""" |
| 157 |
Create an 'index.html' for one package. |
| 158 |
|
| 159 |
:param package: Package object to use. |
| 160 |
:param index_dir: Where 'index.html' should be created. |
| 161 |
""" |
| 162 |
index_template = """\ |
| 163 |
<html> |
| 164 |
<head><title>{title}</title></head> |
| 165 |
<body> |
| 166 |
<h1>{title}</h1> |
| 167 |
<ul> |
| 168 |
{packages} |
| 169 |
</ul> |
| 170 |
</body> |
| 171 |
</html> |
| 172 |
""" |
| 173 |
item_template = '<li><a href="{1}">{0}</a></li>' |
| 174 |
index_filename = os.path.join(index_dir, "index.html") |
| 175 |
if not os.path.isdir(index_dir): |
| 176 |
os.makedirs(index_dir) |
| 177 |
|
| 178 |
parts = [] |
| 179 |
for pkg_filename in package.files: |
| 180 |
pkg_name = os.path.basename(pkg_filename) |
| 181 |
if pkg_name == "index.html": |
| 182 |
# -- ROOT-INDEX: |
| 183 |
pkg_name = os.path.basename(os.path.dirname(pkg_filename)) |
| 184 |
else: |
| 185 |
# pkg_name = package.splitext(pkg_name) |
| 186 |
pkg_name = package.make_pkgname_with_version(pkg_filename) |
| 187 |
pkg_relpath_to = os.path.relpath(pkg_filename, index_dir) |
| 188 |
parts.append(item_template.format(pkg_name, pkg_relpath_to)) |
| 189 |
|
| 190 |
if not parts: |
| 191 |
print("OOPS: Package %s has no files" % package.name) |
| 192 |
return |
| 193 |
|
| 194 |
if verbose: |
| 195 |
root_index = not Package.isa(package.files[0]) |
| 196 |
if root_index: |
| 197 |
info = "with %d package(s)" % len(package.files) |
| 198 |
else: |
| 199 |
package_versions = sorted(set(package.versions)) |
| 200 |
info = ", ".join(reversed(package_versions)) |
| 201 |
message = "%-30s %s" % (package.name, info) |
| 202 |
print(message) |
| 203 |
|
| 204 |
with open(index_filename, "w") as f: |
| 205 |
packages = "\n".join(parts) |
| 206 |
text = index_template.format(title=package.name, packages=packages) |
| 207 |
f.write(text.strip()) |
| 208 |
f.close() |
| 209 |
|
| 210 |
|
| 211 |
def make_package_index(download_dir): |
| 212 |
""" |
| 213 |
Create a pypi server like file structure below download directory. |
| 214 |
|
| 215 |
:param download_dir: Download directory with packages. |
| 216 |
|
| 217 |
EXAMPLE BEFORE: |
| 218 |
+-- downloads/ |
| 219 |
+-- wheelhouse/bob-1.4.2-*.whl |
| 220 |
+-- alice-1.0.zip |
| 221 |
+-- alice-1.0.tar.gz |
| 222 |
+-- bob-1.3.0.tar.gz |
| 223 |
+-- bob-1.4.2.tar.gz |
| 224 |
+-- charly-1.0.tar.bz2 |
| 225 |
|
| 226 |
EXAMPLE AFTERWARDS: |
| 227 |
+-- downloads/ |
| 228 |
+-- simple/ |
| 229 |
| +-- alice/index.html --> ../../alice-*.* |
| 230 |
| +-- bob/index.html --> ../../bob-*.* |
| 231 |
| +-- charly/index.html --> ../../charly-*.* |
| 232 |
| +-- index.html --> alice/index.html, bob/index.html, ... |
| 233 |
+-- wheelhouse/bob-1.4.2-*.whl |
| 234 |
+-- alice-1.0.zip |
| 235 |
+-- alice-1.0.tar.gz |
| 236 |
+-- bob-1.3.0.tar.gz |
| 237 |
+-- bob-1.4.2.tar.gz |
| 238 |
+-- charly-1.0.tar.bz2 |
| 239 |
""" |
| 240 |
if not os.path.isdir(download_dir): |
| 241 |
raise ValueError("No such directory: %r" % download_dir) |
| 242 |
|
| 243 |
pkg_rootdir = os.path.join(download_dir, "simple") |
| 244 |
if os.path.isdir(pkg_rootdir): |
| 245 |
shutil.rmtree(pkg_rootdir, ignore_errors=True) |
| 246 |
os.mkdir(pkg_rootdir) |
| 247 |
|
| 248 |
package_dirs = [download_dir] |
| 249 |
wheelhouse_dir = os.path.join(download_dir, "wheelhouse") |
| 250 |
if os.path.isdir(wheelhouse_dir): |
| 251 |
print("Using wheelhouse: %s" % wheelhouse_dir) |
| 252 |
package_dirs.append(wheelhouse_dir) |
| 253 |
|
| 254 |
# -- STEP: Collect all packages. |
| 255 |
package_map = {} |
| 256 |
packages = [] |
| 257 |
for package_dir in package_dirs: |
| 258 |
new_packages = collect_packages(package_dir, package_map) |
| 259 |
packages.extend(new_packages) |
| 260 |
|
| 261 |
# -- STEP: Make local PYTHON PACKAGE INDEX. |
| 262 |
root_package = Package(None, "Python Package Index") |
| 263 |
root_package.files = [ os.path.join(pkg_rootdir, pkg.name, "index.html") |
| 264 |
for pkg in packages ] |
| 265 |
make_index_for(root_package, pkg_rootdir) |
| 266 |
for package in packages: |
| 267 |
index_dir = os.path.join(pkg_rootdir, package.name) |
| 268 |
make_index_for(package, index_dir) |
| 269 |
|
| 270 |
|
| 271 |
# ----------------------------------------------------------------------------- |
| 272 |
# MAIN: |
| 273 |
# ----------------------------------------------------------------------------- |
| 274 |
if __name__ == "__main__": |
| 275 |
if (len(sys.argv) != 2) or "-h" in sys.argv[1:] or "--help" in sys.argv[1:]: |
| 276 |
print("USAGE: %s DOWNLOAD_DIR" % os.path.basename(sys.argv[0])) |
| 277 |
print(__doc__) |
| 278 |
sys.exit(1) |
| 279 |
make_package_index(sys.argv[1]) |