-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.py
243 lines (191 loc) · 6.35 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
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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
"""
Tasks for maintaining the project.
Execute 'invoke --list' for guidance on using Invoke
"""
import shutil
import pprint
import sys
import os
from invoke import task
import webbrowser
PKG = "github.com/rambo/mumble_createchannels"
CMD_PKG = PKG
ROOT_DIR = os.path.dirname(os.path.realpath(__file__))
BUILD_DIR = os.path.join(ROOT_DIR, "build")
TRAVIS_CONFIG_FILE = os.path.join(ROOT_DIR, ".travis.yml")
def _delete_file(file):
try:
file.unlink(missing_ok=True)
except TypeError:
# missing_ok argument added in 3.8
try:
file.unlink()
except FileNotFoundError:
pass
@task
def format(c):
"""Format code
"""
c.run("pre-commit run go-fmt --all-files")
c.run("pre-commit run go-imports --all-files")
@task
def test(c):
"""Run tests
"""
c.run("env GO111MODULE=on go test -v -race ./...")
@task
def cyclo(c):
"""Check code complexity
"""
c.run("pre-commit run go-cyclo --all-files")
@task
def lint(c):
"""Lint code
"""
c.run("pre-commit run go-lint --all-files")
c.run("pre-commit run go-vet --all-files")
@task
def install_hooks(c):
"""Install pre-commit hooks
"""
c.run("pre-commit install")
@task
def pre_commit(c):
"""Run all pre-commit checks
"""
c.run("pre-commit run --all-files")
@task(help=dict(publish="Publish the coverage result to codecov.io (default False)",),)
def coverage(c, publish=False):
"""Create coverage report
"""
c.run(
"env GO111MODULE=on go test -v -race -coverprofile=coverage.txt -coverpkg=all -covermode=atomic ./..."
)
if publish:
# Publish the results via codecov
c.run("bash <(curl -s https://codecov.io/bash)")
@task
def cc(c):
"""Build the project for all architectures
"""
output = "{{.Dir}}-{{.OS}}-{{.Arch}}"
TRAVIS_TAG = os.environ.get("TRAVIS_TAG")
BINARY = os.environ.get("BINARY")
if TRAVIS_TAG and BINARY:
output = "%s-%s-{{.OS}}-{{.Arch}}" % (BINARY, TRAVIS_TAG)
c.run(
'gox -os="darwin windows" -arch="amd64" -output="build/%s" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose %s'
% (output, CMD_PKG)
)
c.run(
'gox -os="linux" -arch="amd64 arm" -output="build/%s" -ldflags "-X main.Rev=`git rev-parse --short HEAD`" -verbose %s'
% (output, CMD_PKG)
)
@task
def build(c):
"""Build the project
"""
c.run("pre-commit run go-build --all-files")
@task
def run(c):
"""Run the cmd target
"""
options = sys.argv[3:]
c.run("go run {} {}".format(CMD_PKG, " ".join(options)))
@task
def clean_build(c):
"""Clean up files from package building
"""
c.run("rm -fr build/")
@task
def clean_coverage(c):
"""Clean up files from coverage measurement
"""
c.run("find . -name 'coverage.txt' -exec rm -fr {} +")
@task(pre=[clean_build, clean_coverage])
def clean(c):
"""Runs all clean sub-tasks
"""
pass
def _create(d, *keys):
current = d
for key in keys:
try:
current = current[key]
except (TypeError, KeyError):
current[key] = dict()
current = current[key]
def _fix_token(config_file=None, force=False, verify=True):
from ruamel.yaml import YAML
yaml = YAML()
config_file = config_file or TRAVIS_CONFIG_FILE
with open(config_file, "r") as _file:
try:
travis_config = yaml.load(_file)
except Exception:
raise ValueError(
"Failed to parse the travis configuration. "
"Make sure the config only contains valid YAML and keys as specified by travis."
)
# Get the generated token from the top level deploy config added by the travis cli
try:
real_token = travis_config["deploy"]["api_key"]["secure"]
except (TypeError, KeyError):
raise AssertionError("Can't find any top level deployment tokens")
try:
# Find the build stage that deploys to releases
releases_stages = [
stage
for stage in travis_config["jobs"]["include"]
if stage.get("deploy", dict()).get("provider") == "releases"
]
assert (
len(releases_stages) > 0
), "Can't set the new token because there are no stages deploying to releases"
assert (
len(releases_stages) < 2
), "Can't set the new token because there are multiple stages deploying to releases"
except (TypeError, KeyError):
raise AssertionError(
"Can't set the new token because there are no deployment stages")
try:
is_mock_token = releases_stages[0]["deploy"]["token"]["secure"] == "REPLACE_ME"
is_same_token = releases_stages[0]["deploy"]["token"]["secure"] == real_token
unmodified = is_mock_token or is_same_token
except (TypeError, KeyError):
unmodified = False
# Set the new generated token as the stages deploy token
_create(releases_stages[0], "deploy", "token", "secure")
releases_stages[0]["deploy"]["token"]["secure"] = real_token
# Make sure it is fine to overwrite the config file
assert unmodified or force, (
'The secure token in the "{}" stage has already been changed. '
"Retry with --force if you are sure about replacing it.".format(
releases_stages[0].get("stage", "releases deployment")
)
)
# Remove the top level deploy config added by the travis cli
travis_config.pop("deploy")
if not unmodified and verify:
pprint.pprint(travis_config)
if (
not input("Do you want to save this configuration? (y/n) ")
.strip()
.lower()
== "y"
):
return
# Save the new travis config
assert travis_config
with open(config_file, "w") as _file:
yaml.dump(travis_config, _file)
print("Fixed!")
@task(help=dict(
force="Force overriding the current travis configuration",
verify="Verify config changes by asking for the user's approval"
))
def fix_token(c, force=False, verify=True):
"""
Add the token generated by the travis cli script to the correct entry
"""
_fix_token(force=force, verify=verify)