forked from App-an-server/Code-test
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathanalyze_code.py
More file actions
62 lines (52 loc) · 2.09 KB
/
analyze_code.py
File metadata and controls
62 lines (52 loc) · 2.09 KB
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
#!/usr/bin/env python3
import subprocess
import sys
import os
import shutil
def main():
if len(sys.argv) < 2 or len(sys.argv) > 4:
print("Usage: python analyze_code.py /path/to/code [swift|cpp|all] [basic|advanced]")
print(" - 'basic' runs SwiftLint/Cppcheck; 'advanced' adds Clang-Tidy/Infer")
return 1
code_path = sys.argv[1]
lang = "all" if len(sys.argv) == 2 else sys.argv[2]
mode = "basic" if len(sys.argv) <= 3 else sys.argv[3]
if lang not in ["swift", "cpp", "all"]:
print("Invalid language specified. Use 'swift', 'cpp', or 'all'.")
return 1
if mode not in ["basic", "advanced"]:
print("Invalid mode specified. Use 'basic' or 'advanced'.")
return 1
if not os.path.exists(code_path):
print(f"The path '{code_path}' does not exist.")
return 1
def run_command(cmd, tool_name):
if not shutil.which(cmd[0]):
print(f"{tool_name} not found. Please install it.")
return False
try:
print(f"Running {tool_name} on {code_path}")
subprocess.run(cmd)
return True
except Exception as e:
print(f"Error running {tool_name}: {e}")
return False
# Basic analysis
if lang in ["swift", "all"]:
run_command(['swiftlint', 'lint', code_path], "SwiftLint")
if lang in ["cpp", "all"]:
run_command(['cppcheck', code_path], "Cppcheck")
# Advanced analysis
if mode == "advanced":
if lang in ["cpp", "all"]:
cpp_files = ' '.join([f for f in os.listdir(code_path) if f.endswith(('.cpp', '.cxx', '.cc'))])
if cpp_files:
run_command(['clang-tidy', '--quiet', cpp_files, '--', f'-I{code_path}'], "Clang-Tidy")
else:
print("No C++ files found for Clang-Tidy.")
if lang in ["swift", "all", "cpp"]:
run_command(['infer', 'run', '--', 'make'], "Infer")
# Adjust 'make' to your build command (e.g., 'swift build' for SwiftPM)
return 0
if __name__ == "__main__":
sys.exit(main())