From 6f74a68f8619442a16b5a5f67d8b5ca34e6b6451 Mon Sep 17 00:00:00 2001 From: Ederson de Souza Date: Fri, 3 Feb 2017 11:15:16 -0800 Subject: [PATCH] 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" ) --- README.md | 4 ++-- examples/arm.py | 4 +--- examples/receiver.py | 13 ++++++++----- examples/sender.py | 18 ++++++++++++------ tools/checkpython | 35 +++++++++++++++++++++++++++++++++++ 5 files changed, 58 insertions(+), 16 deletions(-) create mode 100755 tools/checkpython diff --git a/README.md b/README.md index 0ad5fad1..72798fd2 100644 --- a/README.md +++ b/README.md @@ -58,8 +58,8 @@ connection there will also receive routed packets. ### Contributing ### Pull-requests are accepted on GitHub. Make sure to check coding style with the -provided script in tools/checkpatch, check for memory leaks with valgrind and -test on real hardware. +provided script in tools/checkpatch and tools/checkpython, check for memory leaks +with valgrind and test on real hardware. ### Samples ### diff --git a/examples/arm.py b/examples/arm.py index 896cffb3..74486936 100755 --- a/examples/arm.py +++ b/examples/arm.py @@ -24,7 +24,5 @@ mav = mavutil.mavlink_connection('udpin:' + sys.argv[1]) mav.wait_heartbeat() mav.mav.command_long_send(mav.target_system, mav.target_component, - mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, - 0, - 1, + mavutil.mavlink.MAV_CMD_COMPONENT_ARM_DISARM, 0, 1, 0, 0, 0, 0, 0, 0) diff --git a/examples/receiver.py b/examples/receiver.py index 84748885..3b642ed3 100644 --- a/examples/receiver.py +++ b/examples/receiver.py @@ -25,13 +25,15 @@ if len(sys.argv) != 3: print("Usage: %s " % (sys.argv[0])) - print("Receive mavlink heartbeats on specified interface. Respond with a ping message") + print("Receive mavlink heartbeats on specified interface. " + "Respond with a ping message") quit() srcSystem = int(sys.argv[2]) -mav = mavutil.mavlink_connection('udpin:' + sys.argv[1], source_system = srcSystem) +mav = mavutil.mavlink_connection( + 'udpin:' + sys.argv[1], source_system=srcSystem) -while(True): +while (True): msg = mav.recv_match(blocking=True) print("Message from %d: %s" % (msg.get_srcSystem(), msg)) if msg.target_system == 0: @@ -40,5 +42,6 @@ print("\tMessage sent to me") else: print("\tMessage sent to other") - mav.mav.ping_send(int(time.time() * 1000), msg.seq, msg.get_srcSystem(), msg.get_srcComponent()) - + mav.mav.ping_send( + int(time.time() * 1000), msg.seq, + msg.get_srcSystem(), msg.get_srcComponent()) diff --git a/examples/sender.py b/examples/sender.py index 6d190f7c..133d83b1 100644 --- a/examples/sender.py +++ b/examples/sender.py @@ -26,23 +26,29 @@ import time if len(sys.argv) != 4: - print("Usage: %s " % (sys.argv[0])) - print("Send mavlink pings, using given and , to specified interface") + print("Usage: %s " % + (sys.argv[0])) + print( + "Send mavlink pings, using given and , " + "to specified interface") quit() -mav = mavutil.mavlink_connection('udpout:' + sys.argv[1], source_system = int(sys.argv[2])) +mav = mavutil.mavlink_connection( + 'udpout:' + sys.argv[1], source_system=int(sys.argv[2])) + def pingloop(): i = 0 - while(True): - mav.mav.ping_send(int(time.time() * 1000), i, int(sys.argv[3]), 0) + while (True): + mav.mav.ping_send(int(time.time() * 1000), i, int(sys.argv[3]), 1) i = i + 1 sleep(1) + pingthread = Thread(target=pingloop) pingthread.daemon = True pingthread.start() -while(True): +while (True): msg = mav.recv_match(blocking=True) print("Message from %d: %s" % (msg.get_srcSystem(), msg)) diff --git a/tools/checkpython b/tools/checkpython new file mode 100755 index 00000000..d23d2807 --- /dev/null +++ b/tools/checkpython @@ -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)