From 55273d55e3d87cdfc6a237549034500707bd01c4 Mon Sep 17 00:00:00 2001 From: Orne Brocaar Date: Wed, 15 Aug 2012 10:02:58 +0200 Subject: [PATCH] Add --fallback-url argument to pypi-server script to make it configurable. --- README.rst | 4 ++++ pypiserver/__init__.py | 4 ++-- pypiserver/core.py | 32 +++++++++++++++++++++++++++----- tests/test_main.py | 11 +++++++++++ 4 files changed, 44 insertions(+), 7 deletions(-) diff --git a/README.rst b/README.rst index ca9380d..cd54646 100644 --- a/README.rst +++ b/README.rst @@ -82,6 +82,10 @@ pypi-server -h will print a detailed usage message:: disable redirect to real PyPI index for packages not found in the local index + --fallback-url FALLBACK_URL + for packages not found in the local index, this URL will be used to + redirect to (default: http://pypi.python.org/simple) + --server METHOD use METHOD to run the server. Valid values include paste, cherrypy, twisted, gunicorn, gevent, wsgiref, auto. The diff --git a/pypiserver/__init__.py b/pypiserver/__init__.py index 362d763..ef1bede 100644 --- a/pypiserver/__init__.py +++ b/pypiserver/__init__.py @@ -1,5 +1,5 @@ -__version_info__ = (0, 6, 1) -version = __version__ = "0.6.1" +__version_info__ = (0, 6, 2, 'dev') +version = __version__ = "0.6.2dev" def app(root=None, diff --git a/pypiserver/core.py b/pypiserver/core.py index 15304b4..3c117f3 100755 --- a/pypiserver/core.py +++ b/pypiserver/core.py @@ -69,7 +69,7 @@ class pkgset(object): assert "/" not in filename dest_fn = os.path.join(self.root, filename) dest_fh = open(dest_fn, "wb") - + dest_fh.write(data) dest_fh.close() @@ -98,6 +98,10 @@ pypi-server understands the following options: disable redirect to real PyPI index for packages not found in the local index + --fallback-url FALLBACK_URL + for packages not found in the local index, this URL will be used to + redirect to (default: http://pypi.python.org/simple) + --server METHOD use METHOD to run the server. Valid values include paste, cherrypy, twisted, gunicorn, gevent, wsgiref, auto. The @@ -147,6 +151,7 @@ def main(argv=None): port = 8080 server = None redirect_to_fallback = True + fallback_url = "http://pypi.python.org/simple" password_file = None update_dry_run = True @@ -154,7 +159,17 @@ def main(argv=None): update_stable_only = True try: - opts, roots = getopt.getopt(argv[1:], "i:p:r:d:P:Uuxh", ["interface=", "passwords=", "port=", "root=", "server=", "disable-fallback", "version", "help"]) + opts, roots = getopt.getopt(argv[1:], "i:p:r:d:P:Uuxh", [ + "interface=", + "passwords=", + "port=", + "root=", + "server=", + "fallback-url=", + "disable-fallback", + "version", + "help" + ]) except getopt.GetoptError: err = sys.exc_info()[1] sys.exit("usage error: %s" % (err,)) @@ -168,9 +183,12 @@ def main(argv=None): roots.append(v) elif k == "--disable-fallback": redirect_to_fallback = False + elif k == "--fallback-url": + fallback_url = v elif k == "--server": if v not in server_names: - sys.exit("unknown server %r. choose one of %s" % (v, ", ".join(server_names.keys()))) + sys.exit("unknown server %r. choose one of %s" % ( + v, ", ".join(server_names.keys()))) server = v elif k == "--version": sys.stdout.write("pypiserver %s\n" % __version__) @@ -189,7 +207,6 @@ def main(argv=None): usage() sys.exit(0) - if len(roots) == 0: roots.append(os.path.expanduser("~/packages")) elif len(roots) > 1: @@ -210,7 +227,12 @@ def main(argv=None): manage.update(packages, update_directory, update_dry_run, stable_only=update_stable_only) return - a = app(root=root, redirect_to_fallback=redirect_to_fallback, password_file=password_file) + a = app( + root=root, + redirect_to_fallback=redirect_to_fallback, + password_file=password_file, + fallback_url=fallback_url + ) server = server or "auto" sys.stdout.write("This is pypiserver %s serving %r on %s:%s\n\n" % (__version__, root, host, port)) run(app=a, host=host, port=port, server=server) diff --git a/tests/test_main.py b/tests/test_main.py index b93164f..5eea8b2 100755 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -74,3 +74,14 @@ def test_root_r(main): def test_root_multiple(main): pytest.raises(SystemExit, main, [".", "."]) pytest.raises(SystemExit, main, ["-r", ".", "."]) + + +def test_fallback_url(main): + main(["--fallback-url", "http://pypi.mirror/simple"]) + assert main.app.module.config.fallback_url == "http://pypi.mirror/simple" + + +def test_fallback_url_default(main): + main([]) + assert main.app.module.config.fallback_url == \ + "http://pypi.python.org/simple"