Skip to content

Commit e99aee1

Browse files
authored
Use pyyaml to parse testinfo.yml file (#5258)
This allows the CodeQL builder to use pyyaml instead of regex to parse the `testinfo.yml` files.
1 parent f95ee3a commit e99aee1

File tree

5 files changed

+35
-15
lines changed

5 files changed

+35
-15
lines changed

.github/workflows/codeql-analysis.yml

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,25 @@ jobs:
131131
with:
132132
fetch-depth: 2
133133

134+
- name: Read repo config
135+
uses: pietrobolcato/[email protected]
136+
id: repo-config
137+
with:
138+
config: repo-config.yml
139+
140+
- name: Set up Python
141+
uses: actions/setup-python@v5
142+
with:
143+
python-version: "${{ steps.repo-config.outputs['python-version'] }}"
144+
145+
- name: Run Poetry Image
146+
uses: abatilo/actions-poetry@v3
147+
with:
148+
poetry-version: "${{ steps.repo-config.outputs['poetry-version'] }}"
149+
150+
- name: Install Dependencies
151+
run: poetry install
152+
134153
# Set up Rust for scanning.
135154
- name: Set up Rust
136155
if: ${{ matrix.language == 'rust' }}
@@ -160,7 +179,7 @@ jobs:
160179

161180
- name: Manual build
162181
if: ${{ matrix.build-mode == 'manual' }}
163-
run: python scripts/build_codeql_language.py ${{ matrix.language }} ${{ matrix.paths }}
182+
run: poetry run python scripts/build_codeql_language.py ${{ matrix.language }} ${{ matrix.paths }}
164183

165184
- name: Perform CodeQL Analysis
166185
uses: github/codeql-action/analyze@v4

poetry.lock

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ license = "MIT"
88

99
dependencies = [
1010
"ronbun (>=0.10.1,<0.11.0)",
11-
"glotter2 (>=0.13.0,<0.14.0)"
11+
"glotter2 (>=0.13.0,<0.14.0)",
12+
"pyyaml (>=6.0.3,<7.0.0)"
1213
]
1314

1415
# Initiator of the collection

scripts/build_codeql_language.py

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import argparse
22
import re
3-
import shlex
43
import subprocess
54
from dataclasses import dataclass
65
from pathlib import Path
7-
from typing import Dict, List
6+
from typing import Any, Dict, List
7+
8+
import yaml
89

910
TEST_INFO_DIR: Dict[str, str] = {
1011
"c": "c",
@@ -14,7 +15,6 @@
1415
"swift": "swift",
1516
}
1617

17-
BUILD_PATTERN = re.compile(r"""^\s+build:\s*['"]([^'"]+)""", re.M)
1818
SOURCE_NAME_PATTERN = re.compile(r"\{\{\s*source\.name\s*\}\}")
1919
SOURCE_EXTENSION_PATTERN = re.compile(r"\{\{\s*source\.extension\s*\}\}")
2020

@@ -23,7 +23,7 @@
2323
class TestInfoStruct:
2424
path: Path
2525
language: str
26-
test_info_str: str
26+
build: str
2727

2828

2929
def main():
@@ -36,22 +36,21 @@ def main():
3636
path = Path(changed_file)
3737
print(f"Building {path}")
3838
command = get_build_command(testinfo_struct, path)
39-
subprocess.run(command, cwd=path.parent, check=True)
39+
subprocess.run(command, cwd=path.parent, check=True, shell=True)
4040

4141

4242
def get_test_info_struct(language: str) -> TestInfoStruct:
4343
path = Path("archive") / language[0] / TEST_INFO_DIR[language] / "testinfo.yml"
44-
testinfo_str = path.read_text(encoding="utf-8")
45-
return TestInfoStruct(path=path, language=language, test_info_str=testinfo_str)
44+
with path.open() as f:
45+
testinfo = yaml.safe_load(f)
4646

47+
return TestInfoStruct(path=path, language=language, build=testinfo["container"]["build"])
4748

48-
def get_build_command(testinfo_struct: TestInfoStruct, path: Path) -> List[str]:
49-
match_str = BUILD_PATTERN.search(testinfo_struct.test_info_str)
50-
assert match_str
5149

52-
build = SOURCE_NAME_PATTERN.sub(path.stem, match_str.group(1))
50+
def get_build_command(testinfo_struct: TestInfoStruct, path: Path) -> str:
51+
build = SOURCE_NAME_PATTERN.sub(path.stem, testinfo_struct.build)
5352
build = SOURCE_EXTENSION_PATTERN.sub(path.suffix, build)
54-
return shlex.split(build)
53+
return build
5554

5655

5756
if __name__ == "__main__":

scripts/generate_cargo_toml.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
path = "{path}"
1414
"""
1515

16+
1617
def main():
1718
cargo_toml = CARGO_TOML
1819
for path in Path("archive/r/rust").glob("*.rs"):

0 commit comments

Comments
 (0)