summaryrefslogtreecommitdiff
path: root/lint/run_strict_deps.py
diff options
context:
space:
mode:
Diffstat (limited to 'lint/run_strict_deps.py')
-rwxr-xr-xlint/run_strict_deps.py32
1 files changed, 20 insertions, 12 deletions
diff --git a/lint/run_strict_deps.py b/lint/run_strict_deps.py
index c963f3cd..7f2ddcf4 100755
--- a/lint/run_strict_deps.py
+++ b/lint/run_strict_deps.py
@@ -16,25 +16,33 @@
import json
import os
-import json
-import os
import sys
+from typing import List
-def dump_meta(src, cmd):
+
+def dump_meta(src: str, cmd: List[str]) -> None:
+ """Dump linter action metadata for further analysis."""
OUT = os.environ.get("OUT")
if OUT:
with open(os.path.join(OUT, "config.json"), "w") as f:
json.dump({"src": src, "cmd": cmd}, f)
-def run_lint(src, cmd):
+def run_lint(src: str, cmd: List[str]) -> int:
+ """Run the lint command for the specified source file."""
dump_meta(src, cmd)
- with open(os.environ.get("META")) as f:
+ META = os.environ.get("META")
+ if META is None:
+ print("Failed to get META", file=sys.stderr)
+ return 1
+
+ direct: List[str] = []
+ with open(META) as f:
direct = json.load(f)["direct deps artifact names"]
- include_dirs = []
+ include_dirs: List[str] = []
for i in range(len(cmd)):
if cmd[i] in ["-I", "-isystem"]:
include_dirs += [cmd[i + 1]]
@@ -42,16 +50,16 @@ def run_lint(src, cmd):
with open(src) as f:
lines = f.read().splitlines()
- failed = False
+ failed: bool = False
- def include_covered(include_path):
+ def include_covered(include_path: str) -> bool:
for d in direct:
rel_path = os.path.relpath(include_path, d)
if not rel_path.startswith('../'):
return True
return False
- def handle_resolved_include(i, to_include, resolved):
+ def handle_resolved_include(i: int, resolved: str) -> None:
nonlocal failed
if not include_covered(resolved):
failed = True
@@ -60,11 +68,11 @@ def run_lint(src, cmd):
" ---> including %r which is only provided by an indirect dependency"
% (resolved, ))
- def handle_include(i, to_include):
+ def handle_include(i: int, to_include: str) -> None:
for d in include_dirs:
candidate = os.path.join(d, to_include)
if os.path.exists(candidate):
- handle_resolved_include(i, to_include, candidate)
+ handle_resolved_include(i, candidate)
for i in range(len(lines)):
to_include = None
@@ -72,7 +80,7 @@ def run_lint(src, cmd):
to_include = lines[i].split('"')[1]
if lines[i].startswith('#include <'):
to_include = lines[i].split('<', 1)[1].split('>')[0]
- if to_include:
+ if to_include: # if non-empty string
handle_include(i, to_include)
return 1 if failed else 0