Tim van der Lippe | 8e12f92 | 2020-12-03 16:33:50 | [diff] [blame^] | 1 | #!/usr/bin/env vpython |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 2 | # 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 | |
| 30 | import os |
| 31 | import os.path |
| 32 | import subprocess |
| 33 | import sys |
| 34 | |
| 35 | from build import devtools_file_hashes |
| 36 | |
| 37 | try: |
| 38 | import json |
| 39 | except ImportError: |
| 40 | import simplejson as json |
| 41 | |
| 42 | scripts_path = os.path.dirname(os.path.abspath(__file__)) |
| 43 | devtools_path = os.path.dirname(scripts_path) |
| 44 | blink_source_path = os.path.dirname(devtools_path) |
| 45 | blink_path = os.path.dirname(blink_source_path) |
| 46 | chromium_src_path = os.path.dirname(os.path.dirname(blink_path)) |
| 47 | devtools_frontend_path = os.path.join(devtools_path, "front_end") |
| 48 | images_path = os.path.join(devtools_frontend_path, "Images") |
| 49 | image_sources_path = os.path.join(images_path, "src") |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 50 | HASHES_FILE_NAME = "optimize_svg.hashes" |
| 51 | HASHES_FILE_PATH = os.path.join(image_sources_path, HASHES_FILE_NAME) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 52 | |
| 53 | file_names = os.listdir(image_sources_path) |
| 54 | svg_file_paths = [os.path.join(image_sources_path, file_name) for file_name in file_names if file_name.endswith(".svg")] |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 55 | SVG_FILE_PATHS_TO_OPTIMIZE = devtools_file_hashes.files_with_invalid_hashes(HASHES_FILE_PATH, svg_file_paths) |
| 56 | SVG_FILE_NAMES = [os.path.basename(file_path) for file_path in SVG_FILE_PATHS_TO_OPTIMIZE] |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 57 | |
| 58 | |
| 59 | def 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 64 | sys.exit(1) |
| 65 | |
| 66 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 67 | check_installed("npx") |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 68 | |
| 69 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 70 | def 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 73 | proc = subprocess.Popen(optimize_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True, cwd=chromium_src_path) |
| 74 | return proc |
| 75 | |
| 76 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 77 | if len(SVG_FILE_NAMES): |
| 78 | print "%d unoptimized svg files found." % len(SVG_FILE_NAMES) |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 79 | else: |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 80 | print "All svg files are already optimized." |
Blink Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 81 | sys.exit() |
| 82 | |
| 83 | processes = {} |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 84 | for 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 Reformat | 4c46d09 | 2018-04-07 15:32:37 | [diff] [blame] | 87 | |
| 88 | for file_name, proc in processes.items(): |
| 89 | (optimize_out, _) = proc.communicate() |
| 90 | print("Optimization of %s finished: %s" % (file_name, optimize_out)) |
| 91 | |
Joel Einbinder | f6f86b6 | 2019-06-10 23:19:12 | [diff] [blame] | 92 | devtools_file_hashes.update_file_hashes(HASHES_FILE_PATH, svg_file_paths) |