├── .dockerignore ├── Dockerfile ├── setup.py ├── .github └── workflows │ └── publish.yml ├── LICENSE ├── README.md └── simplepypi └── bin └── simplepypi /.dockerignore: -------------------------------------------------------------------------------- 1 | .git 2 | -------------------------------------------------------------------------------- /Dockerfile: -------------------------------------------------------------------------------- 1 | from python:latest 2 | 3 | ADD . . 4 | RUN python setup.py install 5 | 6 | CMD simplepypi/bin/simplepypi --addr 0.0.0.0 7 | -------------------------------------------------------------------------------- /setup.py: -------------------------------------------------------------------------------- 1 | from setuptools import setup 2 | 3 | setup(name='simplepypi', 4 | version='0.2.3', 5 | author='Zach Steindler', 6 | author_email='steiza@coffeehousecoders.org', 7 | url='http://github.com/steiza/simplepypi', 8 | description='A really, really, simple HTTP PyPI-like server', 9 | long_description='''It is intended to be used for companies or organizations that need a private PyPi''', 10 | keywords='pypi, replacement, cheeseshop', 11 | classifiers=['Programming Language :: Python', 'License :: OSI Approved :: BSD License'], 12 | license='BSD', 13 | packages=['simplepypi'], 14 | scripts=['simplepypi/bin/simplepypi'], 15 | install_requires=['tornado'], 16 | ) 17 | -------------------------------------------------------------------------------- /.github/workflows/publish.yml: -------------------------------------------------------------------------------- 1 | name: Publish package to PyPI 2 | 3 | on: 4 | workflow_dispatch: 5 | 6 | jobs: 7 | pypi-publish: 8 | name: upload release to PyPI 9 | runs-on: ubuntu-latest 10 | permissions: 11 | # IMPORTANT: this permission is mandatory for OIDC publishing 12 | id-token: write 13 | steps: 14 | - uses: actions/checkout@v3 15 | 16 | - uses: actions/setup-python@v4 17 | with: 18 | python-version: "3.x" 19 | 20 | - name: deps 21 | run: python -m pip install -U build 22 | 23 | - name: build 24 | run: python -m build 25 | 26 | - name: Publish package distributions to PyPI 27 | uses: pypa/gh-action-pypi-publish@v1.10.1 28 | with: 29 | attestations: true 30 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2010-2016 Zachary Steindler 2 | 3 | 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: 4 | 5 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 6 | 7 | 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. 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | #### This is a really, really, simple HTTP PyPI-like server. 2 | 3 | It is intended to be used for companies or organizations that need a private 4 | PyPi. 5 | 6 | It literally supports four functions: 7 | 8 | - Allows uploading of packages 9 | - Downloading by package (or package and version) 10 | - A / page that is navigatable with a web browser 11 | - /pypi/ listing 12 | 13 | It does not support: 14 | 15 | - Stopping you from overwriting a package with the same name / version 16 | - Registering packages 17 | - Any sort of ACLs 18 | 19 | To use it run "simplepypi". You can upload packages by: 20 | 21 | #### Install twine if you haven't already: 22 | 23 | pip install twine 24 | 25 | #### Build your package if you haven't already: 26 | 27 | python setup.py sdist 28 | 29 | #### Upload your package using twine: 30 | 31 | $ twine upload --repository-url http://127.0.0.1:8000/ dist/* 32 | Uploading distributions to http://127.0.0.1:8000/ 33 | Enter your username: 34 | Enter your password: 35 | 36 | #### Then, when you want to install packages from it you do: 37 | 38 | pip install -i http://127.0.0.1:8000/pypi 39 | 40 | #### Or, if you're stuck in the stone ages with `setuptools`/`easy_install`: 41 | 42 | from setuptools import setup 43 | 44 | setup( 45 | ... 46 | install_requires=[''], 47 | dependency_links=['http://127.0.0.1:8000/packages/'], 48 | ) 49 | 50 | python setup.py install 51 | 52 | #### To use the docker image, build and run: 53 | 54 | docker build -t simplepypi . 55 | docker run -it -p 8000:8000 simplepypi 56 | 57 | Not using twine yet? Here is the legacy way of uploading Python packages (not 58 | recommended): 59 | 60 | #### Modify your ~/.pypirc so it looks like: 61 | 62 | [distutils] 63 | index-servers = 64 | pypi 65 | local 66 | 67 | [local] 68 | username: 69 | password: 70 | repository: http://127.0.0.1:8000 71 | 72 | [pypi] 73 | ... 74 | 75 | #### Run this on the setup.py of your favorite package: 76 | 77 | $ python setup.py sdist upload -r local 78 | 79 | And that's it! 80 | -------------------------------------------------------------------------------- /simplepypi/bin/simplepypi: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | import argparse 4 | import logging 5 | import os.path 6 | 7 | from tornado.escape import xhtml_escape 8 | from tornado.ioloop import IOLoop 9 | from tornado.web import Application, RequestHandler 10 | 11 | 12 | def normalize_name(name): 13 | return name.replace('.', '-').replace('_', '-').lower() 14 | 15 | 16 | def write404(self): 17 | self.set_status(404) 18 | 19 | self.write( 20 | '404 Not Found' 21 | '

