source: server/lib/gutenbach/server/__init__.py @ cf0d7e8

no-cups
Last change on this file since cf0d7e8 was cf0d7e8, checked in by Steven Allen <steven@…>, 12 years ago

Use sync decorator instead of manually locking.

  • Property mode set to 100644
File size: 2.4 KB
Line 
1import errors
2from errors import *
3__all__ = ['errors']
4__all__.extend(errors.__all__)
5
6def sync(func):
7    """Lock decorator
8
9    Holds a lock (self.lock) for the durration of a method call.
10    """
11
12    def do(self, *args, **kwargs):
13        with self.lock:
14            return func(self, *args, **kwargs)
15
16    return do
17__all__.append('sync')
18
19
20from job import GutenbachJob
21__all__.append('GutenbachJob')
22
23from printer import GutenbachPrinter
24__all__.append('GutenbachPrinter')
25
26from requests import make_empty_response, GutenbachRequestHandler
27__all__.append('make_empty_response')
28__all__.append('GutenbachRequestHandler')
29
30from server import IPPServer
31__all__.append('IPPServer')
32
33import BaseHTTPServer
34import logging
35import sys
36import traceback
37import os
38import shutil
39
40# configure and initialize logging
41logger = None
42
43def error(self, request=None, client_address=None):
44    logger.fatal(traceback.format_exc())
45    self.gutenbach_printer.running = False
46    sys.exit(1)
47
48def new_logfile(logfile):
49    if os.path.exists(logfile):
50        pth = os.path.abspath(os.path.dirname(logfile))
51        filename = os.path.basename(logfile)
52        logfiles = [f for f in os.listdir(pth) if f.startswith(filename + ".")]
53        lognums = [0]
54        for f in logfiles:
55            try:
56                lognums.append(int(f.lstrip(filename + ".")))
57            except TypeError:
58                pass
59        nextnum = max(lognums) + 1
60        shutil.move(logfile, os.path.join(pth, "%s.%d" % (filename, nextnum)))
61
62def start(config):
63    global logger
64    logkwargs = {}
65    logkwargs['level'] = getattr(logging, config['loglevel'].upper())
66    if 'logfile' in config:
67        logkwargs['filename'] = config['logfile']
68        new_logfile(config['logfile'])           
69    logging.basicConfig(**logkwargs)
70    logger = logging.getLogger(__name__)
71   
72    logger.info("Starting Gutenbach server...")
73    printers = sorted(config['printers'].keys())
74    gutenbach = GutenbachPrinter(printers[0], config['printers'][printers[0]])
75    gutenbach.start()
76
77    logger.info("Starting IPP server...")
78    server_address = ('', config['port'])
79    httpd = BaseHTTPServer.HTTPServer(server_address, IPPServer)
80    httpd.handle_error = error.__get__(httpd)
81    httpd.gutenbach_printer = gutenbach
82    while gutenbach.isAlive():
83        try:
84            httpd.handle_request()
85        except:
86            error(httpd)
87
88__all__.append('start')
Note: See TracBrowser for help on using the repository browser.