|
1 | 1 | #!/usr/bin/env python |
2 | 2 | # -*- coding: utf-8 -*- |
| 3 | +import argparse |
3 | 4 | import os |
4 | 5 | import sys |
5 | 6 |
|
6 | 7 | from django.core.management import execute_from_command_line |
7 | 8 |
|
8 | 9 |
|
9 | 10 | def main(): |
| 11 | + """ |
| 12 | + Runs the Mathics3 Webserver with supplied options. |
| 13 | + """ |
| 14 | + # Process options |
| 15 | + parser = argparse.ArgumentParser( |
| 16 | + prog="Mathics3Server", description="Run the Mathics3 Django webserver." |
| 17 | + ) |
| 18 | + parser.add_argument( |
| 19 | + "--debug", action="store_true", help="Set DEBUG to True in settings" |
| 20 | + ) |
| 21 | + parser.add_argument( |
| 22 | + "--production", action="store_true", help="Run with Daphne production server" |
| 23 | + ) |
| 24 | + parser.add_argument( |
| 25 | + "-p", |
| 26 | + "--port", |
| 27 | + type=str, |
| 28 | + default="8000", |
| 29 | + help="Port to listen on (default: 8000)", |
| 30 | + ) |
| 31 | + parser.add_argument( |
| 32 | + "-b", |
| 33 | + "--host", |
| 34 | + type=str, |
| 35 | + default="localhost", |
| 36 | + help="The host address to listen on (default: localhost)", |
| 37 | + ) |
| 38 | + |
| 39 | + # Use parse_known_args so we don't crash on standard Django commands (like 'runserver') |
| 40 | + # If --help or -h is given in sys.argv, this will print help and exit immediately. |
| 41 | + args, unknown = parser.parse_known_args() |
| 42 | + |
10 | 43 | os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mathics_django.settings") |
11 | | - if len(sys.argv) == 1: |
12 | | - sys.argv.append("runserver") |
13 | | - execute_from_command_line(sys.argv) |
| 44 | + |
| 45 | + # Handle --debug option |
| 46 | + if args.debug: |
| 47 | + # This overrides settings at runtime by setting an environment variable |
| 48 | + # Ensure your mathics_django/settings.py reads DEBUG from the environment |
| 49 | + os.environ["DEBUG"] = "True" |
| 50 | + |
| 51 | + # Handle --production ption |
| 52 | + if args.production: |
| 53 | + try: |
| 54 | + # Import and call Daphne's CLI directly |
| 55 | + from daphne.cli import CommandLineInterface |
| 56 | + |
| 57 | + # The run() method expects a list of arguments starting with the application path |
| 58 | + # Equivalent to: daphne -b args.port -p args.host mathics_django.asgi:application |
| 59 | + CommandLineInterface().run( |
| 60 | + ["mathics_django.asgi:application", "-b", args.host, "-p", args.port] |
| 61 | + ) |
| 62 | + return # Exit after server stops |
| 63 | + except ImportError: |
| 64 | + print( |
| 65 | + "Error: 'daphne' is not installed. Run 'pip install daphne' for production mode." |
| 66 | + ) |
| 67 | + sys.exit(1) |
| 68 | + |
| 69 | + # Fallback to standard Django execution, defaulting to "runserver" commands |
| 70 | + # if --production not provided |
| 71 | + django_args = [sys.argv[0]] + unknown |
| 72 | + |
| 73 | + # If no command is provided, default to runserver with our port and bind address |
| 74 | + if not unknown: |
| 75 | + django_args.extend(["runserver", f"{args.host}:{args.port}"]) |
| 76 | + |
| 77 | + execute_from_command_line(django_args) |
14 | 78 |
|
15 | 79 |
|
16 | 80 | if __name__ == "__main__": |
|
0 commit comments