Not found

' 22 | ) 23 | 24 | 25 | def is_filesystem_traversal(path): 26 | return not os.path.abspath(path).startswith( 27 | os.path.abspath(os.path.dirname(__file__)) 28 | ) 29 | 30 | 31 | def get_package_path(package, version=None, want_file=False, filename=None): 32 | path = os.path.join(os.path.dirname(__file__), 'packages', package) 33 | 34 | if version: 35 | path = os.path.join(path, version) 36 | 37 | if filename: 38 | path = os.path.join(path, filename) 39 | 40 | # Don't allow filesystem traversal 41 | if is_filesystem_traversal(path): 42 | raise ValueError('Bad arguments') 43 | 44 | return path 45 | 46 | 47 | def generate_package_link(package, version): 48 | path = os.path.join( 49 | os.path.dirname(__file__), 'packages', package, version, 50 | ) 51 | 52 | # Don't allow filesystem traversal 53 | if is_filesystem_traversal(path): 54 | raise ValueError('Bad arguments') 55 | 56 | dists = os.listdir(path) 57 | 58 | links = [ 59 | ('' 60 | '{each_dist}
').format( 61 | package=xhtml_escape(package), 62 | version=xhtml_escape(version), 63 | each_dist=xhtml_escape(each_dist), 64 | ) for each_dist in dists 65 | ] 66 | 67 | return ''.join(links) 68 | 69 | 70 | class IndexHandler(RequestHandler): 71 | 72 | def get(self): 73 | self.write( 74 | 'simplepypi' 75 | 'packages' 76 | ) 77 | 78 | def post(self): 79 | name = self.get_argument('name', default=None) 80 | version = self.get_argument('version', default=None) 81 | action = self.get_argument(':action', default=None) 82 | 83 | content = self.request.files['content'][0]['body'] 84 | 85 | if name and version and content and action == 'file_upload': 86 | name = normalize_name(name) 87 | 88 | filename = self.request.files['content'][0]['filename'] 89 | path = get_package_path(name, version, filename=filename) 90 | 91 | if not os.path.exists(os.path.dirname(path)): 92 | os.makedirs(os.path.dirname(path)) 93 | 94 | fd = open(path, 'wb') 95 | fd.write(content) 96 | fd.close() 97 | 98 | 99 | class PypiHandler(RequestHandler): 100 | 101 | def get(self): 102 | self.write('\n') 103 | 104 | packages_path = os.path.join(os.path.dirname(__file__), 'packages') 105 | packages_list = os.listdir(packages_path) 106 | 107 | for each in packages_list: 108 | versions_path = os.path.join(packages_path, each) 109 | versions_list = os.listdir(versions_path) 110 | versions_list.sort() 111 | 112 | if len(versions_list) > 0: 113 | self.write(generate_package_link(each, versions_list[-1])) 114 | 115 | self.write('') 116 | 117 | 118 | class PypiPackageHandler(RequestHandler): 119 | 120 | def get(self, package): 121 | package = normalize_name(package) 122 | 123 | package_path = get_package_path(package) 124 | 125 | if not os.path.exists(package_path): 126 | write404(self) 127 | return 128 | 129 | versions = os.listdir(package_path) 130 | versions.sort() 131 | 132 | if len(versions) == 0: 133 | write404(request) 134 | return 135 | 136 | if os.path.exists(package_path): 137 | self.write('') 138 | for version in versions: 139 | self.write(generate_package_link(package, version)) 140 | self.write('') 141 | 142 | else: 143 | write404(self) 144 | return 145 | 146 | 147 | class PypiPackageVersionHandler(RequestHandler): 148 | 149 | def get(self, package, version): 150 | package = normalize_name(package) 151 | 152 | path = get_package_path(package, version) 153 | 154 | if os.path.exists(path): 155 | self.write( 156 | '{}'.format( 157 | generate_package_link(package, version) 158 | )) 159 | 160 | else: 161 | write404(self) 162 | return 163 | 164 | 165 | class PackageBase(RequestHandler): 166 | 167 | def get(self): 168 | package_path = os.path.join(os.path.dirname(__file__), 'packages') 169 | 170 | contents = os.listdir(package_path) 171 | contents.sort() 172 | 173 | self.write('\n') 174 | 175 | for each in contents: 176 | self.write( 177 | '{each}
'.format( 178 | each=xhtml_escape(each), 179 | ) 180 | ) 181 | 182 | if len(contents) == 0: 183 | self.write('Nothing to see here yet, try uploading a package!') 184 | 185 | self.write('\n') 186 | 187 | 188 | class PackageList(RequestHandler): 189 | 190 | def get(self, package): 191 | package = normalize_name(package) 192 | 193 | package_path = get_package_path(package) 194 | 195 | if not os.path.exists(package_path): 196 | write404(self) 197 | return 198 | 199 | versions = os.listdir(package_path) 200 | versions.sort() 201 | 202 | if len(versions) == 0: 203 | write404(request) 204 | return 205 | 206 | self.write('\n') 207 | 208 | for each_version in versions: 209 | self.write(generate_package_link(package, each_version)) 210 | 211 | self.write('\n') 212 | 213 | 214 | class PackageDownload(RequestHandler): 215 | 216 | def get(self, name, version, filename): 217 | path = get_package_path(name, version, filename=filename) 218 | if not os.path.exists(path): 219 | write404(self) 220 | return 221 | 222 | self.set_header('Content-Type', 'application/octet-stream') 223 | self.set_header( 224 | 'Content-Disposition', 'attachment; filename={0}'.format( 225 | os.path.basename(path) 226 | ) 227 | ) 228 | 229 | with open(path, 'rb') as fd: 230 | while True: 231 | chunk = fd.read(1024**2) 232 | 233 | if chunk: 234 | self.write(chunk) 235 | else: 236 | break 237 | 238 | 239 | def set_up_server(addr, port): 240 | loggers = ['tornado.access', 'tornado.application', 'tornado.general'] 241 | 242 | for each_logger in loggers: 243 | logger = logging.getLogger(each_logger) 244 | logger.setLevel(logging.DEBUG) 245 | 246 | handler = logging.StreamHandler() 247 | handler.setFormatter( 248 | logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')) 249 | 250 | logger.addHandler(handler) 251 | 252 | package_path = os.path.join(os.path.dirname(__file__), 'packages') 253 | 254 | if not os.path.exists(package_path): 255 | os.makedirs(package_path) 256 | 257 | app = Application([ 258 | ('/', IndexHandler), 259 | ('/pypi/', PypiHandler), 260 | ('/pypi/([^/]*)/', PypiPackageHandler), 261 | ('/pypi/([^/]*)/([^/]*)', PypiPackageVersionHandler), 262 | ('/packages', PackageBase), 263 | ('/packages/([^/]*)', PackageList), 264 | ('/packages/([^/]*)/([^/]*)/(.*)', PackageDownload), 265 | ]) 266 | 267 | app.listen(port, address=addr) 268 | print('Running on http://{}:{}'.format(addr, port)) 269 | 270 | IOLoop.current().start() 271 | 272 | 273 | if __name__ == '__main__': 274 | parser = argparse.ArgumentParser( 275 | description='a simple HTTP PyPI-like server') 276 | 277 | parser.add_argument( 278 | '--addr', type=str, default='127.0.0.1', help='address to bind to') 279 | 280 | parser.add_argument( 281 | '--port', type=int, default=8000, help='TCP port to bind to') 282 | 283 | args = parser.parse_args() 284 | 285 | set_up_server(args.addr, args.port) 286 | --------------------------------------------------------------------------------