skia2/tools/svg/svg_downloader.py
Tyler Denniston 96a97497b3 Update SVG asset creation to handle images
This CL updates the infra scripts used to create the CIPD asset for
SVG's corpus on gold to download and include image resources.

Summary of changes:
- Change svg_downloader.py input argument to more generic name
- Add --keep_common_prefix arg to svg_downloader.py to preserve the
  directory hierarchy for images, needed for the W3C test suite.
- Update infra SVG create.py script to download images
- Add svg_images.txt file with a list of the images we need for the W3C
  test suite already in gold.

Actually updating the corpus will happen in a separate CL.

Bug: skia:11229
Change-Id: I5fe9be35db247f577bda6040ca3694a428314d0e
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/361516
Reviewed-by: Florin Malita <fmalita@chromium.org>
Reviewed-by: Kevin Lubick <kjlubick@google.com>
Commit-Queue: Tyler Denniston <tdenniston@google.com>
2021-01-28 21:00:24 +00:00

67 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
# Copyright (c) 2016 The Chromium Authors. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.
"""Downloads SVGs into a specified directory."""
import optparse
import os
import urllib
PARENT_DIR = os.path.dirname(os.path.realpath(__file__))
def download_files(input_file, output_dir, prefix, keep_common_prefix):
with open(input_file, 'r') as f:
lines = f.readlines()
if keep_common_prefix:
common_prefix = os.path.commonprefix(lines)
for url in lines:
file_url = url.strip()
if keep_common_prefix:
rel_file = file_url.replace(common_prefix, '')
dest_dir = os.path.join(output_dir, os.path.dirname(rel_file))
else:
dest_dir = output_dir
dest_file = os.path.join(dest_dir, prefix + os.path.basename(file_url))
if not os.path.exists(dest_dir):
os.makedirs(dest_dir)
print 'Downloading %s to %s' % (file_url, dest_file)
urllib.urlretrieve(file_url, dest_file)
if '__main__' == __name__:
option_parser = optparse.OptionParser()
option_parser.add_option(
'-i', '--input_file',
help='Path to the text file containing URLs. Each line should contain a '
'single URL.',
default=os.path.join(PARENT_DIR, 'svgs.txt'))
option_parser.add_option(
'-o', '--output_dir',
help='The output dir where downloaded SVGs and images will be stored in.')
option_parser.add_option(
'-p', '--prefix',
help='The prefix which downloaded files will begin with.',
default='')
option_parser.add_option(
'-k', '--keep_common_prefix',
help='Preserve everything in the URL after the common prefix as directory '
'hierarchy.',
action='store_true', default=False)
options, unused_args = option_parser.parse_args()
if not options.output_dir:
raise Exception('Must specify --output_dir')
download_files(options.input_file, options.output_dir,
options.prefix, options.keep_common_prefix)