~ubuntu-branches/ubuntu/saucy/gunicorn/saucy-proposed

« back to all changes in this revision

Viewing changes to gunicorn/app/pasterapp.py

  • Committer: Package Import Robot
  • Author(s): Chris Lamb
  • Date: 2013-07-04 17:28:14 UTC
  • mfrom: (1.1.13)
  • Revision ID: package-import@ubuntu.com-20130704172814-xnxr3mrqxeaihrys
Tags: 17.5-1
* New upstream release.
* Refresh and alter structure of 0001-drop-supplemental-groups.patch. Thanks
  to Randall Leeds.

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
import os
7
7
import pkg_resources
8
8
import sys
9
 
import ConfigParser
 
9
 
 
10
try:
 
11
    import configparser as ConfigParser
 
12
except ImportError:
 
13
    import ConfigParser
10
14
 
11
15
from paste.deploy import loadapp, loadwsgi
12
16
SERVER = loadwsgi.SERVER
13
17
 
14
18
from gunicorn.app.base import Application
15
19
from gunicorn.config import Config
 
20
from gunicorn import util
 
21
 
16
22
 
17
23
class PasterBaseApplication(Application):
 
24
    gcfg = None
18
25
 
19
26
    def app_config(self):
20
 
        cx = loadwsgi.loadcontext(SERVER, self.cfgurl, relative_to=self.relpath)
 
27
        cx = loadwsgi.loadcontext(SERVER, self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
21
28
        gc, lc = cx.global_conf.copy(), cx.local_conf.copy()
22
29
        cfg = {}
23
30
 
25
32
        if host and port:
26
33
            cfg['bind'] = '%s:%s' % (host, port)
27
34
        elif host:
28
 
            cfg['bind'] = host
 
35
            cfg['bind'] = host.split(',')
29
36
 
30
37
        cfg['workers'] = int(lc.get('workers', 1))
31
38
        cfg['umask'] = int(lc.get('umask', 0))
51
58
            parser = ConfigParser.ConfigParser()
52
59
            parser.read([self.cfgfname])
53
60
            if parser.has_section('loggers'):
54
 
                if sys.version_info >= (2, 6):
55
 
                    from logging.config import fileConfig
56
 
                else:
57
 
                    # Use our custom fileConfig -- 2.5.1's with a custom Formatter class
58
 
                    # and less strict whitespace (which were incorporated into 2.6's)
59
 
                    from gunicorn.logging_config import fileConfig
60
 
 
 
61
                from logging.config import fileConfig
61
62
                config_file = os.path.abspath(self.cfgfname)
62
63
                fileConfig(config_file, dict(__file__=config_file,
63
64
                                             here=os.path.dirname(config_file)))
64
65
 
65
66
 
66
 
 
67
67
class PasterApplication(PasterBaseApplication):
68
68
 
69
69
    def init(self, parser, opts, args):
70
70
        if len(args) != 1:
71
71
            parser.error("No application name specified.")
72
72
 
73
 
        cfgfname = os.path.normpath(os.path.join(os.getcwd(), args[0]))
 
73
        cwd = util.getcwd()
 
74
        cfgfname = os.path.normpath(os.path.join(cwd, args[0]))
74
75
        cfgfname = os.path.abspath(cfgfname)
75
76
        if not os.path.exists(cfgfname):
76
77
            parser.error("Config file not found: %s" % cfgfname)
85
86
        return self.app_config()
86
87
 
87
88
    def load(self):
88
 
        return loadapp(self.cfgurl, relative_to=self.relpath)
 
89
        return loadapp(self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
 
90
 
89
91
 
90
92
class PasterServerApplication(PasterBaseApplication):
91
93
 
92
94
    def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
93
95
        self.cfg = Config()
 
96
        self.gcfg = gcfg # need to hold this for app_config 
94
97
        self.app = app
95
98
        self.callable = None
96
99
 
107
110
            bind = "%s:%s" % (host, port)
108
111
        else:
109
112
            bind = host
110
 
        cfg["bind"] = bind
 
113
        cfg["bind"] = bind.split(',')
111
114
 
112
115
        if gcfg:
113
116
            for k, v in gcfg.items():
118
121
            for k, v in cfg.items():
119
122
                if k.lower() in self.cfg.settings and v is not None:
120
123
                    self.cfg.set(k.lower(), v)
121
 
        except Exception, e:
 
124
        except Exception as e:
122
125
            sys.stderr.write("\nConfig error: %s\n" % str(e))
123
126
            sys.stderr.flush()
124
127
            sys.exit(1)
125
128
 
126
 
 
127
129
    def load_config(self):
128
130
        if not hasattr(self, "cfgfname"):
129
131
            return
130
132
 
131
133
        cfg = self.app_config()
132
 
        for k,v in cfg.items():
 
134
        for k, v in cfg.items():
133
135
            try:
134
136
                self.cfg.set(k.lower(), v)
135
137
            except:
138
140
 
139
141
    def load(self):
140
142
        if hasattr(self, "cfgfname"):
141
 
            return loadapp(self.cfgurl, relative_to=self.relpath)
 
143
            return loadapp(self.cfgurl, relative_to=self.relpath, global_conf=self.gcfg)
142
144
 
143
145
        return self.app
144
146
 
149
151
    apllications like Pylons or Turbogears2
150
152
    """
151
153
    from gunicorn.app.pasterapp import PasterApplication
152
 
    PasterApplication("%prog [OPTIONS] pasteconfig.ini").run()
 
154
    PasterApplication("%(prog)s [OPTIONS] pasteconfig.ini").run()
 
155
 
153
156
 
154
157
def paste_server(app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
155
158
    """\