diff options
Diffstat (limited to 'testapp/scripts')
-rw-r--r-- | testapp/scripts/vimarsh.py | 92 |
1 files changed, 55 insertions, 37 deletions
diff --git a/testapp/scripts/vimarsh.py b/testapp/scripts/vimarsh.py index 863193a..07966d8 100644 --- a/testapp/scripts/vimarsh.py +++ b/testapp/scripts/vimarsh.py @@ -1,8 +1,11 @@ +from __future__ import print_function + import subprocess import contextlib import os from os import path import argparse +from importlib import import_module from django.conf import settings from django.core import management from django.template import Template, Context, loader @@ -16,51 +19,59 @@ TEMPLATE_DIR = path.join(PARENT_DIR, 'templates') def main(): #Parse command-line to obtain the arguments and/or options - help_msg = ("Enter the subcommand to be executed.\n" - "Available subcommands:\n" - " - create_demo <projectname> [destination-path]\n" - " - run_demo") - - parser = argparse.ArgumentParser(prog="vimarsh", - usage="vimarsh.py subcommand [args]", - formatter_class=argparse.RawTextHelpFormatter) - parser.add_argument("subcommand", type=str, nargs='+', help=help_msg) + # create top-level parser object + parser = argparse.ArgumentParser(prog="vimarsh") + subparser = parser.add_subparsers(dest="subcommand") + + # create parser for the "create_demo" subcommand + create_demo_parser = subparser.add_parser("create_demo", + help="Create a new demo Django project") + create_demo_parser.add_argument("project_name", type=str, + help="name of demo Django project") + create_demo_parser.add_argument("-p", "--path", type=str, + help="path of demo Django project") + + # create parser for the "run_demo" subcommand + run_demo_parser = subparser.add_parser("run_demo", + help="Initialise django server and run the demo project") + + # create parser for the "run_code_server" subcommand + code_server_parser = subparser.add_parser("run_code_server", + help="Initialise Vimarsh code server") + code_server_parser.add_argument("-P", "--ports", type=int, nargs='+', + help="code server ports") + args = parser.parse_args() - if args.subcommand[0] == "create_demo": - if len(args.subcommand) > 3 or len(args.subcommand) <= 1: - parser.print_help() - elif len(args.subcommand) == 3: - project_name = args.subcommand[1] - project_dir = args.subcommand[2] - create_demo(args.subcommand[1], args.subcommand[2]) + if args.subcommand == "create_demo": + if args.path: + create_demo(args.project_name, args.path) else: - project_name = args.subcommand[1] - create_demo(args.subcommand[1]) - - elif args.subcommand[0] == "run_demo": - if len(args.subcommand) != 1: - parser.print_help() + create_demo(args.project_name) + + elif args.subcommand == "run_demo": + try: + run_demo(NAME, PATH) + except Exception as e: + if not NAME or not PATH: + print("Error: Unable to find Project Name or Path variables\n") + else: + print("Error: {0}\n".format(e)) + subparser.print_help() + + elif args.subcommand == "run_code_server": + if args.ports: + run_server(args.ports) else: - try: - run_demo(NAME, PATH) - except Exception, e: - if not NAME or not PATH: - print "Error: Unable to find Project Name and Path variables\n" - else: - print "Error: ", e, "\n" - parser.print_help() - - else: - parser.print_help() - + run_server() -def create_demo(project_name='vimarsh_demo', project_dir=None): +def create_demo(project_name='vimarsh_demo', project_dir=CUR_DIR): try: management.call_command('startproject', project_name, project_dir) - print "Demo Django project '{0}' created at '{1}'" + print("Demo Django project '{0}' created at '{1}'".format(project_name, + project_dir)) except Exception, e: - print "Error: ", e, "\nExiting Vimarsh Installer" + print("Error: {0}\nExiting Vimarsh Installer".format(e)) if project_dir is None: top_dir = path.join(os.getcwd(), project_name) @@ -96,6 +107,13 @@ def run_demo(project_name, top_dir): "--settings={0}.demo_settings").format(project_name) subprocess.call(command, shell=True) +def run_server(): + try: + from testapp.exam import code_server + code_server.main() + except Exception as e: + print("Error: {0}\nExiting Vimarsh code server".format(e)) + def _set_project_details(project_name, top_dir): file_path = path.join(SCRIPT_DIR, 'project_detail.py') detail = "NAME ='{0}'\nPATH ='{1}'".format(project_name, top_dir) |