#!/usr/bin/env python # -*- coding: utf-8 -*- # ************************************************************************** # Copyright © 2016 jianglin # File Name: runserver.py # Author: jianglin # Email: [email protected] # Created: 2016-09-29 16:10:33 (CST) # Last Update:THURSDAY 2016-9-29 16:25:24 (CST) # By: # Description: # ************************************************************************** import pypiserver from os.path import join base_path = '/home/arch/pypiserver' conf = pypiserver.default_config(root=join(base_path, 'packages'), host='127.0.0.1', port=8080, password_file=join(base_path, 'password'), log_file=join(base_path, 'logs/info.log'), welcome_file=None) application = pypiserver.app(**conf)
def main(argv=None): import pypiserver if argv is None: argv = sys.argv command = "serve" c = pypiserver.Configuration(**pypiserver.default_config()) update_dry_run = True update_directory = None update_stable_only = True try: opts, roots = getopt.getopt(argv[1:], "i:p:a:r:d:P:Uuvxoh", [ "interface=", "passwords=", "authenticate=", "port=", "root=", "server=", "fallback-url=", "disable-fallback", "overwrite", "hash-algo=", "log-file=", "log-frmt=", "log-req-frmt=", "log-res-frmt=", "log-err-frmt=", "welcome=", "cache-control=", "version", "help" ]) except getopt.GetoptError: err = sys.exc_info()[1] sys.exit("usage error: %s" % (err, )) for k, v in opts: if k in ("-p", "--port"): try: c.port = int(v) except Exception: err = sys.exc_info()[1] sys.exit("Invalid port(%r) due to: %s" % (v, err)) elif k in ("-a", "--authenticate"): c.authenticated = [ a.lower() for a in re.split("[, ]+", v.strip(" ,")) if a ] if c.authenticated == ['.']: c.authenticated = [] else: actions = ("list", "download", "update") for a in c.authenticated: if a not in actions: errmsg = "Action '%s' for option `%s` not one of %s!" sys.exit(errmsg % (a, k, actions)) elif k in ("-i", "--interface"): c.host = v elif k in ("-r", "--root"): roots.append(v) elif k == "--disable-fallback": c.redirect_to_fallback = False elif k == "--fallback-url": c.fallback_url = v elif k == "--server": c.server = v elif k == "--welcome": c.welcome_file = v elif k == "--version": print("pypiserver %s\n" % pypiserver.__version__) return elif k == "-U": command = "update" elif k == "-x": update_dry_run = False elif k == "-u": update_stable_only = False elif k == "-d": update_directory = v elif k in ("-P", "--passwords"): c.password_file = v elif k in ("-o", "--overwrite"): c.overwrite = True elif k in ("--hash-algo"): c.hash_algo = None if not pypiserver.str2bool(v, c.hash_algo) else v elif k == "--log-file": c.log_file = v elif k == "--log-frmt": c.log_frmt = v elif k == "--log-req-frmt": c.log_req_frmt = v elif k == "--log-res-frmt": c.log_res_frmt = v elif k == "--log-err-frmt": c.log_err_frmt = v elif k == "--cache-control": c.cache_control = v elif k == "-v": c.verbosity += 1 elif k in ("-h", "--help"): print(usage()) sys.exit(0) if (not c.authenticated and c.password_file != '.' or c.authenticated and c.password_file == '.'): auth_err = "When auth-ops-list is empty (-a=.), password-file (-P=%r) must also be empty ('.')!" sys.exit(auth_err % c.password_file) if len(roots) == 0: roots.append(os.path.expanduser("~/packages")) roots = [os.path.abspath(x) for x in roots] c.root = roots verbose_levels = [ logging.WARNING, logging.INFO, logging.DEBUG, logging.NOTSET ] log_level = list(zip(verbose_levels, range(c.verbosity)))[-1][0] init_logging(level=log_level, filename=c.log_file, frmt=c.log_frmt) if command == "update": from pypiserver.manage import update_all_packages update_all_packages(roots, update_directory, dry_run=update_dry_run, stable_only=update_stable_only) return # Fixes #49: # The gevent server adapter needs to patch some # modules BEFORE importing bottle! if c.server and c.server.startswith('gevent'): import gevent.monkey # @UnresolvedImport gevent.monkey.patch_all() from pypiserver import bottle if c.server not in bottle.server_names: sys.exit("unknown server %r. choose one of %s" % (c.server, ", ".join(bottle.server_names.keys()))) bottle.debug(c.verbosity > 1) bottle._stderr = ft.partial(pypiserver._logwrite, logging.getLogger(bottle.__name__), logging.INFO) app = pypiserver.app(**vars(c)) bottle.run(app=app, host=c.host, port=c.port, server=c.server)
def main(argv=None): import pypiserver if argv is None: argv = sys.argv command = "serve" c = pypiserver.Configuration(**pypiserver.default_config()) update_dry_run = True update_directory = None update_stable_only = True try: opts, roots = getopt.getopt(argv[1:], "i:p:a:r:d:P:Uuvxoh", [ "interface=", "passwords=", "authenticate=", "port=", "root=", "server=", "fallback-url=", "disable-fallback", "overwrite", "hash-algo=", "log-file=", "log-frmt=", "log-req-frmt=", "log-res-frmt=", "log-err-frmt=", "welcome=", "cache-control=", "version", "help" ]) except getopt.GetoptError: err = sys.exc_info()[1] sys.exit("usage error: %s" % (err,)) for k, v in opts: if k in ("-p", "--port"): try: c.port = int(v) except Exception: err = sys.exc_info()[1] sys.exit("Invalid port(%r) due to: %s" % (v, err)) elif k in ("-a", "--authenticate"): if '{' in v: try: v = ast.literal_eval(v) except SyntaxError: message = 'Could not parse auth string %s! Please ensure string is correctly formatted.' % v print(message) sys.exit(message) if (not isinstance(v, dict) or not all([isinstance(i, list) for i in v.values()])): message = 'Matrix auth string must be a dict of lists. Please see the README for details.' print(message) sys.exit(message) if isinstance(v, dict): c.authenticated = {} for user in v: c.authenticated[user] = [a.lower() for a in v[user] if a] if c.authenticated[user] == ['.']: c.authenticated[user] = [] else: actions = ("list", "download", "update") for a in c.authenticated[user]: if a not in actions: errmsg = "Action '%s' for option `%s` not one of %s!" sys.exit(errmsg % (a, k, actions)) else: c.authenticated = [a.lower() for a in re.split("[, ]+", v.strip(" ,")) if a] if c.authenticated == ['.']: c.authenticated = [] else: actions = ("list", "download", "update") for a in c.authenticated: if a not in actions: errmsg = "Action '%s' for option `%s` not one of %s!" sys.exit(errmsg % (a, k, actions)) elif k in ("-i", "--interface"): c.host = v elif k in ("-r", "--root"): roots.append(v) elif k == "--disable-fallback": c.redirect_to_fallback = False elif k == "--fallback-url": c.fallback_url = v elif k == "--server": c.server = v elif k == "--welcome": c.welcome_file = v elif k == "--version": print("pypiserver %s\n" % pypiserver.__version__) return elif k == "-U": command = "update" elif k == "-x": update_dry_run = False elif k == "-u": update_stable_only = False elif k == "-d": update_directory = v elif k in ("-P", "--passwords"): c.password_file = v elif k in ("-o", "--overwrite"): c.overwrite = True elif k in ("--hash-algo"): c.hash_algo = None if not pypiserver.str2bool(v, c.hash_algo) else v elif k == "--log-file": c.log_file = v elif k == "--log-frmt": c.log_frmt = v elif k == "--log-req-frmt": c.log_req_frmt = v elif k == "--log-res-frmt": c.log_res_frmt = v elif k == "--log-err-frmt": c.log_err_frmt = v elif k == "--cache-control": c.cache_control = v elif k == "-v": c.verbosity += 1 elif k in ("-h", "--help"): print(usage()) sys.exit(0) if (not c.authenticated and c.password_file != '.' or c.authenticated and c.password_file == '.'): auth_err = "When auth-ops-list is empty (-a=.), password-file (-P=%r) must also be empty ('.')!" sys.exit(auth_err % c.password_file) if len(roots) == 0: roots.append(os.path.expanduser("~/packages")) roots=[os.path.abspath(x) for x in roots] c.root = roots verbose_levels=[ logging.WARNING, logging.INFO, logging.DEBUG, logging.NOTSET] log_level=list(zip(verbose_levels, range(c.verbosity)))[-1][0] init_logging(level=log_level, filename=c.log_file, frmt=c.log_frmt) if command == "update": from pypiserver.manage import update_all_packages update_all_packages(roots, update_directory, dry_run=update_dry_run, stable_only=update_stable_only) return # Fixes #49: # The gevent server adapter needs to patch some # modules BEFORE importing bottle! if c.server and c.server.startswith('gevent'): import gevent.monkey # @UnresolvedImport gevent.monkey.patch_all() from pypiserver import bottle if c.server not in bottle.server_names: sys.exit("unknown server %r. choose one of %s" % ( c.server, ", ".join(bottle.server_names.keys()))) bottle.debug(c.verbosity > 1) bottle._stderr = ft.partial(pypiserver._logwrite, logging.getLogger(bottle.__name__), logging.INFO) app = pypiserver.app(**vars(c)) bottle.run(app=app, host=c.host, port=c.port, server=c.server)