codemap-python 0.1.3__py3-none-any.whl → 0.1.5__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- analysis/call_graph/call_extractor.py +15 -16
- analysis/core/ast_parser.py +7 -13
- analysis/core/import_extractor.py +46 -46
- analysis/explain/explain_runner.py +50 -47
- analysis/explain/summary_generator.py +8 -5
- analysis/runners/phase4_runner.py +41 -44
- analysis/utils/bom_handler.py +78 -14
- analysis/utils/cache_manager.py +26 -21
- analysis/utils/progress_spinner.py +85 -0
- analysis/utils/repo_walk.py +27 -0
- cli.py → codemap_app.py +203 -132
- codemap_cli.py +1 -1
- {codemap_python-0.1.3.dist-info → codemap_python-0.1.5.dist-info}/METADATA +24 -12
- {codemap_python-0.1.3.dist-info → codemap_python-0.1.5.dist-info}/RECORD +19 -17
- {codemap_python-0.1.3.dist-info → codemap_python-0.1.5.dist-info}/top_level.txt +1 -1
- ui/app.py +10 -6
- ui/static/app.js +69 -10
- {codemap_python-0.1.3.dist-info → codemap_python-0.1.5.dist-info}/WHEEL +0 -0
- {codemap_python-0.1.3.dist-info → codemap_python-0.1.5.dist-info}/entry_points.txt +0 -0
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
# AST Call detection
|
|
2
|
-
|
|
3
|
-
import ast
|
|
4
|
-
from analysis.utils.bom_handler import
|
|
1
|
+
# AST Call detection
|
|
2
|
+
|
|
3
|
+
import ast
|
|
4
|
+
from analysis.utils.bom_handler import read_source_file, parse_source_to_ast
|
|
5
5
|
|
|
6
6
|
class FunctionCallVisitor(ast.NodeVisitor):
|
|
7
7
|
def __init__(self, file_path):
|
|
@@ -82,15 +82,14 @@ class FunctionCallVisitor(ast.NodeVisitor):
|
|
|
82
82
|
return None
|
|
83
83
|
|
|
84
84
|
|
|
85
|
-
def extract_function_calls(file_path):
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
visitor
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
return visitor.calls
|
|
85
|
+
def extract_function_calls(file_path):
|
|
86
|
+
source = read_source_file(file_path)
|
|
87
|
+
tree = parse_source_to_ast(source, file_path=file_path)
|
|
88
|
+
return extract_function_calls_from_tree(tree, file_path)
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def extract_function_calls_from_tree(tree, file_path):
|
|
92
|
+
visitor = FunctionCallVisitor(file_path)
|
|
93
|
+
visitor.visit(tree)
|
|
94
|
+
|
|
95
|
+
return visitor.calls
|
analysis/core/ast_parser.py
CHANGED
|
@@ -1,13 +1,12 @@
|
|
|
1
|
-
# AST Parser Module
|
|
2
|
-
import
|
|
3
|
-
from analysis.utils.bom_handler import remove_bom
|
|
1
|
+
# AST Parser Module
|
|
2
|
+
from analysis.utils.bom_handler import read_and_parse_python_file
|
|
4
3
|
|
|
5
4
|
|
|
6
|
-
def parse_python_file(file_path):
|
|
7
|
-
"""Parse a Python file
|
|
5
|
+
def parse_python_file(file_path):
|
|
6
|
+
"""Parse a Python file with automatic encoding and BOM handling.
|
|
8
7
|
|
|
9
8
|
This function:
|
|
10
|
-
1. Reads the file with UTF-8
|
|
9
|
+
1. Reads the file with automatic encoding detection (UTF-8 → Latin-1)
|
|
11
10
|
2. Removes any BOM characters automatically
|
|
12
11
|
3. Parses the cleaned source code
|
|
13
12
|
|
|
@@ -20,12 +19,7 @@ def parse_python_file(file_path):
|
|
|
20
19
|
Raises:
|
|
21
20
|
SyntaxError: If source code has syntax errors
|
|
22
21
|
FileNotFoundError: If file doesn't exist
|
|
22
|
+
ValueError: If file encoding cannot be determined
|
|
23
23
|
"""
|
|
24
|
-
|
|
25
|
-
source = f.read()
|
|
26
|
-
|
|
27
|
-
# Remove BOM if present (handles files from Windows editors, etc.)
|
|
28
|
-
source = remove_bom(source)
|
|
29
|
-
|
|
30
|
-
return ast.parse(source)
|
|
24
|
+
return read_and_parse_python_file(file_path)
|
|
31
25
|
|
|
@@ -1,49 +1,49 @@
|
|
|
1
1
|
# Import Extractor Module
|
|
2
2
|
# analysis/import_extractor.py
|
|
3
3
|
|
|
4
|
-
import ast
|
|
5
|
-
from analysis.utils.bom_handler import
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
def
|
|
9
|
-
"""Extract imports from
|
|
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
|
-
return
|
|
4
|
+
import ast
|
|
5
|
+
from analysis.utils.bom_handler import read_source_file, parse_source_to_ast
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def extract_imports_from_tree(tree, file_path):
|
|
9
|
+
"""Extract imports from an already-parsed AST tree."""
|
|
10
|
+
imports = []
|
|
11
|
+
|
|
12
|
+
for node in ast.walk(tree):
|
|
13
|
+
|
|
14
|
+
# import module
|
|
15
|
+
if isinstance(node, ast.Import):
|
|
16
|
+
for alias in node.names:
|
|
17
|
+
imports.append({
|
|
18
|
+
"type": "import",
|
|
19
|
+
"module": alias.name,
|
|
20
|
+
"name": None,
|
|
21
|
+
"alias": alias.asname,
|
|
22
|
+
"line": node.lineno,
|
|
23
|
+
"file": file_path
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
# from module import name
|
|
27
|
+
elif isinstance(node, ast.ImportFrom):
|
|
28
|
+
module = node.module
|
|
29
|
+
level = node.level # 0 = absolute, >0 = relative
|
|
30
|
+
|
|
31
|
+
for alias in node.names:
|
|
32
|
+
imports.append({
|
|
33
|
+
"type": "from_import",
|
|
34
|
+
"module": module,
|
|
35
|
+
"name": alias.name,
|
|
36
|
+
"alias": alias.asname,
|
|
37
|
+
"level": level,
|
|
38
|
+
"line": node.lineno,
|
|
39
|
+
"file": file_path
|
|
40
|
+
})
|
|
41
|
+
|
|
42
|
+
return imports
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def extract_imports(file_path):
|
|
46
|
+
"""Extract imports from a Python file with automatic encoding and BOM handling."""
|
|
47
|
+
source = read_source_file(file_path)
|
|
48
|
+
tree = parse_source_to_ast(source, file_path=file_path)
|
|
49
|
+
return extract_imports_from_tree(tree, file_path)
|
|
@@ -5,35 +5,32 @@ from __future__ import annotations
|
|
|
5
5
|
|
|
6
6
|
from typing import Optional, Dict, Any
|
|
7
7
|
|
|
8
|
-
import
|
|
9
|
-
import
|
|
10
|
-
|
|
11
|
-
from analysis.
|
|
12
|
-
|
|
13
|
-
from analysis.
|
|
14
|
-
from analysis.
|
|
15
|
-
from analysis.explain.
|
|
16
|
-
from analysis.explain.
|
|
17
|
-
from analysis.
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
for file in files:
|
|
25
|
-
if file.endswith(".py") and not file.startswith("__"):
|
|
26
|
-
py_files.append(os.path.join(root, file))
|
|
27
|
-
return py_files
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
def parse_ast(file_path: str)
|
|
31
|
-
"""Parse a Python file
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
# Remove BOM if present
|
|
35
|
-
source = remove_bom(source)
|
|
36
|
-
return ast.parse(source)
|
|
8
|
+
import json
|
|
9
|
+
import os
|
|
10
|
+
|
|
11
|
+
from analysis.indexing.symbol_index import SymbolIndex, SymbolInfo
|
|
12
|
+
from analysis.graph.callgraph_index import CallGraphIndex, CallSite
|
|
13
|
+
from analysis.explain.docstring_extractor import extract_docstrings
|
|
14
|
+
from analysis.explain.signature_extractor import extract_signatures
|
|
15
|
+
from analysis.explain.return_analyzer import analyze_returns
|
|
16
|
+
from analysis.explain.summary_generator import generate_symbol_summary
|
|
17
|
+
from analysis.utils.repo_walk import filter_skipped_dirs
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def collect_python_files(root_dir: str):
|
|
21
|
+
py_files = []
|
|
22
|
+
for root, dirs, files in os.walk(root_dir):
|
|
23
|
+
dirs[:] = filter_skipped_dirs(dirs)
|
|
24
|
+
for file in files:
|
|
25
|
+
if file.endswith(".py") and not file.startswith("__"):
|
|
26
|
+
py_files.append(os.path.join(root, file))
|
|
27
|
+
return py_files
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
def parse_ast(file_path: str):
|
|
31
|
+
"""Parse a Python file with automatic encoding and BOM handling."""
|
|
32
|
+
from analysis.utils.bom_handler import read_and_parse_python_file
|
|
33
|
+
return read_and_parse_python_file(file_path)
|
|
37
34
|
|
|
38
35
|
|
|
39
36
|
def file_to_module(file_path: str, repo_root: str) -> str:
|
|
@@ -83,7 +80,11 @@ def merge_maps(dst: dict, src: dict):
|
|
|
83
80
|
dst[k].update(src.get(k, {}))
|
|
84
81
|
|
|
85
82
|
|
|
86
|
-
def run(
|
|
83
|
+
def run(
|
|
84
|
+
repo_dir: Optional[str] = None,
|
|
85
|
+
output_dir: Optional[str] = None,
|
|
86
|
+
symbol_snapshot: Optional[list] = None,
|
|
87
|
+
) -> Dict[str, Any]:
|
|
87
88
|
"""
|
|
88
89
|
Callable explain pipeline (Phase-5/6), suitable for CLI/VS Code.
|
|
89
90
|
|
|
@@ -119,23 +120,25 @@ def run(repo_dir: Optional[str] = None, output_dir: Optional[str] = None) -> Dic
|
|
|
119
120
|
# 2) Collect repo python files
|
|
120
121
|
python_files = collect_python_files(repo_dir)
|
|
121
122
|
|
|
122
|
-
# 3) Build symbol index + extractors across repo
|
|
123
|
-
symbol_index = SymbolIndex()
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
123
|
+
# 3) Build symbol index + extractors across repo
|
|
124
|
+
symbol_index = SymbolIndex()
|
|
125
|
+
loaded_snapshot = False
|
|
126
|
+
if isinstance(symbol_snapshot, list) and symbol_snapshot:
|
|
127
|
+
symbol_index.load_snapshot(symbol_snapshot)
|
|
128
|
+
loaded_snapshot = True
|
|
129
|
+
|
|
130
|
+
repo_docstrings = {"module": None, "classes": {}, "functions": {}, "methods": {}}
|
|
131
|
+
repo_signatures = {"functions": {}, "methods": {}}
|
|
132
|
+
repo_returns = {"functions": {}, "methods": {}}
|
|
133
|
+
|
|
134
|
+
for file_path in python_files:
|
|
135
|
+
tree = parse_ast(file_path)
|
|
136
|
+
if not loaded_snapshot:
|
|
137
|
+
module_path = file_to_module(file_path, repo_dir)
|
|
138
|
+
symbol_index.index_file(tree, module_path, file_path)
|
|
139
|
+
|
|
140
|
+
# extract per-file and merge
|
|
141
|
+
merge_maps(repo_docstrings, extract_docstrings(tree))
|
|
139
142
|
|
|
140
143
|
sigs = extract_signatures(tree)
|
|
141
144
|
repo_signatures["functions"].update(sigs.get("functions", {}))
|
|
@@ -11,11 +11,14 @@ from analysis.indexing.symbol_index import SymbolInfo
|
|
|
11
11
|
from analysis.graph.callgraph_index import CallGraphIndex
|
|
12
12
|
|
|
13
13
|
|
|
14
|
-
def _first_line(text: Optional[str]) -> Optional[str]:
|
|
15
|
-
if not text:
|
|
16
|
-
return None
|
|
17
|
-
|
|
18
|
-
|
|
14
|
+
def _first_line(text: Optional[str]) -> Optional[str]:
|
|
15
|
+
if not text:
|
|
16
|
+
return None
|
|
17
|
+
stripped = text.strip()
|
|
18
|
+
if not stripped:
|
|
19
|
+
return None
|
|
20
|
+
line = stripped.splitlines()[0].strip()
|
|
21
|
+
return line or None
|
|
19
22
|
|
|
20
23
|
|
|
21
24
|
def _humanize_name(name: str) -> str:
|
|
@@ -3,39 +3,34 @@ from __future__ import annotations
|
|
|
3
3
|
|
|
4
4
|
from typing import Optional, Dict, Any, List
|
|
5
5
|
|
|
6
|
-
import os
|
|
7
|
-
import
|
|
8
|
-
import
|
|
9
|
-
from analysis.indexing.
|
|
10
|
-
from analysis.
|
|
11
|
-
from analysis.call_graph.
|
|
12
|
-
from analysis.
|
|
13
|
-
from analysis.
|
|
14
|
-
from analysis.
|
|
15
|
-
from analysis.utils.bom_handler import remove_bom
|
|
6
|
+
import os
|
|
7
|
+
import json
|
|
8
|
+
from analysis.indexing.symbol_index import SymbolIndex
|
|
9
|
+
from analysis.indexing.import_resolver import ImportResolver
|
|
10
|
+
from analysis.call_graph.cross_file_resolver import CrossFileResolver
|
|
11
|
+
from analysis.call_graph.call_extractor import extract_function_calls_from_tree
|
|
12
|
+
from analysis.core.import_extractor import extract_imports_from_tree
|
|
13
|
+
from analysis.graph.callgraph_index import build_caller_fqn
|
|
14
|
+
from analysis.utils.repo_walk import filter_skipped_dirs
|
|
16
15
|
|
|
17
16
|
|
|
18
17
|
PROJECT_ROOT = os.path.dirname(os.path.dirname(__file__))
|
|
19
18
|
|
|
20
19
|
|
|
21
|
-
def collect_python_files(root_dir: str) -> List[str]:
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
py_files.append(os.path.join(root, file))
|
|
20
|
+
def collect_python_files(root_dir: str) -> List[str]:
|
|
21
|
+
py_files: List[str] = []
|
|
22
|
+
for root, dirs, files in os.walk(root_dir):
|
|
23
|
+
dirs[:] = filter_skipped_dirs(dirs)
|
|
24
|
+
for file in files:
|
|
25
|
+
if file.endswith(".py") and not file.startswith("__"):
|
|
26
|
+
py_files.append(os.path.join(root, file))
|
|
29
27
|
return py_files
|
|
30
28
|
|
|
31
29
|
|
|
32
|
-
def parse_ast(file_path: str):
|
|
33
|
-
"""Parse a Python file, automatically handling UTF-8 BOM."""
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
# Remove BOM if present
|
|
37
|
-
source = remove_bom(source)
|
|
38
|
-
return ast.parse(source)
|
|
30
|
+
def parse_ast(file_path: str):
|
|
31
|
+
"""Parse a Python file, automatically handling encoding and UTF-8 BOM."""
|
|
32
|
+
from analysis.utils.bom_handler import read_and_parse_python_file
|
|
33
|
+
return read_and_parse_python_file(file_path)
|
|
39
34
|
|
|
40
35
|
|
|
41
36
|
def file_to_module(file_path: str, repo_root: str) -> str:
|
|
@@ -80,25 +75,27 @@ def run(repo_dir: Optional[str] = None, output_dir: Optional[str] = None, force_
|
|
|
80
75
|
|
|
81
76
|
os.makedirs(output_dir, exist_ok=True)
|
|
82
77
|
|
|
83
|
-
python_files = collect_python_files(repo_dir)
|
|
84
|
-
symbol_index = SymbolIndex()
|
|
85
|
-
file_module_map: Dict[str, str] = {}
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
78
|
+
python_files = collect_python_files(repo_dir)
|
|
79
|
+
symbol_index = SymbolIndex()
|
|
80
|
+
file_module_map: Dict[str, str] = {}
|
|
81
|
+
parsed_trees: Dict[str, Any] = {}
|
|
82
|
+
|
|
83
|
+
for file_path in python_files:
|
|
84
|
+
module_path = file_to_module(file_path, repo_dir)
|
|
85
|
+
file_module_map[file_path] = module_path
|
|
86
|
+
tree = parse_ast(file_path)
|
|
87
|
+
parsed_trees[file_path] = tree
|
|
88
|
+
symbol_index.index_file(tree, module_path, file_path)
|
|
89
|
+
|
|
90
|
+
import_resolver = ImportResolver(symbol_index)
|
|
91
|
+
for file_path in python_files:
|
|
92
|
+
module_path = file_module_map[file_path]
|
|
93
|
+
imports = extract_imports_from_tree(parsed_trees[file_path], file_path)
|
|
94
|
+
import_resolver.index_module_imports(module_path, imports)
|
|
95
|
+
|
|
96
|
+
all_calls = []
|
|
97
|
+
for file_path in python_files:
|
|
98
|
+
all_calls.extend(extract_function_calls_from_tree(parsed_trees[file_path], file_path))
|
|
102
99
|
|
|
103
100
|
cross_resolver = CrossFileResolver(symbol_index, import_resolver)
|
|
104
101
|
resolved_calls = []
|
analysis/utils/bom_handler.py
CHANGED
|
@@ -1,13 +1,19 @@
|
|
|
1
|
-
"""BOM (Byte Order Mark)
|
|
1
|
+
"""BOM (Byte Order Mark), encoding, and AST parsing utilities for CodeMap.
|
|
2
2
|
|
|
3
|
-
This module provides utilities to handle
|
|
4
|
-
|
|
3
|
+
This module provides utilities to handle:
|
|
4
|
+
1. UTF-8 BOM (Byte Order Mark) characters added by certain editors
|
|
5
|
+
2. Non-UTF-8 encoded files (e.g., Latin-1, Windows-1252)
|
|
5
6
|
|
|
6
|
-
|
|
7
|
-
|
|
7
|
+
Issues handled:
|
|
8
|
+
- BOM (U+FEFF): invisible character causing "invalid non-printable character U+FEFF"
|
|
9
|
+
- Non-UTF-8: files with different encodings causing UnicodeDecodeError
|
|
8
10
|
|
|
9
|
-
Solution:
|
|
10
|
-
"""
|
|
11
|
+
Solution: Detect encoding with fallback chain, strip BOM, and parse quietly.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
import ast
|
|
15
|
+
import warnings
|
|
16
|
+
from typing import Tuple
|
|
11
17
|
|
|
12
18
|
|
|
13
19
|
def remove_bom(source: str) -> str:
|
|
@@ -35,10 +41,49 @@ def remove_bom(source: str) -> str:
|
|
|
35
41
|
return source
|
|
36
42
|
|
|
37
43
|
|
|
38
|
-
def
|
|
39
|
-
"""
|
|
44
|
+
def detect_encoding(file_path: str) -> Tuple[str, bool]:
|
|
45
|
+
"""Detect file encoding by trying multiple decodings.
|
|
40
46
|
|
|
41
|
-
|
|
47
|
+
Tries encodings in this order:
|
|
48
|
+
1. UTF-8 (most common for Python files)
|
|
49
|
+
2. System default encoding
|
|
50
|
+
3. Latin-1 / ISO-8859-1 (accepts any byte sequence)
|
|
51
|
+
|
|
52
|
+
Args:
|
|
53
|
+
file_path: Path to file to detect encoding for
|
|
54
|
+
|
|
55
|
+
Returns:
|
|
56
|
+
Tuple of (encoding_name: str, is_fallback: bool)
|
|
57
|
+
is_fallback=True means file uses non-standard encoding
|
|
58
|
+
|
|
59
|
+
Raises:
|
|
60
|
+
FileNotFoundError: If file doesn't exist
|
|
61
|
+
"""
|
|
62
|
+
import sys
|
|
63
|
+
|
|
64
|
+
encodings_to_try = [
|
|
65
|
+
('utf-8', False),
|
|
66
|
+
(sys.getdefaultencoding(), False),
|
|
67
|
+
('latin-1', True), # Latin-1 accepts any byte sequence
|
|
68
|
+
]
|
|
69
|
+
|
|
70
|
+
for encoding, is_fallback in encodings_to_try:
|
|
71
|
+
try:
|
|
72
|
+
with open(file_path, 'rb') as f:
|
|
73
|
+
f.read().decode(encoding)
|
|
74
|
+
return (encoding, is_fallback)
|
|
75
|
+
except (UnicodeDecodeError, LookupError):
|
|
76
|
+
continue
|
|
77
|
+
|
|
78
|
+
# Should never reach here since Latin-1 accepts all bytes
|
|
79
|
+
return ('latin-1', True)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
def read_source_file(file_path: str) -> str:
|
|
83
|
+
"""Read a Python file with automatic encoding detection and BOM removal.
|
|
84
|
+
|
|
85
|
+
Handles files with different encodings gracefully by trying multiple
|
|
86
|
+
decodings in order of likelihood, then falling back to Latin-1.
|
|
42
87
|
|
|
43
88
|
Args:
|
|
44
89
|
file_path: Path to Python file to read
|
|
@@ -48,8 +93,27 @@ def read_source_file(file_path: str) -> str:
|
|
|
48
93
|
|
|
49
94
|
Raises:
|
|
50
95
|
FileNotFoundError: If file doesn't exist
|
|
51
|
-
UnicodeDecodeError: If file encoding is not UTF-8
|
|
52
96
|
"""
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
97
|
+
encoding, _is_fallback = detect_encoding(file_path)
|
|
98
|
+
with open(file_path, 'r', encoding=encoding, errors='replace') as f:
|
|
99
|
+
source = f.read()
|
|
100
|
+
return remove_bom(source)
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def parse_source_to_ast(source: str, file_path: str = "<unknown>") -> ast.AST:
|
|
104
|
+
"""Parse source code while suppressing noisy invalid-escape warnings.
|
|
105
|
+
|
|
106
|
+
Some user repositories contain regular string literals like ``"\\S"`` or
|
|
107
|
+
``"\\["``. Python can emit ``SyntaxWarning: invalid escape sequence`` while
|
|
108
|
+
parsing those files even though analysis can continue normally. For CodeMap,
|
|
109
|
+
these warnings are implementation noise, so we suppress them here.
|
|
110
|
+
"""
|
|
111
|
+
with warnings.catch_warnings():
|
|
112
|
+
warnings.filterwarnings("ignore", category=SyntaxWarning)
|
|
113
|
+
return ast.parse(source, filename=file_path)
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def read_and_parse_python_file(file_path: str) -> ast.AST:
|
|
117
|
+
"""Read a Python file with encoding/BOM handling and return its AST."""
|
|
118
|
+
source = read_source_file(file_path)
|
|
119
|
+
return parse_source_to_ast(source, file_path=file_path)
|
analysis/utils/cache_manager.py
CHANGED
|
@@ -5,15 +5,15 @@ import json
|
|
|
5
5
|
import os
|
|
6
6
|
import shutil
|
|
7
7
|
import tempfile
|
|
8
|
-
from datetime import datetime, timezone
|
|
9
|
-
from threading import RLock
|
|
10
|
-
from typing import Any, Dict, List, Optional, Tuple
|
|
11
|
-
|
|
12
|
-
from
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
from threading import RLock
|
|
10
|
+
from typing import Any, Dict, List, Optional, Tuple
|
|
11
|
+
|
|
12
|
+
from analysis.utils.repo_walk import filter_skipped_dirs
|
|
13
|
+
from security_utils import redact_secrets
|
|
14
|
+
|
|
15
|
+
_LOCK = RLock()
|
|
16
|
+
_SENSITIVE_KEYS = ("api_key", "token", "authorization", "bearer", "basic", "secret", "password")
|
|
17
17
|
|
|
18
18
|
|
|
19
19
|
def _project_root() -> str:
|
|
@@ -195,14 +195,14 @@ def save_policy(policy: Dict[str, Any], base_dir: Optional[str] = None) -> Dict[
|
|
|
195
195
|
|
|
196
196
|
def collect_fingerprints(repo_dir: str) -> Dict[str, Dict[str, int]]:
|
|
197
197
|
repo_root = os.path.abspath(repo_dir)
|
|
198
|
-
out: Dict[str, Dict[str, int]] = {}
|
|
199
|
-
if not os.path.isdir(repo_root):
|
|
200
|
-
return out
|
|
201
|
-
for root, dirs, files in os.walk(repo_root):
|
|
202
|
-
dirs[:] =
|
|
203
|
-
for name in files:
|
|
204
|
-
if not name.endswith(".py"):
|
|
205
|
-
continue
|
|
198
|
+
out: Dict[str, Dict[str, int]] = {}
|
|
199
|
+
if not os.path.isdir(repo_root):
|
|
200
|
+
return out
|
|
201
|
+
for root, dirs, files in os.walk(repo_root):
|
|
202
|
+
dirs[:] = filter_skipped_dirs(dirs)
|
|
203
|
+
for name in files:
|
|
204
|
+
if not name.endswith(".py"):
|
|
205
|
+
continue
|
|
206
206
|
fp = os.path.join(root, name)
|
|
207
207
|
try:
|
|
208
208
|
st = os.stat(fp)
|
|
@@ -250,16 +250,21 @@ def save_manifest(repo_dir: str, manifest: Dict[str, Any], base_dir: Optional[st
|
|
|
250
250
|
_atomic_json_write(_manifest_path(repo_dir, base_dir), _scrub_payload(payload))
|
|
251
251
|
|
|
252
252
|
|
|
253
|
-
def should_rebuild(
|
|
253
|
+
def should_rebuild(
|
|
254
|
+
repo_dir: str,
|
|
255
|
+
analysis_version: str = "2.2",
|
|
256
|
+
base_dir: Optional[str] = None,
|
|
257
|
+
current_fingerprints: Optional[Dict[str, Any]] = None,
|
|
258
|
+
) -> bool:
|
|
254
259
|
manifest = load_manifest(repo_dir, base_dir=base_dir)
|
|
255
260
|
if not manifest:
|
|
256
261
|
return True
|
|
257
262
|
if str(manifest.get("analysis_version", "") or "") != str(analysis_version or ""):
|
|
258
263
|
return True
|
|
259
264
|
previous = manifest.get("fingerprints", {}) if isinstance(manifest.get("fingerprints"), dict) else {}
|
|
260
|
-
current = collect_fingerprints(repo_dir)
|
|
261
|
-
delta = diff_fingerprints(previous, current)
|
|
262
|
-
return bool(delta.get("changed_count", 0))
|
|
265
|
+
current = current_fingerprints if isinstance(current_fingerprints, dict) else collect_fingerprints(repo_dir)
|
|
266
|
+
delta = diff_fingerprints(previous, current)
|
|
267
|
+
return bool(delta.get("changed_count", 0))
|
|
263
268
|
|
|
264
269
|
|
|
265
270
|
def _default_metadata(repo_hash: str) -> Dict[str, Any]:
|