-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck-lint
executable file
·116 lines (94 loc) · 3.92 KB
/
check-lint
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
#!/bin/bash -e
TOOL_NAME=pylint
TOOL_CMD="python3 -m $TOOL_NAME"
BASE_DIR=$(dirname "$0")
CONFIG_FILE="$BASE_DIR/../avocado-static-checks.conf"
# Default config path
DEFAULT_CONFIG_PATH="$BASE_DIR/default_configs/pylintrc"
# Collect all Python files in the repository
ALL_FILES=$(git ls-files '*.py')
# Initialize an array to store files checked with custom configs
checked_files=()
# Initialize an overall exit status to track pylint failures
overall_exit_status=0
# Check if the avocado-static-checks.conf file exists
if [ -f "$CONFIG_FILE" ]; then
echo "Found configuration file: $CONFIG_FILE"
# Initialize a flag to track if we're within the [lint] section
in_lint_section=false
# Read and process each line in the config file
while IFS=':' read -r directory config_path || [ -n "$directory" ]; do
# Skip empty lines
if [ -z "$directory" ] && [ -z "$config_path" ]; then
continue
fi
# Exit with an error if no directory is specified before the colon
if [ -z "$directory" ]; then
echo "Error: No directory specified before the colon in the config file."
exit 1
fi
# Handle section headers
if [[ "$directory" =~ ^\s*\[lint\]\s*$ ]]; then
in_lint_section=true
continue
elif [[ "$directory" =~ ^\s*\[.*\]\s*$ ]]; then
in_lint_section=false
continue
fi
# Process lines only within the [lint] section
if $in_lint_section; then
# Trim whitespace
CONFIG_PATH=$(echo "$config_path" | xargs)
DIRECTORY_PATH=$(echo "$directory" | xargs)
# Skip if either directory or config_path is empty
if [ -z "$DIRECTORY_PATH" ] || [ -z "$CONFIG_PATH" ]; then
echo "Error: Invalid line in the config file."
exit 1
fi
# Check if the directory exists
if [ ! -d "$BASE_DIR/../$DIRECTORY_PATH" ]; then
echo "Error: Directory '$DIRECTORY_PATH' specified in the config file does not exist."
exit 1
fi
# Check if the config path exists
if [ ! -f "$BASE_DIR/../$CONFIG_PATH" ]; then
echo "Error: Config file '$CONFIG_PATH' specified for directory '$DIRECTORY_PATH' does not exist."
exit 1
fi
# Get all Python files in the specified directory
FILES=$(git ls-files "$DIRECTORY_PATH/" | grep -e '.py$')
if [ -n "$FILES" ]; then
echo "** Running $TOOL_NAME on directory '$DIRECTORY_PATH' with config from '$CONFIG_PATH'..."
if ! $TOOL_CMD --rcfile="$BASE_DIR/../$CONFIG_PATH" $FILES; then
overall_exit_status=1 # Set to 1 if there were any issues
fi
# Add the files to the custom config list
for file in $FILES; do
checked_files+=("$file")
done
fi
fi
done < "$CONFIG_FILE"
else
# If the configuration file does not exist, print a message and use default config for all files
echo "Configuration file '$CONFIG_FILE' not found. Running $TOOL_NAME with default config on all files..."
if ! $TOOL_CMD --rcfile="$DEFAULT_CONFIG_PATH" $ALL_FILES; then
overall_exit_status=1 # Set to 1 if there were any issues
fi
exit $overall_exit_status
fi
# Calculate remaining files that weren't checked with a custom config
remaining_files=()
for file in $ALL_FILES; do
if [[ ! " ${checked_files[*]} " =~ " $file " ]]; then
remaining_files+=("$file")
fi
done
if [ ${#remaining_files[@]} -gt 0 ]; then
echo "** Running $TOOL_NAME with default config on remaining files..."
if ! $TOOL_CMD --rcfile="$DEFAULT_CONFIG_PATH" "${remaining_files[@]}"; then
overall_exit_status=1
fi
fi
# Exit with the overall exit status
exit $overall_exit_status