Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Tim Peters | d87f81f | 2006-03-09 01:42:24 +0000 | [diff] [blame] | 2 | |
| 3 | """ |
| 4 | SVN helper script. |
| 5 | |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 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. |
Tim Peters | d87f81f | 2006-03-09 01:42:24 +0000 | [diff] [blame] | 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 |
Tim Peters | d87f81f | 2006-03-09 01:42:24 +0000 | [diff] [blame] | 31 | """ |
| 32 | |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 33 | import re |
Tim Peters | d87f81f | 2006-03-09 01:42:24 +0000 | [diff] [blame] | 34 | import os |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 35 | import sys |
| 36 | import subprocess |
| 37 | |
Tim Peters | d87f81f | 2006-03-09 01:42:24 +0000 | [diff] [blame] | 38 | |
Martin v. Löwis | 6d291c1 | 2008-06-14 06:25:37 +0000 | [diff] [blame] | 39 | def propfiles(root, fn): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 40 | default = os.path.join(root, ".svn", "props", fn + ".svn-work") |
Martin v. Löwis | a08702a | 2008-06-14 01:51:58 +0000 | [diff] [blame] | 41 | try: |
| 42 | format = int(open(os.path.join(root, ".svn", "format")).read().strip()) |
| 43 | except IOError: |
Martin v. Löwis | 6d291c1 | 2008-06-14 06:25:37 +0000 | [diff] [blame] | 44 | return [] |
Benjamin Peterson | 4469d0c | 2008-11-30 22:46:23 +0000 | [diff] [blame] | 45 | if format in (8, 9): |
| 46 | # In version 8 and 9, committed props are stored in prop-base, local |
| 47 | # modifications in props |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 48 | return [os.path.join(root, ".svn", "prop-base", fn + ".svn-base"), |
| 49 | os.path.join(root, ".svn", "props", fn + ".svn-work")] |
| 50 | raise ValueError("Unknown repository format") |
| 51 | |
Martin v. Löwis | a08702a | 2008-06-14 01:51:58 +0000 | [diff] [blame] | 52 | |
Martin v. Löwis | 8ff2120 | 2006-03-09 02:20:05 +0000 | [diff] [blame] | 53 | def proplist(root, fn): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 54 | """Return a list of property names for file fn in directory root.""" |
Martin v. Löwis | 8ff2120 | 2006-03-09 02:20:05 +0000 | [diff] [blame] | 55 | result = [] |
Martin v. Löwis | 6d291c1 | 2008-06-14 06:25:37 +0000 | [diff] [blame] | 56 | for path in propfiles(root, fn): |
| 57 | try: |
| 58 | f = open(path) |
| 59 | except IOError: |
| 60 | # no properties file: not under version control, |
| 61 | # or no properties set |
| 62 | continue |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 63 | while True: |
Martin v. Löwis | 6d291c1 | 2008-06-14 06:25:37 +0000 | [diff] [blame] | 64 | # key-value pairs, of the form |
| 65 | # K <length> |
| 66 | # <keyname>NL |
| 67 | # V length |
| 68 | # <value>NL |
| 69 | # END |
| 70 | line = f.readline() |
| 71 | if line.startswith("END"): |
| 72 | break |
| 73 | assert line.startswith("K ") |
| 74 | L = int(line.split()[1]) |
| 75 | key = f.read(L) |
| 76 | result.append(key) |
| 77 | f.readline() |
| 78 | line = f.readline() |
| 79 | assert line.startswith("V ") |
| 80 | L = int(line.split()[1]) |
| 81 | value = f.read(L) |
| 82 | f.readline() |
| 83 | f.close() |
Martin v. Löwis | 8ff2120 | 2006-03-09 02:20:05 +0000 | [diff] [blame] | 84 | return result |
| 85 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 86 | |
| 87 | def set_eol_native(path): |
| 88 | cmd = 'svn propset svn:eol-style native "{}"'.format(path) |
| 89 | propset = subprocess.Popen(cmd, shell=True) |
| 90 | propset.wait() |
| 91 | |
| 92 | |
Thomas Heller | 3b9e9ae | 2006-03-09 18:49:35 +0000 | [diff] [blame] | 93 | possible_text_file = re.compile(r"\.([hc]|py|txt|sln|vcproj)$").search |
Tim Peters | 84457af | 2006-03-09 01:59:27 +0000 | [diff] [blame] | 94 | |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 95 | |
| 96 | def main(): |
| 97 | for arg in sys.argv[1:] or [os.curdir]: |
| 98 | if os.path.isfile(arg): |
| 99 | root, fn = os.path.split(arg) |
Martin v. Löwis | 8ff2120 | 2006-03-09 02:20:05 +0000 | [diff] [blame] | 100 | if 'svn:eol-style' not in proplist(root, fn): |
Florent Xicluna | e4a3380 | 2010-08-09 12:24:20 +0000 | [diff] [blame^] | 101 | set_eol_native(arg) |
| 102 | elif os.path.isdir(arg): |
| 103 | for root, dirs, files in os.walk(arg): |
| 104 | if '.svn' in dirs: |
| 105 | dirs.remove('.svn') |
| 106 | for fn in files: |
| 107 | if possible_text_file(fn): |
| 108 | if 'svn:eol-style' not in proplist(root, fn): |
| 109 | path = os.path.join(root, fn) |
| 110 | set_eol_native(path) |
| 111 | |
| 112 | |
| 113 | if __name__ == '__main__': |
| 114 | main() |