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

Commit 76356d4

Browse files
ulfalizergalak
authored andcommitted
check_compliance.py: Add helper for formatting commands as strings
The " ".join(shlex.quote(word) for word in cmd) pattern was starting to get copied in a bunch of places. Add a helper for it. Piggyback a small improvement to the Pylint test: I discovered earlier that FileNotFoundError is also generated when the cwd= passed to subprocess.Popen() doesn't exists, giving a really confusing error in that case. Just remove the FileNotFoundError check. It's a subclass of OSError, so it'll still be caught. Signed-off-by: Ulf Magnusson <[email protected]>
1 parent 9bd5767 commit 76356d4

File tree

1 file changed

+15
-12
lines changed

1 file changed

+15
-12
lines changed

scripts/check_compliance.py

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -39,25 +39,24 @@ def git(*args, cwd=None):
3939
# directory).
4040

4141
git_cmd = ("git",) + args
42-
git_cmd_s = " ".join(shlex.quote(word) for word in git_cmd) # For errors
43-
4442
try:
4543
git_process = subprocess.Popen(
4644
git_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE, cwd=cwd)
4745
except OSError as e:
48-
err("failed to run '{}': {}".format(git_cmd_s, e))
46+
err(f"failed to run '{cmd2str(git_cmd)}': {e}")
4947

5048
stdout, stderr = git_process.communicate()
5149
stdout = stdout.decode("utf-8")
5250
stderr = stderr.decode("utf-8")
5351
if git_process.returncode or stderr:
54-
err("""\
55-
'{}' exited with status {} (note: output on stderr also counts as a failure).
52+
err(f"""\
53+
'{cmd2str(git_cmd)}' exited with status {git_process.returncode} and/or wrote
54+
to stderr.
5655
5756
==stdout==
58-
{}
57+
{stdout}
5958
==stderr==
60-
{}""".format(git_cmd_s, git_process.returncode, stdout, stderr))
59+
{stderr}""")
6160

6261
return stdout.rstrip()
6362

@@ -872,19 +871,16 @@ def run(self):
872871
return
873872

874873
pylintcmd = ["pylint", "--rcfile=" + pylintrc] + py_files
875-
logger.info(" ".join(shlex.quote(word) for word in pylintcmd))
874+
logger.info(cmd2str(pylintcmd))
876875
try:
877876
# Run pylint on added/modified Python files
878877
process = subprocess.Popen(
879878
pylintcmd,
880879
stdout=subprocess.PIPE,
881880
stderr=subprocess.PIPE,
882881
cwd=GIT_TOP)
883-
except FileNotFoundError:
884-
self.error("pylint not found. Check that the directory it's in is "
885-
"listed in the PATH environment variable.")
886882
except OSError as e:
887-
self.error("Failed to run pylint: " + str(e))
883+
self.error(f"Failed to run {cmd2str(pylintcmd)}: {e}")
888884

889885
stdout, stderr = process.communicate()
890886
if process.returncode or stderr:
@@ -1422,6 +1418,13 @@ def main():
14221418
sys.exit(n_fails)
14231419

14241420

1421+
def cmd2str(cmd):
1422+
# Formats the command-line arguments in the iterable 'cmd' into a string,
1423+
# for error messages and the like
1424+
1425+
return " ".join(shlex.quote(word) for word in cmd)
1426+
1427+
14251428
def err(msg):
14261429
cmd = sys.argv[0] # Empty if missing
14271430
if cmd:

0 commit comments

Comments
 (0)