blob: ce952fff55e6fe88a2a8d0d6c81e73a25a41a523 [file] [log] [blame]
Tim van der Lippe8e12f922020-12-03 16:33:501#!/usr/bin/env vpython
Blink Reformat4c46d092018-04-07 15:32:372# Copyright (c) 2014 Google Inc. All rights reserved.
3#
4# Redistribution and use in source and binary forms, with or without
5# modification, are permitted provided that the following conditions are
6# met:
7#
8# * Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# * Redistributions in binary form must reproduce the above
11# copyright notice, this list of conditions and the following disclaimer
12# in the documentation and/or other materials provided with the
13# distribution.
14# * Neither the name of Google Inc. nor the names of its
15# contributors may be used to endorse or promote products derived from
16# this software without specific prior written permission.
17#
18# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30import os
31import os.path
32import subprocess
33import sys
34
35from build import devtools_file_hashes
36
37try:
38 import json
39except ImportError:
40 import simplejson as json
41
42scripts_path = os.path.dirname(os.path.abspath(__file__))
43devtools_path = os.path.dirname(scripts_path)
44blink_source_path = os.path.dirname(devtools_path)
45blink_path = os.path.dirname(blink_source_path)
46chromium_src_path = os.path.dirname(os.path.dirname(blink_path))
47devtools_frontend_path = os.path.join(devtools_path, "front_end")
48images_path = os.path.join(devtools_frontend_path, "Images")
49image_sources_path = os.path.join(images_path, "src")
Joel Einbinderf6f86b62019-06-10 23:19:1250HASHES_FILE_NAME = "optimize_svg.hashes"
51HASHES_FILE_PATH = os.path.join(image_sources_path, HASHES_FILE_NAME)
Blink Reformat4c46d092018-04-07 15:32:3752
53file_names = os.listdir(image_sources_path)
54svg_file_paths = [os.path.join(image_sources_path, file_name) for file_name in file_names if file_name.endswith(".svg")]
Joel Einbinderf6f86b62019-06-10 23:19:1255SVG_FILE_PATHS_TO_OPTIMIZE = devtools_file_hashes.files_with_invalid_hashes(HASHES_FILE_PATH, svg_file_paths)
56SVG_FILE_NAMES = [os.path.basename(file_path) for file_path in SVG_FILE_PATHS_TO_OPTIMIZE]
Blink Reformat4c46d092018-04-07 15:32:3757
58
59def check_installed(app_name):
60 proc = subprocess.Popen("which %s" % app_name, stdout=subprocess.PIPE, shell=True)
61 proc.communicate()
62 if proc.returncode != 0:
63 print "This script needs \"%s\" to be installed." % app_name
Blink Reformat4c46d092018-04-07 15:32:3764 sys.exit(1)
65
66
Joel Einbinderf6f86b62019-06-10 23:19:1267check_installed("npx")
Blink Reformat4c46d092018-04-07 15:32:3768
69
Joel Einbinderf6f86b62019-06-10 23:19:1270def optimize_svg(svg_input_path):
71 svg_output_path = os.path.join(images_path, os.path.basename(svg_input_path))
72 optimize_command = "npx svgo -i %s -o %s" % (svg_input_path, svg_output_path)
Blink Reformat4c46d092018-04-07 15:32:3773 proc = subprocess.Popen(optimize_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=chromium_src_path)
74 return proc
75
76
Joel Einbinderf6f86b62019-06-10 23:19:1277if len(SVG_FILE_NAMES):
78 print "%d unoptimized svg files found." % len(SVG_FILE_NAMES)
Blink Reformat4c46d092018-04-07 15:32:3779else:
Joel Einbinderf6f86b62019-06-10 23:19:1280 print "All svg files are already optimized."
Blink Reformat4c46d092018-04-07 15:32:3781 sys.exit()
82
83processes = {}
Joel Einbinderf6f86b62019-06-10 23:19:1284for svg_file_path in SVG_FILE_PATHS_TO_OPTIMIZE:
85 name = os.path.splitext(os.path.basename(svg_file_path))[0]
86 processes[name] = optimize_svg(svg_file_path)
Blink Reformat4c46d092018-04-07 15:32:3787
88for file_name, proc in processes.items():
89 (optimize_out, _) = proc.communicate()
90 print("Optimization of %s finished: %s" % (file_name, optimize_out))
91
Joel Einbinderf6f86b62019-06-10 23:19:1292devtools_file_hashes.update_file_hashes(HASHES_FILE_PATH, svg_file_paths)