From d37b4a637eb364a42b7eceaaf208e76a791ccf43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hunor=20Csomort=C3=A1ni?= Date: Fri, 2 Dec 2022 16:05:22 +0100 Subject: check-style: Use run() instead of Popen() The Python docs recommends using run() for all use-cases it can handle: https://docs.python.org/3/library/subprocess.html#using-the-subprocess-module run() waits for the subprocess started to complete, so it's not necessary to use wait() and communicate() anymore. This simplifies the script. Previously running "check-style.py -r" after each commit in an interactive rebase failed, b/c the script did not wait for the amend command to complete. Using run() instead of Popen() solves this issue. --- check-style.py | 39 ++++++++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 13 deletions(-) (limited to 'check-style.py') diff --git a/check-style.py b/check-style.py index bae6fa4ed..955f478ec 100755 --- a/check-style.py +++ b/check-style.py @@ -11,9 +11,13 @@ import tempfile uncrustify_cfg = 'utils/uncrustify.cfg' def run_diff(sha): - proc = subprocess.Popen(["git", "diff", "-U0", "--function-context", sha, "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - files = proc.stdout.read().strip().decode('utf-8') - return files.split('\n') + proc = subprocess.run( + ["git", "diff", "-U0", "--function-context", sha, "HEAD"], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + encoding="utf-8", + ) + return proc.stdout.strip().splitlines() def find_chunks(diff): file_entry_re = re.compile('^\+\+\+ b/(.*)$') @@ -76,9 +80,11 @@ def reformat_chunks(chunks, rewrite): tmp = create_temp_file(chunk['file'], chunk['start'], chunk['end']) # uncrustify chunk - proc = subprocess.Popen(["uncrustify", "-c", uncrustify_cfg, "-f", tmp.name], stdout=subprocess.PIPE) - reindented = proc.stdout.readlines() - proc.wait() + proc = subprocess.run( + ["uncrustify", "-c", uncrustify_cfg, "-f", tmp.name], + stdout=subprocess.PIPE, + ) + reindented = proc.stdout.splitlines(keepends=True) if proc.returncode != 0: continue @@ -89,18 +95,25 @@ def reformat_chunks(chunks, rewrite): if dry_run is True: # Show changes - proc = subprocess.Popen(["diff", "-up", "--color=always", chunk['file'], formatted.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - diff = proc.stdout.read().decode('utf-8') + proc = subprocess.run( + ["diff", "-up", "--color=always", chunk['file'], formatted.name], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + encoding="utf-8", + ) + diff = proc.stdout if diff != '': output = re.sub('\t', '↦\t', diff) print(output) changed = True else: # Apply changes - diff = subprocess.Popen(["diff", "-up", chunk['file'], formatted.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) - patch = subprocess.Popen(["patch", chunk['file']], stdin=diff.stdout) - diff.stdout.close() - patch.communicate() + diff = subprocess.run( + ["diff", "-up", chunk['file'], formatted.name], + stdout=subprocess.PIPE, + stderr=subprocess.STDOUT, + ) + patch = subprocess.run(["patch", chunk['file']], input=diff.stdout) formatted.close() @@ -130,7 +143,7 @@ chunks = find_chunks(diff) changed = reformat_chunks(chunks, rewrite) if dry_run is not True and rewrite is True: - proc = subprocess.Popen(["git", "commit", "--all", "--amend", "-C", "HEAD"], stdout=subprocess.DEVNULL) + subprocess.run(["git", "commit", "--all", "--amend", "-C", "HEAD"], stdout=subprocess.DEVNULL) os._exit(0) elif dry_run is True and changed is True: print ("\nIssue the following command in your local tree to apply the suggested changes (needs uncrustify installed):\n\n $ git rebase origin/main --exec \"./check-style.py -r\" \n") -- cgit v1.2.1