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
31See http://dev.chromium.org/developers/how-tos/depottools/presubmit-scripts
32for more details about the presubmit API built into gcl.
33"""
34
35import os
36import re
37import sys
38
39
40def _CompileDevtoolsFrontend(input_api, output_api):
41    # FIXME: Make this run on other platforms as well.
42    if not input_api.platform.startswith('linux'):
43        return []
44    local_paths = [f.LocalPath() for f in input_api.AffectedFiles()]
45
46    # FIXME: The compilation does not actually run if injected script-related files
47    # have changed, as they reside in core/inspector, which is not affected
48    # by this presubmit.
49    # Once this is fixed, InjectedScriptHost.idl and JavaScriptCallFrame.idl
50    # should be added to the list of triggers.
51    if (any("devtools/front_end" in path for path in local_paths) or
52        any("protocol.json" in path for path in local_paths) or
53        any("InjectedScriptSource.js" in path for path in local_paths) or
54        any("InjectedScriptCanvasModuleSource.js" in path for path in local_paths)):
55        lint_path = input_api.os_path.join(input_api.PresubmitLocalPath(),
56            "scripts", "compile_frontend.py")
57        out, _ = input_api.subprocess.Popen(
58            [input_api.python_executable, lint_path],
59            stdout=input_api.subprocess.PIPE,
60            stderr=input_api.subprocess.STDOUT).communicate()
61        if "WARNING" in out or "ERROR" in out:
62            return [output_api.PresubmitError(out)]
63    return []
64
65
66def _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    image_source_file_paths = [path for path in absolute_local_paths if "devtools/front_end/Images/src" 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 = 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 = [re.sub(".*/", "", 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)]
89
90
91def _CheckOptimizePNGHashes(input_api, output_api):
92    if not input_api.platform.startswith('linux'):
93        return []
94
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
101
102    absolute_local_paths = [af.AbsoluteLocalPath() for af in input_api.AffectedFiles(include_deletes=False)]
103    image_source_file_paths = [path for path in absolute_local_paths if "devtools/front_end/Images/src" in path and path.endswith(".svg")]
104    image_sources_path = input_api.os_path.join(input_api.PresubmitLocalPath(), "front_end", "Images", "src")
105    hashes_file_name = "optimize_png.hashes"
106    hashes_file_path = image_sources_path + "/" + hashes_file_name
107    invalid_hash_file_paths = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, image_source_file_paths)
108    if len(invalid_hash_file_paths) == 0:
109        return []
110    invalid_hash_file_names = [re.sub(".*/", "", file_path) for file_path in invalid_hash_file_paths]
111    file_paths_str = ", ".join(invalid_hash_file_names)
112    error_message = "The following PNG files should be optimized using optimize_png_images.py script before uploading: \n  - %s" % file_paths_str
113    return [output_api.PresubmitError(error_message)]
114
115
116def CheckChangeOnUpload(input_api, output_api):
117    results = []
118    results.extend(_CompileDevtoolsFrontend(input_api, output_api))
119    results.extend(_CheckConvertSVGToPNGHashes(input_api, output_api))
120    results.extend(_CheckOptimizePNGHashes(input_api, output_api))
121    return results
122
123
124def CheckChangeOnCommit(input_api, output_api):
125    return []
126