-
Notifications
You must be signed in to change notification settings - Fork 2
/
tasks.py
136 lines (99 loc) · 3.63 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
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
import json
import os
import time
from distutils.util import strtobool
import yaml
from github import Github
from invoke import task
BASEDIR = os.path.abspath(os.path.dirname(__file__))
@task()
def init_app(c, env=None):
# Prevent execute this function more than once
if not os.environ.get("APP_SETTINGS"):
# Load the basic configs
env_vars = load_yaml_from_file(
os.path.join(BASEDIR, "resources", "settings.yml")
)
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, env=None):
run(c, "python -m tvsort_sl.app", False)
def run(c, command, with_venv=True):
if with_venv:
command = "{} && {}".format(get_venv_action(), command)
print("Running: {}".format(command))
c.run(command)
def set_env_var(c, name, value, env, is_protected=True):
# env codes: t - Travis-CI , None - Dev
if isinstance(value, dict):
value = json.dumps(value)
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:
print(f"Set local var: {name}={value}")
os.environ[name] = value
def load_yaml_from_file(file_path):
with open(file_path, "r") as stream:
return yaml.safe_load(stream)
def is_unix():
return os.name == "posix"
def get_venv_action():
if is_unix():
return f"source {BASEDIR}/venv/bin/activate"
else:
return f"{BASEDIR}\\venv\\Scripts\\activate"
@task(init_app)
def test(c, cov=False, file=None):
# cov - if to use coverage, file - if to run specific file
command = "pytest -s -p no:warnings"
if cov:
command = "{} --cov=slots_tracker_server --cov-report term-missing".format(
command
)
if file:
command = "{} {}".format(command, file)
run(c, command)
@task(init_app)
def mutmut(c):
command = "mutmut run"
run(c, command)
@task()
def bump_version(c):
files_to_update = ['setup.py', '.bumpversion.cfg']
github_client = Github(os.environ['GITHUB_ACCESS_TOKEN'])
travis_pull_request = os.environ['TRAVIS_PULL_REQUEST']
travis_pull_request_branch = os.environ['TRAVIS_PULL_REQUEST_BRANCH']
travis_pull_request_slug = os.environ['TRAVIS_PULL_REQUEST_SLUG']
try:
travis_pull_request = int(travis_pull_request)
except ValueError:
travis_pull_request = strtobool(travis_pull_request)
if not travis_pull_request:
print('Not running on PR')
return
# Convert travis_pull_request from string to int
travis_pull_request = int(travis_pull_request)
repo = github_client.get_repo(travis_pull_request_slug)
pr = repo.get_pull(travis_pull_request)
for pr_file in pr.get_files():
pr_filename = pr_file.filename
if pr_filename in files_to_update:
print('Version was already bumped, exiting')
return
print('Bumping version')
run(c, 'bumpversion --verbose patch --allow-dirty', with_venv=False)
# Updating GitHub with new changes
for filename in files_to_update:
# Separate commits so that Travis will only build the last one
time.sleep(10)
file_object = repo.get_contents(path=filename, ref=travis_pull_request_branch)
with open(filename) as f:
repo.update_file(file_object.path, "Update version, file: {}".format(filename), f.read(),
file_object.sha, branch=travis_pull_request_branch)