1
0
Fork 0

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>
This commit is contained in:
Hunor Csomortáni 2022-12-02 16:05:22 +01:00
parent 72a1791372
commit ce4e58d118

View file

@ -11,9 +11,13 @@ import tempfile
uncrustify_cfg = 'tools/uncrustify.cfg' uncrustify_cfg = 'tools/uncrustify.cfg'
def run_diff(sha): def run_diff(sha):
proc = subprocess.Popen(["git", "diff", "-U0", "--function-context", sha, "HEAD"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc = subprocess.run(
files = proc.stdout.read().strip().decode('utf-8') ["git", "diff", "-U0", "--function-context", sha, "HEAD"],
return files.split('\n') stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
return proc.stdout.strip().splitlines()
def find_chunks(diff): def find_chunks(diff):
file_entry_re = re.compile('^\+\+\+ b/(.*)$') 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']) tmp = create_temp_file(chunk['file'], chunk['start'], chunk['end'])
# uncrustify chunk # uncrustify chunk
proc = subprocess.Popen(["uncrustify", "-c", uncrustify_cfg, "-f", tmp.name], stdout=subprocess.PIPE) proc = subprocess.run(
reindented = proc.stdout.readlines() ["uncrustify", "-c", uncrustify_cfg, "-f", tmp.name],
proc.wait() stdout=subprocess.PIPE,
)
reindented = proc.stdout.splitlines(keepends=True)
if proc.returncode != 0: if proc.returncode != 0:
continue continue
@ -89,18 +95,25 @@ def reformat_chunks(chunks, rewrite):
if dry_run is True: if dry_run is True:
# Show changes # Show changes
proc = subprocess.Popen(["diff", "-up", "--color=always", chunk['file'], formatted.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) proc = subprocess.run(
diff = proc.stdout.read().decode('utf-8') ["diff", "-up", "--color=always", chunk['file'], formatted.name],
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
encoding="utf-8",
)
diff = proc.stdout
if diff != '': if diff != '':
output = re.sub('\t', '\t', diff) output = re.sub('\t', '\t', diff)
print(output) print(output)
changed = True changed = True
else: else:
# Apply changes # Apply changes
diff = subprocess.Popen(["diff", "-up", chunk['file'], formatted.name], stdout=subprocess.PIPE, stderr=subprocess.STDOUT) diff = subprocess.run(
patch = subprocess.Popen(["patch", chunk['file']], stdin=diff.stdout) ["diff", "-up", chunk['file'], formatted.name],
diff.stdout.close() stdout=subprocess.PIPE,
patch.communicate() stderr=subprocess.STDOUT,
)
patch = subprocess.run(["patch", chunk['file']], input=diff.stdout)
formatted.close() formatted.close()
@ -130,7 +143,7 @@ chunks = find_chunks(diff)
changed = reformat_chunks(chunks, rewrite) changed = reformat_chunks(chunks, rewrite)
if dry_run is not True and rewrite is True: 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) os._exit(0)
elif dry_run is True and changed is True: 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") 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")