Home > Python, Sysadmin > Automated Ignore Files with Subversion

Automated Ignore Files with Subversion

We always have tons of garbage files lying around that we don’t want going into version control. Subversion’s svn:ignore property doesn’t have the best documentation around. So, in order help out, here are two little scripts that work in tandem to ignore whatever you want.

The easiest way is to place .svnignore files in all of your directories that you wish to ignore files in. This file has a list of patterns or file names, one on each line, that should be ignored in this directory.

For example, to ignore foo.py and bar.py the file would have something like this:

   foo.py
   bar.py

Now, what if we have a set of files that we want to ignore across a project? The easiest way is to write scripts that automatically manage these .svnignore files.

In one project, we have template directories named ‘t’ with compiled cheetah files that we do not want in subversion. However, we need the __init__.py files in those directories to be in subversion.

Here is a python script to manage those ignore files:

#!/usr/bin/env python
""" Adds all python files in directories named t except
for __init__.py to the .svnignore files in those
directories recursively from the current directory.
"""
import os
import glob
 
for root, dirs, files in os.walk(os.curdir):
    if os.path.basename(root) <> 't':
        continue
    files = glob.glob("%s/*.py" % root)
    files = [os.path.basename(f) for f in files]
    files = [f for f in files if f != '__init__.py']
    ignore_path = os.path.join(root, '.svnignore')
    if os.path.exists(ignore_path):
        igh = open(ignore_path)
        ignores = dict([(f.strip(), True) for f in igh])
        igh.close()
    else:
        ignores = {}
    ignores.update(dict([(f, True) for f in files]))
 
    igh = open(ignore_path, 'w')
    for ignore in ignores.keys():
        igh.write("%s\n" % ignore)
    igh.close()

If a ignore file exists, we pull it in and update it with what we want to ignore. It’s always important that scripts do not break any previous work. So if I manually put an entry in the .svnignore file, the script better not erase it.

Alright, so there we go. We now have ignore files everywhere. The other piece of the puzzle is a script that uses these ignore files and sets the svn properties on the directories.

So here is the last piece of the puzzle:

#!/usr/bin/env python
""" Sets the contents of all .svnignore files to svn:ignore properties
recursively in the current directory. Also attemps to add the
.svnignore file to subversion.
"""
import os
for root, dirs, files in os.walk(os.curdir):
    if '.svnignore' not in files:
        continue
    path = os.path.join(root, '.svnignore')
    os.system('svn propset svn:ignore -F "%s" "%s"' % (path, root))
    os.system('svn add "%s"' % path)
Categories: Python, Sysadmin Tags:
  1. No comments yet.
  1. No trackbacks yet.