From 22322954123de62faa2def6a24061cef39e708e8 Mon Sep 17 00:00:00 2001 From: Ralf Schmitt Date: Tue, 22 Jan 2013 23:00:48 +0100 Subject: [PATCH] implement multi-root support one can now specify multiple package roots. watch out for filename conflicts! --- pypiserver/__init__.py | 1 - pypiserver/_app.py | 19 ++++++++++++++++--- pypiserver/core.py | 17 +++++------------ tests/test_main.py | 6 +++--- 4 files changed, 24 insertions(+), 19 deletions(-) diff --git a/pypiserver/__init__.py b/pypiserver/__init__.py index c749b2c..7338099 100644 --- a/pypiserver/__init__.py +++ b/pypiserver/__init__.py @@ -20,7 +20,6 @@ def app(root=None, if fallback_url is None: fallback_url = "http://pypi.python.org/simple" - os.listdir(root) _app.configure(root=root, redirect_to_fallback=redirect_to_fallback, fallback_url=fallback_url, password_file=password_file) _app.app.module = _app diff --git a/pypiserver/_app.py b/pypiserver/_app.py index a0f15bb..84b0180 100644 --- a/pypiserver/_app.py +++ b/pypiserver/_app.py @@ -1,4 +1,4 @@ -import sys, os +import sys, os, itertools if sys.version_info >= (3, 0): from urllib.parse import urljoin @@ -39,8 +39,21 @@ def configure(root=None, if fallback_url is None: fallback_url = "http://pypi.python.org/simple" - packages = lambda: listdir(root) - packages.root = root + if not isinstance(root, (list, tuple)): + roots = [root] + else: + roots = root + + roots = [os.path.abspath(r) for r in roots] + for r in roots: + try: + os.listdir(r) + except Exception: + err = sys.exc_info()[1] + sys.exit("Error: while trying to list %r: %s" % (r, err)) + + packages = lambda: itertools.chain(*[listdir(r) for r in roots]) + packages.root = roots[0] config.redirect_to_fallback = redirect_to_fallback config.fallback_url = fallback_url diff --git a/pypiserver/core.py b/pypiserver/core.py index 83dbeee..412e134 100755 --- a/pypiserver/core.py +++ b/pypiserver/core.py @@ -1,7 +1,7 @@ #! /usr/bin/env python """minimal PyPI like server for use with pip/easy_install""" -import os, sys, getopt, re, mimetypes, warnings +import os, sys, getopt, re, mimetypes, warnings, itertools warnings.filterwarnings("ignore", "Python 2.5 support may be dropped in future versions of Bottle") from pypiserver import bottle, __version__, app @@ -238,31 +238,24 @@ def main(argv=None): if len(roots) == 0: roots.append(os.path.expanduser("~/packages")) - elif len(roots) > 1: - sys.exit("Error: more than one root directory specified: %r" % (roots,)) - root = os.path.abspath(roots[0]) + roots = [os.path.abspath(x) for x in roots] - try: - os.listdir(root) - except Exception: - err = sys.exc_info()[1] - sys.exit("Error: while trying to list %r: %s" % (root, err)) if command == "update": - packages = frozenset(listdir(root)) + packages = frozenset(itertools.chain(*[listdir(r) for r in roots])) from pypiserver import manage manage.update(packages, update_directory, update_dry_run, stable_only=update_stable_only) return a = app( - root=root, + root=roots, 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 http://%s:%s\n\n" % (__version__, root, host, port)) + sys.stdout.write("This is pypiserver %s serving %r on http://%s:%s\n\n" % (__version__, ", ".join(roots), host, port)) sys.stdout.flush() run(app=a, host=host, port=port, server=server) diff --git a/tests/test_main.py b/tests/test_main.py index 3fade4f..087195d 100755 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -70,9 +70,9 @@ def test_root_r(main): assert main.pkgdir == os.path.abspath(".") -def test_root_multiple(main): - pytest.raises(SystemExit, main, [".", "."]) - pytest.raises(SystemExit, main, ["-r", ".", "."]) +# def test_root_multiple(main): +# pytest.raises(SystemExit, main, [".", "."]) +# pytest.raises(SystemExit, main, ["-r", ".", "."]) def test_fallback_url(main):