Fix Crashes in the New Autofill UI
[chromium-blink-merge.git] / remoting / webapp / verify-webapp.py
blobcd56bc19f57d287bed5b2ff0fd8d4c858d36cf64
1 #!/usr/bin/env python
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
3 # Use of this source code is governed by a BSD-style license that can be
4 # found in the LICENSE file.
6 """Verifies that a given messages.json file defines all the strings used by the
7 a given set of files. For file formats where it is not possible to infer which
8 strings represent message identifiers, localized strings should be explicitly
9 annotated with the string "i18n-content", for example:
11 LocalizeString(/*i18n-content*/"PRODUCT_NAME");
13 This script also recognises localized strings in HTML and manifest.json files:
15 HTML: <span i18n-content="PRODUCT_NAME"></span>
16 or ...i18n-value-name-1="BUTTON_NAME"...
17 manifest.json: __MSG_PRODUCT_NAME__
19 Note that these forms must be exact; extra spaces are not permitted, though
20 either single or double quotes are recognized.
22 In addition, the script checks that all the messages are still in use; if
23 this is not the case then a warning is issued, but the script still succeeds.
24 """
26 import json
27 import os
28 import re
29 import sys
31 all_tags = set([])
33 WARNING_MESSAGE = """
34 To remove this warning, either remove the unused tags from
35 messages.json, add the files that use the tags listed above to
36 remoting.gyp, or annotate existing uses of those tags with the
37 prefix /*i18n-content*/
38 """
41 def ExtractTagFromLine(line):
42 """Extract a tag from a line of HTML, C++, JS or JSON."""
43 # HTML-style (tags)
44 m = re.search('i18n-content=[\'"]([^\'"]*)[\'"]', line)
45 if m: return m.group(1)
46 # HTML-style (substitutions)
47 m = re.search('i18n-value-name-[1-9]=[\'"]([^\'"]*)[\'"]', line)
48 if m: return m.group(1)
49 # C++/Javascript style
50 m = re.search('/\*i18n-content\*/[\'"]([^\`"]*)[\'"]', line)
51 if m: return m.group(1)
52 # Manifest style
53 m = re.search('__MSG_(.*)__', line)
54 if m: return m.group(1)
55 return None
58 def CheckFileForUnknownTag(filename, messages):
59 """Parse |filename|, looking for tags and report any that are not included in
60 |messages|. Return True if all tags are present and correct, or False if
61 any are missing. If no tags are found, print a warning message and return
62 True."""
63 result = True
64 matches = False
65 f = open(filename, 'r')
66 lines = f.readlines()
67 for i in xrange(0, len(lines)):
68 tag = ExtractTagFromLine(lines[i])
69 if tag:
70 all_tags.add(tag)
71 matches = True
72 if not tag in messages:
73 result = False
74 print '%s/%s:%d: error: Undefined tag: %s' % \
75 (os.getcwd(), filename, i + 1, tag)
76 if not matches:
77 print '%s/%s:0: warning: No tags found' % (os.getcwd(), filename)
78 f.close()
79 return result
82 def main():
83 if len(sys.argv) < 4:
84 print 'Usage: verify-webapp.py <touch> <messages> <message_users...>'
85 return 1
87 touch_file = sys.argv[1]
88 messages = json.load(open(sys.argv[2], 'r'))
89 exit_code = 0
90 for f in sys.argv[3:]:
91 if not CheckFileForUnknownTag(f, messages):
92 exit_code = 1
94 warnings = False
95 for tag in messages:
96 if tag not in all_tags:
97 print ('%s/%s:0: warning: %s is defined but not used') % \
98 (os.getcwd(), sys.argv[2], tag)
99 warnings = True
100 if warnings:
101 print WARNING_MESSAGE
103 if exit_code == 0:
104 f = open(touch_file, 'a')
105 f.close()
106 os.utime(touch_file, None)
108 return exit_code
111 if __name__ == '__main__':
112 sys.exit(main())