Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / devtools / PRESUBMIT.py
blobd2fe54158e9f50cb7ae6ca0165393bea07b6aa2c
1 # Copyright (C) 2014 Google Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
7 # * Redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer.
9 # * Redistributions in binary form must reproduce the above
10 # copyright notice, this list of conditions and the following disclaimer
11 # in the documentation and/or other materials provided with the
12 # distribution.
13 # * Neither the name of Google Inc. nor the names of its
14 # contributors may be used to endorse or promote products derived from
15 # this software without specific prior written permission.
17 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 """DevTools JSDoc validator presubmit script
31 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32 for more details about the presubmit API built into gcl.
33 """
35 import sys
37 compile_note = "Be sure to run your patch by the compile_frontend.py script prior to committing!"
39 def _CompileDevtoolsFrontend(input_api, output_api):
40 local_paths = [f.LocalPath() for f in input_api.AffectedFiles()]
42 # FIXME: The compilation does not actually run if injected script-related files
43 # have changed, as they reside in core/inspector, which is not affected
44 # by this presubmit.
45 # Once this is fixed, injected_script_externs.js
46 # should be added to the list of triggers.
47 devtools_front_end = input_api.os_path.join("devtools", "front_end")
48 if (any(devtools_front_end in path for path in local_paths) or
49 any("protocol.json" in path for path in local_paths) or
50 any("compile_frontend.py" in path for path in local_paths) or
51 any("InjectedScriptSource.js" in path for path in local_paths)):
52 lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
53 "scripts", "compile_frontend.py")
54 out, _ = input_api.subprocess.Popen(
55 [input_api.python_executable, lint_path],
56 stdout=input_api.subprocess.PIPE,
57 stderr=input_api.subprocess.STDOUT).communicate()
58 if "ERROR" in out or "WARNING" in out:
59 return [output_api.PresubmitError(out)]
60 if "NOTE" in out:
61 return [output_api.PresubmitPromptWarning(out + compile_note)]
62 return []
65 def _CheckConvertSVGToPNGHashes(input_api, output_api):
66 if not input_api.platform.startswith('linux'):
67 return []
69 original_sys_path = sys.path
70 try:
71 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
72 import devtools_file_hashes
73 finally:
74 sys.path = original_sys_path
76 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
77 images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src")
78 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")]
79 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
80 hashes_file_name = "svg2png.hashes"
81 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
82 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
83 if len(invalid_hash_file_paths) == 0:
84 return []
85 invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
86 file_paths_str = ", ".join(invalid_hash_file_names)
87 error_message = "The following SVG files should be converted to PNG using convert_svg_images_png.py script before uploading: \n - %s" % file_paths_str
88 return [output_api.PresubmitError(error_message)]
91 def _CheckOptimizePNGHashes(input_api, output_api):
92 if not input_api.platform.startswith('linux'):
93 return []
95 original_sys_path = sys.path
96 try:
97 sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
98 import devtools_file_hashes
99 finally:
100 sys.path = original_sys_path
102 absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
103 images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src")
104 image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")]
105 image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
106 hashes_file_name = "optimize_png.hashes"
107 hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
108 invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
109 if len(invalid_hash_file_paths) == 0:
110 return []
111 invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
112 file_paths_str = ", ".join(invalid_hash_file_names)
113 error_message = "The following PNG files should be optimized using optimize_png_images.py script before uploading: \n - %s" % file_paths_str
114 return [output_api.PresubmitError(error_message)]
117 def _CheckCSSViolations(input_api, output_api):
118 results = []
119 for f in input_api.AffectedFiles(include_deletes=False):
120 if not f.LocalPath().endswith(".css"):
121 continue
122 for line_number, line in f.ChangedContents():
123 if "/deep/" in line:
124 results.append(output_api.PresubmitError(("%s:%d uses /deep/ selector") % (f.LocalPath(), line_number)))
125 if "::shadow" in line:
126 results.append(output_api.PresubmitError(("%s:%d uses ::shadow selector") % (f.LocalPath(), line_number)))
127 return results
130 def CheckChangeOnUpload(input_api, output_api):
131 results = []
132 results.extend(_CompileDevtoolsFrontend(input_api, output_api))
133 results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api))
134 results.extend(_CheckOptimizePNGHashes(input_api, output_api))
135 results.extend(_CheckCSSViolations(input_api, output_api))
136 return results
139 def CheckChangeOnCommit(input_api, output_api):
140 return []