summaryrefslogtreecommitdiff
path: root/check-style.py
diff options
context:
space:
mode:
authorHunor Csomortáni <csomh@redhat.com>2022-12-02 16:05:22 +0100
committerCarlos Garnacho <carlosg@gnome.org>2022-12-07 12:29:23 +0100
commitd37b4a637eb364a42b7eceaaf208e76a791ccf43 (patch)
tree2f1284b9db082c2ccec46ec21c352564038bc241 /check-style.py
parent655deba98917d2ff4498eda261861112d6c1d672 (diff)
downloadtracker-d37b4a637eb364a42b7eceaaf208e76a791ccf43.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.
Diffstat (limited to 'check-style.py')
-rwxr-xr-xcheck-style.py39
1 files changed, 26 insertions, 13 deletions
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")