]>
Commit | Line | Data |
---|---|---|
729e4ab9 A |
1 | #! /usr/bin/python |
2 | ||
f3c0d7a5 A |
3 | # Copyright (C) 2016 and later: Unicode, Inc. and others. |
4 | # License & terms of use: http://www.unicode.org/copyright.html | |
5 | ||
4388f060 | 6 | # Copyright (C) 2009-2011, International Business Machines Corporation, Google and Others. |
729e4ab9 A |
7 | # All rights reserved. |
8 | ||
9 | # | |
10 | # Script to check and fix svn property settings for ICU source files. | |
11 | # Also check for the correct line endings on files with svn:eol-style = native | |
12 | # | |
13 | # THIS SCRIPT DOES NOT WORK ON WINDOWS | |
14 | # It only works correctly on platforms where the native line ending is a plain \n | |
15 | # | |
16 | # usage: | |
17 | # icu-svnprops-check.py [options] | |
18 | # | |
19 | # options: | |
20 | # -f | --fix Fix any problems that are found | |
21 | # -h | --help Print a usage line and exit. | |
22 | # | |
23 | # The tool operates recursively on the directory from which it is run. | |
24 | # Only files from the svn repository are checked. | |
25 | # No changes are made to the repository; only the working copy will be altered. | |
26 | ||
27 | import sys | |
28 | import os | |
29 | import os.path | |
30 | import re | |
31 | import getopt | |
32 | ||
729e4ab9 A |
33 | |
34 | # file_types: The parsed form of the svn auto-props specification. | |
35 | # A list of file types - .cc, .cpp, .txt, etc. | |
36 | # each element is a [type, proplist] | |
37 | # "type" is a regular expression string that will match a file name | |
38 | # prop list is another list, one element per property. | |
39 | # Each property item is a two element list, [prop name, prop value] | |
40 | file_types = list() | |
41 | ||
42 | def parse_auto_props(): | |
43 | aprops = svn_auto_props.splitlines() | |
44 | for propline in aprops: | |
45 | if re.match("\s*(#.*)?$", propline): # Match comment and blank lines | |
46 | continue | |
47 | if re.match("\s*\[auto-props\]", propline): # Match the [auto-props] line. | |
48 | continue | |
49 | if not re.match("\s*[^\s]+\s*=", propline): # minimal syntax check for <file-type> = | |
50 | print "Bad line from autoprops definitions: " + propline | |
51 | continue | |
52 | file_type, string_proplist = propline.split("=", 1) | |
53 | ||
54 | #transform the file type expression from autoprops into a normal regular expression. | |
55 | # e.g. "*.cpp" ==> ".*\.cpp$" | |
56 | file_type = file_type.strip() | |
57 | file_type = file_type.replace(".", "\.") | |
58 | file_type = file_type.replace("*", ".*") | |
59 | file_type = file_type + "$" | |
60 | ||
61 | # example string_proplist at this point: " svn:eol-style=native;svn:executable" | |
62 | # split on ';' into a list of properties. The negative lookahead and lookbehind | |
63 | # in the split regexp are to prevent matching on ';;', which is an escaped ';' | |
64 | # within a property value. | |
65 | string_proplist = re.split("(?<!;);(?!;)", string_proplist) | |
66 | proplist = list() | |
67 | for prop in string_proplist: | |
68 | if prop.find("=") >= 0: | |
69 | prop_name, prop_val = prop.split("=", 1) | |
70 | else: | |
71 | # properties with no explicit value, e.g. svn:executable | |
72 | prop_name, prop_val = prop, "" | |
73 | prop_name = prop_name.strip() | |
74 | prop_val = prop_val.strip() | |
75 | # unescape any ";;" in a property value, e.g. the mime-type from | |
76 | # *.java = svn:eol-style=native;svn:mime-type=text/plain;;charset=utf-8 | |
77 | prop_val = prop_val.replace(";;", ";"); | |
78 | proplist.append((prop_name, prop_val)) | |
79 | ||
80 | file_types.append((file_type, proplist)) | |
81 | # print file_types | |
82 | ||
83 | ||
84 | def runCommand(cmd): | |
85 | output_file = os.popen(cmd); | |
86 | output_text = output_file.read(); | |
87 | exit_status = output_file.close(); | |
88 | if exit_status: | |
89 | print >>sys.stderr, '"', cmd, '" failed. Exiting.' | |
90 | sys.exit(exit_status) | |
91 | return output_text | |
92 | ||
f3c0d7a5 | 93 | svn_auto_props = runCommand("svn propget svn:auto-props http://source.icu-project.org/repos/icu") |
729e4ab9 A |
94 | |
95 | def usage(): | |
96 | print "usage: " + sys.argv[0] + " [-f | --fix] [-h | --help]" | |
97 | ||
98 | ||
99 | # | |
f3c0d7a5 | 100 | # UTF-8 file check. For text files with svn:mime-type=text/anything, check the specified charset |
729e4ab9 | 101 | # file_name: name of a text file. |
f3c0d7a5 | 102 | # base_mime_type: svn:mime-type property from the auto-props settings for this file type. |
729e4ab9 | 103 | # actual_mime_type: existing svn:mime-type property value for the file. |
f3c0d7a5 A |
104 | # return: The correct svn:mime-type property value, |
105 | # either the original, if it looks OK, otherwise the value from auto-props | |
729e4ab9 A |
106 | # |
107 | def check_utf8(file_name, base_mime_type, actual_mime_type): | |
108 | ||
729e4ab9 A |
109 | f = open(file_name, 'r') |
110 | bytes = f.read() | |
111 | f.close() | |
f3c0d7a5 | 112 | file_is_utf8 = True |
729e4ab9 A |
113 | try: |
114 | bytes.decode("UTF-8") | |
115 | except UnicodeDecodeError: | |
f3c0d7a5 A |
116 | file_is_utf8 = False |
117 | ||
118 | if not file_is_utf8 and actual_mime_type.find("utf-8") >= 0: | |
119 | print "Error: %s is not valid utf-8, but has a utf-8 mime type." % file_name | |
120 | return actual_mime_type | |
121 | ||
122 | if file_is_utf8 and actual_mime_type.find("charset") >=0 and actual_mime_type.find("utf-8") < 0: | |
123 | print "Warning: %s is valid utf-8, but has a mime-type of %s." % (file_name, actual_mime_type) | |
729e4ab9 | 124 | |
f3c0d7a5 A |
125 | if ord(bytes[0]) == 0xef: |
126 | if not file_name.endswith(".txt"): | |
127 | print "Warning: file %s contains a UTF-8 BOM: " % file_name | |
128 | ||
129 | # If the file already has a charset in its mime-type, don't make any change. | |
130 | ||
131 | if actual_mime_type.find("charset=") >= 0: | |
132 | return actual_mime_type; | |
729e4ab9 | 133 | |
f3c0d7a5 | 134 | return base_mime_type |
729e4ab9 A |
135 | |
136 | ||
137 | def main(argv): | |
138 | fix_problems = False; | |
139 | try: | |
140 | opts, args = getopt.getopt(argv, "fh", ("fix", "help")) | |
141 | except getopt.GetoptError: | |
142 | print "unrecognized option: " + argv[0] | |
143 | usage() | |
144 | sys.exit(2) | |
145 | for opt, arg in opts: | |
146 | if opt in ("-h", "--help"): | |
147 | usage() | |
148 | sys.exit() | |
149 | if opt in ("-f", "--fix"): | |
150 | fix_problems = True | |
151 | if args: | |
152 | print "unexpected command line argument" | |
153 | usage() | |
154 | sys.exit() | |
155 | ||
156 | parse_auto_props() | |
157 | output = runCommand("svn ls -R "); | |
158 | file_list = output.splitlines() | |
159 | ||
160 | for f in file_list: | |
161 | if os.path.isdir(f): | |
162 | # print "Skipping dir " + f | |
163 | continue | |
164 | if not os.path.isfile(f): | |
165 | print "Repository file not in working copy: " + f | |
166 | continue; | |
167 | ||
168 | for file_pattern, props in file_types: | |
169 | if re.match(file_pattern, f): | |
170 | # print "doing " + f | |
171 | for propname, propval in props: | |
172 | actual_propval = runCommand("svn propget --strict " + propname + " " + f) | |
173 | #print propname + ": " + actual_propval | |
174 | if propname == "svn:mime-type" and propval.find("text/") == 0: | |
175 | # check for UTF-8 text files, should have svn:mime-type=text/something; charset=utf8 | |
176 | propval = check_utf8(f, propval, actual_propval) | |
177 | if not (propval == actual_propval or (propval == "" and actual_propval == "*")): | |
178 | print "svn propset %s '%s' %s" % (propname, propval, f) | |
179 | if fix_problems: | |
180 | os.system("svn propset %s '%s' %s" % (propname, propval, f)) | |
729e4ab9 A |
181 | |
182 | ||
183 | if __name__ == "__main__": | |
184 | main(sys.argv[1:]) |