svn_apply_autoprops.py [plain text]
import fnmatch
import os
import re
import sys
SVN_CONFIG_FILENAME = '$HOME/.subversion/config'
SVN_WC_ADM_DIR_NAME = '.svn'
def get_autoprop_lines(fd):
lines = []
reading_autoprops = 0
re_start_autoprops = re.compile('^\s*\[auto-props\]\s*')
re_end_autoprops = re.compile('^\s*\[\w+\]\s*')
for line in fd.xreadlines():
if reading_autoprops:
if re_end_autoprops.match(line):
reading_autoprops = 0
continue
else:
if re_start_autoprops.match(line):
reading_autoprops = 1
continue
if reading_autoprops:
lines += [line]
return lines
def process_autoprop_lines(lines):
result = []
for line in lines:
try:
(fnmatch, props) = line.split('=', 1)
except ValueError:
continue
fnmatch = fnmatch.strip()
props = props.strip()
props_list = []
for prop in props.split(';'):
prop = prop.strip()
if not len(prop):
continue
try:
(prop_name, prop_value) = prop.split('=', 1)
prop_name = prop_name.strip()
prop_value = prop_value.strip()
except ValueError:
prop_name = prop
prop_value = '*'
if len(prop_name):
props_list += [(prop_name, prop_value)]
result += [(fnmatch, props_list)]
return result
def filter_walk(autoprop_lines, dirname, filenames):
try:
filenames.remove(SVN_WC_ADM_DIR_NAME)
except ValueError:
filenames = []
print "Will not process files in '%s' because it does not have a '%s' " \
"directory." \
% (dirname, SVN_WC_ADM_DIR_NAME)
return
filenames.sort()
for autoprops_line in autoprop_lines:
fnmatch_str = autoprops_line[0]
prop_list = autoprops_line[1]
matching_filenames = fnmatch.filter(filenames, fnmatch_str)
if not matching_filenames:
continue
for prop in prop_list:
command = ['svn', 'propset', prop[0], prop[1]]
for f in matching_filenames:
command += ["%s/%s" % (dirname, f)]
status = os.spawnvp(os.P_WAIT, 'svn', command)
if status:
print 'Command "%s" failed with exit status %s' \
% (command, status)
sys.exit(1)
def main():
config_filename = os.path.expandvars(SVN_CONFIG_FILENAME)
try:
fd = file(config_filename)
except IOError:
print "Cannot open svn configuration file '%s' for reading: %s" \
% (config_filename, sys.exc_value.strerror)
autoprop_lines = get_autoprop_lines(fd)
fd.close()
autoprop_lines = process_autoprop_lines(autoprop_lines)
os.path.walk('.', filter_walk, autoprop_lines)
if __name__ == '__main__':
sys.exit(main())