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.
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',
28 def _ReadFile(filename
):
29 with
open(filename
) as f
:
32 def _ListFilesInPublic():
34 for path
, dirs
, files
in os
.walk(LOCAL_PUBLIC_TEMPLATES_PATH
):
36 os
.path
.join(path
, filename
)[len(LOCAL_PUBLIC_TEMPLATES_PATH
+ os
.sep
):]
37 for filename
in files
)
41 name
= os
.path
.splitext(name
)[0]
42 s1
= re
.sub('([a-z])([A-Z])', r
'\1_\2', name
)
43 s2
= re
.sub('([A-Z]+)([A-Z][a-z])', r
'\1_\2', s1
)
44 return s2
.replace('.', '_').lower()
46 def _FindMatchingTemplates(template_name
, template_path_list
):
48 unix_name
= _UnixName(template_name
)
49 for template
in template_path_list
:
50 if unix_name
== _UnixName(template
.split(os
.sep
)[-1]):
51 matches
.append(template
)
54 def _SanitizeAPIName(name
, api_path
):
55 if not api_path
.endswith(os
.sep
):
57 filename
= os
.path
.splitext(name
)[0][len(api_path
):].replace(os
.sep
, '_')
58 if 'experimental' in filename
:
59 filename
= 'experimental_' + filename
.replace('experimental_', '')
62 def _CreateIntegrationTestArgs(affected_files
):
63 if (any(fnmatch
.fnmatch(name
, '%s*.py' % SERVER2_PATH
)
64 for name
in affected_files
) or
65 any(fnmatch
.fnmatch(name
, '%s*' % PRIVATE_TEMPLATES_PATH
)
66 for name
in affected_files
)):
69 for name
in affected_files
:
70 if (fnmatch
.fnmatch(name
, '%s*' % PUBLIC_TEMPLATES_PATH
) or
71 fnmatch
.fnmatch(name
, '%s*' % INTROS_PATH
) or
72 fnmatch
.fnmatch(name
, '%s*' % ARTICLES_PATH
)):
73 args
.extend(_FindMatchingTemplates(name
.split(os
.sep
)[-1],
74 _ListFilesInPublic()))
75 if fnmatch
.fnmatch(name
, '%s*' % API_PATH
):
76 args
.extend(_FindMatchingTemplates(_SanitizeAPIName(name
, API_PATH
),
77 _ListFilesInPublic()))
80 def _CheckHeadingIDs(input_api
):
81 ids_re
= re
.compile('<h[23].*id=.*?>')
82 headings_re
= re
.compile('<h[23].*?>')
84 for name
in input_api
.AbsoluteLocalPaths():
85 if not os
.path
.exists(name
):
87 if (fnmatch
.fnmatch(name
, '*%s*' % INTROS_PATH
) or
88 fnmatch
.fnmatch(name
, '*%s*' % ARTICLES_PATH
)):
89 contents
= input_api
.ReadFile(name
)
90 if (len(re
.findall(headings_re
, contents
)) !=
91 len(re
.findall(ids_re
, contents
))):
92 bad_files
.append(name
)
95 def _CheckLinks(input_api
, output_api
, results
):
96 for affected_file
in input_api
.AffectedFiles():
97 name
= affected_file
.LocalPath()
98 absolute_path
= affected_file
.AbsoluteLocalPath()
99 if not os
.path
.exists(absolute_path
):
101 if (fnmatch
.fnmatch(name
, '%s*' % PUBLIC_TEMPLATES_PATH
) or
102 fnmatch
.fnmatch(name
, '%s*' % INTROS_PATH
) or
103 fnmatch
.fnmatch(name
, '%s*' % ARTICLES_PATH
) or
104 fnmatch
.fnmatch(name
, '%s*' % API_PATH
)):
105 contents
= _ReadFile(absolute_path
)
107 if input_api
.platform
== 'win32':
108 args
= [input_api
.python_executable
]
109 args
.extend([os
.path
.join('docs', 'server2', 'link_converter.py'),
113 output
= input_api
.subprocess
.check_output(
115 cwd
=input_api
.PresubmitLocalPath(),
116 universal_newlines
=True)
117 if output
!= contents
:
119 for i
, (line1
, line2
) in enumerate(
120 zip(contents
.split('\n'), output
.split('\n'))):
122 changes
= ('%s\nLine %d:\n-%s\n+%s\n' %
123 (changes
, i
+ 1, line1
, line2
))
125 results
.append(output_api
.PresubmitPromptWarning(
126 'File %s may have an old-style <a> link to an API page. Please '
127 'run docs/server2/link_converter.py to convert the link[s], or '
128 'convert them manually.\n\nSuggested changes are: %s' %
131 def _CheckChange(input_api
, output_api
):
133 output_api
.PresubmitError('File %s needs an id for each heading.' % name
)
134 for name
in _CheckHeadingIDs(input_api
)]
136 integration_test
= []
137 # From depot_tools/presubmit_canned_checks.py:529
138 if input_api
.platform
== 'win32':
139 integration_test
= [input_api
.python_executable
]
140 integration_test
.append(
141 os
.path
.join('docs', 'server2', 'integration_test.py'))
142 integration_test
.extend(_CreateIntegrationTestArgs(input_api
.LocalPaths()))
143 input_api
.subprocess
.check_call(integration_test
,
144 cwd
=input_api
.PresubmitLocalPath())
145 except input_api
.subprocess
.CalledProcessError
:
146 results
.append(output_api
.PresubmitError('IntegrationTest failed!'))
148 # TODO(kalman): Re-enable this check, or decide to delete it forever. Now
149 # that we have multiple directories it no longer works.
150 # See http://crbug.com/297178.
151 #_CheckLinks(input_api, output_api, results)
155 def CheckChangeOnUpload(input_api
, output_api
):
156 return _CheckChange(input_api
, output_api
)
158 def CheckChangeOnCommit(input_api
, output_api
):
159 return _CheckChange(input_api
, output_api
)