Skip to content

Commit 07a023d

Browse files
cfriedtkartben
authored andcommitted
scripts: ci: check_compliance: GIT_TOP and ZEPHYR_BASE as paths
Use pathlib.Path objects for GIT_TOP and ZEPHYR_BASE instead of strings. Signed-off-by: Chris Friedt <[email protected]>
1 parent af342e5 commit 07a023d

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

scripts/ci/check_compliance.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def get_files(filter=None, paths=None):
7575
out = git('diff', '--name-only', *filter_arg, COMMIT_RANGE, *paths_arg)
7676
files = out.splitlines()
7777
for file in list(files):
78-
if not os.path.isfile(os.path.join(GIT_TOP, file)):
78+
if not (GIT_TOP / file).exists():
7979
# Drop submodule directories from the list.
8080
files.remove(file)
8181
return files
@@ -207,8 +207,8 @@ class CheckPatch(ComplianceTest):
207207
path_hint = "<git-top>"
208208

209209
def run(self):
210-
checkpatch = os.path.join(ZEPHYR_BASE, 'scripts', 'checkpatch.pl')
211-
if not os.path.exists(checkpatch):
210+
checkpatch = ZEPHYR_BASE / 'scripts' / 'checkpatch.pl'
211+
if not checkpatch.exists():
212212
self.skip(f'{checkpatch} not found')
213213

214214
# check for Perl installation on Windows
@@ -411,8 +411,7 @@ def get_modules(self, modules_file, sysbuild_modules_file, settings_file):
411411
"""
412412
# Invoke the script directly using the Python executable since this is
413413
# not a module nor a pip-installed Python utility
414-
zephyr_module_path = os.path.join(ZEPHYR_BASE, "scripts",
415-
"zephyr_module.py")
414+
zephyr_module_path = ZEPHYR_BASE / "scripts" / "zephyr_module.py"
416415
cmd = [sys.executable, zephyr_module_path,
417416
'--kconfig-out', modules_file,
418417
'--sysbuild-kconfig-out', sysbuild_modules_file,
@@ -423,9 +422,9 @@ def get_modules(self, modules_file, sysbuild_modules_file, settings_file):
423422
except subprocess.CalledProcessError as ex:
424423
self.error(ex.output.decode("utf-8"))
425424

426-
modules_dir = ZEPHYR_BASE + '/modules'
425+
modules_dir = ZEPHYR_BASE / 'modules'
427426
modules = [name for name in os.listdir(modules_dir) if
428-
os.path.exists(os.path.join(modules_dir, name, 'Kconfig'))]
427+
modules_dir / name / 'Kconfig']
429428

430429
with open(modules_file, 'r') as fp_module_file:
431430
content = fp_module_file.read()
@@ -434,7 +433,7 @@ def get_modules(self, modules_file, sysbuild_modules_file, settings_file):
434433
for module in modules:
435434
fp_module_file.write("ZEPHYR_{}_KCONFIG = {}\n".format(
436435
re.sub('[^a-zA-Z0-9]', '_', module).upper(),
437-
modules_dir + '/' + module + '/Kconfig'
436+
modules_dir / module / 'Kconfig'
438437
))
439438
fp_module_file.write(content)
440439

@@ -468,12 +467,11 @@ def get_kconfig_dts(self, kconfig_dts_file, settings_file):
468467
"""
469468
# Invoke the script directly using the Python executable since this is
470469
# not a module nor a pip-installed Python utility
471-
zephyr_drv_kconfig_path = os.path.join(ZEPHYR_BASE, "scripts", "dts",
472-
"gen_driver_kconfig_dts.py")
470+
zephyr_drv_kconfig_path = ZEPHYR_BASE / "scripts" / "dts" / "gen_driver_kconfig_dts.py"
473471
binding_paths = []
474-
binding_paths.append(os.path.join(ZEPHYR_BASE, "dts", "bindings"))
472+
binding_paths.append(ZEPHYR_BASE / "dts" / "bindings")
475473

