-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathanalyse.py
More file actions
386 lines (359 loc) · 13.5 KB
/
analyse.py
File metadata and controls
386 lines (359 loc) · 13.5 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
from collections.abc import Generator
from dataclasses import dataclass
import json
import logging
from pathlib import Path
from typing import Any, TypedDict
from tree_sitter import Node as TreeSitterNode
from sphinx_codelinks.analyse import utils
from sphinx_codelinks.analyse.models import (
MarkedContentType,
MarkedRst,
NeedIdRefs,
OneLineNeed,
SourceComment,
SourceFile,
SourceMap,
)
from sphinx_codelinks.analyse.oneline_parser import (
OnelineParserInvalidWarning,
oneline_parser,
)
from sphinx_codelinks.config import (
UNIX_NEWLINE,
OneLineCommentStyle,
SourceAnalyseConfig,
)
# initialize logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
# log to the console
console = logging.StreamHandler()
console.setLevel(logging.INFO)
logger.addHandler(console)
class AnalyseWarningType(TypedDict):
file_path: str
lineno: int
msg: str
type: str
sub_type: str
@dataclass
class AnalyseWarning:
file_path: str
lineno: int
msg: str
type: str
sub_type: str
class SourceAnalyse:
def __init__(
self,
analyse_config: SourceAnalyseConfig,
) -> None:
self.analyse_config = analyse_config
self.src_files: list[SourceFile] = []
self.src_comments: list[SourceComment] = []
self.need_id_refs: list[NeedIdRefs] = []
self.oneline_needs: list[OneLineNeed] = []
self.marked_rst: list[MarkedRst] = []
self.all_marked_content: list[NeedIdRefs | OneLineNeed | MarkedRst] = []
# Use explicitly configured git_root if provided, otherwise auto-detect
if self.analyse_config.git_root is not None:
self.git_root: Path | None = self.analyse_config.git_root.resolve()
else:
self.git_root = utils.locate_git_root(self.analyse_config.src_dir)
self.git_remote_url: str | None = (
utils.get_remote_url(self.git_root) if self.git_root else None
)
self.git_commit_rev: str | None = (
utils.get_current_rev(self.git_root) if self.git_root else None
)
self.project_path: Path = (
self.git_root if self.git_root else self.analyse_config.src_dir
)
self.oneline_warnings: list[AnalyseWarning] = []
def get_src_strings(self) -> Generator[tuple[Path, bytes], Any, None]: # type: ignore[explicit-any]
"""Load source files and extract their content."""
for src_path in self.analyse_config.src_files:
if not utils.is_text_file(src_path):
continue
with src_path.open("r", encoding="utf-8", newline="") as f:
# Normalize all line endings to Unix LF
text = f.read()
text = text.replace("\r\n", "\n").replace("\r", "\n")
yield src_path, text.encode("utf-8")
def create_src_objects(self) -> None:
parser, query = utils.init_tree_sitter(self.analyse_config.comment_type)
for src_path, src_string in self.get_src_strings():
comments: list[TreeSitterNode] | None = utils.extract_comments(
src_string, parser, query
)
if not comments:
continue
src_comments: list[SourceComment] = [
SourceComment(node) for node in comments
]
src_file = SourceFile(src_path.absolute())
src_file.add_comments(src_comments)
self.src_files.append(src_file)
self.src_comments.extend(src_comments)
logger.info(f"Source files loaded: {len(self.src_files)}")
logger.info(f"Source comments extracted: {len(self.src_comments)}")
def extract_marker(
self,
text: str,
) -> Generator[tuple[str, list[str], int, int, int], None, None]:
lines = text.splitlines()
row_offset = 0
for line in lines:
for marker in self.analyse_config.need_id_refs_config.markers:
marker_idx = line.find(marker)
if marker_idx == -1:
continue
markered_text = line[marker_idx + len(marker) :].strip()
need_ids = markered_text.replace(",", " ").split()
start_column = marker_idx + len(marker)
end_column = start_column + len(markered_text)
yield marker, need_ids, row_offset, start_column, end_column
row_offset += 1
# @Extract need ID references from code comments, IMPL_LNK_1, impl, [FE_LNK]
def extract_anchors(
self,
text: str,
filepath: Path,
tagged_scope: TreeSitterNode | None,
src_comment: SourceComment,
) -> list[NeedIdRefs]:
"""Extract need-ids-refs from a comment."""
anchors: list[NeedIdRefs] = []
for (
marker,
need_ids,
row_offset,
start_column,
end_column,
) in self.extract_marker(text):
lineno = src_comment.node.start_point.row + row_offset + 1
remote_url = self.git_remote_url
if self.git_remote_url and self.git_commit_rev:
remote_url = utils.form_https_url(
self.git_remote_url,
self.git_commit_rev,
self.project_path,
filepath,
lineno,
)
source_map: SourceMap = {
"start": {
"row": lineno - 1,
"column": start_column,
},
"end": {
"row": lineno - 1,
"column": end_column,
},
}
anchors.append(
NeedIdRefs(
filepath,
remote_url,
source_map,
src_comment,
tagged_scope,
need_ids,
marker,
)
)
return anchors
def extract_oneline_need(
self,
text: str,
src_comment: SourceComment,
oneline_comment_style: OneLineCommentStyle,
) -> Generator[tuple[dict[str, str | list[str] | int], int]]:
lines = text.splitlines(keepends=True)
row_offset = 0
if len(lines) == 1:
# single line comment has no newline char in the extracted comment
lines[0] = f"{lines[0]}{UNIX_NEWLINE}"
for line in lines:
resolved = oneline_parser(line, oneline_comment_style)
if not resolved:
row_offset += 1
continue
if isinstance(resolved, OnelineParserInvalidWarning):
if not src_comment.source_file:
row_offset += 1
continue
lineno = src_comment.node.start_point.row + row_offset + 1
warning = AnalyseWarning(
str(src_comment.source_file.filepath),
lineno,
resolved.msg,
MarkedContentType.need,
resolved.sub_type.value,
)
self.oneline_warnings.append(warning)
row_offset += 1
continue
yield resolved, row_offset
row_offset += 1
# @Extract one-line traceability needs from comments, IMPL_ONE_1, impl, [FE_DEF, FE_CMT]
def extract_oneline_needs(
self,
text: str,
filepath: Path,
tagged_scope: TreeSitterNode | None,
src_comment: SourceComment,
oneline_comment_style: OneLineCommentStyle,
) -> list[OneLineNeed]:
row_offset = 0
oneline_needs = []
for resolved, row_offset in self.extract_oneline_need(
text, src_comment, oneline_comment_style
):
lineno = src_comment.node.start_point.row + row_offset + 1
remote_url = self.git_remote_url
if self.git_remote_url and self.git_commit_rev:
remote_url = utils.form_https_url(
self.git_remote_url,
self.git_commit_rev,
self.project_path,
filepath,
lineno,
)
source_map: SourceMap = {
"start": {
"row": lineno - 1,
"column": resolved["start_column"], # type: ignore[typeddict-item] # dynamic keys
},
"end": {
"row": lineno - 1,
"column": resolved["end_column"], # type: ignore[typeddict-item] # dynamic keys
},
}
del resolved["start_column"]
del resolved["end_column"]
oneline_needs.append(
OneLineNeed(
filepath,
remote_url,
source_map,
src_comment,
tagged_scope,
resolved, # type: ignore[arg-type] # int arguments were deleted
)
)
return oneline_needs
# @Extract marked reStructuredText blocks from comments, IMPL_MRST_1, impl, [FE_RST_EXTRACTION]
def extract_marked_rst(
self,
text: str,
filepath: Path,
tagged_scope: TreeSitterNode | None,
src_comment: SourceComment,
) -> MarkedRst | None:
"""Extract marked rst from a comment.
Presumably, only one marked rst text in a comment.
"""
extracted_rst = utils.extract_rst(
text,
self.analyse_config.marked_rst_config.start_sequence,
self.analyse_config.marked_rst_config.end_sequence,
)
if not extracted_rst:
return None
if UNIX_NEWLINE in extracted_rst["rst_text"]:
rst_text = utils.remove_leading_sequences(extracted_rst["rst_text"], ["*"])
else:
rst_text = extracted_rst["rst_text"]
lineno = src_comment.node.start_point.row + extracted_rst["row_offset"] + 1
remote_url = self.git_remote_url
if self.git_remote_url and self.git_commit_rev:
remote_url = utils.form_https_url(
self.git_remote_url,
self.git_commit_rev,
self.project_path,
filepath,
lineno,
)
source_map: SourceMap = {
"start": {
"row": lineno - 1,
"column": extracted_rst["start_idx"],
},
"end": {
"row": lineno - 1,
"column": extracted_rst["end_idx"],
},
}
return MarkedRst(
filepath,
remote_url,
source_map,
src_comment,
tagged_scope,
rst_text,
)
def extract_marked_content(self) -> None:
for src_comment in self.src_comments:
text = (
src_comment.node.text.decode("utf-8") if src_comment.node.text else None
)
if not text:
continue
filepath = (
src_comment.source_file.filepath if src_comment.source_file else None
)
if not filepath:
continue
tagged_scope: TreeSitterNode | None = utils.find_associated_scope(
src_comment.node, self.analyse_config.comment_type
)
if self.analyse_config.get_need_id_refs:
anchors = self.extract_anchors(
text, filepath, tagged_scope, src_comment
)
self.need_id_refs.extend(anchors)
if self.analyse_config.get_oneline_needs:
oneline_needs = self.extract_oneline_needs(
text,
filepath,
tagged_scope,
src_comment,
self.analyse_config.oneline_comment_style,
)
self.oneline_needs.extend(oneline_needs)
if self.analyse_config.get_rst:
marked_rst = self.extract_marked_rst(
text, filepath, tagged_scope, src_comment
)
if marked_rst:
self.marked_rst.append(marked_rst)
if self.analyse_config.get_need_id_refs:
logger.info(f"Need-id-refs extracted: {len(self.need_id_refs)}")
if self.analyse_config.get_oneline_needs:
logger.info(f"Oneline needs extracted: {len(self.oneline_needs)}")
if self.analyse_config.get_rst:
logger.info(f"Marked rst extracted: {len(self.marked_rst)}")
def merge_marked_content(self) -> None:
self.all_marked_content.extend(self.need_id_refs)
self.oneline_needs.sort(key=lambda x: x.source_map["start"]["row"])
self.all_marked_content.extend(self.oneline_needs)
self.all_marked_content.extend(self.marked_rst)
self.all_marked_content.sort(
key=lambda x: (x.filepath, x.source_map["start"]["row"])
)
def dump_marked_content(self, outdir: Path) -> None:
output_path = outdir / "marked_content.json"
if not output_path.parent.exists():
output_path.parent.mkdir(parents=True)
to_dump = [
marked_content.to_dict() for marked_content in self.all_marked_content
]
with output_path.open("w") as f:
json.dump(to_dump, f)
logger.info(f"Marked content dumped to {output_path}")
def run(self) -> None:
self.create_src_objects()
self.extract_marked_content()
self.merge_marked_content()