Home | History | Annotate | Download | only in scripts
      1 #! /usr/bin/env python
      2 
      3 """
      4 SVN helper script.
      5 
      6 Try to set the svn:eol-style property to "native" on every .py, .txt, .c and
      7 .h file in the directory tree rooted at the current directory.
      8 
      9 Files with the svn:eol-style property already set (to anything) are skipped.
     10 
     11 svn will itself refuse to set this property on a file that's not under SVN
     12 control, or that has a binary mime-type property set.  This script inherits
     13 that behavior, and passes on whatever warning message the failing "svn
     14 propset" command produces.
     15 
     16 In the Python project, it's safe to invoke this script from the root of
     17 a checkout.
     18 
     19 No output is produced for files that are ignored.  For a file that gets
     20 svn:eol-style set, output looks like:
     21 
     22     property 'svn:eol-style' set on 'Lib\ctypes\__init__.py'
     23 
     24 For a file not under version control:
     25 
     26     svn: warning: 'patch-finalizer.txt' is not under version control
     27 
     28 and for a file with a binary mime-type property:
     29 
     30     svn: File 'Lib\test\test_pep263.py' has binary mime type property
     31 """
     32 
     33 import re
     34 import os
     35 
     36 def propfiles(root, fn):
     37     default = os.path.join(root, ".svn", "props", fn+".svn-work")
     38     try:
     39         format = int(open(os.path.join(root, ".svn", "format")).read().strip())
     40     except IOError:
     41         return []
     42     if format in (8, 9):
     43         # In version 8 and 9, committed props are stored in prop-base, local
     44         # modifications in props
     45         return [os.path.join(root, ".svn", "prop-base", fn+".svn-base"),
     46                 os.path.join(root, ".svn", "props", fn+".svn-work")]
     47     raise ValueError, "Unknown repository format"
     48 
     49 def proplist(root, fn):
     50     "Return a list of property names for file fn in directory root"
     51     result = []
     52     for path in propfiles(root, fn):
     53         try:
     54             f = open(path)
     55         except IOError:
     56             # no properties file: not under version control,
     57             # or no properties set
     58             continue
     59         while 1:
     60             # key-value pairs, of the form
     61             # K <length>
     62             # <keyname>NL
     63             # V length
     64             # <value>NL
     65             # END
     66             line = f.readline()
     67             if line.startswith("END"):
     68                 break
     69             assert line.startswith("K ")
     70             L = int(line.split()[1])
     71             key = f.read(L)
     72             result.append(key)
     73             f.readline()
     74             line = f.readline()
     75             assert line.startswith("V ")
     76             L = int(line.split()[1])
     77             value = f.read(L)
     78             f.readline()
     79         f.close()
     80     return result
     81 
     82 possible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search
     83 
     84 for root, dirs, files in os.walk('.'):
     85     if '.svn' in dirs:
     86         dirs.remove('.svn')
     87     for fn in files:
     88         if possible_text_file(fn):
     89             if 'svn:eol-style' not in proplist(root, fn):
     90                 path = os.path.join(root, fn)
     91                 os.system('svn propset svn:eol-style native "%s"' % path)
     92