Skip to content

Commit c55d4cf

Browse files
authored
ASOC Fixes (IBM#68)
* fix: ASOC issues * fix: change to binary * fix: corrected binary
1 parent add3df7 commit c55d4cf

File tree

3 files changed

+108
-101
lines changed

3 files changed

+108
-101
lines changed

manage.py

+101-95
Original file line numberDiff line numberDiff line change
@@ -1,127 +1,133 @@
1-
import os, sys, argparse, subprocess, signal
1+
import os, sys, argparse, subprocess, signal, shlex
22

33
# Project defaults
44
FLASK_APP = 'server/__init__.py'
55
DEFAULT_IP = '0.0.0.0:3000'
66

7+
78
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+
2022

2123
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+
4548

4649
cm = CommandManager()
4750

4851
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 .'))
5255

5356
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+
}))
6164

6265
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+
}))
7073

7174
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+
}))
7982

8083
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+
}))
8891

8992
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"'))
9396

9497
# Create and format argument parser for CLI
9598
parser = argparse.ArgumentParser(description=cm.availableCommands(),
96-
formatter_class=argparse.RawDescriptionHelpFormatter)
99+
formatter_class=argparse.RawDescriptionHelpFormatter)
97100
parser.add_argument("subcommand", help="subcommand to run (see list above)")
98101
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+
100105
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+
111117
# Take in command line input for configuration
112118
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)
121127
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()
124130
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)

server/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@
1111

1212
if 'FLASK_LIVE_RELOAD' in os.environ and os.environ['FLASK_LIVE_RELOAD'] == 'true':
1313
import livereload
14-
app.debug = True
14+
app.debug = False
1515
server = livereload.Server(app.wsgi_app)
1616
server.serve(port=os.environ['port'], host=os.environ['host'])

tests/app_tests.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
21
from server import app
32
import unittest
43

4+
55
class ServerTestCase(unittest.TestCase):
66

77
def setUp(self):
88
# create a test client
99
self.app = app.test_client()
10-
self.app.testing = True
10+
self.app.testing = True
1111

1212
def tearDown(self):
1313
pass
1414

1515
def test_root_endpoint(self):
16-
result = self.app.get('/')
17-
self.assertEqual(result.status_code, 200)
16+
result = self.app.get('/')
17+
self.assertEqual(result.status_code, 200)
1818

1919
def test_health_endpoint(self):
2020
result = self.app.get('/health')
21-
assert b'UP' in result.data
21+
self.assertTrue(b'UP' in result.data)
22+
2223

2324
if __name__ == '__main__':
2425
unittest.main()

0 commit comments

Comments
 (0)