-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
88 lines (63 loc) · 2.12 KB
/
tasks.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
import json
import os
import yaml
from invoke import task
BASEDIR = os.path.abspath(os.path.dirname(__file__))
active_venv = 'source {}/venv/bin/activate'.format(BASEDIR)
heroku_app_name = 'slots-tracker'
@task()
def run(c, command, with_venv=True):
if with_venv:
command = '{} && {}'.format(active_venv, command)
print('Running: {}'.format(command))
c.run(command)
@task()
def init_app(c, env=None):
# Load the basic configs
env_vars = load_yaml_from_file('{}/resources/config.yml'.format(BASEDIR))
for name, data in env_vars.items():
set_env_var(c, name, data.get('value'), env, data.get('is_protected', False))
@task(init_app)
def run_app(c):
run(c, 'flask run')
@task(init_app)
def test(c, cov=False, file=None):
# cov - if to use coverage, file - if to run specific file
set_env_var(c, 'APP_SETTINGS', 'config.TestingConfig', '')
command = 'pytest -s -v'
if cov:
command = '{} --cov=viz_ai_image/app.py --cov-report term-missing'.format(command)
if file:
command = '{} {}'.format(command, file)
run(c, command)
# run scripts
@task(init_app)
def run_command(c, command):
run(c, 'cd {} && flask {}'.format(BASEDIR, command))
# helper
def set_env_var(c, name, value, env, is_protected=True):
# env codes: t - Travis-CI
if isinstance(value, dict):
value = json.dumps(value)
command = ''
if env == 't':
command = "travis env set {} '{}'".format(name, value)
if not is_protected:
command = '{} --public'.format(command)
if command:
run(c, command, False)
else:
os.environ[name] = value
def load_yaml_from_file(file_path):
with open(file_path, 'r') as stream:
return yaml.safe_load(stream)
@task()
def build_venv(c):
run(c, 'python3 -m venv venv', with_venv=False)
install_requirements(c)
@task()
def install_requirements(c, dev=False):
requirements_name = 'dev.txt' if dev else 'requirements.txt'
requirements_path = '{}/{}'.format(BASEDIR, requirements_name)
run(c, 'pip install --upgrade pip')
run(c, 'pip install -r {}'.format(requirements_path))