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:
parent
72a1791372
commit
ce4e58d118
1 changed files with 26 additions and 13 deletions
|
@ -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")
|
||||
|
|
Loading…
Reference in a new issue