diff options
author | Hunor Csomortáni <csomh@redhat.com> | 2022-12-02 16:05:22 +0100 |
---|---|---|
committer | Hunor Csomortáni <csomh@redhat.com> | 2022-12-06 14:08:37 +0100 |
commit | ce4e58d118dae7d52ef375efc48e06e08ab3bfe4 (patch) | |
tree | 09badbf2c33f97828ff02b982ba093931392bdfc | |
parent | 72a1791372ac45f19aa2406fd0670c47adeb1894 (diff) | |
download | mutter-ce4e58d118dae7d52ef375efc48e06e08ab3bfe4.tar.gz |
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.
Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2733>
-rwxr-xr-x | check-style.py | 39 |
1 files changed, 26 insertions, 13 deletions
diff --git a/check-style.py b/check-style.py index 29f719c43..a4daefaa4 100755 --- a/check-style.py +++ b/check-style.py @@ -11,9 +11,13 @@ import tempfile uncrustify_cfg = 'tools/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") |