Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 1 | #!/usr/bin/python |
| 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 |
David Ostrovsky | 2536d06 | 2013-11-14 00:35:07 +0100 | [diff] [blame] | 24 | from util import 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') |
| 28 | CACHE_DIR = path.join(GERRIT_HOME, 'buck-cache') |
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 hashfile(p): |
| 33 | d = sha1() |
| 34 | with open(p, 'rb') as f: |
| 35 | while True: |
| 36 | b = f.read(8192) |
| 37 | if not b: |
| 38 | break |
| 39 | d.update(b) |
| 40 | return d.hexdigest() |
| 41 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 42 | |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 43 | def safe_mkdirs(d): |
| 44 | if path.isdir(d): |
| 45 | return |
| 46 | try: |
| 47 | makedirs(d) |
| 48 | except OSError as err: |
| 49 | if not path.isdir(d): |
| 50 | raise err |
| 51 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 52 | |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 53 | def download_properties(root_dir): |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 54 | """ Get the download properties. |
| 55 | |
| 56 | First tries to find the properties file in the given root directory, |
| 57 | and if not found there, tries in the Gerrit settings folder in the |
| 58 | user's home directory. |
| 59 | |
| 60 | Returns a set of download properties, which may be empty. |
| 61 | |
| 62 | """ |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 63 | p = {} |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 64 | local_prop = path.join(root_dir, LOCAL_PROPERTIES) |
| 65 | if not path.isfile(local_prop): |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 66 | local_prop = path.join(GERRIT_HOME, LOCAL_PROPERTIES) |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 67 | if path.isfile(local_prop): |
| 68 | try: |
| 69 | with open(local_prop) as fd: |
| 70 | for line in fd: |
| 71 | if line.startswith('download.'): |
| 72 | d = [e.strip() for e in line.split('=', 1)] |
| 73 | name, url = d[0], d[1] |
| 74 | p[name[len('download.'):]] = url |
David Pursehouse | 6fb2c4d | 2013-05-16 16:51:46 +0900 | [diff] [blame] | 75 | except OSError: |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 76 | pass |
| 77 | return p |
| 78 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 79 | |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 80 | def cache_entry(args): |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 81 | if args.v: |
| 82 | h = args.v |
| 83 | else: |
Urs Wolfer | a11276a | 2014-04-26 13:20:00 +0200 | [diff] [blame] | 84 | h = sha1(args.u.encode('utf-8')).hexdigest() |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 85 | name = '%s-%s' % (path.basename(args.o), h) |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 86 | return path.join(CACHE_DIR, name) |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 87 | |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 88 | |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 89 | opts = OptionParser() |
| 90 | opts.add_option('-o', help='local output file') |
| 91 | opts.add_option('-u', help='URL to download') |
| 92 | opts.add_option('-v', help='expected content SHA-1') |
| 93 | opts.add_option('-x', action='append', help='file to delete from ZIP') |
| 94 | opts.add_option('--exclude_java_sources', action='store_true') |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 95 | opts.add_option('--unsign', action='store_true') |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 96 | args, _ = opts.parse_args() |
| 97 | |
| 98 | root_dir = args.o |
| 99 | while root_dir: |
| 100 | root_dir, n = path.split(root_dir) |
| 101 | if n == 'buck-out': |
| 102 | break |
| 103 | |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 104 | redirects = download_properties(root_dir) |
David Pursehouse | 45286f1 | 2013-05-17 13:46:40 +0900 | [diff] [blame] | 105 | cache_ent = cache_entry(args) |
Shawn Pearce | 1b89f85 | 2013-05-13 20:26:34 -0700 | [diff] [blame] | 106 | src_url = resolve_url(args.u, redirects) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 107 | |
| 108 | if not path.exists(cache_ent): |
| 109 | try: |
| 110 | safe_mkdirs(path.dirname(cache_ent)) |
David Pursehouse | 9d9d68f | 2013-05-16 16:56:03 +0900 | [diff] [blame] | 111 | except OSError as err: |
| 112 | print('error creating directory %s: %s' % |
| 113 | (path.dirname(cache_ent), err), file=stderr) |
| 114 | exit(1) |
Sasa Zivkov | 4709fff | 2013-06-13 17:01:25 +0200 | [diff] [blame] | 115 | |
| 116 | print('Download %s' % src_url, file=stderr) |
| 117 | try: |
| 118 | check_call(['curl', '--proxy-anyauth', '-sfo', cache_ent, src_url]) |
| 119 | except OSError as err: |
| 120 | print('could not invoke curl: %s\nis curl installed?' % err, file=stderr) |
| 121 | exit(1) |
David Pursehouse | 9d9d68f | 2013-05-16 16:56:03 +0900 | [diff] [blame] | 122 | except CalledProcessError as err: |
| 123 | print('error using curl: %s' % err, file=stderr) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 124 | exit(1) |
| 125 | |
| 126 | if args.v: |
| 127 | have = hashfile(cache_ent) |
| 128 | if args.v != have: |
Chirayu Desai | 4c5ee48 | 2013-05-13 13:48:43 +0530 | [diff] [blame] | 129 | print(( |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 130 | '%s:\n' + |
| 131 | 'expected %s\n' + |
Shawn Pearce | eb2eeec | 2013-05-22 08:54:04 -0700 | [diff] [blame] | 132 | 'received %s\n') % (src_url, args.v, have), file=stderr) |
| 133 | try: |
| 134 | remove(cache_ent) |
| 135 | except OSError as err: |
| 136 | if path.exists(cache_ent): |
| 137 | print('error removing %s: %s' % (cache_ent, err), file=stderr) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 138 | exit(1) |
| 139 | |
| 140 | exclude = [] |
| 141 | if args.x: |
| 142 | exclude += args.x |
| 143 | if args.exclude_java_sources: |
| 144 | try: |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 145 | with ZipFile(cache_ent, 'r') as zf: |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 146 | for n in zf.namelist(): |
| 147 | if n.endswith('.java'): |
| 148 | exclude.append(n) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 149 | except (BadZipfile, LargeZipFile) as err: |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 150 | print('error opening %s: %s' % (cache_ent, err), file=stderr) |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 151 | exit(1) |
| 152 | |
| 153 | if args.unsign: |
| 154 | try: |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 155 | with ZipFile(cache_ent, 'r') as zf: |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 156 | for n in zf.namelist(): |
| 157 | if (n.endswith('.RSA') |
| 158 | or n.endswith('.SF') |
| 159 | or n.endswith('.LIST')): |
| 160 | exclude.append(n) |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 161 | except (BadZipfile, LargeZipFile) as err: |
David Pursehouse | 15a9f53 | 2014-06-23 10:55:20 +0900 | [diff] [blame] | 162 | print('error opening %s: %s' % (cache_ent, err), file=stderr) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 163 | exit(1) |
| 164 | |
| 165 | safe_mkdirs(path.dirname(args.o)) |
| 166 | if exclude: |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 167 | try: |
| 168 | shutil.copyfile(cache_ent, args.o) |
| 169 | except (shutil.Error, IOError) as err: |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 170 | print('error copying to %s: %s' % (args.o, err), file=stderr) |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 171 | exit(1) |
David Pursehouse | 14f9aa6 | 2013-05-16 17:10:09 +0900 | [diff] [blame] | 172 | try: |
| 173 | check_call(['zip', '-d', args.o] + exclude) |
| 174 | except CalledProcessError as err: |
| 175 | print('error removing files from zip: %s' % err, file=stderr) |
David Pursehouse | 8ea0650 | 2013-08-08 10:43:14 +0900 | [diff] [blame] | 176 | exit(1) |
Shawn Pearce | fd6bb9f | 2013-05-08 14:14:24 -0700 | [diff] [blame] | 177 | else: |
| 178 | try: |
| 179 | link(cache_ent, args.o) |
| 180 | except OSError as err: |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 181 | try: |
| 182 | shutil.copyfile(cache_ent, args.o) |
| 183 | except (shutil.Error, IOError) as err: |
David Ostrovsky | 6e0a3e5 | 2013-10-26 09:55:15 +0200 | [diff] [blame] | 184 | print('error copying to %s: %s' % (args.o, err), file=stderr) |
David Pursehouse | e277fc3 | 2013-08-08 10:44:23 +0900 | [diff] [blame] | 185 | exit(1) |