109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#!/usr/bin/env python
209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# Copyright (c) 2014 Google Inc. All rights reserved.
309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#
409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# Redistribution and use in source and binary forms, with or without
509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# modification, are permitted provided that the following conditions are
609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# met:
709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#
809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#     * Redistributions of source code must retain the above copyright
909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# notice, this list of conditions and the following disclaimer.
1009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#     * Redistributions in binary form must reproduce the above
1109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# copyright notice, this list of conditions and the following disclaimer
1209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# in the documentation and/or other materials provided with the
1309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# distribution.
1409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#     * Neither the name of Google Inc. nor the names of its
1509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# contributors may be used to endorse or promote products derived from
1609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# this software without specific prior written permission.
1709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)#
1809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
1909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
2009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
2109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
2209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
2309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
2809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
3009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import devtools_file_hashes
3109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import hashlib
3209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import os
3309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import os.path
3409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import re
3509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import subprocess
3609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)import sys
3709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
3809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)try:
3909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    import json
4009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)except ImportError:
4109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    import simplejson as json
4209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
4309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
4409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)def check_installed(app_name):
4509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    proc = subprocess.Popen("which %s" % app_name, stdout=subprocess.PIPE, shell=True)
4609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    proc.communicate()
4709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    if proc.returncode != 0:
4809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        print "This script needs \"%s\" to be installed." % app_name
4909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)        sys.exit(1)
5009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
5109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
5209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)check_installed("inkscape")
5309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
5409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)scripts_path = os.path.dirname(os.path.abspath(__file__))
5509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)devtools_path = os.path.dirname(scripts_path)
5609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)devtools_frontend_path = devtools_path + "/front_end"
5709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)images_path = devtools_frontend_path + "/Images"
5809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)image_sources_path = images_path + "/src"
5909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)hashes_file_name = "svg2png.hashes"
6009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)hashes_file_path = image_sources_path + "/" + hashes_file_name
6109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)file_names = os.listdir(image_sources_path)
6309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)svg_file_paths = [image_sources_path + "/" + file_name for file_name in file_names if file_name.endswith(".svg")]
6409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)svg_file_paths_to_convert = devtools_file_hashes.files_with_invalid_hashes(hashes_file_path, svg_file_paths)
6609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)svg_file_names = [re.sub(".svg$", "", re.sub(".*/", "", file_path)) for file_path in svg_file_paths_to_convert]
6709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
6909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)def convert_svg_to_png(svg_file_name, png_file_name, dpi):
7009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    svg_full_path = image_sources_path + "/" + svg_file_name + ".svg"
7109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    png_full_path = images_path + "/" + png_file_name + ".png"
7209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    convert_command = "inkscape -f %s -e %s -d %s" % (svg_full_path, png_full_path, dpi)
7309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    proc = subprocess.Popen(convert_command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
7409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    return proc
7509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
7609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)processes = {}
7709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)for file_name in svg_file_names:
7809380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    name = re.sub(".svg$", "", file_name)
7909380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    name2x = name + "_2x"
8009380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    processes[name] = convert_svg_to_png(name, name, 90)
8109380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    processes[name2x] = convert_svg_to_png(name, name2x, 180)
8209380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
8309380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)for file_name, proc in processes.items():
8409380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    (convert_out, _) = proc.communicate()
8509380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)    print("Conversion of %s finished: %s" % (file_name, convert_out))
8609380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)
8709380295ba73501a205346becac22c6978e4671dTorne (Richard Coles)devtools_file_hashes.update_file_hashes(hashes_file_path, svg_file_paths)
88