blob: 5864cdcfb48ab6fde6ec5e8f6b5d5e02513644a5 [file] [log] [blame]
Tim Petersd87f81f2006-03-09 01:42:24 +00001#! /usr/bin/env python
2
3"""
4SVN helper script.
5
Tim Peters84457af2006-03-09 01:59:27 +00006Try 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.
Tim Petersd87f81f2006-03-09 01:42:24 +00008
9Files with the svn:eol-style property already set (to anything) are skipped.
10
11svn will itself refuse to set this property on a file that's not under SVN
12control, or that has a binary mime-type property set. This script inherits
13that behavior, and passes on whatever warning message the failing "svn
14propset" command produces.
15
16In the Python project, it's safe to invoke this script from the root of
17a checkout.
18
19No output is produced for files that are ignored. For a file that gets
20svn:eol-style set, output looks like:
21
22 property 'svn:eol-style' set on 'Lib\ctypes\__init__.py'
23
24For a file not under version control:
25
26 svn: warning: 'patch-finalizer.txt' is not under version control
27
28and for a file with a binary mime-type property:
29
30 svn: File 'Lib\test\test_pep263.py' has binary mime type property
Tim Petersd87f81f2006-03-09 01:42:24 +000031"""
32
Tim Peters84457af2006-03-09 01:59:27 +000033import re
Tim Petersd87f81f2006-03-09 01:42:24 +000034import os
35
Martin v. Löwisa08702a2008-06-14 01:51:58 +000036def propfile(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 default
42 # XXX I don't know what version uses what format;
43 # this condition is just anecdotal
44 if format >= 8:
45 return os.path.join(root, ".svn", "prop-base", fn+".svn-base")
46 return default
47
Martin v. Löwis8ff21202006-03-09 02:20:05 +000048def proplist(root, fn):
49 "Return a list of property names for file fn in directory root"
Martin v. Löwisa08702a2008-06-14 01:51:58 +000050 path = propfile(root, fn)
Martin v. Löwis8ff21202006-03-09 02:20:05 +000051 try:
52 f = open(path)
53 except IOError:
54 # no properties file: not under version control
55 return []
56 result = []
57 while 1:
58 # key-value pairs, of the form
59 # K <length>
60 # <keyname>NL
61 # V length
62 # <value>NL
63 # END
64 line = f.readline()
65 if line.startswith("END"):
66 break
67 assert line.startswith("K ")
68 L = int(line.split()[1])
69 key = f.read(L)
70 result.append(key)
71 f.readline()
72 line = f.readline()
73 assert line.startswith("V ")
74 L = int(line.split()[1])
75 value = f.read(L)
76 f.readline()
77 f.close()
78 return result
79
Thomas Heller3b9e9ae2006-03-09 18:49:35 +000080possible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search
Tim Peters84457af2006-03-09 01:59:27 +000081
Tim Petersd87f81f2006-03-09 01:42:24 +000082for root, dirs, files in os.walk('.'):
83 if '.svn' in dirs:
84 dirs.remove('.svn')
85 for fn in files:
Tim Peters84457af2006-03-09 01:59:27 +000086 if possible_text_file(fn):
Martin v. Löwis8ff21202006-03-09 02:20:05 +000087 if 'svn:eol-style' not in proplist(root, fn):
88 path = os.path.join(root, fn)
Tim Petersd87f81f2006-03-09 01:42:24 +000089 os.system('svn propset svn:eol-style native "%s"' % path)