Development of an internal social media platform with personalised dashboards for students
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pasterapp.py 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. # -*- coding: utf-8 -
  2. #
  3. # This file is part of gunicorn released under the MIT license.
  4. # See the NOTICE for more information.
  5. from __future__ import print_function
  6. import os
  7. import pkg_resources
  8. import sys
  9. try:
  10. import configparser as ConfigParser
  11. except ImportError:
  12. import ConfigParser
  13. from paste.deploy import loadapp, loadwsgi
  14. SERVER = loadwsgi.SERVER
  15. from gunicorn.app.base import Application
  16. from gunicorn.config import Config, get_default_config_file
  17. from gunicorn import util
  18. def _has_logging_config(paste_file):
  19. cfg_parser = ConfigParser.ConfigParser()
  20. cfg_parser.read([paste_file])
  21. return cfg_parser.has_section('loggers')
  22. def paste_config(gconfig, config_url, relative_to, global_conf=None):
  23. # add entry to pkg_resources
  24. sys.path.insert(0, relative_to)
  25. pkg_resources.working_set.add_entry(relative_to)
  26. config_url = config_url.split('#')[0]
  27. cx = loadwsgi.loadcontext(SERVER, config_url, relative_to=relative_to,
  28. global_conf=global_conf)
  29. gc, lc = cx.global_conf.copy(), cx.local_conf.copy()
  30. cfg = {}
  31. host, port = lc.pop('host', ''), lc.pop('port', '')
  32. if host and port:
  33. cfg['bind'] = '%s:%s' % (host, port)
  34. elif host:
  35. cfg['bind'] = host.split(',')
  36. cfg['default_proc_name'] = gc.get('__file__')
  37. # init logging configuration
  38. config_file = config_url.split(':')[1]
  39. if _has_logging_config(config_file):
  40. cfg.setdefault('logconfig', config_file)
  41. for k, v in gc.items():
  42. if k not in gconfig.settings:
  43. continue
  44. cfg[k] = v
  45. for k, v in lc.items():
  46. if k not in gconfig.settings:
  47. continue
  48. cfg[k] = v
  49. return cfg
  50. def load_pasteapp(config_url, relative_to, global_conf=None):
  51. return loadapp(config_url, relative_to=relative_to,
  52. global_conf=global_conf)
  53. class PasterBaseApplication(Application):
  54. gcfg = None
  55. def app_config(self):
  56. return paste_config(self.cfg, self.cfgurl, self.relpath,
  57. global_conf=self.gcfg)
  58. def load_config(self):
  59. super(PasterBaseApplication, self).load_config()
  60. # reload logging conf
  61. if hasattr(self, "cfgfname"):
  62. parser = ConfigParser.ConfigParser()
  63. parser.read([self.cfgfname])
  64. if parser.has_section('loggers'):
  65. from logging.config import fileConfig
  66. config_file = os.path.abspath(self.cfgfname)
  67. fileConfig(config_file, dict(__file__=config_file,
  68. here=os.path.dirname(config_file)))
  69. class PasterApplication(PasterBaseApplication):
  70. def init(self, parser, opts, args):
  71. if len(args) != 1:
  72. parser.error("No application name specified.")
  73. cwd = util.getcwd()
  74. cfgfname = os.path.normpath(os.path.join(cwd, args[0]))
  75. cfgfname = os.path.abspath(cfgfname)
  76. if not os.path.exists(cfgfname):
  77. parser.error("Config file not found: %s" % cfgfname)
  78. self.cfgurl = 'config:%s' % cfgfname
  79. self.relpath = os.path.dirname(cfgfname)
  80. self.cfgfname = cfgfname
  81. sys.path.insert(0, self.relpath)
  82. pkg_resources.working_set.add_entry(self.relpath)
  83. return self.app_config()
  84. def load(self):
  85. # chdir to the configured path before loading,
  86. # default is the current dir
  87. os.chdir(self.cfg.chdir)
  88. return load_pasteapp(self.cfgurl, self.relpath, global_conf=self.gcfg)
  89. class PasterServerApplication(PasterBaseApplication):
  90. def __init__(self, app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
  91. self.cfg = Config()
  92. self.gcfg = gcfg # need to hold this for app_config
  93. self.app = app
  94. self.callable = None
  95. gcfg = gcfg or {}
  96. cfgfname = gcfg.get("__file__")
  97. if cfgfname is not None:
  98. self.cfgurl = 'config:%s' % cfgfname
  99. self.relpath = os.path.dirname(cfgfname)
  100. self.cfgfname = cfgfname
  101. cfg = kwargs.copy()
  102. if port and not host.startswith("unix:"):
  103. bind = "%s:%s" % (host, port)
  104. else:
  105. bind = host
  106. cfg["bind"] = bind.split(',')
  107. if gcfg:
  108. for k, v in gcfg.items():
  109. cfg[k] = v
  110. cfg["default_proc_name"] = cfg['__file__']
  111. try:
  112. for k, v in cfg.items():
  113. if k.lower() in self.cfg.settings and v is not None:
  114. self.cfg.set(k.lower(), v)
  115. except Exception as e:
  116. print("\nConfig error: %s" % str(e), file=sys.stderr)
  117. sys.stderr.flush()
  118. sys.exit(1)
  119. if cfg.get("config"):
  120. self.load_config_from_file(cfg["config"])
  121. else:
  122. default_config = get_default_config_file()
  123. if default_config is not None:
  124. self.load_config_from_file(default_config)
  125. def load(self):
  126. # chdir to the configured path before loading,
  127. # default is the current dir
  128. os.chdir(self.cfg.chdir)
  129. return self.app
  130. def run():
  131. """\
  132. The ``gunicorn_paster`` command for launching Paster compatible
  133. applications like Pylons or Turbogears2
  134. """
  135. util.warn("""This command is deprecated.
  136. You should now use the `--paste` option. Ex.:
  137. gunicorn --paste development.ini
  138. """)
  139. from gunicorn.app.pasterapp import PasterApplication
  140. PasterApplication("%(prog)s [OPTIONS] pasteconfig.ini").run()
  141. def paste_server(app, gcfg=None, host="127.0.0.1", port=None, *args, **kwargs):
  142. """\
  143. A paster server.
  144. Then entry point in your paster ini file should looks like this:
  145. [server:main]
  146. use = egg:gunicorn#main
  147. host = 127.0.0.1
  148. port = 5000
  149. """
  150. util.warn("""This command is deprecated.
  151. You should now use the `--paste` option. Ex.:
  152. gunicorn --paste development.ini
  153. """)
  154. from gunicorn.app.pasterapp import PasterServerApplication
  155. PasterServerApplication(app, gcfg=gcfg, host=host, port=port, *args, **kwargs).run()