1#!/usr/bin/env python 2# Copyright (c) 2016, the R8 project authors. Please see the AUTHORS file 3# for details. All rights reserved. Use of this source code is governed by a 4# BSD-style license that can be found in the LICENSE file. 5 6# Script for downloading from x20 a dependency in the same way we use cloud 7# storage. 8 9import optparse 10import os 11import shutil 12import subprocess 13import sys 14import tarfile 15import utils 16 17GMSCORE_DEPS = '/google/data/ro/teams/gmscore-size/deps' 18 19def parse_options(): 20 return optparse.OptionParser().parse_args() 21 22def extract_dir(filename): 23 return filename[0:len(filename) - len('.tar.gz')] 24 25def unpack_archive(filename): 26 dest_dir = extract_dir(filename) 27 if os.path.exists(dest_dir): 28 print 'Deleting existing dir %s' % dest_dir 29 shutil.rmtree(dest_dir) 30 dirname = os.path.dirname(os.path.abspath(filename)) 31 with tarfile.open(filename, 'r:gz') as tar: 32 tar.extractall(path=dirname) 33 34def Main(): 35 (options, args) = parse_options() 36 assert len(args) == 1 37 sha1_file = args[0] 38 dest = sha1_file[:-5] 39 print 'Ensuring %s' % dest 40 with open(sha1_file, 'r') as input_sha: 41 sha1 = input_sha.readline() 42 if os.path.exists(dest) and utils.get_sha1(dest) == sha1: 43 print 'sha1 matches, not downloading' 44 dest_dir = extract_dir(dest) 45 if os.path.exists(dest_dir): 46 print 'destination directory exists, no extraction' 47 else: 48 unpack_archive(dest) 49 return 50 src = os.path.join(GMSCORE_DEPS, sha1) 51 if not os.path.exists(src): 52 print 'File (%s) does not exist on x20' % src 53 print 'Downloading %s to %s' % (src, dest) 54 shutil.copyfile(src, dest) 55 unpack_archive(dest) 56 57if __name__ == '__main__': 58 sys.exit(Main()) 59