-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathScanner.py
More file actions
209 lines (185 loc) Β· 6.43 KB
/
Scanner.py
File metadata and controls
209 lines (185 loc) Β· 6.43 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
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
import locale
import logging
import os
from os.path import relpath
from pathlib import Path
from typing import Union, Callable
from codelimit.common.Codebase import Codebase
from codelimit.common.Configuration import Configuration
from codelimit.common.Language import Language
from codelimit.common.Location import Location
from codelimit.common.Measurement import Measurement
from codelimit.common.ScanResultTable import ScanResultTable
from codelimit.common.ScanTotals import ScanTotals
from codelimit.common.SourceFileEntry import SourceFileEntry
from codelimit.common.Token import Token
from codelimit.common.lexer_utils import lex
from codelimit.common.report.Report import Report
from codelimit.common.scope.scope_utils import build_scopes, unfold_scopes, count_lines
from codelimit.common.source_utils import filter_tokens
from codelimit.common.utils import (
calculate_checksum,
)
from codelimit.languages import Languages
from pathspec import PathSpec
from pygments.lexer import Lexer
from pygments.lexers import get_lexer_for_filename
from pygments.util import ClassNotFound
from rich import print
from rich.live import Live
locale.setlocale(locale.LC_ALL, "")
def scan_codebase(path: Path, cached_report: Union[Report, None] = None) -> Codebase:
scan_totals = ScanTotals()
if Configuration.verbose:
def add_file_entry(entry: SourceFileEntry):
scan_totals.add(entry)
codebase = scan_path(path, cached_report, add_file_entry)
print(ScanResultTable(scan_totals))
else:
with Live(refresh_per_second=2) as live:
def add_file_entry(entry: SourceFileEntry):
scan_totals.add(entry)
table = ScanResultTable(scan_totals)
live.update(table)
codebase = scan_path(path, cached_report, add_file_entry)
live.stop()
live.refresh()
return codebase
def scan_path(path: Path, cached_report: Union[Report, None] = None,
add_file_entry_callback: Union[Callable[[SourceFileEntry], None], None] = None,
) -> Codebase:
result = Codebase(str(path.resolve().absolute()))
excludes_spec = generate_exclude_spec(path)
for root, dirs, files in os.walk(path.absolute()):
files = [f for f in files if not f[0] == "."]
dirs[:] = [d for d in dirs if not d[0] == "."]
for file in files:
rel_path = Path(os.path.join(root, file)).relative_to(path.absolute())
if is_excluded(rel_path, excludes_spec):
continue
try:
lexer = get_lexer_for_filename(rel_path)
lexer_name = lexer.__class__.name
file_path = os.path.join(root, file)
languages = Languages.by_name.keys()
if lexer_name in languages:
file_entry = _scan_file(
result, lexer, path, file_path, cached_report
)
if add_file_entry_callback:
add_file_entry_callback(file_entry)
except ClassNotFound:
pass
return result
def _scan_file(
codebase: Codebase,
lexer: Lexer,
root: Path,
path: str,
cached_report: Union[Report, None] = None,
) -> SourceFileEntry:
checksum = calculate_checksum(path)
rel_path = relpath(path, root)
cached_entry = None
if cached_report:
rel_path = relpath(path, root)
try:
cached_entry = cached_report.codebase.files[rel_path]
except KeyError:
pass
if cached_entry and cached_entry.checksum() == checksum:
entry = SourceFileEntry(
rel_path,
checksum,
cached_entry.language,
cached_entry.loc,
cached_entry.measurements(),
)
else:
entry = _analyze_file(path, rel_path, checksum, lexer)
codebase.add_file(entry)
return entry
def _read_file(path: Path):
try:
with open(path) as f:
return f.read()
except UnicodeDecodeError:
with open(path, encoding="latin-1") as f:
return f.read()
def _analyze_file(path, rel_path, checksum, lexer):
logging.info(f"Analyzing {rel_path}")
code = _read_file(path)
all_tokens = lex(lexer, code, False)
language_name = lexer.__class__.name
language = Languages.by_name[language_name]
if language:
measurements = scan_file(all_tokens, language)
else:
measurements = []
file_loc = sum([m.value for m in measurements])
entry = SourceFileEntry(
rel_path, checksum, lexer.__class__.name, file_loc, measurements
)
return entry
def scan_file(tokens: list[Token], language: Language) -> list[Measurement]:
scopes = build_scopes(tokens, language)
scopes = unfold_scopes(scopes)
measurements: list[Measurement] = []
code_tokens = filter_tokens(tokens)
if scopes:
for scope in scopes:
length = count_lines(scope, code_tokens)
start_location = code_tokens[scope.header.token_range.start].location
last_token = code_tokens[scope.block.end - 1]
end_location = Location(
last_token.location.line,
last_token.location.column + len(last_token.value),
)
measurements.append(
Measurement(scope.header.name(), start_location, end_location, length)
)
return measurements
def generate_exclude_spec(root: Path) -> PathSpec:
excludes = DEFAULT_EXCLUDES.copy()
excludes.extend(Configuration.exclude)
gitignore_excludes = _read_gitignore(root)
if gitignore_excludes:
excludes.extend(gitignore_excludes)
return PathSpec.from_lines("gitignore", excludes)
def _read_gitignore(path: Path) -> list[str] | None:
gitignore_path = path.joinpath(".gitignore")
if gitignore_path.exists():
return gitignore_path.read_text().splitlines()
return None
def is_excluded(path: Path, spec: PathSpec):
return spec.match_file(path)
DEFAULT_EXCLUDES = [
".bzr",
".direnv",
".eggs",
".git",
".git-rewrite",
".hg",
".ipynb_checkpoints",
".mypy_cache",
".nox",
".pants.d",
".pytest_cache",
".pytype",
".ruff_cache",
".svn",
".tox",
".venv",
".vscode",
"__pypackages__",
"_build",
"buck-out",
"build",
"dist",
"node_modules",
"venv",
"test",
"tests",
"acceptancetests",
"integrationtests"
]