#!/usr/bin/python " SVN hook that performs some validations against styleguide. " from stylecheck import * def command_output(cmd): " Capture a command's standard output. " import subprocess return subprocess.Popen( cmd.split(), stdout=subprocess.PIPE).communicate()[0] def files_changed(look_cmd): """ List the files added or updated by this transaction. "svnlook changed" gives output like: U trunk/file1.cpp A trunk/file2.cpp """ def filename(line): return line[4:] def added_or_updated(line): return line and line[0] in ("A", "U") return [ filename(line) for line in command_output(look_cmd % "changed").split("\n") if added_or_updated(line)] def file_contents(filename, look_cmd): " Return a file's contents for this transaction. " return command_output( "%s %s" % (look_cmd % "cat", filename)) #def file_invalid(filename, look_cmd): # " Return True if this version of the file contains anything not styleguidy. " # data = file_contents(filename, look_cmd) # return (re.search("[A-Za-z0-9]\(", data) != None) or \ # (re.search(".{80}$", data, re.M) != None) def doChecks(look_cmd): def is_ada_file(fname): import os return os.path.splitext(fname)[1] in [".adb",".ads"] Result = 0 files = [ ff for ff in files_changed(look_cmd) if is_ada_file(ff)] for file in files: print "Checking " + file fileResult = doCheckFile(file_contents(file, look_cmd)) if fileResult != True: sys.stderr.write(file + ": " + fileResult) Result = 1 return Result def main(): usage = """usage: %prog REPOS TXN Run pre-commit options on a repository transaction.""" from optparse import OptionParser parser = OptionParser(usage=usage) parser.add_option("-r", "--revision", help="Test mode. TXN actually refers to a revision.", action="store_true", default=False) errors = 0 try: (opts, (repos, txn_or_rvn)) = parser.parse_args() look_opt = ("--transaction", "--revision")[opts.revision] look_cmd = "svnlook %s %s %s %s" % ( "%s", repos, look_opt, txn_or_rvn) errors += doChecks(look_cmd) except: parser.print_help() errors += 1 return errors if __name__ == "__main__": import sys sys.exit(main())