/usr/share/subversion/hook-scripts/validate-files.py is in subversion-tools 1.9.3-2ubuntu1.1.
This file is owned by root:root, with mode 0o755.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 | #!/usr/bin/python
#
# Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
"""Subversion pre-commit hook script that runs user configured commands
to validate files in the commit and reject the commit if the commands
exit with a non-zero exit code. The script expects a validate-files.conf
file placed in the conf dir under the repo the commit is for."""
import sys
import os
import subprocess
import fnmatch
# Deal with the rename of ConfigParser to configparser in Python3
try:
# Python >= 3.0
import configparser
except ImportError:
# Python < 3.0
import ConfigParser as configparser
class Config(configparser.SafeConfigParser):
"""Superclass of SafeConfigParser with some customizations
for this script"""
def optionxform(self, option):
"""Redefine optionxform so option names are case sensitive"""
return option
def getlist(self, section, option):
"""Returns value of option as a list using whitespace to
split entries"""
value = self.get(section, option)
if value:
return value.split()
else:
return None
def get_matching_rules(self, repo):
"""Return list of unique rules names that apply to a given repo"""
rules = {}
for option in self.options('repositories'):
if fnmatch.fnmatch(repo, option):
for rule in self.getlist('repositories', option):
rules[rule] = True
return rules.keys()
def get_rule_section_name(self, rule):
"""Given a rule name provide the section name it is defined in."""
return 'rule:%s' % (rule)
class Commands:
"""Class to handle logic of running commands"""
def __init__(self, config):
self.config = config
def svnlook_changed(self, repo, txn):
"""Provide list of files changed in txn of repo"""
svnlook = self.config.get('DEFAULT', 'svnlook')
cmd = "'%s' changed -t '%s' '%s'" % (svnlook, txn, repo)
p = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
changed = []
while True:
line = p.stdout.readline()
if not line:
break
line = line.decode().strip()
text_mod = line[0:1]
# Only if the contents of the file changed (by addition or update)
# directories always end in / in the svnlook changed output
if line[-1] != "/" and (text_mod == "A" or text_mod == "U"):
changed.append(line[4:])
# wait on the command to finish so we can get the
# returncode/stderr output
data = p.communicate()
if p.returncode != 0:
sys.stderr.write(data[1].decode())
sys.exit(2)
return changed
def user_command(self, section, repo, txn, fn):
""" Run the command defined for a given section.
Replaces $REPO, $TXN and $FILE with the repo, txn and fn arguments
in the defined command.
Returns a tuple of the exit code and the stderr output of the command"""
cmd = self.config.get(section, 'command')
cmd_env = os.environ.copy()
cmd_env['REPO'] = repo
cmd_env['TXN'] = txn
cmd_env['FILE'] = fn
p = subprocess.Popen(cmd, shell=True, env=cmd_env, stderr=subprocess.PIPE)
data = p.communicate()
return (p.returncode, data[1].decode())
def main(repo, txn):
exitcode = 0
config = Config()
config.read(os.path.join(repo, 'conf', 'validate-files.conf'))
commands = Commands(config)
rules = config.get_matching_rules(repo)
# no matching rules so nothing to do
if len(rules) == 0:
sys.exit(0)
changed = commands.svnlook_changed(repo, txn)
# this shouldn't ever happen
if len(changed) == 0:
sys.exit(0)
for rule in rules:
section = config.get_rule_section_name(rule)
pattern = config.get(section, 'pattern')
# skip leading slashes if present in the pattern
if pattern[0] == '/': pattern = pattern[1:]
for fn in fnmatch.filter(changed, pattern):
(returncode, err_mesg) = commands.user_command(section, repo,
txn, fn)
if returncode != 0:
sys.stderr.write(
"\nError validating file '%s' with rule '%s' " \
"(exit code %d):\n" % (fn, rule, returncode))
sys.stderr.write(err_mesg)
exitcode = 1
return exitcode
if __name__ == "__main__":
if len(sys.argv) != 3:
sys.stderr.write("invalid args\n")
sys.exit(0)
try:
sys.exit(main(sys.argv[1], sys.argv[2]))
except configparser.Error as e:
sys.stderr.write("Error with the validate-files.conf: %s\n" % e)
sys.exit(2)
|