Skip to content

Commit 9a9ebab

Browse files
Formating & Linting & Comments
1 parent 996a51e commit 9a9ebab

4 files changed

Lines changed: 54 additions & 28 deletions

File tree

scripts_bazel/generate_sourcelinks_cli.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,22 @@
3535
logger = logging.getLogger(__name__)
3636

3737

38+
def clean_external_prefix(path: Path) -> Path:
39+
"""
40+
As it can be in combo builds that the path is:
41+
`external/score_docs_as_code+/....` or similar
42+
We have to remove this prefix from the file
43+
before we pass it to the extract function. Otherwise we have
44+
this prefix inside the `file` attribute leading to wrong links
45+
"""
46+
if not str(path).startswith("external/"):
47+
return path
48+
# This allows for files / folders etc. to have `external` in their name too.
49+
path_raw = str(path).removeprefix("external/")
50+
filepath_split = str(path_raw).split("/", maxsplit=1)
51+
return Path("".join(filepath_split[1:]))
52+
53+
3854
def main():
3955
parser = argparse.ArgumentParser(
4056
description="Generate source code links JSON from source files"
@@ -67,8 +83,9 @@ def main():
6783
metadata_set = True
6884
abs_file_path = file_path.resolve()
6985
assert abs_file_path.exists(), abs_file_path
86+
clean_path = clean_external_prefix(file_path)
7087
references = _extract_references_from_file(
71-
abs_file_path.parent, Path(abs_file_path.name), file_path
88+
abs_file_path.parent, Path(abs_file_path.name), clean_path
7289
)
7390
all_need_references.extend(references)
7491
store_source_code_links_with_metadata_json(

scripts_bazel/merge_sourcelinks.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,13 @@
2727
logger = logging.getLogger(__name__)
2828

2929

30-
3130
"""
3231
if bazel-out/k8-fastbuild/bin/external/ in file_path => module is external
3332
otherwise it's local
3433
if local => module_name & hash == empty
3534
if external => parse thing for module_name => look up known_good json for hash & url
3635
"""
3736

38-
39-
40-
def add_needid_to_metaneed_mapping(mapping: dict[str, dict[str, str]], metadata: dict[str, str], needid: str):
41-
mapping
42-
pass
43-
4437
def main():
4538
parser = argparse.ArgumentParser(
4639
description="Merge multiple sourcelinks JSON files into one"
@@ -67,7 +60,6 @@ def main():
6760
all_files = [x for x in args.files if "known_good.json" not in str(x)]
6861

6962
merged = []
70-
needs_metadata_mapping = {}
7163
for json_file in all_files:
7264
with open(json_file) as f:
7365
data = json.load(f)

src/extensions/score_source_code_linker/generate_source_code_links_json.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ def _extract_references_from_line(line: str):
5050
def _extract_references_from_file(
5151
root: Path, file_path_name: Path, file_path: Path
5252
) -> list[NeedLink]:
53-
"""Scan a single file for template strings and return findings."""
53+
"""Scan a single file for template strings and return findings.
54+
Examples:
55+
# ROOT: <local_root>/docs-as-code/src/extensions/score_source_code_linker
56+
#FILE PATH:
57+
external/score_docs_as_code+/src/extensions/score_source_code_linker/testlink.py
58+
#FILE PATH NAME: testlink.py
59+
"""
5460
assert root.is_absolute(), "Root path must be absolute"
5561
assert not file_path_name.is_absolute(), "File path must be relative to the root"
5662
# assert file_path.is_relative_to(root), (

src/extensions/score_source_code_linker/xml_parser.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,23 @@
5454

5555

5656
def get_metadata_from_test_path(filepath: Path) -> MetaData:
57+
"""
58+
Will parse out the metadata from the testpath.
59+
If test is local then the metadata will be:
60+
61+
"module_name": "local_module",
62+
"hash": "",
63+
"url": "",
64+
65+
Else it will parse the module_name e.g. `score_docs_as_code`
66+
match this in the known_good_json and grab the accompanying
67+
hash, url as well and return metadata like so for example:
68+
69+
"module_name": "score_docs_as_code",
70+
"hash": "c1207676afe6cafd25c35d420e73279a799515d8",
71+
"url": "https://github.com/eclipse-score/docs-as-code"
72+
73+
"""
5774
known_good_json = os.environ.get("KNOWN_GOOD_JSON")
5875
module_name = parse_module_name_from_path(filepath)
5976
md: MetaData = {
@@ -193,13 +210,17 @@ def read_test_xml_file(file: Path) -> tuple[list[DataOfTestCase], list[str], lis
193210
return test_case_needs, non_prop_tests, missing_prop_tests
194211

195212

196-
# /home/maximilianp/score_personal/reference_integration/bazel-testlogs/external/score_docs_as_code+/src/helper_lib/helper_lib_tests/test.xml
197213
def find_xml_files(dir: Path) -> list[Path]:
198214
"""
199215
Recursively search all test.xml files inside 'bazel-testlogs'
200216
201217
Returns:
202218
- list[Path] => Paths to all found 'test.xml' files.
219+
220+
Example combo TestPath for future reference:
221+
222+
'<local path to folder>/reference_integration/bazel-testlogs
223+
/feature_integration_tests/test_cases/fit/test.xml'
203224
"""
204225

205226
test_file_name = "test.xml"
@@ -208,21 +229,19 @@ def find_xml_files(dir: Path) -> list[Path]:
208229
for root, _, files in os.walk(dir):
209230
if test_file_name in files:
210231
xml_paths.append(Path(os.path.join(root, test_file_name)))
211-
print("=========================================")
212-
print(xml_paths[0])
213-
print("=========================================")
232+
214233
return xml_paths
215234

216235

217-
def find_test_folder(base_path: Path | None = None) -> tuple[Path | None, Path | None]:
236+
def find_test_folder(base_path: Path | None = None) -> Path | None:
218237
ws_root = base_path if base_path is not None else find_ws_root()
219238
assert ws_root is not None
220239
if os.path.isdir(ws_root / "tests-report"):
221-
return ws_root, ws_root / "tests-report"
240+
return ws_root / "tests-report"
222241
if os.path.isdir(ws_root / "bazel-testlogs"):
223-
return ws_root, ws_root / "bazel-testlogs"
242+
return ws_root / "bazel-testlogs"
224243
logger.info("could not find tests-report or bazel-testlogs to parse testcases")
225-
return ws_root, None
244+
return None
226245

227246

228247
def run_xml_parser(app: Sphinx, env: BuildEnvironment):
@@ -231,19 +250,10 @@ def run_xml_parser(app: Sphinx, env: BuildEnvironment):
231250
building testcase needs.
232251
It gets called from the source_code_linker __init__
233252
"""
234-
root_path, testlogs_dir = find_test_folder()
235-
# early return
253+
testlogs_dir = find_test_folder()
236254
if testlogs_dir is None:
237255
return
238256
xml_file_paths = find_xml_files(testlogs_dir)
239-
# scl_with_metadata = load_source_code_links_with_metadata_json(
240-
# app.outdir / "score_source_links_metadata.json"
241-
# )[0]
242-
# metadata: MetaData = {
243-
# "module_name": scl_with_metadata.module_name,
244-
# "hash": scl_with_metadata.hash,
245-
# "url": scl_with_metadata.url,
246-
# }
247257
test_case_needs = build_test_needs_from_files(app, env, xml_file_paths)
248258
# Saving the test case needs for cache
249259
store_data_of_test_case_json(
@@ -301,6 +311,7 @@ def construct_and_add_need(app: Sphinx, tn: DataOfTestCase):
301311
assert tn.module_name is not None
302312
assert tn.hash is not None
303313
assert tn.url is not None
314+
# Have to build metadata here for the gh link func
304315
metadata = ModuleInfo(name=tn.module_name, hash=tn.hash, url=tn.url)
305316
# IDK if this is ideal or not
306317
with contextlib.suppress(BaseException):

0 commit comments

Comments
 (0)