Skip to content

Commit

Permalink
Django. Added noruntimes and noerrpages switches for uwsgi_run and u…
Browse files Browse the repository at this point in the history
…wsgi_sysinit commands
  • Loading branch information
idlesign committed Nov 12, 2019
1 parent 2c79c4b commit a685123
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 40 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ uwsgiconf changelog

Unreleased
----------
+ Django. Added 'noruntimes' and 'noerrpages' switches for 'uwsgi_run' and 'uwsgi_sysinit' commands.
* 'allow_shared_sockets' param of 'sockets.from_dns' and such now allows 'False'.
* ArgsFormatter now doesn't drop every line starting with %.

Expand Down
14 changes: 10 additions & 4 deletions tests/contrib/test_django.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,17 @@ class Settings(object):

from uwsgiconf.contrib.django.uwsgify.management.commands.uwsgi_run import Command

Command().handle(compile=False, use_static_handler=True, embedded=False)
Command().handle(
compile=False,
contribute_static=True,
contribute_runtimes=True,
contribute_errpages=True,
embedded=False,
)
Command().handle(compile=True, embedded=False)

with pytest.raises(ImportError): # py3 - ModuleNotFoundError
Command().handle(compile=False, use_static_handler=True, embedded=True)
Command().handle(compile=False, contribute_static=True, embedded=True)


@pytest.mark.skipif(PY2, reason='Not tested on PY2')
Expand Down Expand Up @@ -109,7 +115,7 @@ def test_uwsgi_sysinit_systemd(patch_base_command, capsys):
from uwsgiconf.contrib.django.uwsgify.management.commands.uwsgi_sysinit import Command

Command().handle(
systype='systemd', nostatic=True)
systype='systemd', nostatic=True, noruntimes=True)

out, err = capsys.readouterr()

Expand All @@ -120,7 +126,7 @@ def test_uwsgi_sysinit_systemd(patch_base_command, capsys):
assert ('/run/user/%s/dummy' % uid) in out
assert 'Description=dummy uWSGI Service' in out
assert 'bin/python' in out
assert 'dummy/manage.py uwsgi_run' in out
assert 'dummy/manage.py uwsgi_run --nostatic --noruntimes\n' in out


@pytest.mark.skipif(PY2, reason='Not tested on PY2')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Command(FifoCommand):

help = 'Allows managing of uWSGI log related stuff'

def add_arguments(self, parser):
def add_arguments(self, parser): # pragma: nocover

