Skip to content

Commit 35e64f8

Browse files
authored
keep all generated files in a cache folder (#29)
* keep all generated files in a cache folder * Use constants instead of hard-coded values * change workflow triggers
1 parent b37e680 commit 35e64f8

File tree

9 files changed

+51
-34
lines changed

9 files changed

+51
-34
lines changed

.github/workflows/run-dev-tests.yml

+4-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@ name: "Check python code"
22

33
on:
44
push:
5+
branches: [main]
56
paths:
67
- "**.py"
78
- "**requirements*.txt"
89
- pyproject.toml
910
- ".github/workflows/run-dev-tests.yml"
1011
- "!docs/**"
1112
pull_request:
12-
types: opened
13+
# types: opened
14+
branches: [main]
1315
paths:
1416
- "**.py"
1517
- "**requirements*.txt"
@@ -99,7 +101,7 @@ jobs:
99101
- run: coverage report && coverage xml
100102

101103
- uses: codecov/codecov-action@v3
102-
if: matrix.os == 'ubuntu-latest' && matrix.version == '12' && matrix.py == '3.10'
104+
if: matrix.os == 'ubuntu-22.04' && matrix.version == '12' && matrix.py == '3.10'
103105
with:
104106
token: ${{secrets.CODECOV_TOKEN}}
105107
files: ./coverage.xml

.gitignore

+2-10
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
1-
# local demo specific
2-
.changed_files.json
3-
clang_format*.txt
4-
clang_tidy*.txt
1+
# local dev artifacts
2+
.cpp-linter_cache/
53
act.exe
6-
clang_tidy_output.yml
7-
clang_format_output.xml
8-
event_payload.json
9-
comments.json
10-
*.diff
11-
!tests/capture_tools_output/*/*/*.diff
124

135
#### ignores for Python
146
# Byte-compiled / optimized / DLL files

cpp_linter/__init__.py

+5
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@
3636
GITHUB_SHA = os.getenv("GITHUB_SHA", "")
3737
GITHUB_TOKEN = os.getenv("GITHUB_TOKEN", os.getenv("GIT_REST_API", ""))
3838
IS_ON_WINDOWS = platform.system().lower() == "windows"
39+
CACHE_PATH = Path(os.getenv("CPP_LINTER_CACHE", ".cpp-linter_cache"))
40+
CLANG_FORMAT_XML = CACHE_PATH / "clang_format_output.xml"
41+
CLANG_TIDY_YML = CACHE_PATH / "clang_tidy_output.yml"
42+
CLANG_TIDY_STDOUT = CACHE_PATH / "clang_tidy_report.txt"
43+
CHANGED_FILES_JSON = CACHE_PATH / "changed_files.json"
3944

4045

4146
def make_headers(use_diff: bool = False) -> Dict[str, str]:

cpp_linter/clang_format_xml.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import PurePath
33
from typing import List, Optional
44
import xml.etree.ElementTree as ET
5-
from . import GlobalParser, get_line_cnt_from_cols
5+
from . import GlobalParser, get_line_cnt_from_cols, CLANG_FORMAT_XML
66

77

88
class FormatReplacement:
@@ -86,8 +86,14 @@ def log_command(self, style: str, line_filter: List[int]) -> Optional[str]:
8686
:param line_filter: A list of lines numbers used to narrow notifications.
8787
"""
8888
if style not in (
89-
"llvm", "gnu", "google", "chromium", "microsoft", "mozilla", "webkit"
90-
):
89+
"llvm",
90+
"gnu",
91+
"google",
92+
"chromium",
93+
"microsoft",
94+
"mozilla",
95+
"webkit",
96+
):
9197
# potentially the style parameter could be a str of JSON/YML syntax
9298
style = "Custom"
9399
else:
@@ -119,7 +125,7 @@ def parse_format_replacements_xml(src_filename: str):
119125
:param src_filename: The source file's name for which the contents of the xml
120126
file exported by clang-tidy.
121127
"""
122-
tree = ET.parse("clang_format_output.xml")
128+
tree = ET.parse(str(CLANG_FORMAT_XML))
123129
fixit = XMLFixit(src_filename)
124130
for child in tree.getroot():
125131
if child.tag == "replacement":

cpp_linter/clang_tidy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path, PurePath
33
import re
44
from typing import Tuple, Union, List, cast
5-
from . import GlobalParser
5+
from . import GlobalParser, CLANG_TIDY_STDOUT
66

77
NOTE_HEADER = re.compile(r"^(.*):(\d+):(\d+):\s(\w+):(.*)\[(.*)\]$")
88

@@ -99,7 +99,7 @@ def parse_tidy_output() -> None:
9999
"""Parse clang-tidy output in a file created from stdout. The results are
100100
saved to :attr:`~cpp_linter.GlobalParser.tidy_notes`."""
101101
notification = None
102-
tidy_out = Path("clang_tidy_report.txt").read_text(encoding="utf-8")
102+
tidy_out = CLANG_TIDY_STDOUT.read_text(encoding="utf-8")
103103
for line in tidy_out.splitlines():
104104
match = re.match(NOTE_HEADER, line)
105105
if match is not None:

cpp_linter/clang_tidy_yml.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from pathlib import Path, PurePath
33
from typing import List, cast, Dict, Any
44
import yaml
5-
from . import GlobalParser, get_line_cnt_from_cols, logger
5+
from . import GlobalParser, get_line_cnt_from_cols, logger, CLANG_TIDY_YML
66

77

88
CWD_HEADER_GUARD = bytes(
@@ -87,7 +87,7 @@ def parse_tidy_suggestions_yml():
8787
"""Read a YAML file from clang-tidy and create a list of suggestions from it.
8888
Output is saved to :attr:`~cpp_linter.GlobalParser.tidy_advice`.
8989
"""
90-
yml_file = Path("clang_tidy_output.yml").read_text(encoding="utf-8")
90+
yml_file = CLANG_TIDY_YML.read_text(encoding="utf-8")
9191
yml = yaml.safe_load(yml_file)
9292
fixit = YMLFixit(yml["MainSourceFile"])
9393

cpp_linter/git.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import re
55
import subprocess
66
from typing import Tuple, List, Dict, Any, Optional
7-
from . import logger
7+
from . import logger, CACHE_PATH
88

99

1010
def get_sha(parent: int = 1) -> str:
@@ -36,7 +36,9 @@ def get_diff(parents: int = 1) -> str:
3636
logger.info("getting diff between %s...%s", head, base)
3737
result = subprocess.run(["git", "status", "-v"], capture_output=True, check=True)
3838
diff_start = result.stdout.find(b"diff --git")
39-
Path(f"{head}...{base[:6]}.diff").write_bytes(result.stdout[diff_start:])
39+
Path(CACHE_PATH, f"{head}...{base[:6]}.diff").write_bytes(
40+
result.stdout[diff_start:]
41+
)
4042
return result.stdout[diff_start:].decode(encoding="utf-8")
4143

4244

cpp_linter/run.py

+18-10
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@
2626
GITHUB_SHA,
2727
make_headers,
2828
IS_ON_RUNNER,
29+
CACHE_PATH,
30+
CLANG_FORMAT_XML,
31+
CLANG_TIDY_YML,
32+
CLANG_TIDY_STDOUT,
33+
CHANGED_FILES_JSON,
2934
log_response_msg,
3035
range_of_changed_lines,
3136
assemble_version_exec,
@@ -187,7 +192,8 @@ def filter_out_non_source_files(
187192
Globals.FILES = files
188193
if not IS_ON_RUNNER: # if not executed on a github runner
189194
# dump altered json of changed files
190-
Path(".changed_files.json").write_text(
195+
CACHE_PATH.mkdir(exist_ok=True)
196+
CHANGED_FILES_JSON.write_text(
191197
json.dumps(Globals.FILES, indent=2),
192198
encoding="utf-8",
193199
)
@@ -295,14 +301,15 @@ def run_clang_tidy(
295301
296302
cpp-linter --extra-arg=-std=c++14 --extra-arg=-Wall
297303
"""
304+
CACHE_PATH.mkdir(exist_ok=True)
298305
if checks == "-*": # if all checks are disabled, then clang-tidy is skipped
299306
# clear the clang-tidy output file and exit function
300-
Path("clang_tidy_report.txt").write_bytes(b"")
307+
CLANG_TIDY_STDOUT.write_bytes(b"")
301308
return
302309
filename = PurePath(filename).as_posix()
303310
cmds = [
304311
assemble_version_exec("clang-tidy", version),
305-
"--export-fixes=clang_tidy_output.yml",
312+
f"--export-fixes={str(CLANG_TIDY_YML)}",
306313
]
307314
if checks:
308315
cmds.append(f"-checks={checks}")
@@ -323,12 +330,12 @@ def run_clang_tidy(
323330
cmds.append(f"--extra-arg={extra_arg}")
324331
cmds.append(filename)
325332
# clear yml file's content before running clang-tidy
326-
Path("clang_tidy_output.yml").write_bytes(b"")
333+
CLANG_TIDY_YML.write_bytes(b"")
327334
logger.info('Running "%s"', " ".join(cmds))
328335
results = subprocess.run(cmds, capture_output=True)
329-
Path("clang_tidy_report.txt").write_bytes(results.stdout)
336+
CLANG_TIDY_STDOUT.write_bytes(results.stdout)
330337
logger.debug("Output from clang-tidy:\n%s", results.stdout.decode())
331-
if Path("clang_tidy_output.yml").stat().st_size:
338+
if CLANG_TIDY_YML.stat().st_size:
332339
parse_tidy_suggestions_yml() # get clang-tidy fixes from yml
333340
if results.stderr:
334341
logger.debug(
@@ -353,8 +360,9 @@ def run_clang_format(
353360
:param lines_changed_only: A flag that forces focus on only changes in the event's
354361
diff info.
355362
"""
363+
CACHE_PATH.mkdir(exist_ok=True)
356364
if not style: # if `style` == ""
357-
Path("clang_format_output.xml").write_bytes(b"")
365+
CLANG_FORMAT_XML.write_bytes(b"")
358366
return # clear any previous output and exit
359367
cmds = [
360368
assemble_version_exec("clang-format", version),
@@ -370,7 +378,7 @@ def run_clang_format(
370378
cmds.append(PurePath(filename).as_posix())
371379
logger.info('Running "%s"', " ".join(cmds))
372380
results = subprocess.run(cmds, capture_output=True)
373-
Path("clang_format_output.xml").write_bytes(results.stdout)
381+
CLANG_FORMAT_XML.write_bytes(results.stdout)
374382
if results.returncode:
375383
logger.debug(
376384
"%s raised the following error(s):\n%s", cmds[0], results.stderr.decode()
@@ -394,7 +402,7 @@ def create_comment_body(
394402
`make_annotations()` after `capture_clang_tools_output()` is finished.
395403
"""
396404
ranges = range_of_changed_lines(file_obj, lines_changed_only)
397-
if Path("clang_tidy_report.txt").stat().st_size:
405+
if CLANG_TIDY_STDOUT.exists() and CLANG_TIDY_STDOUT.stat().st_size:
398406
parse_tidy_output() # get clang-tidy fixes from stdout
399407
comment_output = ""
400408
if Globals.PAYLOAD_TIDY:
@@ -409,7 +417,7 @@ def create_comment_body(
409417
Globals.PAYLOAD_TIDY += comment_output
410418
GlobalParser.tidy_notes.clear() # empty list to avoid duplicated output
411419

412-
if Path("clang_format_output.xml").stat().st_size:
420+
if CLANG_FORMAT_XML.exists() and CLANG_FORMAT_XML.stat().st_size:
413421
parse_format_replacements_xml(PurePath(filename).as_posix())
414422
if GlobalParser.format_advice and GlobalParser.format_advice[-1].replaced_lines:
415423
should_comment = lines_changed_only == 0

tests/capture_tools_output/test_tools_output.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,15 @@ def prep_tmp_dir(
9898
repo_cache = tmp_path.parent / repo / commit
9999
repo_cache.mkdir(parents=True, exist_ok=True)
100100
monkeypatch.chdir(str(repo_cache))
101-
filter_out_non_source_files(["c", "h"], [".github"], [], lines_changed_only)
101+
filter_out_non_source_files(
102+
["c", "h", "hpp", "cpp"], [".github"], [], lines_changed_only
103+
)
102104
cpp_linter.run.verify_files_are_present()
103105
repo_path = tmp_path / repo.split("/")[1]
104106
shutil.copytree(
105107
str(repo_cache),
106108
str(repo_path),
107-
ignore=shutil.ignore_patterns("\\.changed_files.json"),
109+
ignore=shutil.ignore_patterns(f"{cpp_linter.CACHE_PATH}/**"),
108110
)
109111
monkeypatch.chdir(repo_path)
110112

0 commit comments

Comments
 (0)