476-
dts_root_paths = self.get_module_setting_root('dts', settings_file)
474+
dts_root_paths = get_module_setting_root('dts', settings_file)
477475
for p in dts_root_paths:
478476
binding_paths.append(p / "dts" / "bindings")
479477

@@ -506,10 +504,10 @@ def get_v2_model(self, kconfig_dir, settings_file):
506504
kconfig_sysbuild_file = os.path.join(kconfig_dir, 'boards', 'Kconfig.sysbuild')
507505
kconfig_defconfig_file = os.path.join(kconfig_dir, 'boards', 'Kconfig.defconfig')
508506

509-
board_roots = self.get_module_setting_root('board', settings_file)
510-
board_roots.insert(0, Path(ZEPHYR_BASE))
511-
soc_roots = self.get_module_setting_root('soc', settings_file)
512-
soc_roots.insert(0, Path(ZEPHYR_BASE))
507+
board_roots = get_module_setting_root('board', settings_file)
508+
board_roots.insert(0, ZEPHYR_BASE)
509+
soc_roots = get_module_setting_root('soc', settings_file)
510+
soc_roots.insert(0, ZEPHYR_BASE)
513511
root_args = argparse.Namespace(**{'board_roots': board_roots,
514512
'soc_roots': soc_roots, 'board': None,
515513
'board_dir': []})
@@ -572,7 +570,7 @@ def get_v2_model(self, kconfig_dir, settings_file):
572570

573571
kconfig_file = os.path.join(kconfig_dir, 'arch', 'Kconfig')
574572

575-
root_args = argparse.Namespace(**{'arch_roots': [Path(ZEPHYR_BASE)], 'arch': None})
573+
root_args = argparse.Namespace(**{'arch_roots': [ZEPHYR_BASE], 'arch': None})
576574
v2_archs = list_hardware.find_v2_archs(root_args)
577575

578576
with open(kconfig_file, 'w') as fp:
@@ -586,20 +584,20 @@ def parse_kconfig(self):
586584
"""
587585
# Put the Kconfiglib path first to make sure no local Kconfiglib version is
588586
# used
589-
kconfig_path = os.path.join(ZEPHYR_BASE, "scripts", "kconfig")
590-
if not os.path.exists(kconfig_path):
587+
kconfig_path = ZEPHYR_BASE / "scripts" / "kconfig"
588+
if not kconfig_path.exists():
591589
self.error(kconfig_path + " not found")
592590

593591
kconfiglib_dir = tempfile.mkdtemp(prefix="kconfiglib_")
594592

595-
sys.path.insert(0, kconfig_path)
593+
sys.path.insert(0, str(kconfig_path))
596594
# Import globally so that e.g. kconfiglib.Symbol can be referenced in
597595
# tests
598596
global kconfiglib
599597
import kconfiglib
600598

601599
# Look up Kconfig files relative to ZEPHYR_BASE
602-
os.environ["srctree"] = ZEPHYR_BASE
600+
os.environ["srctree"] = str(ZEPHYR_BASE)
603601

604602
# Parse the entire Kconfig tree, to make sure we see all symbols
605603
os.environ["SOC_DIR"] = "soc/"
@@ -881,7 +879,7 @@ def check_no_undef_within_kconfig(self, kconf):
881879
self.failure(f"Undefined Kconfig symbols:\n\n {undef_ref_warnings}")
882880

883881
def check_soc_name_sync(self, kconf):
884-
root_args = argparse.Namespace(**{'soc_roots': [Path(ZEPHYR_BASE)]})
882+
root_args = argparse.Namespace(**{'soc_roots': [ZEPHYR_BASE]})
885883
v2_systems = list_hardware.find_v2_systems(root_args)
886884

887885
soc_names = {soc.name for soc in v2_systems.get_socs()}
@@ -950,7 +948,7 @@ def check_no_undef_outside_kconfig(self, kconf):
950948
grep_stdout = git("grep", "--line-number", "-I", "--null",
951949
"--perl-regexp", regex, "--", ":!/doc/releases",
952950
":!/doc/security/vulnerabilities.rst",
953-
cwd=Path(GIT_TOP))
951+
cwd=GIT_TOP)
954952

955953
# splitlines() supports various line terminators
956954
for grep_line in grep_stdout.splitlines():
@@ -1247,7 +1245,7 @@ def run(self):
12471245
def check_kconfig_header(self, fname):
12481246
# Checks for a spammy copy-pasted header format
12491247

1250-
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
1248+
with open(GIT_TOP / fname, encoding="utf-8") as f:
12511249
contents = f.read()
12521250

12531251
# 'Kconfig - yada yada' has a copy-pasted redundant filename at the
@@ -1273,7 +1271,7 @@ def check_redundant_zephyr_source(self, fname):
12731271
# Checks for 'source "$(ZEPHYR_BASE)/Kconfig[.zephyr]"', which can be
12741272
# be simplified to 'source "Kconfig[.zephyr]"'
12751273

1276-
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
1274+
with open(GIT_TOP / fname, encoding="utf-8") as f:
12771275
# Look for e.g. rsource as well, for completeness
12781276
match = re.search(
12791277
r'^\s*(?:o|r|or)?source\s*"\$\(?ZEPHYR_BASE\)?/(Kconfig(?:\.zephyr)?)"',
@@ -1288,7 +1286,7 @@ def check_redundant_zephyr_source(self, fname):
12881286
def check_redundant_document_separator(self, fname):
12891287
# Looks for redundant '...' document separators in bindings
12901288

1291-
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
1289+
with open(GIT_TOP / fname, encoding="utf-8") as f:
12921290
if re.search(r"^\.\.\.", f.read(), re.MULTILINE):
12931291
self.failure(f"""\
12941292
Redundant '...' document separator in {fname}. Binding YAML files are never
@@ -1297,7 +1295,7 @@ def check_redundant_document_separator(self, fname):
12971295
def check_source_file(self, fname):
12981296
# Generic nits related to various source files
12991297

