]>
git.saurik.com Git - apple/icu.git/blob - icuSources/tools/icu-svnprops-check.py
3 # Copyright (C) 2016 and later: Unicode, Inc. and others.
4 # License & terms of use: http://www.unicode.org/copyright.html
6 # Copyright (C) 2009-2011, International Business Machines Corporation, Google and Others.
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
13 # THIS SCRIPT DOES NOT WORK ON WINDOWS
14 # It only works correctly on platforms where the native line ending is a plain \n
17 # icu-svnprops-check.py [options]
20 # -f | --fix Fix any problems that are found
21 # -h | --help Print a usage line and exit.
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.
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]
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
47 if re
.match("\s*\[auto-props\]", propline
): # Match the [auto-props] line.
49 if not re
.match("\s*[^\s]+\s*=", propline
): # minimal syntax check for <file-type> =
50 print "Bad line from autoprops definitions: " + propline
52 file_type
, string_proplist
= propline
.split("=", 1)
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
+ "$"
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
)
67 for prop
in string_proplist
:
68 if prop
.find("=") >= 0:
69 prop_name
, prop_val
= prop
.split("=", 1)
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
))
80 file_types
.append((file_type
, proplist
))
85 output_file
= os
.popen(cmd
);
86 output_text
= output_file
.read();
87 exit_status
= output_file
.close();
89 print >>sys
.stderr
, '"', cmd
, '" failed. Exiting.'
93 svn_auto_props
= runCommand("svn propget svn:auto-props http://source.icu-project.org/repos/icu")
96 print "usage: " + sys
.argv
[0] + " [-f | --fix] [-h | --help]"
100 # UTF-8 file check. For text files with svn:mime-type=text/anything, check the specified charset
101 # file_name: name of a text file.
102 # base_mime_type: svn:mime-type property from the auto-props settings for this file type.
103 # actual_mime_type: existing svn:mime-type property value for the file.
104 # return: The correct svn:mime-type property value,
105 # either the original, if it looks OK, otherwise the value from auto-props
107 def check_utf8(file_name
, base_mime_type
, actual_mime_type
):
109 f
= open(file_name
, 'r')
114 bytes.decode("UTF-8")
115 except UnicodeDecodeError:
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
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
)
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
129 # If the file already has a charset in its mime-type, don't make any change.
131 if actual_mime_type
.find("charset=") >= 0:
132 return actual_mime_type
;
134 return base_mime_type
138 fix_problems
= False;
140 opts
, args
= getopt
.getopt(argv
, "fh", ("fix", "help"))
141 except getopt
.GetoptError
:
142 print "unrecognized option: " + argv
[0]
145 for opt
, arg
in opts
:
146 if opt
in ("-h", "--help"):
149 if opt
in ("-f", "--fix"):
152 print "unexpected command line argument"
157 output
= runCommand("svn ls -R ");
158 file_list
= output
.splitlines()
162 # print "Skipping dir " + f
164 if not os
.path
.isfile(f
):
165 print "Repository file not in working copy: " + f
168 for file_pattern
, props
in file_types
:
169 if re
.match(file_pattern
, 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
)
180 os
.system("svn propset %s '%s' %s" % (propname
, propval
, f
))
183 if __name__
== "__main__":