This repository has been archived by the owner on Feb 6, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
manager.py
204 lines (152 loc) · 5.41 KB
/
manager.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
from flask.ext.script import Command, Option
from beavy.app import app, manager, migrate
import sys
import os
import re
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
@migrate.configure
def configure_alembic(config):
paths = list(filter(lambda x: os.path.isdir(x),
map(lambda x: os.path.join(BASE_DIR,
'beavy_modules', x, "migrations"),
(app.config.get("MODULES", None) or []))))
if paths:
print("Adding module migrations:\n - {}".format("\n - ".join(paths))) # noqa
app_migrations_path = os.path.join(BASE_DIR,
'beavy_apps',
app.config.get("APP"),
'migrations')
if os.path.isdir(app_migrations_path):
paths.append(app_migrations_path)
print('Adding App migration: \n - {}'.format(app_migrations_path))
paths.append(os.path.join('migrations', 'versions'))
config.set_main_option('version_locations', " ".join(paths))
return config
try:
from behave.configuration import options as behave_options
from behave.__main__ import main as behave_main
has_behave = True
except ImportError:
has_behave = False
def reformat_options(opts):
res = []
for args, kwargs in opts:
if not args:
continue
if "config_help" in kwargs:
del kwargs['config_help']
res.append(Option(*args, **kwargs))
return res
def get_all_beavy_paths(fn):
fn("beavy")
for module in app.config.get("MODULES", []):
fn("beavy_modules/{}".format(module))
return fn("beavy_apps/{}".format(app.config["APP"]))
class Behave(Command):
def get_options(self):
return reformat_options(behave_options)
def run(self, *args, **kwargs):
frontend = os.environ.get("APP", app.config.get("APP", None))
if not frontend:
print("You need to configure the APP to be used!")
exit(1)
exit(behave_main(sys.argv[2:] + ['--no-capture',
"beavy_apps/{}/tests/features".format(frontend)]))
if has_behave:
manager.add_command("behave", Behave())
try:
import pytest
has_pytest = True
except ImportError:
has_pytest = False
def pytest(path=None, no_coverage=False, maxfail=0, # noqa
debug=False, verbose=False):
import pytest
arguments = []
def add_path_with_coverage(x):
arguments.append("--cov={}".format(x))
arguments.append(x)
def simple_add(x):
arguments.append(x)
if maxfail:
arguments.append("--maxfail={}".format(maxfail))
if verbose:
arguments.append("-vv")
if debug:
arguments.append("--pdb")
if no_coverage:
add_path = simple_add
else:
arguments.extend(["--cov-config", ".coveragerc"])
add_path = add_path_with_coverage
if path:
add_path(path)
else:
add_path("beavy")
get_all_beavy_paths(add_path)
exit(pytest.main(arguments))
if has_pytest:
manager.command(pytest)
class GetPaths(Command):
def run(self):
results = []
get_all_beavy_paths(results.append)
print(" ".join(results))
manager.add_command("paths", GetPaths())
# Setup app
@manager.command
def create_app(name):
"""
Setup beavy template and infrastructure for a new app
given the @name.
"""
ROOT_DIR = os.path.dirname(os.path.abspath(__file__))
if not re.match("^[a-z][a-z0-9]{2,24}$", name):
print("""Sorry, the app name has to be a lower-cased 3-25 character
long string only containing letters, numbers and underscore and starting
with a letter!""")
print("RegEx: ^[a-z][a-z0-9]{2,24}$ ")
exit(1)
APP_DIR = os.path.join(ROOT_DIR, "beavy_apps", name)
if os.path.exists(APP_DIR):
print("{} directory already exists. Exiting.".format(name))
exit(1)
# minimal setup
os.mkdir(APP_DIR)
open(os.path.join(APP_DIR, "__init__.py"), 'w').close()
# create minimal frontend
os.mkdir(os.path.join(APP_DIR, "frontend"))
with open(os.path.join(APP_DIR, "frontend",
"application.jsx"), 'w') as jsx:
jsx.write("""
import React from "react";
import { MainMenu } from "components/MainMenu";
import UserModal from "containers/UserModal";
import UserMenuWidget from "containers/UserMenuWidget";
import { getExtensions } from "config/extensions";
// This is your app entry point
export default class Application extends React.Component {
render() {
return <div>
<UserModal />
<MainMenu
logo='http://beavy.xyz/logos/logo.svg'
navigationTools={<UserMenuWidget />}
>
{getExtensions('MainMenuItem').map(x=>x.call(this))}
</MainMenu>
{this.props.children}
</div>;
}
}
""")
# create testing infrastructure
os.mkdir(os.path.join(APP_DIR, "tests"))
with open(os.path.join(APP_DIR, "tests", "environment.py"), 'w') as env:
env.write("from beavy.testing.environment import *\n")
os.mkdir(os.path.join(APP_DIR, "tests", "steps"))
with open(os.path.join(APP_DIR, "tests", "steps", "steps.py"), 'w') as stp:
stp.write("from beavy.testing.steps import *\n")
print("{} successfully created!".format(name))
if __name__ == '__main__':
manager.run()