Skip to content
This repository was archived by the owner on Apr 6, 2022. It is now read-only.

Commit c9837d6

Browse files
ulfalizernashif
authored andcommitted
check_compliance.py: Simplify Kconfig tests with git() helper
Use the git() helper instead instead of subprocess.Popen to get rid of some code duplication. Also add some more comments to the Kconfig tests. Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 77d600c commit c9837d6

File tree

1 file changed

+26
-51
lines changed

1 file changed

+26
-51
lines changed

scripts/check_compliance.py

Lines changed: 26 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -29,17 +29,18 @@
2929
logger = None
3030

3131

32-
def git(*args):
32+
def git(*args, cwd=None):
3333
# Helper for running a Git command. Returns the rstrip()ed stdout output.
3434
# Called like git("diff"). Exits with SystemError (raised by sys.exit()) on
35-
# errors.
35+
# errors. 'cwd' is the working directory to use (default: current
36+
# directory).
3637

3738
git_cmd = ("git",) + args
3839
git_cmd_s = " ".join(shlex.quote(word) for word in git_cmd) # For errors
3940

4041
try:
4142
git_process = subprocess.Popen(
42-
git_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
43+
git_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
4344
except OSError as e:
4445
err("failed to run '{}': {}".format(git_cmd_s, e))
4546

@@ -365,35 +366,22 @@ def check_no_undef_outside_kconfig(self, kconf):
365366
# - {, e.g. for Python scripts ("CONFIG_FOO_{}_BAR".format(...)")
366367
#
367368
# - *, meant for comments like '#endif /* CONFIG_FOO_* */
368-
#
369-
# We skip doc/releases, which often references removed symbols.
370-
371-
# Warning: Needs to work with both --perl-regexp and the 're' module
372-
regex = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{*])"
373369

374-
grep_cmd = ["git", "grep", "--line-number", "-I", "--null",
375-
"--perl-regexp", regex,
376-
"--", ":!doc/releases"]
370+
defined_syms = get_defined_syms(kconf)
377371

378-
grep_process = subprocess.Popen(grep_cmd,
379-
stdout=subprocess.PIPE,
380-
stderr=subprocess.PIPE,
381-
cwd=ZEPHYR_BASE)
372+
# Maps each undefined symbol to a list <filename>:<linenr> strings
373+
undef_to_locs = collections.defaultdict(list)
382374

383-
grep_stdout, grep_stderr = grep_process.communicate()
384-
# Fail if there's anything on stderr too, so that it doesn't get missed
385-
if grep_process.returncode or grep_stderr:
386-
self.error("'{}' failed with exit code {} (while searching for "
387-
"Kconfig symbol references)\n\nstdout:\n{}\n\n"
388-
"stderr:\n{}"
389-
.format(" ".join(grep_cmd), grep_process.returncode,
390-
grep_stdout, grep_stderr))
375+
# Warning: Needs to work with both --perl-regexp and the 're' module
376+
regex = r"\bCONFIG_[A-Z0-9_]+\b(?!\s*##|[$@{*])"
391377

392-
defined_syms = get_defined_syms(kconf)
393-
undef_to_locs = collections.defaultdict(list)
378+
# Skip doc/releases, which often references removed symbols
379+
grep_stdout = git("grep", "--line-number", "-I", "--null",
380+
"--perl-regexp", regex, "--", ":!/doc/releases",
381+
cwd=ZEPHYR_BASE)
394382

395383
# splitlines() supports various line terminators
396-
for grep_line in grep_stdout.decode("utf-8").splitlines():
384+
for grep_line in grep_stdout.splitlines():
397385
path, lineno, line = grep_line.split("\0")
398386

399387
# Extract symbol references (might be more than one) within the
@@ -433,37 +421,24 @@ def check_no_undef_outside_kconfig(self, kconf):
433421

434422

435423
def get_defined_syms(kconf):
436-
# Returns all defined Kconfig symbols. This is complicated by samples and
437-
# tests defining their own Kconfig trees. For those, just grep for
438-
# 'config FOO' to find definitions. Doing it "properly" with Kconfiglib is
439-
# still useful for the main tree, because some symbols are defined using
440-
# preprocessor macros.
424+
# Returns a set() with the names of all defined Kconfig symbols (with no
425+
# 'CONFIG_' prefix). This is complicated by samples and tests defining
426+
# their own Kconfig trees. For those, just grep for 'config FOO' to find
427+
# definitions. Doing it "properly" with Kconfiglib is still useful for the
428+
# main tree, because some symbols are defined using preprocessor macros.
441429

442430
# Warning: Needs to work with both --extended-regexp and the 're' module
443-
regex = "^\s*(menu)?config\s*([A-Z0-9_]+)\s*(#|$)"
444-
445-
grep_cmd = ["git", "grep", "-I", "-h", "--extended-regexp", regex,
446-
"--", ":samples", ":tests"]
431+
regex = r"^\s*(menu)?config\s*([A-Z0-9_]+)\s*(#|$)"
447432

448-
grep_process = subprocess.Popen(grep_cmd,
449-
stdout=subprocess.PIPE,
450-
stderr=subprocess.PIPE,
451-
cwd=ZEPHYR_BASE)
452-
453-
grep_stdout, grep_stderr = grep_process.communicate()
454-
# Fail if there's anything on stderr too, so that it doesn't get missed
455-
if grep_process.returncode or grep_stderr:
456-
self.error("'{}' failed with exit code {} (while searching for "
457-
"Kconfig symbol definitions)\n\nstdout:\n{}\n\n"
458-
"stderr:\n{}"
459-
.format(" ".join(grep_cmd), grep_process.returncode,
460-
grep_stdout, grep_stderr))
433+
# Grep samples/ and tests/ for symbol definitions
434+
grep_stdout = git("grep", "-I", "-h", "--extended-regexp", regex, "--",
435+
":samples", ":tests")
461436

437+
# Start with the symbols from the main Kconfig tree in 'res'
462438
res = set(sym.name for sym in kconf.unique_defined_syms)
463-
464-
for def_line in grep_stdout.decode("utf-8").splitlines():
439+
# Add the grepped definitions from samples and tests
440+
for def_line in grep_stdout.splitlines():
465441
res.add(re.match(regex, def_line).group(2))
466-
467442
return res
468443

469444

0 commit comments

Comments
 (0)