2013-09-08 14:30:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.core.management import execute_from_command_line
|
|
|
|
|
2015-02-18 16:44:26 +01:00
|
|
|
from openslides import __version__ as openslides_version
|
2013-09-08 14:30:26 +02:00
|
|
|
from openslides.utils.main import (
|
2013-10-28 16:34:53 +01:00
|
|
|
get_default_settings_path,
|
|
|
|
setup_django_settings_module,
|
2015-01-16 14:18:34 +01:00
|
|
|
write_settings,
|
|
|
|
UnknownCommand,
|
|
|
|
ExceptionArgumentParser,
|
|
|
|
get_development_settings_path,
|
2013-10-28 16:34:53 +01:00
|
|
|
start_browser,
|
2015-01-16 14:18:34 +01:00
|
|
|
is_development)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Main entrance to OpenSlides.
|
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
parser = get_parser()
|
|
|
|
try:
|
|
|
|
known_args, unknown_args = parser.parse_known_args()
|
|
|
|
except UnknownCommand:
|
|
|
|
unknown_command = True
|
2013-10-13 17:17:56 +02:00
|
|
|
else:
|
2015-01-16 14:18:34 +01:00
|
|
|
unknown_command = False
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if unknown_command:
|
|
|
|
# Run a command, that is defined by the django management api
|
|
|
|
development = is_development()
|
|
|
|
setup_django_settings_module(development=development)
|
|
|
|
execute_from_command_line(sys.argv)
|
|
|
|
else:
|
|
|
|
# Run a command that is defined here
|
|
|
|
# These are commands that can not rely on an existing settings
|
|
|
|
known_args.callback(known_args)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def get_parser():
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Parses all command line arguments.
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
if len(sys.argv) == 1 and not is_development():
|
2013-09-08 14:30:26 +02:00
|
|
|
sys.argv.append('start')
|
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Init parser
|
2013-09-08 14:30:26 +02:00
|
|
|
description = 'Start script for OpenSlides.'
|
|
|
|
if 'manage.py' not in sys.argv[0]:
|
|
|
|
description += (' If it is called without any argument, this will be '
|
|
|
|
'treated as if it is called with the "start" subcommand. '
|
2013-10-13 17:17:56 +02:00
|
|
|
'That means OpenSlides will setup default settings and '
|
|
|
|
'database, start the tornado webserver, launch the '
|
|
|
|
'default web browser and open the webinterface.')
|
2015-01-16 14:18:34 +01:00
|
|
|
parser = ExceptionArgumentParser(description=description)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Add version argument
|
2013-09-08 14:30:26 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--version',
|
|
|
|
action='version',
|
2015-02-18 16:44:26 +01:00
|
|
|
version=openslides_version,
|
2013-09-08 14:30:26 +02:00
|
|
|
help='Show version number and exit.')
|
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Init subparsers
|
2013-09-08 14:30:26 +02:00
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
dest='subcommand',
|
|
|
|
title='Available subcommands',
|
2013-11-23 18:49:51 +01:00
|
|
|
description="Type '%s <subcommand> --help' for help on a "
|
2013-10-13 17:17:56 +02:00
|
|
|
"specific subcommand." % parser.prog,
|
2013-09-08 14:30:26 +02:00
|
|
|
help='You can choose only one subcommand at once.')
|
|
|
|
|
|
|
|
# Subcommand start
|
|
|
|
subcommand_start = subparsers.add_parser(
|
|
|
|
'start',
|
|
|
|
help='Setup settings and database, start tornado webserver, launch the '
|
2015-01-16 14:18:34 +01:00
|
|
|
'default web browser and open the webinterface. The environment '
|
|
|
|
'variable DJANGO_SETTINGS_MODULE is ignored.')
|
2013-10-13 17:17:56 +02:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--no-browser',
|
|
|
|
action='store_true',
|
|
|
|
help='Do not launch the default web browser.')
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--settings_path',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='The used settings file. The file is created, if it does not exist.')
|
2013-09-08 14:30:26 +02:00
|
|
|
subcommand_start.set_defaults(callback=start)
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--development',
|
2013-09-08 14:30:26 +02:00
|
|
|
action='store_true',
|
2015-01-16 14:18:34 +01:00
|
|
|
help='Command for development purposes.')
|
|
|
|
|
|
|
|
# Subcommand createsettings
|
|
|
|
subcommand_createsettings = subparsers.add_parser(
|
|
|
|
'createsettings',
|
|
|
|
help='Create the settings file.')
|
|
|
|
subcommand_createsettings.set_defaults(callback=createsettings)
|
|
|
|
subcommand_createsettings.add_argument(
|
|
|
|
'--settings_path',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='The used settings file. The file is created, even if it exists.')
|
|
|
|
subcommand_createsettings.add_argument(
|
|
|
|
'--development',
|
|
|
|
action='store_true',
|
|
|
|
help='Command for development purposes.')
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
return parser
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def start(args):
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Starts OpenSlides: Runs migrations and runs runserver.
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = args.settings_path
|
|
|
|
development = is_development()
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if settings_path is None:
|
|
|
|
if development:
|
|
|
|
settings_path = get_development_settings_path()
|
|
|
|
else:
|
|
|
|
settings_path = get_default_settings_path()
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Write settings if it does not exists.
|
|
|
|
if not os.path.isfile(settings_path):
|
|
|
|
createsettings(args)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Set the django setting module and run migrations
|
|
|
|
# A manual given environment variable will be overwritten
|
|
|
|
setup_django_settings_module(settings_path, development=development)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
execute_from_command_line(['manage.py', 'migrate'])
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if not args.no_browser:
|
|
|
|
start_browser('http://0.0.0.0:8000')
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Start the webserver
|
|
|
|
execute_from_command_line(['manage.py', 'runserver', '0.0.0.0:8000'])
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def createsettings(args):
|
2013-10-28 16:34:53 +01:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Creates settings for OpenSlides.
|
2013-10-28 16:34:53 +01:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = args.settings_path
|
|
|
|
development = is_development()
|
|
|
|
context = {}
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if development:
|
|
|
|
if settings_path is None:
|
|
|
|
settings_path = get_development_settings_path()
|
|
|
|
context = {
|
|
|
|
'openslides_user_data_path': repr(os.path.join(os.getcwd(), 'development')),
|
|
|
|
'debug': 'True'}
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = write_settings(settings_path, **context)
|
|
|
|
print('Settings created at %s' % settings_path)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
exit(main())
|