forked from mavlink-router/mavlink-router
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
tools/checkpython: Checking - and formatting - python code
Uses yapf [https://github.com/google/yapf] to format code. Currently, is invoked as a subprocess, but being a Python module itself, that could be changed in future. A helper script gathers modified '.py' files and feeds them to yapf. One can use git range format, such as HEAD~3, to define which patches will have its Python files inspected, but the whole file is reformatted, not only what was changed: that could be improved, as yapf accepts a line range of file to format, but that sounded too much effort for now. Current Python files were reformatted. Manual intervention was done to tweak long strings: breaking them manually and applying format looked better than: print( "Really long string" )
- Loading branch information
1 parent
0c6ca67
commit 6f74a68
Showing
5 changed files
with
58 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
#!/usr/bin/env python | ||
|
||
from __future__ import print_function | ||
|
||
import subprocess | ||
import sys | ||
|
||
|
||
def run_command(cmd): | ||
try: | ||
output = subprocess.check_output( | ||
cmd, stderr=subprocess.STDOUT, shell=True, universal_newlines=True) | ||
return output | ||
|
||
except subprocess.CalledProcessError as e: | ||
print("Could not run command `%s`: %s" % (cmd, e.output)) | ||
return None | ||
|
||
|
||
revision = "" | ||
if len(sys.argv) == 2: | ||
revision = sys.argv[1] | ||
if len(sys.argv) > 2: | ||
print("Usage: %s [revision]") | ||
quit() | ||
|
||
cmd = "git diff --diff-filter=ACMR --oneline --name-only %s -- '*.py'" % revision | ||
file_list = run_command(cmd) | ||
|
||
if len(file_list) == 0: | ||
print("No changes on current working dir and no revision specified. Nothing to do") | ||
else: | ||
# TODO Maybe use as a module, so we can output which files were changed? | ||
cmd = "yapf -i %s" % (file_list.replace("\n", " ")) | ||
run_command(cmd) |