Skip to content

Commit c38e1ce

Browse files
author
MomIsBestFriend
committed
CI: Added unwanted patterns check
1 parent e81faa1 commit c38e1ce

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed

ci/code_checks.sh

+4
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ if [[ -z "$CHECK" || "$CHECK" == "patterns" ]]; then
207207
invgrep -R --include=*.{py,pyx} 'xrange' pandas
208208
RET=$(($RET + $?)) ; echo $MSG "DONE"
209209

210+
MSG='Check for use of not concatenated strings' ; echo $MSG
211+
python ./scripts/validate_string_concatenation.py pandas
212+
RET=$(($RET + $?)) ; echo $MSG "DONE"
213+
210214
MSG='Check that no file in the repo contains trailing whitespaces' ; echo $MSG
211215
INVGREP_APPEND=" <- trailing whitespaces found"
212216
invgrep -RI --exclude=\*.{svg,c,cpp,html,js} --exclude-dir=env "\s$" *
+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#!/usr/bin/env python
2+
"""
3+
Check where there is a string that needs to be concatenated.
4+
"""
5+
6+
import os
7+
import sys
8+
import token
9+
import tokenize
10+
11+
FILE_EXTENTIONS_TO_CHECK = [".py", ".pyx"]
12+
13+
14+
def main():
15+
path = sys.argv[1]
16+
17+
if not os.path.exists(path):
18+
raise ValueError("Please enter a valid path, to a file/directory.")
19+
20+
if os.path.isfile(path):
21+
# Means that the given path is of a single file.
22+
sys.exit(is_concatenated(path))
23+
24+
status_codes = set()
25+
# Means that the given path is of a directory.
26+
for subdir, _, files in os.walk(path):
27+
for file_name in files:
28+
ext = os.path.splitext(os.path.join(subdir, file_name))[1]
29+
if ext in FILE_EXTENTIONS_TO_CHECK:
30+
status_codes.add(is_concatenated(os.path.join(subdir, file_name)))
31+
32+
if 1 in status_codes:
33+
sys.exit(1)
34+
35+
sys.exit(0)
36+
37+
38+
def is_concatenated(file_path):
39+
"""
40+
Checking if the file containing strings that needs to be concatenated.
41+
42+
Parameters
43+
----------
44+
file_path : str
45+
File path pointing to a single file.
46+
47+
Returns
48+
-------
49+
int
50+
Status code representing if the file needs a fix.
51+
0 - All good.
52+
1 - Needs to be fixed.
53+
"""
54+
with open(file_path, "r") as file_name:
55+
toks = list(tokenize.generate_tokens(file_name.readline))
56+
for i in range(len(toks) - 1):
57+
tok = toks[i]
58+
tok2 = toks[i + 1]
59+
if tok[0] == token.STRING and tok[0] == tok2[0]:
60+
print(
61+
"{file_path}:{line_number}:\t{start} and {end}".format(
62+
file_path=file_path,
63+
line_number=tok[2][0],
64+
start=tok[1],
65+
end=tok2[1],
66+
)
67+
)
68+
return 1
69+
return 0
70+
71+
72+
if __name__ == "__main__":
73+
main()

0 commit comments

Comments
 (0)