Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / devtools / PRESUBMIT.py
1 # Copyright (C) 2014 Google Inc. All rights reserved.
2 #
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions are
5 # met:
6 #
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.
16 #
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.
28
29 """DevTools JSDoc validator presubmit script
30
31 See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32 for more details about the presubmit API built into gcl.
33 """
34
35 import sys
36
37 compile_note = "Be sure to run your patch by the compile_frontend.py script prior to committing!"
38
39 def _CompileDevtoolsFrontend(input_api, output_api):
40     local_paths = [f.LocalPath() for f in input_api.AffectedFiles()]
41
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, InjectedScriptHost.idl and JavaScriptCallFrame.idl
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) or
52         any("InjectedScriptCanvasModuleSource.js" in path for path in local_paths)):
53         lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
54             "scripts", "compile_frontend.py")
55         out, _ = input_api.subprocess.Popen(
56             [input_api.python_executable, lint_path],
57             stdout=input_api.subprocess.PIPE,
58             stderr=input_api.subprocess.STDOUT).communicate()
59         if "ERROR" in out or "WARNING" in out:
60             return [output_api.PresubmitError(out)]
61         if "NOTE" in out:
62             return [output_api.PresubmitPromptWarning(out + compile_note)]
63     return []
64
65
66 def _CheckConvertSVGToPNGHashes(input_api, output_api):
67     if not input_api.platform.startswith('linux'):
68         return []
69
70     original_sys_path = sys.path
71     try:
72         sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
73         import devtools_file_hashes
74     finally:
75         sys.path = original_sys_path
76
77     absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
78     images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src")
79     image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")]
80     image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
81     hashes_file_name = "svg2png.hashes"
82     hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
83     invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
84     if len(invalid_hash_file_paths) == 0:
85         return []
86     invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
87     file_paths_str = ", ".join(invalid_hash_file_names)
88     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
89     return [output_api.PresubmitError(error_message)]
90
91
92 def _CheckOptimizePNGHashes(input_api, output_api):
93     if not input_api.platform.startswith('linux'):
94         return []
95
96     original_sys_path = sys.path
97     try:
98         sys.path = sys.path + [input_api.os_path.join(input_api.PresubmitLocalPath(), 'scripts')]
99         import devtools_file_hashes
100     finally:
101         sys.path = original_sys_path
102
103     absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
104     images_src_path = input_api.os_path.join("devtools", "front_end", "Images", "src")
105     image_source_file_paths = [path for path in absolute_local_paths if images_src_path in path and path.endswith(".svg")]
106     image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
107     hashes_file_name = "optimize_png.hashes"
108     hashes_file_path = input_api.os_path.join(image_sources_path, hashes_file_name)
109     invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
110     if len(invalid_hash_file_paths) == 0:
111         return []
112     invalid_hash_file_names = [input_api.os_path.basename(file_path) for file_path in invalid_hash_file_paths]
113     file_paths_str = ", ".join(invalid_hash_file_names)
114     error_message = "The following PNG files should be optimized using optimize_png_images.py script before uploading: \n  - %s" % file_paths_str
115     return [output_api.PresubmitError(error_message)]
116
117
118 def CheckChangeOnUpload(input_api, output_api):
119     results = []
120     results.extend(_CompileDevtoolsFrontend(input_api, output_api))
121     results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api))
122     results.extend(_CheckOptimizePNGHashes(input_api, output_api))
123     return results
124
125
126 def CheckChangeOnCommit(input_api, output_api):
127     return []