Extract SIGPIPE ignoring code to a common place.
[chromium-blink-merge.git] / chrome / common / extensions / PRESUBMIT.py
blob2e9a11474b82c6d4c4cb79ccd79828e00ef030bd
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 # Use of this source code is governed by a BSD-style license that can be
3 # found in the LICENSE file.
5 """Presubmit script for changes affecting extensions.
7 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
8 for more details about the presubmit API built into gcl.
9 """
10 import fnmatch
11 import os
12 import re
14 EXTENSIONS_PATH = os.path.join('chrome', 'common', 'extensions')
15 DOCS_PATH = os.path.join(EXTENSIONS_PATH, 'docs')
16 SERVER2_PATH = os.path.join(DOCS_PATH, 'server2')
17 API_PATH = os.path.join(EXTENSIONS_PATH, 'api')
18 TEMPLATES_PATH = os.path.join(DOCS_PATH, 'templates')
19 PRIVATE_TEMPLATES_PATH = os.path.join(TEMPLATES_PATH, 'private')
20 PUBLIC_TEMPLATES_PATH = os.path.join(TEMPLATES_PATH, 'public')
21 INTROS_PATH = os.path.join(TEMPLATES_PATH, 'intros')
22 ARTICLES_PATH = os.path.join(TEMPLATES_PATH, 'articles')
24 LOCAL_PUBLIC_TEMPLATES_PATH = os.path.join('docs',
25 'templates',
26 'public')
28 def _ListFilesInPublic():
29 all_files = []
30 for path, dirs, files in os.walk(LOCAL_PUBLIC_TEMPLATES_PATH):
31 all_files.extend(
32 os.path.join(path, filename)[len(LOCAL_PUBLIC_TEMPLATES_PATH + os.sep):]
33 for filename in files)
34 return all_files
36 def _UnixName(name):
37 name = os.path.splitext(name)[0]
38 s1 = re.sub('([a-z])([A-Z])', r'\1_\2', name)
39 s2 = re.sub('([A-Z]+)([A-Z][a-z])', r'\1_\2', s1)
40 return s2.replace('.', '_').lower()
42 def _FindMatchingTemplates(template_name, template_path_list):
43 matches = []
44 unix_name = _UnixName(template_name)
45 for template in template_path_list:
46 if unix_name == _UnixName(template.split(os.sep)[-1]):
47 matches.append(template)
48 return matches
50 def _SanitizeAPIName(name, api_path):
51 if not api_path.endswith(os.sep):
52 api_path += os.sep
53 filename = os.path.splitext(name)[0][len(api_path):].replace(os.sep, '_')
54 if 'experimental' in filename:
55 filename = 'experimental_' + filename.replace('experimental_', '')
56 return filename
58 def _CreateIntegrationTestArgs(affected_files):
59 if (any(fnmatch.fnmatch(name, '%s*.py' % SERVER2_PATH)
60 for name in affected_files) or
61 any(fnmatch.fnmatch(name, '%s*' % PRIVATE_TEMPLATES_PATH)
62 for name in affected_files)):
63 return ['-a']
64 args = []
65 for name in affected_files:
66 if (fnmatch.fnmatch(name, '%s*' % PUBLIC_TEMPLATES_PATH) or
67 fnmatch.fnmatch(name, '%s*' % INTROS_PATH) or
68 fnmatch.fnmatch(name, '%s*' % ARTICLES_PATH)):
69 args.extend(_FindMatchingTemplates(name.split(os.sep)[-1],
70 _ListFilesInPublic()))
71 if fnmatch.fnmatch(name, '%s*' % API_PATH):
72 args.extend(_FindMatchingTemplates(_SanitizeAPIName(name, API_PATH),
73 _ListFilesInPublic()))
74 return args
76 def _CheckHeadingIDs(input_api):
77 ids_re = re.compile('<h[23].*id=.*?>')
78 headings_re = re.compile('<h[23].*?>')
79 bad_files = []
80 for name in input_api.AbsoluteLocalPaths():
81 if (fnmatch.fnmatch(name, '*%s*' % INTROS_PATH) or
82 fnmatch.fnmatch(name, '*%s*' % ARTICLES_PATH)):
83 contents = input_api.ReadFile(name)
84 if (len(re.findall(headings_re, contents)) !=
85 len(re.findall(ids_re, contents))):
86 bad_files.append(name)
87 return bad_files
89 def _CheckVersions(input_api, output_api, results):
90 version = '_VERSION ='
91 for affected_file in input_api.AffectedFiles():
92 local_path = affected_file.LocalPath()
93 if not fnmatch.fnmatch(local_path, '%s*' % SERVER2_PATH):
94 continue
95 if local_path.endswith('PRESUBMIT.py'):
96 continue
97 if any(version in line for line in affected_file.NewContents()):
98 found = False
99 for _, text in affected_file.ChangedContents():
100 if version in text:
101 found = True
102 break
103 if not found:
104 results.append(output_api.PresubmitError(
105 '_VERSION of %s needs to be incremented.' % affected_file))
107 def _CheckChange(input_api, output_api):
108 results = [
109 output_api.PresubmitError('File %s needs an id for each heading.' % name)
110 for name in _CheckHeadingIDs(input_api)]
111 try:
112 integration_test = []
113 # From depot_tools/presubmit_canned_checks.py:529
114 if input_api.platform == 'win32':
115 integration_test = [input_api.python_executable]
116 integration_test.append(
117 os.path.join('docs', 'server2', 'integration_test.py'))
118 integration_test.extend(_CreateIntegrationTestArgs(input_api.LocalPaths()))
119 input_api.subprocess.check_call(integration_test,
120 cwd=input_api.PresubmitLocalPath())
121 except input_api.subprocess.CalledProcessError:
122 results.append(output_api.PresubmitError('IntegrationTest failed!'))
123 _CheckVersions(input_api, output_api, results)
124 return results
126 def CheckChangeOnUpload(input_api, output_api):
127 return _CheckChange(input_api, output_api)
129 def CheckChangeOnCommit(input_api, output_api):
130 return _CheckChange(input_api, output_api)