forked from dbcli/mssql-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.py
179 lines (139 loc) · 5.69 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
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
from __future__ import print_function
from shutil import copyfile
import os
import sys
import utility
# Environment variables below allow the build process to use a python interpreter and pip version different
# from the user's PATH. When building our linux packages on various distros, we want to be build machine agnostic, so we
# redirect python calls to the python we bundle in. Usage of these variables can be seen in our build_scripts folder.
PIP = os.getenv('CUSTOM_PIP', 'pip')
PYTHON = os.getenv('CUSTOM_PYTHON', 'python')
def print_heading(heading, f=None):
print('{0}\n{1}\n{0}'.format('=' * len(heading), heading), file=f)
def clean_and_copy_sqltoolsservice(platform):
"""
Cleans the SqlToolsService directory and copies over the SqlToolsService binaries for the given platform.
:param platform: string
"""
import mssqlcli.mssqltoolsservice.externals as mssqltoolsservice
mssqltoolsservice.clean_up_sqltoolsservice()
mssqltoolsservice.copy_sqltoolsservice(platform)
def code_analysis():
utility.exec_command(
'{0} -m flake8 mssqlcli setup.py dev_setup.py build.py utility.py dos2unix.py'.format(PYTHON),
utility.ROOT_DIR)
def build():
"""
Builds mssql-cli package.
"""
print_heading('Cleanup')
# clean
utility.clean_up(utility.MSSQLCLI_DIST_DIRECTORY)
utility.clean_up_egg_info_sub_directories(utility.ROOT_DIR)
print_heading('Running setup')
# install general requirements.
utility.exec_command(
'{0} install -r requirements-dev.txt'.format(PIP),
utility.ROOT_DIR)
# convert windows line endings to unix for mssql-cli bash script
utility.exec_command(
'{0} dos2unix.py mssql-cli mssql-cli'.format(PYTHON),
utility.ROOT_DIR)
# run flake8
code_analysis()
if utility.get_current_platform().startswith('win'):
platforms_to_build = ['win32', 'win_amd64']
else:
platforms_to_build = [utility.get_current_platform()]
for platform in platforms_to_build:
# For the current platform, populate the appropriate binaries and
# generate the wheel.
clean_and_copy_sqltoolsservice(platform)
utility.clean_up(utility.MSSQLCLI_BUILD_DIRECTORY)
print_heading('Building mssql-cli pip package')
utility.exec_command('{0} --version'.format(PYTHON), utility.ROOT_DIR)
utility.exec_command('{0} setup.py check -r -s bdist_wheel --plat-name {1}'.format(PYTHON, platform),
utility.ROOT_DIR,
continue_on_error=False)
# Copy back the SqlToolsService binaries for this platform.
clean_and_copy_sqltoolsservice(utility.get_current_platform())
copy_and_rename_wheels()
def copy_and_rename_wheels():
# Create a additional copy of each build artifact with a ever green name with 'dev-latest' as it's version.
for pkg_name in os.listdir(utility.MSSQLCLI_DIST_DIRECTORY):
first_dash = pkg_name.find('-')
second_dash = pkg_name.find('-', first_dash + 1, len(pkg_name))
pkg_daily_name = pkg_name.replace(pkg_name[first_dash + 1:second_dash], 'dev-latest')
original_path = os.path.join(utility.MSSQLCLI_DIST_DIRECTORY, pkg_name)
updated_path = os.path.join(utility.MSSQLCLI_DIST_DIRECTORY, pkg_daily_name)
copyfile(original_path, updated_path)
def validate_package():
"""
Install mssql-cli package locally.
"""
root_dir = os.path.abspath(os.path.join(os.path.abspath(__file__), '..'))
# Local install of mssql-cli.
mssqlcli_wheel_dir = os.listdir(utility.MSSQLCLI_DIST_DIRECTORY)
# To ensure we have a clean install, we disable the cache as to prevent
# cache overshadowing actual changes made.
current_platform = utility.get_current_platform()
mssqlcli_wheel_name = [
pkge for pkge in mssqlcli_wheel_dir if current_platform in pkge and
'dev-latest' not in pkge]
utility.exec_command(
'{0} install --no-cache-dir --no-index ./dist/{1}'.format(
PIP,
mssqlcli_wheel_name[0]),
root_dir, continue_on_error=False
)
def unit_test():
"""
Run all unit tests.
"""
utility.exec_command(
'pytest --cov mssqlcli '
'tests/test_mssqlcliclient.py '
'tests/test_main.py '
'tests/test_fuzzy_completion.py '
'tests/test_rowlimit.py '
'tests/test_sqlcompletion.py '
'tests/test_prioritization.py '
'mssqlcli/jsonrpc/tests '
'mssqlcli/jsonrpc/contracts/tests '
'tests/test_telemetry.py '
'tests/test_special.py',
utility.ROOT_DIR,
continue_on_error=False)
def integration_test():
"""
Run full integration test via tox which includes build, unit tests, code coverage, and packaging.
"""
utility.exec_command(
'tox',
utility.ROOT_DIR,
continue_on_error=False)
def test():
"""
Run unit test and integration test.
"""
unit_test()
integration_test()
def validate_actions(user_actions, valid_targets):
for user_action in user_actions:
if user_action.lower() not in valid_targets.keys():
print('{0} is not a supported action'.format(user_action))
print('Supported actions are {}'.format(list(valid_targets.keys())))
sys.exit(1)
if __name__ == '__main__':
default_actions = ['build', 'unit_test']
targets = {
'build': build,
'validate_package': validate_package,
'unit_test': unit_test,
'integration_test': integration_test,
'test': test
}
actions = sys.argv[1:] or default_actions
validate_actions(actions, targets)
for action in actions:
targets[action.lower()]()