super(Command, self).add_arguments(parser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Command(FifoCommand):

help = 'Reloads uWSGI master process, workers'

def add_arguments(self, parser):
def add_arguments(self, parser): # pragma: nocover

super(Command, self).add_arguments(parser)

Expand Down
14 changes: 11 additions & 3 deletions uwsgiconf/contrib/django/uwsgify/management/commands/uwsgi_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,21 @@ class Command(BaseCommand):

help = 'Runs uWSGI to serve your project'

def add_arguments(self, parser):
def add_arguments(self, parser): # pragma: nocover

super(Command, self).add_arguments(parser)

parser.add_argument(
'--nostatic', action='store_false', dest='use_static_handler',
help='Tells uWSGI to NOT to serve static and media files.',
'--nostatic', action='store_false', dest='contribute_static',
help='Do not serve static and media files.',
)
parser.add_argument(
'--noruntimes', action='store_false', dest='contribute_runtimes',
help='Do not automatically use a runtime directory to store pid and fifo files.',
)
parser.add_argument(
'--noerrpages', action='store_false', dest='contribute_errpages',
help='Do not to configure custom error pages (403, 404, 500).',
)
parser.add_argument(
'--compile', action='store_true', dest='compile',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Command(FifoCommand):

help = 'Shutdown uWSGI instance'

def add_arguments(self, parser):
def add_arguments(self, parser): # pragma: nocover

super(Command, self).add_arguments(parser)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,25 @@ class Command(BaseCommand):

help = 'Generates configuration files for Systemd, Upstart, etc.'

def add_arguments(self, parser):
def add_arguments(self, parser): # pragma: nocover

super(Command, self).add_arguments(parser)

parser.add_argument(
'--systype', dest='systype',
help='System type alias to make configuration for. E.g.: systemd, upstart.',
)

parser.add_argument(
'--nostatic', action='store_true', dest='nostatic',
help='Tells uWSGI to NOT to serve static and media files.',
help='Do not serve static and media files.',
)
parser.add_argument(
'--noruntimes', action='store_true', dest='noruntimes',
help='Do not automatically use a runtime directory to store pid and fifo files.',
)
parser.add_argument(
'--noerrpages', action='store_true', dest='noerrpages',
help='Do not to configure custom error pages (403, 404, 500).',
)

def handle(self, *args, **options):
Expand All @@ -31,8 +38,9 @@ def handle(self, *args, **options):
mutator = SectionMutator.spawn()
command = 'manage.py uwsgi_run'

if options['nostatic']:
command = command + ' --nostatic'
for opt in ('nostatic', 'noruntimes', 'noerrpages'):
if options.get(opt, False):
command = command + ' --%s' % opt

config = get_config(
systype,
Expand Down
59 changes: 34 additions & 25 deletions uwsgiconf/contrib/django/uwsgify/toolbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,17 +86,22 @@ def spawn(cls, options=None, dir_base=None):
:rtype: SectionMutator
"""
options = options or {
options_all = {
'compile': True,
'embedded': False,

'contribute_static': False,
'contribute_runtimes': False,
'contribute_errpages': False,
}
options_all.update(options or {})

dir_base = os.path.abspath(dir_base or find_project_dir())

name_module = ConfModule.default_name
name_project = get_project_name(dir_base)
path_conf = os.path.join(dir_base, name_module)
embedded = options.get('embedded')
embedded = options_all['embedded']

# Read an existing config for further modification of first section.
section = cls._get_section_existing(
Expand All @@ -111,7 +116,7 @@ def spawn(cls, options=None, dir_base=None):
section=section,
dir_base=dir_base,
project_name=name_project,
options=options)
options=options_all)

mutator.mutate(embedded=embedded)

Expand Down Expand Up @@ -188,13 +193,6 @@ def _get_section_new(cls, dir_base):

def contribute_static(self):
"""Contributes static and media file serving settings to an existing section."""

options = self.options
if options['compile'] or not options['use_static_handler']:
return

from django.core.management import call_command

settings = self.settings
statics = self.section.statics

Expand All @@ -205,6 +203,10 @@ def contribute_static(self):
for url, path in static_tuples:
path and statics.register_static_map(url, path)

if self.options['compile']:
return

from django.core.management import call_command
call_command('collectstatic', clear=True, interactive=False)

def contribute_error_pages(self):
Expand Down Expand Up @@ -245,9 +247,7 @@ def mutate(self, embedded=False):

section.project_name = project_name

self.contribute_runtime_dir()

main = section.main_process
main = section.main_process

if embedded:

Expand All @@ -269,25 +269,34 @@ def mutate(self, embedded=False):
'Embedded mode: %s' % ('yes' if embedded else 'no'),
format_options='blue')

main.set_pid_file(
self.get_pid_filepath(),
before_priv_drop=False, # For vacuum to cleanup properly.
safe=True,
)

section.master_process.set_basic_params(
fifo_file=self.get_fifo_filepath(),
)

# todo maybe autoreload in debug

apps = section.applications
apps.set_basic_params(
manage_script_name=True,
)

self.contribute_error_pages()
self.contribute_static()
options = self.options

if options['contribute_runtimes']:

self.contribute_runtime_dir()

main.set_pid_file(
self.get_pid_filepath(),
before_priv_drop=False, # For vacuum to cleanup properly.
safe=True,
)

section.master_process.set_basic_params(
fifo_file=self.get_fifo_filepath(),
)

if options['contribute_static']:
self.contribute_static()

if options['contribute_errpages']:
self.contribute_error_pages()


def run_uwsgi(config_section, compile_only=False, embedded=False):
Expand Down

0 comments on commit a685123

Please sign in to comment.