1300-
with open(os.path.join(GIT_TOP, fname), encoding="utf-8") as f:
1298+
with open(GIT_TOP / fname, encoding="utf-8") as f:
13011299
contents = f.read()
13021300

13031301
if not contents.endswith("\n"):
@@ -1516,7 +1514,7 @@ def run(self):
15161514
BOARD_SIZE_LIMIT = 100 << 10
15171515

15181516
for file in get_files(filter="d"):
1519-
full_path = os.path.join(GIT_TOP, file)
1517+
full_path = GIT_TOP / file
15201518
mime_type = magic.from_file(full_path, mime=True)
15211519

15221520
if not mime_type.startswith("image/"):
@@ -1597,7 +1595,7 @@ class YAMLLint(ComplianceTest):
15971595
path_hint = "<git-top>"
15981596

15991597
def run(self):
1600-
config_file = os.path.join(ZEPHYR_BASE, ".yamllint")
1598+
config_file = ZEPHYR_BASE / ".yamllint"
16011599

16021600
for file in get_files(filter="d"):
16031601
if Path(file).suffix not in ['.yaml', '.yml']:
@@ -1819,7 +1817,7 @@ def run(self):
18191817
m = magic.Magic(mime=True, mime_encoding=True)
18201818

18211819
for file in get_files(filter="d"):
1822-
full_path = os.path.join(GIT_TOP, file)
1820+
full_path = GIT_TOP / file
18231821
mime_type = m.from_file(full_path)
18241822

18251823
if not mime_type.startswith("text/"):
@@ -1929,11 +1927,12 @@ def _main(args):
19291927

19301928
# Propagate this decision to child processes.
19311929
os.environ['ZEPHYR_BASE'] = ZEPHYR_BASE
1930+
ZEPHYR_BASE = Path(ZEPHYR_BASE)
19321931

19331932
# The absolute path of the top-level git directory. Initialize it here so
19341933
# that issues running Git can be reported to GitHub.
19351934
global GIT_TOP
1936-
GIT_TOP = git("rev-parse", "--show-toplevel")
1935+
GIT_TOP = Path(git("rev-parse", "--show-toplevel"))
19371936

19381937
# The commit range passed in --commit, e.g. "HEAD~3"
19391938
global COMMIT_RANGE

0 commit comments

Comments
 (0)