summaryrefslogtreecommitdiff
path: root/Scripts
diff options
context:
space:
mode:
authorErik de Castro Lopo <erikd@mega-nerd.com>2012-01-15 20:08:35 +1100
committerErik de Castro Lopo <erikd@mega-nerd.com>2012-01-15 20:08:35 +1100
commitbb290bb7b9b58d802226e0d043bd8adb1b03a8d7 (patch)
tree24525792b3cf554b0a13b2e892f2853416d23757 /Scripts
parentb98875b191a42bc0780e0878c1c3c43b41fc0e84 (diff)
Add Scripts/ directory.
Diffstat (limited to 'Scripts')
-rwxr-xr-xScripts/cstyle.py143
-rwxr-xr-xScripts/git-pre-commit-hook75
2 files changed, 218 insertions, 0 deletions
diff --git a/Scripts/cstyle.py b/Scripts/cstyle.py
new file mode 100755
index 0000000..79431d4
--- /dev/null
+++ b/Scripts/cstyle.py
@@ -0,0 +1,143 @@
+#!/usr/bin/python
+#
+# Copyright (C) 2005-2012 Erik de Castro Lopo <erikd@mega-nerd.com>
+#
+# Released under the 2 clause BSD license.
+
+import re, string, sys
+
+# Take a line and split it ont the double quoted strings so we can test the
+# non double quotes strings for double spaces.
+def split_string_on_quoted_str (line):
+ for k in range (0, len (line)):
+ if line [k] == '"':
+ start = k
+ for k in range (start + 1, len (line)):
+ if line [k] == '"' and line [k - 1] != '\\':
+ return [line [:start + 1]] + split_string_on_quoted_str (line [k:])
+ return [line]
+
+class cstyle_checker:
+ def __init__ (self):
+ self.error_count = 0
+
+ def get_error_count (self):
+ return self.error_count
+
+ def check_file (self, filename):
+ self.filename = filename
+ self.file = open (filename, "r")
+ self.line_num = 0
+ comment_nest = 0
+
+ self.line_num = 1
+ while 1:
+ line = self.file.readline ()
+ if not line:
+ break
+
+ orig_line = line.rstrip (line)
+ # Delete C++ style comments.
+ line = re.sub ("([ ]{1}|\t*)//.*", '', "".join (split_string_on_quoted_str (line)))
+
+ # Handle comments.
+ open_comment = line.find ('/*')
+ close_comment = line.find ('*/')
+ if comment_nest > 0 and close_comment < 0:
+ line = ""
+ elif open_comment >= 0 and close_comment < 0:
+ line = line [:open_comment]
+ comment_nest += 1
+ elif open_comment < 0 and close_comment >= 0:
+ line = line [close_comment:]
+ comment_nest -= 1
+ elif open_comment >= 0 and close_comment > 0:
+ line = line [:open_comment] + line [close_comment:]
+
+ # Check for errors finding comments.
+ if comment_nest < 0 or comment_nest > 1:
+ print ("Weird")
+ sys.exit (1)
+ elif comment_nest == 0:
+ # If wer're not inside comments, check the line.
+ self.line_checks (line, orig_line)
+ self.line_num += 1
+
+ self.file.close ()
+ self.filename = None
+ return
+
+ def line_checks (self, line, orig_line):
+ # print (line, end = "")
+
+ # Check for error which occur in comments abd code.
+ if re.search ("[ \t]+$", line):
+ self.error ("contains trailing whitespace")
+
+ if line.find (" ") >= 0:
+ self.error ("multiple space instead of tab")
+
+ if re.search ("[^ ];", line):
+ self.error ("missing space before semi-colon")
+
+ # The C #define requires that there be no spaces in the
+ # first argument, remove first part of line.
+ if re.search ("\s*#\s*define\s+", line):
+ line = re.sub ("\s*#\s*define\s+[^\s]+", '', line)
+
+ # Open and close parenthesis.
+ if re.search ("[^\s\(\[\*&']\(", line):
+ self.error ("missing space before open parenthesis")
+ if re.search ("\)-[^>]", line) or re.search ("\)[^,'\s\n\)\]-]", line):
+ self.error ("missing space after close parenthesis")
+
+ # Open and close square brace.
+ if re.search ("[^\s\(\]]\[", line):
+ self.error ("missing space before open square brace")
+ if re.search ("\][^,\)\]\[\s\.-]", line):
+ self.error ("missing space after close square brace")
+
+ if re.search ("[^\s][\*/%+-][=][^\s]", line):
+ self.error ("missing space around [*/%+-][=]")
+
+ if re.search ("[^\s][<>!=^/][=]{1,2}[^\s]", line):
+ self.error ("missing space around comparison")
+
+ if re.search ("[a-zA-Z0-9][<>!=^/]{1,2}[a-zA-Z0-9]", line):
+ if not re.search (".*#include.*[a-zA-Z0-9]/[a-zA-Z]", line):
+ self.error ("missing space around operator")
+
+ if re.search (";[a-zA-Z0-9]", line):
+ self.error ("missing space after semi-colon")
+
+ # Space after comma
+ if re.search (",[^\s\n]", line):
+ self.error ("missing space after comma")
+
+ if re.search ("\s(do|for|if|when)\s.*{$", line):
+ self.error ("trailing open parenthesis should be on the next line")
+
+ return
+
+ def error (self, msg):
+ print ("%s (%d) : %s" % (self.filename, self.line_num, msg))
+ self.error_count += 1
+
+#-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
+
+if len (sys.argv) < 1:
+ print ("Usage : yada yada")
+ sys.exit (1)
+
+# Create a ner cstyle_checker object
+cstyle = cstyle_checker ()
+
+error_count = 0
+for filename in sys.argv [1:]:
+ cstyle.check_file (filename)
+
+if cstyle.get_error_count ():
+ sys.exit (1)
+
+sys.exit (0)
+
diff --git a/Scripts/git-pre-commit-hook b/Scripts/git-pre-commit-hook
new file mode 100755
index 0000000..38cbcd7
--- /dev/null
+++ b/Scripts/git-pre-commit-hook
@@ -0,0 +1,75 @@
+#!/bin/sh
+#
+
+if git rev-parse --verify HEAD >/dev/null 2>&1 ; then
+ against=HEAD
+else
+ # Initial commit: diff against an empty tree object
+ against=4b825dc642cb6eb9a060e54bf8d69288fbee4904
+ fi
+
+files=`git diff-index --name-only --cached $against`
+
+# Redirect output to stderr.
+exec 1>&2
+
+#-------------------------------------------------------------------------------
+# Check the copyright notice of all files to be commited.
+
+user=`git config --global user.email`
+year=`date +"%Y"`
+
+missing_copyright_year=""
+for f in $files ; do
+ if test `head -5 $f | grep -c -i copyright` -gt 0 ; then
+ user_copyright=`grep -i copyright $f | grep $user | grep -c $year`
+ if test $user_copyright -lt 1 ; then
+ missing_copyright_year="$missing_copyright_year $f"
+ fi
+ fi
+ done
+
+if test -n "$missing_copyright_year" ; then
+ echo "Missing current year in the copyright notice of the following files:"
+ for f in $missing_copyright_year ; do
+ echo " $f"
+ done
+ echo "Commit aborted."
+ exit 1
+ fi
+
+#-------------------------------------------------------------------------------
+# Check the formatting of all C files.
+
+cfiles=""
+for f in $files ; do
+ if test `echo $f | grep -c "\.[ch]$"` -gt 0 ; then
+ cfiles="$cfiles $f"
+ fi
+ done
+
+if test -n "$cfiles" ; then
+ Scripts/cstyle.py $cfiles
+ if test $? -ne 0 ; then
+ echo
+ echo "Commit aborted. Fix the above error before trying again."
+ exit 1
+ fi
+ fi
+
+#-------------------------------------------------------------------------------
+# Prevent files with non-ascii filenames from being committed.
+
+if test $(git diff --cached --name-only --diff-filter=A -z $against | LC_ALL=C tr -d '[ -~]\0' | wc -c) != 0 ; then
+ echo "Error: Attempt to add a non-ascii file name."
+ echo
+ echo "This can cause problems if you want to work"
+ echo "with people on other platforms."
+ echo
+ echo "To be portable it is advisable to rename the file ..."
+ echo
+ echo "Commit aborted."
+ exit 1
+ fi
+
+exit 0