Created
August 16, 2011 21:41
-
-
Save kunigami/1150253 to your computer and use it in GitHub Desktop.
Checking SVN mime types when adding rather than when committing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
################################################################################ | |
# svn_mime_filter.py | |
# Script to check if any of the file does not match the svn mime type | |
# | |
# TODOs: | |
# 1 - Report only once files that have the same extension | |
################################################################################ | |
import os | |
import re | |
import sys | |
################################################################################ | |
# Read arguments | |
################################################################################ | |
if len(sys.argv) != 2: | |
print 'Usage: python %s <path to root>' % (sys.argv[0]) | |
sys.exit(1) | |
root = sys.argv[1] | |
################################################################################ | |
# Open the configuration file and get all patterns | |
################################################################################ | |
home_path = os.environ['HOME'] | |
print 'home_path: ', home_path | |
config_path = os.path.join(home_path, '.subversion/config') | |
print 'config_path: ', config_path | |
f = open(config_path) | |
patterns = [ ] | |
read_auto_props = False | |
for l in f: | |
l = l.strip().rstrip() | |
# Find the [auto-props] line | |
if l == '[auto-props]': | |
read_auto_props = True | |
elif read_auto_props: | |
# Ignore comments | |
if (not l) or l[0] == '#': | |
continue | |
# Extract the matching pattern (before reaching '=') | |
p = l.split('=')[0].strip() | |
# Convert POSIX RE to python RE | |
p = p.replace('.','\\.') | |
p = p.replace('*', '.*') | |
p = p + '$' | |
patterns.append(p) | |
################################################################################ | |
# Get a list of all files that will be added by svn add | |
################################################################################ | |
def traverse(currdir): | |
ls = os.listdir(currdir) | |
for f in ls: | |
# Complete path | |
nextdir = os.path.join(currdir, f) | |
if os.path.isdir(nextdir): | |
traverse(nextdir) | |
else: # File | |
files.append([f, nextdir]) | |
files = [ ] | |
traverse(root) | |
################################################################################ | |
# For each file to be added, match it against all current patterns | |
################################################################################ | |
for f in files: | |
#print 'analysing file:', f | |
found = False | |
for p in patterns: | |
res = re.match(p, f[0]) | |
if res != None: | |
found = True | |
#print 'matched', p | |
break | |
if not found: | |
print 'file not found: ', f[1] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment