From 24882a93248c45b207cf8f4c0c01f5b1cd3c7e3a Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Date: Sat, 16 Mar 2013 00:21:53 +0100 Subject: [PATCH] tool for checking coding style --- pcbnew/scripting/TODO.txt | 11 +++-- tools/checkcoding.py | 91 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 6 deletions(-) create mode 100755 tools/checkcoding.py diff --git a/pcbnew/scripting/TODO.txt b/pcbnew/scripting/TODO.txt index aeeff2cc49..c0f3160f3f 100644 --- a/pcbnew/scripting/TODO.txt +++ b/pcbnew/scripting/TODO.txt @@ -1,12 +1,11 @@ * think about documentation, how to do it -* Action plugins: +* Action plugins: right click hooks, - toolbar hooks, - menu hooks, -* IO plugins + toolbar hooks, + menu hooks, +* IO plugins * better footprint wizard (preview in footprint wizard list) -* fix WX asserts - + diff --git a/tools/checkcoding.py b/tools/checkcoding.py new file mode 100755 index 0000000000..0f6bb59add --- /dev/null +++ b/tools/checkcoding.py @@ -0,0 +1,91 @@ +#!/usr/bin/env python +import subprocess, os, difflib + +EXTENSIONS=["cpp","cxx","h","hpp"] + +# +# Function to call uncrustify, it returns the re-formated code and +# any errors +# + +def uncrustify_file(filename): + args = ("uncrustify", "-c", "uncrustify.cfg", "-f", filename) + popen = subprocess.Popen(args, stdout=subprocess.PIPE,stderr=subprocess.PIPE) + popen.wait() + return [popen.stdout.readlines(),popen.stderr.read()] + + + +# +# This function talks to bzr, and gets the list of modified files +# + +def bzr_modified(): + modifieds = [] + args = ("bzr","status") + popen = subprocess.Popen(args, stdout=subprocess.PIPE) + popen.wait() + output = popen.stdout.readlines() + + in_modifieds = False + for line in output: + line = line.rstrip("\r\n") + if line.endswith(":"): in_modifieds = False + if line.startswith("modified:"): + in_modifieds = True + continue + if in_modifieds: + modifieds.append( line.lstrip("\t ").rstrip("\t ") ) + + return modifieds + +def extension(filename): + return os.path.splitext(filename)[1][1:].strip().lower() + +def read_file(filename): + f = open(filename,'r') + data = f.readlines() + f.close() + return data + +def ask_user(filename): + msg = 'Shall I clean %s ?'%filename + return raw_input("%s (y/N/E) " % msg).lower() + + +modified_files = bzr_modified() + + +for file in modified_files: + + if extension(file) in EXTENSIONS: + + [uncrustified,errors] = uncrustify_file(file) + original = read_file(file) + + if len(errors.split("\n"))>2: + print "There was a problem processing "+file+":"+errors + continue + + if uncrustified==original: + print file + " looks perfect!, well done!" + else: + print "Suggestions for: "+file + + diff = difflib.unified_diff(original,uncrustified,file,file+".uncrustified") + + for line in diff: + print line.rstrip("\r\n") + + reply = ask_user(file) + + if reply in ["y","yes"]: + f = open(file,'w') + for line in uncrustified: + f.write(line) + f.close() + print file + " UPDATED" + + if reply in ["e","ed","edit"]: + os.system("$EDITOR "+file) +