Björn Pedersen | c357ceb | 2015-05-27 10:17:36 +0200 | [diff] [blame] | 1 | #!/usr/bin/env python |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 2 | # Copyright (C) 2013 The Android Open Source Project |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | # you may not use this file except in compliance with the License. |
| 6 | # You may obtain a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | # See the License for the specific language governing permissions and |
| 14 | # limitations under the License. |
| 15 | |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 16 | from __future__ import print_function |
| 17 | |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 18 | from hashlib import sha1 |
| 19 | from optparse import OptionParser |
Shawn Pearce | f98b379 | 2013-08-06 12:20:30 -0700 | [diff] [blame] | 20 | from os import link, makedirs, path, remove |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 21 | import shutil |
David Pursehouse | 2879877 | 2013-05-09 14:58:20 +0100 | [diff] [blame] | 22 | from subprocess import check_call, CalledProcessError |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 23 | from sys import stderr |
Dave Borowitz | 868f764 | 2015-11-18 15:25:24 -0500 | [diff] [blame] | 24 | from util import hash_file, resolve_url |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 25 | from zipfile import ZipFile, BadZipfile, LargeZipFile |
| 26 | |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 27 | GERRIT_HOME = path.expanduser('~/.gerritcodereview') |
Dave Borowitz | 9c35979 | 2017-11-14 08:40:55 -0500 | [diff] [blame] | 28 | CACHE_DIR = path.join(GERRIT_HOME, 'bazel-cache', 'downloaded-artifacts') |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 29 | LOCAL_PROPERTIES = 'local.properties' |
| 30 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 31 | |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 32 | def safe_mkdirs(d): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 33 | if path.isdir(d): |
| 34 | return |
| 35 | try: |
| 36 | makedirs(d) |
| 37 | except OSError as err: |
| 38 | if not path.isdir(d): |
| 39 | raise err |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 40 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 41 | |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 42 | def download_properties(root_dir): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 43 | """ Get the download properties. |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 44 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 45 | First tries to find the properties file in the given root directory, |
| 46 | and if not found there, tries in the Gerrit settings folder in the |
| 47 | user's home directory. |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 48 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 49 | Returns a set of download properties, which may be empty. |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 50 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 51 | """ |
| 52 | p = {} |
| 53 | local_prop = path.join(root_dir, LOCAL_PROPERTIES) |
| 54 | if not path.isfile(local_prop): |
| 55 | local_prop = path.join(GERRIT_HOME, LOCAL_PROPERTIES) |
| 56 | if path.isfile(local_prop): |
| 57 | try: |
| 58 | with open(local_prop) as fd: |
| 59 | for line in fd: |
| 60 | if line.startswith('download.'): |
| 61 | d = [e.strip() for e in line.split('=', 1)] |
| 62 | name, url = d[0], d[1] |
| 63 | p[name[len('download.'):]] = url |
| 64 | except OSError: |
| 65 | pass |
| 66 | return p |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 67 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 68 | |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 69 | def cache_entry(args): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 70 | if args.v: |
| 71 | h = args.v |
| 72 | else: |
| 73 | h = sha1(args.u.encode('utf-8')).hexdigest() |
| 74 | name = '%s-%s' % (path.basename(args.o), h) |
| 75 | return path.join(CACHE_DIR, name) |
| 76 | |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 77 | |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 78 | opts = OptionParser() |
| 79 | opts.add_option('-o', help='local output file') |
| 80 | opts.add_option('-u', help='URL to download') |
| 81 | opts.add_option('-v', help='expected content SHA-1') |
| 82 | opts.add_option('-x', action='append', help='file to delete from ZIP') |
| 83 | opts.add_option('--exclude_java_sources', action='store_true') |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 84 | opts.add_option('--unsign', action='store_true') |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 85 | args, _ = opts.parse_args() |
| 86 | |
| 87 | root_dir = args.o |
David Ostrovsky | e89717d | 2017-08-30 09:03:20 +0200 | [diff] [blame] | 88 | while root_dir and path.dirname(root_dir) != root_dir: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 89 | root_dir, n = path.split(root_dir) |
| 90 | if n == 'WORKSPACE': |
| 91 | break |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 92 | |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 93 | redirects = download_properties(root_dir) |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 94 | cache_ent = cache_entry(args) |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 95 | src_url = resolve_url(args.u, redirects) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 96 | |
| 97 | if not path.exists(cache_ent): |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 98 | try: |
| 99 | safe_mkdirs(path.dirname(cache_ent)) |
| 100 | except OSError as err: |
| 101 | print('error creating directory %s: %s' % |
| 102 | (path.dirname(cache_ent), err), file=stderr) |
| 103 | exit(1) |
Sasa Zivkov | 4709fff | 2013-06-13 17:01:25 +0200 | [diff] [blame] | 104 | |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 105 | print('Download %s' % src_url, file=stderr) |
| 106 | try: |
| 107 | check_call(['curl', '--proxy-anyauth', '-ksSfLo', cache_ent, src_url]) |
| 108 | except OSError as err: |
Chad Horohoe | 6914232 | 2018-05-17 10:19:22 -0700 | [diff] [blame] | 109 | print('could not invoke curl: %s\nis curl installed?' % err, |
| 110 | file=stderr) |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 111 | exit(1) |
| 112 | except CalledProcessError as err: |
| 113 | print('error using curl: %s' % err, file=stderr) |
| 114 | exit(1) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 115 | |
| 116 | if args.v: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 117 | have = hash_file(sha1(), cache_ent).hexdigest() |
| 118 | if args.v != have: |
| 119 | print(( |
| 120 | '%s:\n' + |
| 121 | 'expected %s\n' + |
| 122 | 'received %s\n') % (src_url, args.v, have), file=stderr) |
| 123 | try: |
| 124 | remove(cache_ent) |
| 125 | except OSError as err: |
| 126 | if path.exists(cache_ent): |
| 127 | print('error removing %s: %s' % (cache_ent, err), file=stderr) |
| 128 | exit(1) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 129 | |
| 130 | exclude = [] |
| 131 | if args.x: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 132 | exclude += args.x |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 133 | if args.exclude_java_sources: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 134 | try: |
| 135 | with ZipFile(cache_ent, 'r') as zf: |
| 136 | for n in zf.namelist(): |
| 137 | if n.endswith('.java'): |
| 138 | exclude.append(n) |
| 139 | except (BadZipfile, LargeZipFile) as err: |
| 140 | print('error opening %s: %s' % (cache_ent, err), file=stderr) |
| 141 | exit(1) |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 142 | |
| 143 | if args.unsign: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 144 | try: |
| 145 | with ZipFile(cache_ent, 'r') as zf: |
| 146 | for n in zf.namelist(): |
| 147 | if (n.endswith('.RSA') |
| 148 | or n.endswith('.SF') |
| 149 | or n.endswith('.LIST')): |
| 150 | exclude.append(n) |
| 151 | except (BadZipfile, LargeZipFile) as err: |
| 152 | print('error opening %s: %s' % (cache_ent, err), file=stderr) |
| 153 | exit(1) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 154 | |
| 155 | safe_mkdirs(path.dirname(args.o)) |
| 156 | if exclude: |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 157 | try: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 158 | shutil.copyfile(cache_ent, args.o) |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 159 | except (shutil.Error, IOError) as err: |
Chad Horohoe | dd22470 | 2018-05-16 22:33:06 -0400 | [diff] [blame] | 160 | print('error copying to %s: %s' % (args.o, err), file=stderr) |
| 161 | exit(1) |
| 162 | try: |
| 163 | check_call(['zip', '-d', args.o] + exclude) |
| 164 | except CalledProcessError as err: |
| 165 | print('error removing files from zip: %s' % err, file=stderr) |
| 166 | exit(1) |
| 167 | else: |
| 168 | try: |
| 169 | link(cache_ent, args.o) |
| 170 | except OSError as err: |
| 171 | try: |
| 172 | shutil.copyfile(cache_ent, args.o) |
| 173 | except (shutil.Error, IOError) as err: |
| 174 | print('error copying to %s: %s' % (args.o, err), file=stderr) |
| 175 | exit(1) |