1 # Copyright (C) 2014 W. Trevor King <wking@tremily.us>
3 # This file is part of package-cache.
5 # package-cache is free software: you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by the Free
7 # Software Foundation, either version 3 of the License, or (at your option) any
10 # package-cache is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 # You should have received a copy of the GNU General Public License along with
16 # package-cache. If not, see <http://www.gnu.org/licenses/>.
18 import argparse as _argparse
19 import logging as _logging
20 import logging.handlers as _logging_handlers
21 import wsgiref.simple_server as _wsgiref_simple_server
23 from . import __version__
24 from . import LOG as _MAIN_LOG
25 from . import server as _server
28 LOG = _logging.getLogger(__name__)
31 class LoggingRequestHandler (_wsgiref_simple_server.WSGIRequestHandler):
32 def log_message(self, format, *args):
33 """Log a message using LOG
35 Use a logger instead of printing directly to stderr. This
36 method overrides the http.server.BaseHTTPRequestHandler
37 inherited by WSGIRequestHandler.
41 self.address_string(),
42 self.log_date_time_string(),
46 def run(*args, **kwargs):
47 """Run the package-cache server using Python's WSGI reference server
49 parser = _argparse.ArgumentParser(description=__doc__)
51 '--version', action='version',
52 version='%(prog)s {}'.format(__version__))
54 '--verbose', '-v', action='count',
55 help='Increment the logging verbosity')
57 '--syslog', action='store_const', const=True,
58 help='Increment the logging verbosity')
60 '--host', metavar='HOSTNAME', default='localhost',
61 help='Host to listen as')
63 '--port', metavar='INT', default=4000, type=int,
64 help='Port to listen to')
66 '--source', metavar='URL', action='append',
67 help='URL for an upstream mirror')
69 '--cache', metavar='PATH', default='/tmp/package-cache',
70 help='Path to the package cache directory')
72 args = parser.parse_args()
75 _MAIN_LOG.setLevel(max(
77 _MAIN_LOG.level - 10 * args.verbose))
79 while _MAIN_LOG.handlers:
80 h = _MAIN_LOG.handlers[0]
81 _MAIN_LOG.removeHandler(h)
82 handler = _logging_handlers.SysLogHandler(
83 address='/dev/log', facility='daemon')
84 _MAIN_LOG.addHandler(handler)
86 server = _server.Server(sources=args.source or [], cache=args.cache)
87 wsgi = _wsgiref_simple_server.make_server(
88 host=args.host, port=args.port, app=server,
89 handler_class=LoggingRequestHandler)
90 LOG.info('serving WSGI on {}:{}'.format(args.host, args.port))