Mercurial > p > roundup > code
comparison roundup/scripts/roundup_demo.py @ 8086:39013eba1aaf
refactor: move template prompt to new function; ruff lint cleanups
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 14 Jul 2024 13:27:37 -0400 |
| parents | 05f36a527a45 |
| children | 0cb81ee2e572 |
comparison
equal
deleted
inserted
replaced
| 8085:3a674b7eb07f | 8086:39013eba1aaf |
|---|---|
| 2 # | 2 # |
| 3 # Copyright 2004 Richard Jones (richard@mechanicalcat.net) | 3 # Copyright 2004 Richard Jones (richard@mechanicalcat.net) |
| 4 # | 4 # |
| 5 | 5 |
| 6 import argparse | 6 import argparse |
| 7 import sys | |
| 8 | 7 |
| 9 # --- patch sys.path to make sure 'import roundup' finds correct version | 8 # --- patch sys.path to make sure 'import roundup' finds correct version |
| 10 import os.path as osp | 9 import os.path as osp |
| 10 import sys | |
| 11 | 11 |
| 12 thisdir = osp.dirname(osp.abspath(__file__)) | 12 thisdir = osp.dirname(osp.abspath(__file__)) |
| 13 rootdir = osp.dirname(osp.dirname(thisdir)) | 13 rootdir = osp.dirname(osp.dirname(thisdir)) |
| 14 if (osp.exists(thisdir + '/__init__.py') and | 14 if (osp.exists(thisdir + '/__init__.py') and |
| 15 osp.exists(rootdir + '/roundup/__init__.py')): | 15 osp.exists(rootdir + '/roundup/__init__.py')): |
| 16 # the script is located inside roundup source code | 16 # the script is located inside roundup source code |
| 17 sys.path.insert(0, rootdir) | 17 sys.path.insert(0, rootdir) |
| 18 # --/ | 18 # --/ |
| 19 | 19 |
| 20 # import also verifies python version as side effect | 20 # import also verifies python version as side effect |
| 21 from roundup import version_check # noqa: F401 E402 | 21 from roundup import __version__ as roundup_version # noqa: E402 |
| 22 from roundup import admin, configuration, demo, instance # noqa: E402 | 22 from roundup import admin, configuration, demo, instance, version_check # noqa: F401 E402 |
| 23 from roundup import __version__ as roundup_version # noqa: E402 | 23 from roundup.anypy.my_input import my_input # noqa: E402 |
| 24 from roundup.anypy.my_input import my_input # noqa: E402 | 24 from roundup.backends import list_backends # noqa: E402 |
| 25 from roundup.backends import list_backends # noqa: E402 | 25 from roundup.i18n import _ # noqa: E402 |
| 26 from roundup.i18n import _ # noqa: E402 | |
| 27 | |
| 28 | 26 |
| 29 DEFAULT_HOME = './demo' | 27 DEFAULT_HOME = './demo' |
| 30 DEFAULT_TEMPLATE = 'classic' | 28 DEFAULT_TEMPLATE = 'classic' |
| 31 DEFAULT_BACKEND = 'sqlite' | 29 DEFAULT_BACKEND = 'sqlite' |
| 32 DEFAULT_PORT = 8917 | 30 DEFAULT_PORT = 8917 |
| 44 print("%s\n" % usage) | 42 print("%s\n" % usage) |
| 45 | 43 |
| 46 if msg: | 44 if msg: |
| 47 print(msg) | 45 print(msg) |
| 48 | 46 |
| 47 | |
| 48 def ask_for_template(default_template, templates): | |
| 49 import pdb; pdb.set_trace() | |
| 50 template = my_input( | |
| 51 _('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') % | |
| 52 {'template_list': ','.join(templates), | |
| 53 'default_template': default_template}) | |
| 54 | |
| 55 if not template: | |
| 56 template = default_template | |
| 57 elif template not in templates: | |
| 58 print("Unknown template: %s. Exiting." % template) | |
| 59 return None | |
| 60 | |
| 61 return template | |
| 62 | |
| 63 | |
| 49 def run(): | 64 def run(): |
| 50 templates = admin.AdminTool().listTemplates().keys() | 65 templates = admin.AdminTool().listTemplates().keys() |
| 51 backends = list_backends() | 66 backends = list_backends() |
| 52 | 67 |
| 53 cli = argparse.ArgumentParser( | 68 cli = argparse.ArgumentParser( |
| 54 description= """ | 69 description=""" |
| 55 Instant gratification demo - Roundup Issue Tracker | 70 Instant gratification demo - Roundup Issue Tracker |
| 56 | 71 |
| 57 Run a demo server. Config and database files are created in | 72 Run a demo server. Config and database files are created in |
| 58 'directory' (current setting/default '%(home)s') which should | 73 'directory' (current setting/default '%(home)s') which should |
| 59 not exist or should exist and already be a tracker home | 74 not exist or should exist and already be a tracker home |
| 68 formatter_class=argparse.RawTextHelpFormatter, | 83 formatter_class=argparse.RawTextHelpFormatter, |
| 69 add_help=True) | 84 add_help=True) |
| 70 | 85 |
| 71 cli.add_argument('-B', '--bind_address', | 86 cli.add_argument('-B', '--bind_address', |
| 72 default="127.0.0.1", | 87 default="127.0.0.1", |
| 73 help=( "Choose address for server to listen at.\n" | 88 help=("Choose address for server to listen at.\n" |
| 74 "Use 0.0.0.0 to bind to all addreses. Use\n" | 89 "Use 0.0.0.0 to bind to all addreses. Use\n" |
| 75 "the external name of the computer to bind to\n" | 90 "the external name of the computer to bind to\n" |
| 76 "the external host interface.\n" | 91 "the external host interface.\n" |
| 77 "Default: %(default)s.\n\n")) | 92 "Default: %(default)s.\n\n")) |
| 78 cli.add_argument('-b', '--backend_db', | 93 cli.add_argument('-b', '--backend_db', |
| 79 choices=backends, | 94 choices=backends, |
| 80 help=( "Choose backend database. Default: %s.\n\n" % | 95 help=("Choose backend database. Default: %s.\n\n" % |
| 81 DEFAULT_BACKEND)) | 96 DEFAULT_BACKEND)) |
| 82 cli.add_argument('-H', '--hostname', | 97 cli.add_argument('-H', '--hostname', |
| 83 default="localhost", | 98 default="localhost", |
| 84 help=( "Choose hostname for the server.\n" | 99 help=("Choose hostname for the server.\n" |
| 85 "Default: %(default)s.\n\n" | 100 "Default: %(default)s.\n\n" |
| 86 )) | 101 )) |
| 87 cli.add_argument('-t', '--template', | 102 cli.add_argument('-t', '--template', |
| 88 choices=templates, | 103 choices=templates, |
| 89 help="Use specified template. (*)\n\n") | 104 help="Use specified template. (*)\n\n") |
| 90 cli.add_argument('-p', '--port', | 105 cli.add_argument('-p', '--port', |
| 91 type=int, | 106 type=int, |
| 92 help=( "Listen at this port. Default: search for\n" | 107 help=("Listen at this port. Default: search for\n" |
| 93 "open port starting at %s\n\n" % DEFAULT_PORT)) | 108 "open port starting at %s\n\n" % DEFAULT_PORT)) |
| 94 cli.add_argument('-P', '--urlport', | 109 cli.add_argument('-P', '--urlport', |
| 95 type=int, | 110 type=int, |
| 96 help=( "Set docker external port. If using\n" | 111 help=("Set docker external port. If using\n" |
| 97 " docker ... -p 9090:8917 ...\n" | 112 " docker ... -p 9090:8917 ...\n" |
| 98 "this should be set to 9090.\n" | 113 "this should be set to 9090.\n" |
| 99 "Default: as selected by --port\n\n")) | 114 "Default: as selected by --port\n\n")) |
| 100 cli.add_argument('-V', '--version', action='version', | 115 cli.add_argument('-V', '--version', action='version', |
| 101 version='Roundup version %s'%roundup_version, | 116 version='Roundup version %s' % roundup_version, |
| 102 help=( | 117 help=( |
| 103 "Show program's version number: %s and exit\n" % | 118 "Show program's version number: %s and exit\n" % |
| 104 roundup_version)) | 119 roundup_version)) |
| 105 | 120 |
| 106 cli.add_argument('directory', nargs='?', | 121 cli.add_argument('directory', nargs='?', |
| 107 help="Create home for tracker in directory. (*)\n") | 122 help="Create home for tracker in directory. (*)\n") |
| 108 | 123 |
| 109 # add 'nuke' to choices so backend will accept nuke if only 2 args. | 124 # add 'nuke' to choices so backend will accept nuke if only 2 args. |
| 110 choices = backends + ['nuke'] | 125 choices = backends + ['nuke'] |
| 111 cli.add_argument('backend', nargs='?', metavar='backend', choices=choices, | 126 cli.add_argument('backend', nargs='?', metavar='backend', choices=choices, |
| 112 help=( "Choose backend database. " | 127 help=("Choose backend database. " |
| 113 "Depricated, use -b instead.\n" | 128 "Depricated, use -b instead.\n" |
| 114 "If it is used, you *must* specify directory.\n\n")) | 129 "If it is used, you *must* specify directory.\n\n")) |
| 115 | 130 |
| 116 cli.add_argument('nuke', nargs='?', metavar='nuke', choices=['nuke'], | 131 cli.add_argument('nuke', nargs='?', metavar='nuke', choices=['nuke'], |
| 117 help=( "The word 'nuke' will delete tracker and reset.\n" | 132 help=("The word 'nuke' will delete tracker and reset.\n" |
| 118 "E.G. %(prog)s -b sqlite \\ \n" | 133 "E.G. %(prog)s -b sqlite \\ \n" |
| 119 "-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]}) | 134 "-t classic ./mytracker nuke\n") % {"prog": sys.argv[0]}) |
| 120 | 135 |
| 121 cli_args = cli.parse_args() | 136 cli_args = cli.parse_args() |
| 122 | 137 |
| 123 # collect all positional args in order in array to parse | 138 # collect all positional args in order in array to parse |
| 124 # strip all None. | 139 # strip all None. |
| 125 cli_args.cmd = [ x for x in [cli_args.directory, cli_args.backend, cli_args.nuke] if x != None ] | 140 cli_args.cmd = [x for x in [cli_args.directory, cli_args.backend, cli_args.nuke] |
| 141 if x is not None] | |
| 126 | 142 |
| 127 try: | 143 try: |
| 128 nuke = cli_args.cmd[-1] == 'nuke' | 144 nuke = cli_args.cmd[-1] == 'nuke' |
| 129 if nuke: | 145 if nuke: |
| 130 _ignore = cli_args.cmd.pop() # remove nuke | 146 _ignore = cli_args.cmd.pop() # remove nuke |
| 153 home = DEFAULT_HOME | 169 home = DEFAULT_HOME |
| 154 | 170 |
| 155 # if there is no tracker in home, force nuke | 171 # if there is no tracker in home, force nuke |
| 156 try: | 172 try: |
| 157 instance.open(home) | 173 instance.open(home) |
| 158 valid_home = True | |
| 159 except configuration.NoConfigError: | 174 except configuration.NoConfigError: |
| 160 nuke = True | 175 nuke = True |
| 161 valid_home = False | |
| 162 | 176 |
| 163 # if we are to create the tracker, prompt for settings | 177 # if we are to create the tracker, prompt for settings |
| 164 if nuke: | 178 if nuke: |
| 165 # FIXME: i'd like to have an option to abort the tracker creation | 179 # FIXME: i'd like to have an option to abort the tracker creation |
| 166 # say, by entering a single dot. but i cannot think of | 180 # say, by entering a single dot. but i cannot think of |
| 167 # appropriate prompt for that. | 181 # appropriate prompt for that. |
| 168 if not cli_args.template in templates: | 182 if cli_args.template not in templates: |
| 169 template = my_input( | 183 template = ask_for_template(template, templates) |
| 170 _('Enter tracker template to use (one of (%(template_list)s)) [%(default_template)s]: ') % | |
| 171 { 'template_list': ','.join(templates), | |
| 172 'default_template': template}) | |
| 173 if not template: | 184 if not template: |
| 174 template = DEFAULT_TEMPLATE | 185 sys.exit(1) |
| 175 elif template not in templates: | |
| 176 print("Unknown template: %s. Exiting." % template) | |
| 177 exit(1) | |
| 178 # install | 186 # install |
| 179 url_port = cli_args.urlport or cli_args.port or DEFAULT_PORT | 187 url_port = cli_args.urlport or cli_args.port or DEFAULT_PORT |
| 180 demo.install_demo(home, backend, | 188 demo.install_demo(home, backend, |
| 181 admin.AdminTool().listTemplates()[template]['path'], | 189 admin.AdminTool().listTemplates()[template]['path'], |
| 182 use_port=url_port, use_host=cli_args.hostname) | 190 use_port=url_port, use_host=cli_args.hostname) |
| 183 else: | 191 elif (cli_args.backend or cli_args.template or cli_args.backend_db): |
| 184 # make sure that no options are specified that are only useful on initialization. | 192 # options were specified that are only useful on initialization. |
| 185 if ( cli_args.backend or cli_args.template or | 193 usage(home, cli, msg=( |
| 186 cli_args.backend_db ): | 194 "Specifying backend or template is only allowed when\n" |
| 187 usage(home, cli, msg=( | 195 "creating a tracker or with nuke.\n")) |
| 188 "Specifying backend or template is only allowed when\n" | 196 sys.exit(1) |
| 189 "creating a tracker or with nuke.\n")) | |
| 190 exit(1) | |
| 191 # run | 197 # run |
| 192 demo.run_demo(home, bind_addr=cli_args.bind_address, | 198 demo.run_demo(home, bind_addr=cli_args.bind_address, |
| 193 bind_port=cli_args.port) | 199 bind_port=cli_args.port) |
| 194 | 200 |
| 201 | |
| 195 if __name__ == '__main__': | 202 if __name__ == '__main__': |
| 196 run() | 203 run() |
| 197 | 204 |
| 198 # vim: set et sts=4 sw=4 : | 205 # vim: set et sts=4 sw=4 : |
