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 _ListFilesInPublic():
30 for path
, dirs
, files
in os
.walk(LOCAL_PUBLIC_TEMPLATES_PATH
):
32 os
.path
.join(path
, filename
)[len(LOCAL_PUBLIC_TEMPLATES_PATH
+ os
.sep
):]
33 for filename
in files
)
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
):
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
)
50 def _SanitizeAPIName(name
, api_path
):
51 if not api_path
.endswith(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_', '')
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
)):
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()))
76 def _CheckHeadingIDs(input_api
):
77 ids_re
= re
.compile('<h[23].*id=.*?>')
78 headings_re
= re
.compile('<h[23].*?>')
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
)
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
):
95 if local_path
.endswith('PRESUBMIT.py'):
97 if any(version
in line
for line
in affected_file
.NewContents()):
99 for _
, text
in affected_file
.ChangedContents():
104 results
.append(output_api
.PresubmitError(
105 '_VERSION of %s needs to be incremented.' % affected_file
))
107 def _CheckChange(input_api
, output_api
):
109 output_api
.PresubmitError('File %s needs an id for each heading.' % name
)
110 for name
in _CheckHeadingIDs(input_api
)]
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
)
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
)