|
1 |
| -import os, sys, argparse, subprocess, signal |
| 1 | +import os, sys, argparse, subprocess, signal, shlex |
2 | 2 |
|
3 | 3 | # Project defaults
|
4 | 4 | FLASK_APP = 'server/__init__.py'
|
5 | 5 | DEFAULT_IP = '0.0.0.0:3000'
|
6 | 6 |
|
| 7 | + |
7 | 8 | class Command:
|
8 |
| - def __init__(self, name, descr, runcmd, env={}): |
9 |
| - self.name = name |
10 |
| - self.descr = descr |
11 |
| - self.runcmd = runcmd |
12 |
| - self.env = env |
13 |
| - |
14 |
| - def run(self, conf): |
15 |
| - cmd = self.runcmd(conf) |
16 |
| - env = os.environ |
17 |
| - env.update(conf) |
18 |
| - env.update(self.env) |
19 |
| - subprocess.call(cmd, env=env, shell=True) |
| 9 | + def __init__(self, name, descr, runcmd, env={}): |
| 10 | + self.name = name |
| 11 | + self.descr = descr |
| 12 | + self.runcmd = runcmd |
| 13 | + self.env = env |
| 14 | + |
| 15 | + def run(self, conf): |
| 16 | + cmd = self.runcmd(conf) |
| 17 | + env = os.environ |
| 18 | + env.update(conf) |
| 19 | + env.update(self.env) |
| 20 | + subprocess.call(shlex.split(cmd), env=env, shell=True) |
| 21 | + |
20 | 22 |
|
21 | 23 | class CommandManager:
|
22 |
| - def __init__(self): |
23 |
| - self.commands = {} |
24 |
| - |
25 |
| - def add(self, command): |
26 |
| - self.commands[command.name] = command |
27 |
| - |
28 |
| - def configure(self, conf): |
29 |
| - self.conf = conf |
30 |
| - |
31 |
| - def run(self, command): |
32 |
| - if command in self.commands: |
33 |
| - self.commands[command].run(self.conf) |
34 |
| - else: |
35 |
| - print("invalid command specified\n") |
36 |
| - print(self.availableCommands()) |
37 |
| - |
38 |
| - def availableCommands(self): |
39 |
| - commands = sorted(self.commands.values(), key=lambda c: c.name) |
40 |
| - space = max([len(c.name) for c in commands]) + 2 |
41 |
| - description = 'available subcommands:\n' |
42 |
| - for c in commands: |
43 |
| - description += ' ' + c.name + ' ' * (space - len(c.name)) + c.descr + '\n' |
44 |
| - return description |
| 24 | + def __init__(self): |
| 25 | + self.commands = {} |
| 26 | + |
| 27 | + def add(self, command): |
| 28 | + self.commands[command.name] = command |
| 29 | + |
| 30 | + def configure(self, conf): |
| 31 | + self.conf = conf |
| 32 | + |
| 33 | + def run(self, command): |
| 34 | + if command in self.commands: |
| 35 | + self.commands[command].run(self.conf) |
| 36 | + else: |
| 37 | + print("invalid command specified\n") |
| 38 | + print(self.availableCommands()) |
| 39 | + |
| 40 | + def availableCommands(self): |
| 41 | + commands = sorted(self.commands.values(), key=lambda c: c.name) |
| 42 | + space = max([len(c.name) for c in commands]) + 2 |
| 43 | + description = 'available subcommands:\n' |
| 44 | + for c in commands: |
| 45 | + description += ' ' + c.name + ' ' * (space - len(c.name)) + c.descr + '\n' |
| 46 | + return description |
| 47 | + |
45 | 48 |
|
46 | 49 | cm = CommandManager()
|
47 | 50 |
|
48 | 51 | cm.add(Command(
|
49 |
| - "build", |
50 |
| - "compiles python files in project into .pyc binaries", |
51 |
| - lambda c: 'python -m compileall .')) |
| 52 | + "build", |
| 53 | + "compiles python files in project into .pyc binaries", |
| 54 | + lambda c: 'python -m compileall .')) |
52 | 55 |
|
53 | 56 | cm.add(Command(
|
54 |
| - "start", |
55 |
| - "runs server with gunicorn in a production setting", |
56 |
| - lambda c: 'gunicorn -b {0}:{1} server:app'.format(c['host'], c['port']), |
57 |
| - { |
58 |
| - 'FLASK_APP': FLASK_APP, |
59 |
| - 'FLASK_DEBUG': 'false' |
60 |
| - })) |
| 57 | + "start", |
| 58 | + "runs server with gunicorn in a production setting", |
| 59 | + lambda c: 'gunicorn -b {0}:{1} server:app'.format(c['host'], c['port']), |
| 60 | + { |
| 61 | + 'FLASK_APP': FLASK_APP, |
| 62 | + 'FLASK_DEBUG': 'false' |
| 63 | + })) |
61 | 64 |
|
62 | 65 | cm.add(Command(
|
63 |
| - "run", |
64 |
| - "runs dev server using Flask's native debugger & backend reloader", |
65 |
| - lambda c: 'python -m flask run --host={0} --port={1} --debugger --reload'.format(c['host'], c['port']), |
66 |
| - { |
67 |
| - 'FLASK_APP': FLASK_APP, |
68 |
| - 'FLASK_DEBUG': 'true' |
69 |
| - })) |
| 66 | + "run", |
| 67 | + "runs dev server using Flask's native debugger & backend reloader", |
| 68 | + lambda c: 'python -m flask run --host={0} --port={1} --debugger --reload'.format(c['host'], c['port']), |
| 69 | + { |
| 70 | + 'FLASK_APP': FLASK_APP, |
| 71 | + 'FLASK_DEBUG': 'true' |
| 72 | + })) |
70 | 73 |
|
71 | 74 | cm.add(Command(
|
72 |
| - "livereload", |
73 |
| - "runs dev server using livereload for dynamic webpage reloading", |
74 |
| - lambda c: 'python -m flask run', |
75 |
| - { |
76 |
| - 'FLASK_APP': FLASK_APP, |
77 |
| - 'FLASK_LIVE_RELOAD': 'true', |
78 |
| - })) |
| 75 | + "livereload", |
| 76 | + "runs dev server using livereload for dynamic webpage reloading", |
| 77 | + lambda c: 'python -m flask run', |
| 78 | + { |
| 79 | + 'FLASK_APP': FLASK_APP, |
| 80 | + 'FLASK_LIVE_RELOAD': 'true', |
| 81 | + })) |
79 | 82 |
|
80 | 83 | cm.add(Command(
|
81 |
| - "debug", |
82 |
| - "runs dev server in debug mode; use with an IDE's remote debugger", |
83 |
| - lambda c: 'python -m flask run --host={0} --port={1} --no-debugger --no-reload'.format(c['host'], c['port']), |
84 |
| - { |
85 |
| - 'FLASK_APP': FLASK_APP, |
86 |
| - 'FLASK_DEBUG': 'true' |
87 |
| - })) |
| 84 | + "debug", |
| 85 | + "runs dev server in debug mode; use with an IDE's remote debugger", |
| 86 | + lambda c: 'python -m flask run --host={0} --port={1} --no-debugger --no-reload'.format(c['host'], c['port']), |
| 87 | + { |
| 88 | + 'FLASK_APP': FLASK_APP, |
| 89 | + 'FLASK_DEBUG': 'true' |
| 90 | + })) |
88 | 91 |
|
89 | 92 | cm.add(Command(
|
90 |
| - "test", |
91 |
| - "runs all tests inside of `tests` directory", |
92 |
| - lambda c: 'python -m unittest discover -s tests -p "*.py"')) |
| 93 | + "test", |
| 94 | + "runs all tests inside of `tests` directory", |
| 95 | + lambda c: 'python -m unittest discover -s tests -p "*.py"')) |
93 | 96 |
|
94 | 97 | # Create and format argument parser for CLI
|
95 | 98 | parser = argparse.ArgumentParser(description=cm.availableCommands(),
|
96 |
| - formatter_class=argparse.RawDescriptionHelpFormatter) |
| 99 | + formatter_class=argparse.RawDescriptionHelpFormatter) |
97 | 100 | parser.add_argument("subcommand", help="subcommand to run (see list above)")
|
98 | 101 | parser.add_argument("ipaddress", nargs='?', default=DEFAULT_IP,
|
99 |
| - help="address and port to run on (i.e. {0})".format(DEFAULT_IP)) |
| 102 | + help="address and port to run on (i.e. {0})".format(DEFAULT_IP)) |
| 103 | + |
| 104 | + |
100 | 105 | def livereload_check():
|
101 |
| - check = subprocess.call("lsof -n -i4TCP:3000", shell=True) |
102 |
| - if (check == 0): |
103 |
| - output = subprocess.check_output("pgrep Python", shell=True) |
104 |
| - pypid = int(output) |
105 |
| - os.kill(pypid, signal.SIGKILL) |
106 |
| - print("Discovered rogue Python process: {0}".format(pypid)) |
107 |
| - print("Killing PID {0}...".format(pypid)) |
108 |
| - else: |
109 |
| - print(" No rogue Python process running") |
110 |
| - |
| 106 | + check = subprocess.call("lsof -n -i4TCP:3000", shell=True) |
| 107 | + if (check == 0): |
| 108 | + output = subprocess.check_output("pgrep Python", shell=True) |
| 109 | + pypid = int(output) |
| 110 | + os.kill(pypid, signal.SIGKILL) |
| 111 | + print("Discovered rogue Python process: {0}".format(pypid)) |
| 112 | + print("Killing PID {0}...".format(pypid)) |
| 113 | + else: |
| 114 | + print(" No rogue Python process running") |
| 115 | + |
| 116 | + |
111 | 117 | # Take in command line input for configuration
|
112 | 118 | try:
|
113 |
| - args = parser.parse_args() |
114 |
| - cmd = args.subcommand |
115 |
| - addr = args.ipaddress.split(':') |
116 |
| - cm.configure({ |
117 |
| - 'host': addr[0], |
118 |
| - 'port': addr[1], |
119 |
| - }) |
120 |
| - cm.run(cmd) |
| 119 | + args = parser.parse_args() |
| 120 | + cmd = args.subcommand |
| 121 | + addr = args.ipaddress.split(':') |
| 122 | + cm.configure({ |
| 123 | + 'host': addr[0], |
| 124 | + 'port': addr[1], |
| 125 | + }) |
| 126 | + cm.run(cmd) |
121 | 127 | except KeyboardInterrupt:
|
122 |
| - if 'FLASK_LIVE_RELOAD' in os.environ and os.environ['FLASK_LIVE_RELOAD'] == 'true': |
123 |
| - livereload_check() |
| 128 | + if 'FLASK_LIVE_RELOAD' in os.environ and os.environ['FLASK_LIVE_RELOAD'] == 'true': |
| 129 | + livereload_check() |
124 | 130 | except:
|
125 |
| - if len(sys.argv) == 1: |
126 |
| - print(cm.availableCommands()) |
127 |
| - sys.exit(0) |
| 131 | + if len(sys.argv) == 1: |
| 132 | + print(cm.availableCommands()) |
| 133 | + sys.exit(0) |
0 commit comments