-
Notifications
You must be signed in to change notification settings - Fork 2
/
tools.sh
481 lines (414 loc) · 16.4 KB
/
tools.sh
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
#!/bin/bash
# The following script runs development tools such as code formatters, docstring
# coverage, linting, and type checking on project files such as jupyter
# notebooks (.ipynb), python files (.py) and markdown files (.md). The default
# behavior is to run all of the tools jupyter notebooks (except for the markdown
# code block formatting tool blacken-docs when it is used on markdown files).
# Main variables ---------------------------------------------------------------
# Get the name of the current directory
current_dir=$(basename "$(pwd)")
# Assign the names of the project and app directories
project_dir="dash-test-app"
nb_app_dir="notebooks"
py_app_dir="src"
# Colors for printing
cyan='\033[0;36m'
purple='\033[0;35m'
blue='\033[0;34m'
yellow='\033[0;33m'
bcyan='\033[1;36m'
color_off='\033[0m'
# Array of current tools that can be applied to jupyter notebooks and python
# (.ipynb and .py) files. Note that the markdown code block formatter
# blacken-docs is not included because it is handled separately using the
# -md|--markdown option.
current_tools=("black" "isort" "interrogate" "flake8" "mypy")
# Array of tools that will be run, subject to any tool that is skipped or
# any markdown file that needs formatting.
tools_to_run=()
# Markdown option variables ----------------------------------------------------
# Booleans to indicate if markdown or markdown-only formatting has been
# requested.
markdown=false
markdown_only=false
# Array that holds all the arguments to the -md|--markdown|-mdo|--markdown-only
# option. This array will only consist of valid files using the "-f" command
# which checks if a file exists and is not a directory.
md_files=()
# Array that collects the options what were passed in.
options_called=()
# The set of options that conflict with the -mdo|--markdown-only option.
mdo_conflict_options=("-s" "--skip" "-t" "--type")
# The set of options gathered from options_called that are in the set of
# conflict options (mdo_conflict_options) and thus will be ignored due to
# -mdo|--markdown-only being passed in.
options_for_warning=()
# Skip option variables --------------------------------------------------------
# Array that holds all the arguments to the -s|--skip option.
skip_args=()
# Type option variables --------------------------------------------------------
# File type to run tools on where "nb" stands for notebook (.ipynb). The default
# behavior is to run the tools only on jupyter notebooks unless the -t|--type
# option is specified.
file_type="nb"
# Array that holds all the arguments to the -t|--type option.
type_args=()
# End of variables -------------------------------------------------------------
# $# is number of arguments at run time.
# While there are still args left to be processed.
while [ $# -gt 0 ]; do
option="$1"
options_called+=("$1")
# Throw out the value thereby reducing number of args left to be processed.
shift
case "$option" in
-md|--markdown|-mdo|--markdown-only)
# Error checking ---------------------------------------------------------
# Check if a markdown file(s) was provided.
if [[ $# -eq 0 || "$1" =~ ^(-|--) ]]; then
echo
echo "Error: No markdown file(s) provided to \"${option}\". Please provide a"
echo "markdown file(s) as an argument."
echo
exit 1
fi
# Check if two markdown options were passed in.
if [[ "$markdown" = true || "$markdown_only" = true ]]; then
echo
echo "Error: Two markdown options were used at the same time. Only pass in one"
echo "markdown option at a time."
echo
exit 1
fi
# Markdown Option Processing ---------------------------------------------
# Determine which markdown option was passed in and update its boolean.
if [[ " $option " =~ ( -md | --markdown ) ]]; then
markdown=true
else
markdown_only=true
fi
# Add the markdown files to the md_files array up until there are no more
# args or you reach the next cli option.
for i in "$@"; do
# Break if you reach a cli option.
if [[ "$i" =~ ^(-|--) ]]; then
break
fi
# Check if the file passed in is a valid file (exists and is not a
# directory).
if [[ -f "$i" ]]; then
md_files+=("$i")
shift
else
echo
echo "Error: The following argument is not a valid file (it is either a directory or"
echo "it doesn't exist):"
echo " ${i}"
echo
exit 1
fi
done
;;
-s|--skip)
# Error Checking ---------------------------------------------------------
# Check if a tool name(s) was provided.
if [[ $# -eq 0 || "$1" =~ ^(-|--) ]]; then
echo
echo "Error: No tool name(s) provided to \"${option}\". Please provide a tool name(s) as"
echo "an argument."
echo
exit 1
fi
# Skip Option Processing -------------------------------------------------
# Add the tool names to the skip_args array up until there are no more
# args or you reach the next cli option.
for i in "$@"; do
# Break if you reach a cli option.
if [[ "$i" =~ ^(-|--) ]]; then
break
fi
skip_args+=("$i")
shift
done
# Add a tool from the array of current tools to the array of tools_to_run
# if it is not a tool name found in the skip_args array.
for tool in "${current_tools[@]}"; do
[[ ! " ${skip_args[*]} " =~ " $tool " ]] && tools_to_run+=("$tool")
done
;;
-t|--type)
# Error Checking ---------------------------------------------------------
# Check if a type was provided.
if [[ $# -eq 0 || "$1" =~ ^(-|--) ]]; then
echo
echo "Error: No type provided to \"${option}\". Please provide all|py as an argument."
echo
exit 1
fi
# Type Option Processing -------------------------------------------------
# Add the type to the type_args array up until there are no more args or
# you reach the next cli option. Since only 1 type should be passed in
# (and it must be all|py), error checking must handle the case of too many
# args passed to -t|--type.
for i in "$@"; do
# Break if you reach a cli option.
if [[ "$i" =~ ^(-|--) ]]; then
break
fi
type_args+=("$i")
shift
if [[ ${#type_args[@]} -gt 1 ]]; then
echo
echo "Error: Too many arguments supplied to \"${option}\". Please provide only all|py as"
echo "an argument."
echo
exit 1
fi
done
# Update the type variable with arg passed in.
if [[ " ${type_args[0]} " =~ " py " ]]; then
file_type="py"
elif [[ " ${type_args[0]} " =~ " all " ]]; then
file_type="all"
else
echo
echo "Error: Argument to -t|--type must be either all|py."
echo
exit 1
fi
;;
-h|--help)
echo
echo "------------------------------- tools.sh Help -------------------------------"
echo
echo "tools.sh runs development tools on project files from either the project root"
echo "directory or any of the first level directories within the root. The default"
echo "behavior is to run these tools on jupyter notebooks (.ipynb) only. CLI options"
echo "allow for modifying this behavior to be able to run these tools on python and"
echo "markdown files as well. Additionally, there is an option to allow for skipping"
echo "tools during the run process if needed."
echo
echo
echo "Current Tools In Use:"
echo
echo " black: code formatter"
echo " blacken-docs: code formatter for markdown code blocks. Not run by default,"
echo " see the -md|--markdown or -mdo|--markdown-only cli options."
echo " isort: import formatter"
echo " interrogate: docstring coverage"
echo " flake8: linter"
echo " mypy: type checker"
echo
echo
echo "Available CLI Options:"
echo
echo " -h|--help"
echo " Provides a list of cli options."
echo
echo " -md|--markdown <markdown_file>"
echo " Runs blacken-docs on <markdown_file> to format code blocks within."
echo
echo " -mdo|--markdown-only <markdown_file>"
echo " ONLY runs blacken-docs on <markdown_file> to format code blocks within."
echo " No other tools are run."
echo
echo " -s|--skip <tool_name>"
echo " Skips application of the <tool_name>'s provided."
echo
echo " -t|--type (all|py)"
echo " all"
echo " Runs tools on both jupyter notebooks and python files (.ipynb and"
echo " .py)."
echo " py"
echo " Runs tools on python files (.py) only."
echo
exit 0
;;
*)
echo
echo "Error: The following argument is not a valid cli option:"
echo " ${option}"
echo
echo "Use -h|--help to see a list of valid options."
echo
exit 1
;;
esac
done
# If -mdo|--markdown-only was passed in, gather any conflicting options that
# were also passed in.
for conflict_option in "${mdo_conflict_options[@]}"; do
if [[ "$markdown_only" = true && " ${options_called[*]} " =~ " $conflict_option " ]]; then
options_for_warning+=("$conflict_option")
fi
done
# If the previous code block produced a non-empty options_for_warning array,
# warn the user that options that conflict with markdown-only will be ignored.
if [[ ${#options_for_warning[@]} -gt 0 ]]; then
echo
echo "Warning: The markdown-only option was passed in. The following option(s) will"
echo "be ignored:"
for i in "${options_for_warning[@]}"; do
echo " $i"
done
echo
fi
# Update tools_to_run based on either no arguments passed in when the script is
# run or any markdown options that were passed in.
if [[ "$markdown_only" = true ]]; then
file_type="mdo"
# The entire array becomes only the blacken-docs tool.
tools_to_run=("blacken-docs")
elif [[ ${#tools_to_run[@]} -eq 0 ]]; then
# If tools_to_run was empty then no tools are skipped.
tools_to_run=("${current_tools[@]}")
if [[ "$markdown" = true ]]; then
tools_to_run+=("blacken-docs")
fi
else
# tools_to_run is non-empty meaning a tool(s) is skipped.
if [[ "$markdown" = true ]]; then
tools_to_run+=("blacken-docs")
fi
fi
# Print out the dev tools header based on the file_type value.
case "$file_type" in
all)
echo -e "${cyan}==== ${bcyan}DEV TOOLS for Both Jupyter Notebooks (.ipynb) and Python Files (.py)${cyan} ====${color_off}"
;;
mdo)
echo -e "${cyan}====================== ${bcyan}DEV TOOL for Markdown-Only (.md)${cyan} ======================${color_off}"
;;
nb)
echo -e "${cyan}================== ${bcyan}DEV TOOLS for Jupyter Notebooks (.ipynb)${cyan} ==================${color_off}"
;;
py)
echo -e "${cyan}====================== ${bcyan}DEV TOOLS for Python Files (.py)${cyan} ======================${color_off}"
;;
esac
# Run the tools based on the tools_to_run array and the file_type value.
for i in ${tools_to_run[@]}; do
case "$i" in
black)
if [[ $file_type == "all" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}black${cyan} (code formatter):${color_off}"
black .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}black${cyan} (code formatter):${color_off}"
nbqa black .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}blacken-docs${cyan} (markdown code block formatter):${color_off}"
nbqa blacken-docs .
elif [[ $file_type == "nb" ]]; then
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}black${cyan} (code formatter):${color_off}"
nbqa black .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}blacken-docs${cyan} (markdown code block formatter):${color_off}"
nbqa blacken-docs .
elif [[ $file_type == "py" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}black${cyan} (code formatter):${color_off}"
black .
fi
;;
isort)
if [[ $file_type == "all" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}isort${cyan} (import formatter):${color_off}"
isort .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}isort${cyan} (import formatter):${color_off}"
nbqa isort .
elif [[ $file_type == "nb" ]]; then
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}isort${cyan} (import formatter):${color_off}"
nbqa isort .
elif [[ $file_type == "py" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}isort${cyan} (import formatter):${color_off}"
isort .
fi
;;
interrogate)
if [[ $file_type == "all" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}interrogate${cyan} (docstring coverage):${color_off}"
interrogate .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}interrogate${cyan} (docstring coverage):${color_off}"
nbqa interrogate .
elif [[ $file_type == "nb" ]]; then
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}interrogate${cyan} (docstring coverage):${color_off}"
nbqa interrogate .
elif [[ $file_type == "py" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}interrogate${cyan} (docstring coverage):${color_off}"
interrogate .
fi
;;
flake8)
if [[ $file_type == "all" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}flake8${cyan} (code linter):${color_off}"
flake8 .
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}flake8${cyan} (code linter):${color_off}"
nbqa flake8 .
elif [[ $file_type == "nb" ]]; then
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}flake8${cyan} (code linter):${color_off}"
nbqa flake8 .
elif [[ $file_type == "py" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}flake8${cyan} (code linter):${color_off}"
flake8 .
fi
;;
mypy)
if [[ $file_type == "all" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}mypy${cyan} (type checker):${color_off}"
# Adjust the target and pass in the config file path based on the
# directory this script is run in.
if [ $current_dir = $project_dir ]; then
mypy ./src --config-file="./pyproject.toml"
elif [ $current_dir = $py_app_dir ]; then
mypy . --config-file="../pyproject.toml"
fi
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}mypy${cyan} (type checker):${color_off}"
if [ $current_dir = $project_dir ]; then
nbqa mypy ./notebooks --config-file="./pyproject.toml"
elif [ $current_dir = $nb_app_dir ]; then
nbqa mypy . --config-file="../pyproject.toml"
fi
elif [[ $file_type == "nb" ]]; then
echo
echo -e "${yellow}(on jupyter files)${cyan} Running ${bcyan}mypy${cyan} (type checker):${color_off}"
if [ $current_dir = $project_dir ]; then
nbqa mypy ./notebooks --config-file="./pyproject.toml"
elif [ $current_dir = $nb_app_dir ]; then
nbqa mypy . --config-file="../pyproject.toml"
fi
elif [[ $file_type == "py" ]]; then
echo
echo -e "${purple}(on python files)${cyan} Running ${bcyan}mypy${cyan} (type checker):${color_off}"
if [ $current_dir = $project_dir ]; then
mypy ./src --config-file="./pyproject.toml"
elif [ $current_dir = $py_app_dir ]; then
mypy . --config-file="../pyproject.toml"
fi
fi
;;
blacken-docs)
echo
echo -e "${blue}(on markdown files)${cyan} Running ${bcyan}blacken-docs${cyan} (markdown code block formatter):${color_off}"
blacken-docs ${md_files[*]}
;;
esac
done
echo
echo -e "${cyan}=========================== ${bcyan}DEV TOOLS NOW COMPLETE${cyan} ===========================${color_off}"