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.

djangoapp.py 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. import os
  6. import sys
  7. from gunicorn.app.base import Application
  8. from gunicorn import util
  9. def is_setting_mod(path):
  10. return (os.path.isfile(os.path.join(path, "settings.py")) or
  11. os.path.isfile(os.path.join(path, "settings.pyc")))
  12. def find_settings_module(path):
  13. path = os.path.abspath(path)
  14. project_path = None
  15. settings_name = "settings"
  16. if os.path.isdir(path):
  17. project_path = None
  18. if not is_setting_mod(path):
  19. for d in os.listdir(path):
  20. if d in ('..', '.'):
  21. continue
  22. root = os.path.join(path, d)
  23. if is_setting_mod(root):
  24. project_path = root
  25. break
  26. else:
  27. project_path = path
  28. elif os.path.isfile(path):
  29. project_path = os.path.dirname(path)
  30. settings_name, _ = os.path.splitext(os.path.basename(path))
  31. return project_path, settings_name
  32. def make_default_env(cfg):
  33. if cfg.django_settings:
  34. os.environ['DJANGO_SETTINGS_MODULE'] = cfg.django_settings
  35. if cfg.pythonpath and cfg.pythonpath is not None:
  36. paths = cfg.pythonpath.split(",")
  37. for path in paths:
  38. pythonpath = os.path.abspath(cfg.pythonpath)
  39. if pythonpath not in sys.path:
  40. sys.path.insert(0, pythonpath)
  41. try:
  42. os.environ['DJANGO_SETTINGS_MODULE']
  43. except KeyError:
  44. # not settings env set, try to build one.
  45. cwd = util.getcwd()
  46. project_path, settings_name = find_settings_module(cwd)
  47. if not project_path:
  48. raise RuntimeError("django project not found")
  49. pythonpath, project_name = os.path.split(project_path)
  50. os.environ['DJANGO_SETTINGS_MODULE'] = "%s.%s" % (project_name,
  51. settings_name)
  52. if pythonpath not in sys.path:
  53. sys.path.insert(0, pythonpath)
  54. if project_path not in sys.path:
  55. sys.path.insert(0, project_path)
  56. class DjangoApplication(Application):
  57. def init(self, parser, opts, args):
  58. if args:
  59. if ("." in args[0] and not (os.path.isfile(args[0])
  60. or os.path.isdir(args[0]))):
  61. self.cfg.set("django_settings", args[0])
  62. else:
  63. # not settings env set, try to build one.
  64. project_path, settings_name = find_settings_module(
  65. os.path.abspath(args[0]))
  66. if project_path not in sys.path:
  67. sys.path.insert(0, project_path)
  68. if not project_path:
  69. raise RuntimeError("django project not found")
  70. pythonpath, project_name = os.path.split(project_path)
  71. self.cfg.set("django_settings", "%s.%s" % (project_name,
  72. settings_name))
  73. self.cfg.set("pythonpath", pythonpath)
  74. def load(self):
  75. # chdir to the configured path before loading,
  76. # default is the current dir
  77. os.chdir(self.cfg.chdir)
  78. # set settings
  79. make_default_env(self.cfg)
  80. # load wsgi application and return it.
  81. mod = util.import_module("gunicorn.app.django_wsgi")
  82. return mod.make_wsgi_application()
  83. class DjangoApplicationCommand(Application):
  84. def __init__(self, options, admin_media_path):
  85. self.usage = None
  86. self.prog = None
  87. self.cfg = None
  88. self.config_file = options.get("config") or ""
  89. self.options = options
  90. self.admin_media_path = admin_media_path
  91. self.callable = None
  92. self.project_path = None
  93. self.do_load_config()
  94. def init(self, *args):
  95. if 'settings' in self.options:
  96. self.options['django_settings'] = self.options.pop('settings')
  97. cfg = {}
  98. for k, v in self.options.items():
  99. if k.lower() in self.cfg.settings and v is not None:
  100. cfg[k.lower()] = v
  101. return cfg
  102. def load(self):
  103. # chdir to the configured path before loading,
  104. # default is the current dir
  105. os.chdir(self.cfg.chdir)
  106. # set settings
  107. make_default_env(self.cfg)
  108. # load wsgi application and return it.
  109. mod = util.import_module("gunicorn.app.django_wsgi")
  110. return mod.make_command_wsgi_application(self.admin_media_path)
  111. def run():
  112. """\
  113. The ``gunicorn_django`` command line runner for launching Django
  114. applications.
  115. """
  116. util.warn("""This command is deprecated.
  117. You should now run your application with the WSGI interface
  118. installed with your project. Ex.:
  119. gunicorn myproject.wsgi:application
  120. See https://docs.djangoproject.com/en/1.8/howto/deployment/wsgi/gunicorn/
  121. for more info.""")
  122. from gunicorn.app.djangoapp import DjangoApplication
  123. DjangoApplication("%(prog)s [OPTIONS] [SETTINGS_PATH]").run()