From c22d3a99670153af2497228c03234557e1c68e64 Mon Sep 17 00:00:00 2001 From: Adarnof Date: Thu, 5 Oct 2017 00:52:45 -0400 Subject: [PATCH] Command line utility to create project. Shamelessly stolen from wagtail. --- allianceauth/bin/__init__.py | 0 allianceauth/bin/allianceauth.py | 79 ++++++++++++++++++++++++++++++++ setup.py | 4 ++ 3 files changed, 83 insertions(+) create mode 100644 allianceauth/bin/__init__.py create mode 100644 allianceauth/bin/allianceauth.py diff --git a/allianceauth/bin/__init__.py b/allianceauth/bin/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/allianceauth/bin/allianceauth.py b/allianceauth/bin/allianceauth.py new file mode 100644 index 00000000..9094d409 --- /dev/null +++ b/allianceauth/bin/allianceauth.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python +import os +from optparse import OptionParser + +from django.core.management import ManagementUtility + + +def create_project(parser, options, args): + # Validate args + if len(args) < 2: + parser.error("Please specify a name for your Alliance Auth installation") + elif len(args) > 3: + parser.error("Too many arguments") + + project_name = args[1] + try: + dest_dir = args[2] + except IndexError: + dest_dir = None + + # Make sure given name is not already in use by another python package/module. + try: + __import__(project_name) + except ImportError: + pass + else: + parser.error("'%s' conflicts with the name of an existing " + "Python module and cannot be used as a project " + "name. Please try another name." % project_name) + + print("Creating an Alliance Auth project called %(project_name)s" % {'project_name': project_name}) # noqa + + # Create the project from the Alliance Auth template using startapp + + # First find the path to Alliance Auth + import allianceauth + allianceauth_path = os.path.dirname(allianceauth.__file__) + template_path = os.path.join(allianceauth_path, 'project_template') + + # Call django-admin startproject + utility_args = ['django-admin.py', + 'startproject', + '--template=' + template_path, + project_name] + + if dest_dir: + utility_args.append(dest_dir) + + utility = ManagementUtility(utility_args) + utility.execute() + + print("Success! %(project_name)s has been created" % {'project_name': project_name}) # noqa + + +COMMANDS = { + 'start': create_project, +} + + +def main(): + # Parse options + parser = OptionParser(usage="Usage: %prog start project_name [directory]") + (options, args) = parser.parse_args() + + # Find command + try: + command = args[0] + except IndexError: + parser.print_help() + return + + if command in COMMANDS: + COMMANDS[command](parser, options, args) + else: + parser.error("Unrecognised command: " + command) + + +if __name__ == "__main__": + main() \ No newline at end of file diff --git a/setup.py b/setup.py index 7e9e35dc..3e49f6cc 100644 --- a/setup.py +++ b/setup.py @@ -51,4 +51,8 @@ setup( url='https://github.com/allianceauth/allianceauth', zip_safe=False, include_package_data=True, + entry_points=""" + [console_scripts] + allianceauth=allianceauth.bin.allianceauth:main + """, )