Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django3 ASGI #100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from setuptools import setup, find_packages

version = '2.3.dev0'
version = '2.4.dev1'


def read_file(name):
Expand Down
6 changes: 6 additions & 0 deletions src/djangorecipe/binscripts.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,9 @@ def log(self, msg):
# Run WSGI handler for the application
from django.core.wsgi import get_wsgi_application
return get_wsgi_application()


def asgi(settings_file):
os.environ.setdefault('DJANGO_SETTINGS_MODULE', settings_file)
from django.core.asgi import get_asgi_application
return get_asgi_application()
13 changes: 13 additions & 0 deletions src/djangorecipe/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,16 @@

application = %(module_name)s.%(attrs)s(%(arguments)s)
"""

ASGI_TEMPLATE = """

%(relative_paths_setup)s
import sys
sys.path[0:0] = [
%(path)s,
]
%(initialization)s
import %(module_name)s

application = %(module_name)s.%(attrs)s(%(arguments)s)
"""
33 changes: 32 additions & 1 deletion src/djangorecipe/recipe.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import pkg_resources
import zc.recipe.egg

from djangorecipe.boilerplate import WSGI_TEMPLATE
from djangorecipe.boilerplate import WSGI_TEMPLATE, ASGI_TEMPLATE


class Recipe(object):
Expand Down Expand Up @@ -56,6 +56,7 @@ def __init__(self, buildout, name, options):

# mod_wsgi support script
options.setdefault('wsgi', 'false')
options.setdefault('asgi', 'false')
options.setdefault('logfile', '')

# respect relative-paths (from zc.recipe.egg)
Expand Down Expand Up @@ -86,6 +87,7 @@ def install(self):
script_paths.extend(self.create_manage_script(extra_paths, ws))
script_paths.extend(self.create_test_runner(extra_paths, ws))
script_paths.extend(self.make_wsgi_script(extra_paths, ws))
script_paths.extend(self.make_asgi_script(extra_paths, ws))
script_paths += self.create_scripts_with_settings(
extra_paths, ws)
return script_paths
Expand Down Expand Up @@ -153,6 +155,34 @@ def make_wsgi_script(self, extra_paths, ws):
zc.buildout.easy_install.script_template = _script_template
return scripts

def make_asgi_script(self, extra_paths, ws):
scripts = []
_script_template = zc.buildout.easy_install.script_template
settings = self.get_settings()
zc.buildout.easy_install.script_template = (
zc.buildout.easy_install.script_header +
ASGI_TEMPLATE +
self.options['deploy-script-extra']
)
if self.options.get('asgi', '').lower() == 'true':
scripts.extend(
zc.buildout.easy_install.scripts(
[(self.options.get('asgi-script') or
'%s.%s' % (self.options.get('control-script',
self.name),
'asgi'),
'djangorecipe.binscripts', 'asgi')],
ws,
sys.executable,
self.options['bin-directory'],
extra_paths=extra_paths,
relative_paths=self._relative_paths,
arguments="'%s'" % settings,
initialization=self.options['initialization'],
))
zc.buildout.easy_install.script_template = _script_template
return scripts

def create_scripts_with_settings(self, extra_paths, ws):
"""Create duplicates of existing scripts - *with* a settings env.

Expand Down Expand Up @@ -217,6 +247,7 @@ def update(self):
self.create_manage_script(extra_paths, ws)
self.create_test_runner(extra_paths, ws)
self.make_wsgi_script(extra_paths, ws)
self.make_asgi_script(extra_paths, ws)

def create_file(self, filename, template, options):
if os.path.exists(filename):
Expand Down