⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enforcer

📁 subversion-1.4.3-1.tar.gz 配置svn的源码
💻
📖 第 1 页 / 共 2 页
字号:
#!/usr/bin/python# -*- coding:utf-8;mode:python;mode:font-lock -*-### Utility for Subversion commit hook scripts# This script enforces certain coding guidelines### Copyright (c) 2005 Wilfredo Sanchez Vega <wsanchez@wsanchez.net>.# All rights reserved.## Permission to use, copy, modify, and distribute this software for any# purpose with or without fee is hereby granted, provided that the above# copyright notice and this permission notice appear in all copies.## THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHORS DISCLAIM ALL# WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED# WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE# AUTHORS BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL# DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR# PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER# TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR# PERFORMANCE OF THIS SOFTWARE.##import sysimport osimport getoptimport popen2## FIXME: Should probably retool this using python bindings, not svnlook#__doc__ = '''Enforcer is a utility which can be used in a Subversion pre-commithook script to enforce various requirements which a repositoryadministrator would like to impose on data coming into the repository.A couple of example scenarios: - In a Java project I work on, we use log4j extensively.  Use of   System.out.println() bypasses the control that we get from log4j,   so we would like to discourage the addition of println calls in our   code.   We want to deny any commits that add a println into the code.  The   world being full of exceptions, we do need a way to allow some uses   of println, so we will allow it if the line of code that calls   println ends in a comment that says it is ok:       System.out.println("No log4j here"); // (authorized)   We also do not (presently) want to refuse a commit to a file which   already has a println in it.  There are too many already in the   code and a given developer may not have time to fix them up before   commiting an unrelated change to a file. - The above project uses WebObjects, and you can enable debugging in   a WebObjects component by turning on the WODebug flag in the   component WOD file.  That is great for debugging, but massively   bloats the log files when the application is deployed.   We want to disable any commit of a file enabling WODebug,   regardless of whether the committer made the change or not; these   have to be cleaned up before any successful commit.What this script does is it uses svnlook to peek into the transactionis progress.  As it sifts through the transaction, it calls out to aset of hooks which allow the repository administrator to examine whatis going on and decide whether it is acceptable.  Hooks may be written(in Python) into a configuration file.  If the hook raises anexception, enforcer will exit with an error status (and presumably thecommit will be denied by th pre-commit hook). The following hooks areavailable: verify_file_added(filename)  - called when a file is added. verify_file_removed(fielname)  - called when a file is removed. verify_file_copied(destination_filename, source_filename)  - called when a file is copied. verify_file_modified(filename)  - called when a file is modified. verify_line_added(filename, line)  - called for each line that is added to a file.    (verify_file_modified() will have been called on the file    beforehand) verify_line_removed(filename, line)  - called for each line that is removed from a file.    (verify_file_modified() will have been called on the file    beforehand) verify_property_line_added(filename, property, line)  - called for each line that is added to a property on a file. verify_property_line_removed(filename, property, line)  - called for each line that is removed from a property on a file.In addition, these functions are available to be called from within ahook routine: open_file(filename)  - Returns an open file-like object from which the data of the given    file (as available in the transaction being processed) can be    read.In our example scenarios, we can deny the addition of println calls byhooking into verify_line_added(): if the file is a Java file, and theadded line calls println, raise an exception.Similarly, we can deny the commit of any WOD file enabling WODebug byhooking into verify_file_modified(): open the file using open_file(),then raise if WODebug is enabled anywhere in the file.Note that verify_file_modified() is called once per modified file,whereas verify_line_added() and verify_line_removed() may each becalled zero or many times for each modified file, depending on thechange.  This makes verify_file_modified() appropriate for checkingthe entire file and the other two appropriate for checking specificchanges to files.These example scenarios are implemented in the provided exampleconfiguration file "enforcer.conf".When writing hooks, it is usually easier to test the hooks on commitedtransactions already in the repository, rather than installing thehook and making commits to test the them.  Enforcer allows you tospecify either a transaction ID (for use in a hook script) or arevision number (for testing).  You can then, for example, find arevision that you would like to have blocked (or not) and test yourhooks against that revision.'''__author__ = "Wilfredo Sanchez Vega <wsanchez@wsanchez.net>"### Handle command line##program     = os.path.split(sys.argv[0])[1]debug       = 0transaction = Nonerevision    = Nonedef usage(e=None):    if e:        print e        print ""    print "usage: %s [options] repository config" % program    print "options:"    print "\t-d, --debug             Print debugging output; use twice for more"    print "\t-r, --revision    rev   Specify revision to check"    print "\t-t, --transaction txn   Specify transaction to check"    print "Exactly one of --revision or --transaction is required"    sys.exit(1)# Read optionstry:    (optargs, args) = getopt.getopt(sys.argv[1:], "dt:r:", ["debug", "transaction=", "revision="])except getopt.GetoptError, e:    usage(e)for optarg in optargs:    (opt, arg) = optarg    if   opt in ("-d", "--debug"      ): debug += 1    elif opt in ("-t", "--transaction"): transaction = arg    elif opt in ("-r", "--revision"   ): revision    = argif transaction and revision:    usage("Cannot specify both transaction and revision to check")if not transaction and not revision:    usage("Must specify transaction or revision to check")if not len(args): usage("No repository")repository = args.pop(0)if not len(args): usage("No config")configuration_filename = args.pop(0)if len(args): usage("Too many arguments")### Validation# All rule enforcement goes in these routines##def open_file(filename):    """    Retrieves the contents of the given file.    """    cat_cmd = [ "svnlook", "cat", None, repository, filename ]    if   transaction: cat_cmd[2] = "--transaction=" + transaction    elif revision:    cat_cmd[2] = "--revision="    + revision    else: raise ValueError("No transaction or revision")    cat_out, cat_in = popen2.popen2(cat_cmd)    cat_in.close()    return cat_outdef verify_file_added(filename):    """    Here we verify file additions which may not meet our requirements.    """    if debug: print "Added file %r" % filename    if configuration.has_key("verify_file_added"):        configuration["verify_file_added"](filename)def verify_file_removed(filename):    """    Here we verify file removals which may not meet our requirements.    """    if debug: print "Removed file %r" % filename    if configuration.has_key("verify_file_removed"):        configuration["verify_file_removed"](filename)def verify_file_copied(destination_filename, source_filename):    """

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -