forked from way-cooler/way-cooler
/
ci.py
102 lines (87 loc) · 3.28 KB
/
ci.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
#!/usr/bin/env python2
import sys
import os
import re
import subprocess
from docopt import docopt
VERSION_REGEX = '(\\d+\\.\\d+\\.\\d+)'
BRANCH_REGEX = '^release-' + VERSION_REGEX + '$'
# If we grab the first 'version=' line in the Cargo files we'll be fine
CARGO_VERSION_LINE = '^version = "' + VERSION_REGEX + '"$'
FILE_REGEX_MAP = {
"Cargo.toml": CARGO_VERSION_LINE,
"Cargo.lock": CARGO_VERSION_LINE,
}
DOCOPT_USAGE = """way-cooler CI integration.
Usage:
ci.py travis-check
ci.py prepare-deploy
ci.py bump <old version> <new version> [-v]
ci.py (-h | --help)
ci.py --version
Options:
-h --help Show this menu
-v Be verbose, print actions taken
--version Show version information
"""
def check_file_version(file_name, regex, expected):
reg = re.compile(regex)
with open(file_name) as f:
for line in f.readlines():
match = reg.match(line)
if match: match = match.group(0)
if not match:
continue
elif match == expected:
print('\t' + file_name + " updated.")
return True
else:
print('\t {}: expected "{}" got "{}"'.format(file_name, expected, match))
return False
print('\t' + file_name + ": did not find any version match!")
return False
def check_release_branch(version):
all_clear = True
for (file_name, file_regex) in FILE_REGEX_MAP.items():
print("Checking " + file_name)
if not check_file_version(file_name, file_regex, version):
all_clear = False
return all_clear
if __name__ == "__main__":
print("Running way-cooler ci script...")
args = docopt(DOCOPT_USAGE, version="ci.py v1.0")
if args["travis-check"]:
print("Running travis-check...")
travis_pr_branch = os.environ["TRAVIS_PULL_REQUEST_BRANCH"]
if not travis_pr_branch:
print("Not running in a PR.")
sys.exit(0)
print("PR " + travis_pr_branch + " detected, checking for versions.")
version_match = re.match(BRANCH_REGEX, travis_pr_branch)
if not version_match:
print("Not in a release branch PR.")
sys.exit(0)
print("Checking versions in branch " + travis_pr_branch)
compare_version = ('version = "{}"'.format(version_match.group(0).split('-')[1]))
if not check_release_branch(compare_version):
sys.stderr.write("Not all files matched!\n")
sys.exit(2)
print("All version checks passed.")
elif args["bump"]:
sys.stderr.write("Not supported yet")
sys.exit(1)
elif args["prepare-deploy"]:
print("Not compiling for multiple targets yet :(")
print("cargo build --release --verbose")
retcode = subprocess.call(["cargo", "build", "--release", "--verbose"])
if retcode != 0:
sys.stderr.write("Cargo build exited with {}\n".format(retcode))
sys.exit(2)
print("Moving way-cooler => way-cooler_linux_x86_64")
build_dir = os.environ["TRAVIS_BUILD_DIR"]
os.rename(build_dir + "/target/release/way-cooler",
build_dir + "/way-cooler_linux_x86_64")
else:
sys.stderr.write("Invalid arguments!\n")
print(DOCOPT_USAGE)
sys.exit(1)