-
Notifications
You must be signed in to change notification settings - Fork 38
/
build.py
135 lines (103 loc) · 3.41 KB
/
build.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
# /usr/bin/env python
import glob
import json
import os
import shutil
import sys
from pathlib import Path
from subprocess import call
cwd = os.getcwd()
PLUGIN_FILE_TEMPLATE = 'gauge-python-{}'
ZIP = 'zip'
VERSION = 'version'
PLUGIN_JSON = 'python.json'
BIN = os.path.join(cwd, 'bin')
DEPLOY = os.path.join(cwd, 'deploy')
ROOT_DIR = Path(__file__).resolve().parent
def install():
plugin_zip = create_zip()
call(['gauge', 'uninstall', 'python', '-v', get_version()])
exit_code = call(['gauge', 'install', 'python', '-f',
os.path.join(BIN, plugin_zip)])
generate_package()
p = os.listdir("dist")[0]
print(f"Installing getgauge package using pip: \n\tpip install dist/{p}")
call([sys.executable, "-m", "pip", "install",
f"dist/{p}", "--upgrade", "--user"])
sys.exit(exit_code)
def create_setup_file():
with open("setup.tmpl", "r", encoding="utf-8") as tmpl:
tmpl_content = tmpl.read()
with open("setup.py", "w+", encoding="utf-8") as setup:
v = get_version()
setup.write(tmpl_content.format(v))
def generate_package():
shutil.rmtree('dist', True)
print('Creating getgauge package.')
create_setup_file()
with open(os.devnull, 'w', encoding="utf-8") as fnull:
call([sys.executable, 'setup.py', 'sdist'], stdout=fnull, stderr=fnull)
def create_zip():
wd = cwd
if os.path.exists(DEPLOY):
shutil.rmtree(DEPLOY)
copy_files(wd)
output_file = PLUGIN_FILE_TEMPLATE.format(get_version())
shutil.make_archive(output_file, ZIP, DEPLOY)
shutil.rmtree(DEPLOY)
if os.path.exists(BIN):
shutil.rmtree(BIN)
os.mkdir(BIN)
plugin_zip = f"{output_file}.zip"
shutil.move(plugin_zip, BIN)
print('Zip file created.')
return plugin_zip
def get_version():
with open(PLUGIN_JSON, "r", encoding="utf-8") as json_data:
data = json.loads(json_data.read())
return data[VERSION]
def copy_files(wd):
copy(os.path.join(wd, 'skel'), os.path.join(DEPLOY, 'skel'))
copy(os.path.join(wd, PLUGIN_JSON), DEPLOY)
copy(os.path.join(wd, 'check_and_install_getgauge.py'), DEPLOY)
copy(os.path.join(wd, 'start.py'), DEPLOY)
copy(os.path.join(wd, 'start.sh'), DEPLOY)
copy(os.path.join(wd, 'start.bat'), DEPLOY)
def copy(src, dest):
try:
shutil.copytree(src, dest)
except OSError:
shutil.copy(src, dest)
usage = """
Usage: python build.py --[option]
Options:
--test : runs unit tests.
--install : installs python plugin and generates the pip package
"""
def run_tests() -> int:
pp = "PYTHONPATH"
os.environ[pp] = str(ROOT_DIR)
exit_code = 0
all_python_test_files = glob.glob(f"{ROOT_DIR}/tests/**/test_*.py", recursive=True)
for i, file_name_path in enumerate(all_python_test_files):
command = ["coverage", "run", file_name_path]
exit_code = call(command) if exit_code == 0 else exit_code
# Keep coverage files
os.rename(".coverage", f".coverage.{i}")
call(["coverage", "combine"])
return exit_code
def main():
if len(sys.argv) < 2:
print(usage)
else:
if sys.argv[1] == '--dist':
create_zip()
generate_package()
else:
exit_code = run_tests()
if exit_code != 0:
sys.exit(exit_code)
elif sys.argv[1] == '--install':
install()
if __name__ == '__main__':
main()