-
Notifications
You must be signed in to change notification settings - Fork 1
/
clean.cmd
287 lines (252 loc) · 7.3 KB
/
clean.cmd
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
@echo off
setlocal EnableDelayedExpansion
pushd %~dp0
rem Set needed environment variables.
set PROPERTIES_FILE=project.properties
set CLEAN_TEMPFILE=%TEMP%\temp_clean_%RANDOM%.txt
rem Read properties from the properties file and set in the current environment.
FOR /f %%N IN (%PROPERTIES_FILE%) DO (
set TEST_LINE=%%N
IF NOT "!TEST_LINE:~0,1!"=="#" (
SET %%N
)
)
if not defined PYTHON_MODULE_NAME (
echo "Property 'PYTHON_MODULE_NAME' must be set in the %PROPERTIES_FILE% file."
goto error_end
)
rem Look for options on the command line.
set MY_VERBOSE=
set MY_PUBLISH=
:process_arguments
if "%1" == "-h" (
echo Command: %0 [options]
echo Usage:
echo - Execute a clean build for this project.
echo Options:
echo -h This message.
echo -v Display verbose information.
echo -p Publish project summaries if successful.
GOTO real_end
) else if "%1" == "-v" (
set MY_VERBOSE=--verbose
) else if "%1" == "-p" (
set MY_PUBLISH=1
) else if "%1" == "" (
goto after_process_arguments
) else (
echo Argument '%1' not understood. Stopping.
echo Type '%0 -h' to see valid arguments.
goto error_end
)
shift
goto process_arguments
:after_process_arguments
rem Announce what this script does.
echo {Analysis of project started.}
rem Cleanly start the main part of the script
rem Make sure that Git has been installed and that the script is being executed
rem from within a Git repository.
where git > nul 2>&1
if ERRORLEVEL 1 (
echo.
echo Git is either not installed or not referenced in the PATH variable.
goto error_end
)
git rev-parse --is-inside-work-tree > nul 2>&1
if ERRORLEVEL 1 (
echo.
echo Script must be executed from within a Git repository due to dependencies.
goto error_end
)
rem Check to see if the Pipfile is newer than the Pipfile.lock file.
python utils\find_outdated_piplock_file.py
if ERRORLEVEL 2 (
echo.
echo Analysis of project cannot proceed without a Pipfile.
goto error_end
)
if ERRORLEVEL 1 (
echo {'Pipfile' and 'Pipfile.lock' are not in sync with each other.}
echo {Syncing python packages with new PipEnv 'Pipfile'.}
erase Pipfile.lock
pipenv lock
if ERRORLEVEL 1 (
echo.
echo {Creating new Pipfile.lock file failed.}
goto error_end
)
pipenv sync -d
if ERRORLEVEL 1 (
echo.
echo {Syncing python packages with PipEnv failed.}
goto error_end
)
)
echo {Executing black formatter on Python code.}
pipenv run black %MY_VERBOSE% .
if ERRORLEVEL 1 (
echo.
echo {Executing black formatter on Python code failed.}
goto error_end
)
echo {Executing import sorter on Python code.}
pipenv run isort %MY_VERBOSE% .
if ERRORLEVEL 1 (
echo.
echo {Executing import sorter on Python code failed.}
goto error_end
)
echo {Executing pre-commit hooks on Python code.}
pipenv run pre-commit run --all
if ERRORLEVEL 1 (
echo.
echo {Executing pre-commit hooks on Python code failed.}
goto error_end
)
if "%SOURCERY_USER_KEY%" == "" (
echo {Sourcery user key not defined. Skipping Sourcery static analyzer.}
) else (
echo {Executing Sourcery static analyzer on Python code.}
pipenv run sourcery login --token %SOURCERY_USER_KEY%
if ERRORLEVEL 1 (
echo.
echo {Logging into Sourcery failed.}
goto error_end
)
if defined MY_PUBLISH (
echo { Executing Sourcery against full project contents.}
set SOURCERY_LIMIT=
) else (
echo { Executing Sourcery against changed project contents.}
set "SOURCERY_LIMIT=--diff ^"git diff^""
)
pipenv run sourcery review --fix --verbose . !SOURCERY_LIMIT!
if ERRORLEVEL 1 (
echo.
echo {Executing Sourcery fix on project code failed.}
goto error_end
)
pipenv run sourcery review --check --verbose . !SOURCERY_LIMIT!
if ERRORLEVEL 1 (
echo.
echo {Executing Sourcery check on project code after fix failed. Failures remain.}
goto error_end
)
)
echo {Executing flake8 static analyzer on Python code.}
pipenv run flake8 -j 4 --exclude dist,build %MY_VERBOSE%
if ERRORLEVEL 1 (
echo.
echo {Executing static analyzer on Python code failed.}
goto error_end
)
echo {Executing bandit security analyzer on Python code.}
pipenv run bandit -q -r application_properties
if ERRORLEVEL 1 (
echo.
echo {Executing security analyzer on Python code failed.}
goto error_end
)
echo {Executing pylint static analyzer on Python source code.}
pipenv run pylint -j 1 --rcfile=setup.cfg --recursive=y %MY_VERBOSE% %PYTHON_MODULE_NAME%
if ERRORLEVEL 1 (
echo.
echo {Executing pylint static analyzer on Python source code failed.}
goto error_end
)
echo {Executing mypy static analyzer on Python source code.}
set STUBS_DIRECTORY=
for /r %%i in (*.pyi) do set STUBS_DIRECTORY=%%i
if defined STUBS_DIRECTORY (
set STUBS_DIRECTORY=stubs
)
pipenv run mypy --strict %PYTHON_MODULE_NAME% !STUBS_DIRECTORY!
if ERRORLEVEL 1 (
echo.
echo {Executing mypy static analyzer on Python source code failed.}
goto error_end
)
pipenv run mypy --strict test !STUBS_DIRECTORY!
if ERRORLEVEL 1 (
echo.
echo {Executing mypy static analyzer on Python test code failed.}
goto error_end
)
echo {Executing pylint utils analyzer on Python source code to verify suppressions and document them.}
pipenv run python ..\pylint_utils\main.py --config setup.cfg --recurse -r publish\pylint_suppression.json %PYTHON_MODULE_NAME%
if ERRORLEVEL 1 (
echo.
echo {Executing reporting of pylint suppressions in Python source code failed.}
goto error_end
)
echo {Executing pylint static analyzer on test Python code.}
pipenv run pylint -j 1 --rcfile=setup.cfg --ignore test\resources --recursive=y %MY_VERBOSE% test
if ERRORLEVEL 1 (
echo.
echo {Executing pylint static analyzer on test Python code failed.}
goto error_end
)
echo {Executing pylint static analyzer on utils Python code.}
pipenv run pylint -j 1 --rcfile=setup.cfg --recursive=y %MY_VERBOSE% utils
if ERRORLEVEL 1 (
echo.
echo {Executing pylint static analyzer on utils Python code failed.}
goto error_end
)
git diff --name-only --staged --diff-filter=d > %CLEAN_TEMPFILE%
set ALL_FILES=
for /f "tokens=*" %%x in (%CLEAN_TEMPFILE%) do (
set TEST_FILE=%%x
if /i [!TEST_FILE:~-3!]==[.py] set ALL_FILES=!ALL_FILES! !TEST_FILE!
)
if "%ALL_FILES%" == "" (
echo {Not executing pylint suppression checker on Python source code. No eligible Python files staged.}
) else (
echo {Executing pylint suppression checker on Python source code.}
pipenv run python ..\pylint_utils\main.py --config setup.cfg -s %ALL_FILES%
if ERRORLEVEL 1 (
echo.
echo {Executing reporting of unused pylint suppressions in modified Python source code failed.}
goto error_end
)
)
if defined MY_PUBLISH (
echo {Building package for current repository.}
call package.cmd > %CLEAN_TEMPFILE%
if ERRORLEVEL 1 (
cat %CLEAN_TEMPFILE%
echo.
echo {Building package for repository failed.}
goto error_end
)
)
echo {Executing unit tests on Python code.}
call ptest.cmd -c -m
if ERRORLEVEL 1 (
echo.
echo {Executing unit tests on Python code failed.}
goto error_end
)
if defined MY_PUBLISH (
echo {Publishing summaries after successful analysis of project.}
call ptest.cmd -p
if ERRORLEVEL 1 (
echo.
echo {Publishing summaries failed.}
goto error_end
)
)
rem Cleanly exit the script
echo.
set PC_EXIT_CODE=0
echo {Analysis of project succeeded.}
goto real_end
:error_end
set PC_EXIT_CODE=1
echo {Analysis of project failed.}
:real_end
erase /f /q %CLEAN_TEMPFILE% > nul 2>&1
set CLEAN_TEMPFILE=
popd
exit /B %PC_EXIT_CODE%