1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283
|
#!/usr/bin/env python3
# Copyright (c) 2024 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.
"""Uploads files to Google Storage and output DEPS blob."""
import hashlib
import optparse
import os
import json
import tempfile
import re
import sys
import tarfile
from download_from_google_storage import Gsutil
from download_from_google_storage import GSUTIL_DEFAULT_PATH
from typing import List
MISSING_GENERATION_MSG = (
'missing generation number, please retrieve from Cloud Storage'
'before saving to DEPS')
USAGE_STRING = """%prog [options] target [target2 ...].
Target(s) is the files or directies intended to be uploaded to Google Storage.
If a single target is a directory, it will be compressed and uploaded as a
tar.gz file.
If target is "-", then a list of directories will be taken from standard input.
The list of directories will be compressed together and uploaded as one tar.gz
file.
Example usage
------------
./upload_to_google_storage_first_class.py --bucket gsutil-upload-playground
--object-name my_object_name hello_world.txt
./upload_to_google_storage_first_class.py --bucket gsutil-upload-playground
--object-name my_object_name my_dir1
./upload_to_google_storage_first_class.py --bucket gsutil-upload-playground
--object-name my_object_name my_dir1 my_dir2
Scan the current directory and upload all files larger than 1MB:
find . -name .svn -prune -o -size +1000k -type f -print0 |
./upload_to_google_storage_first_class.py --bucket gsutil-upload-playground
--object-name my_object_name -
"""
def get_targets(args: List[str], parser: optparse.OptionParser,
use_null_terminator: bool) -> List[str]:
"""Get target(s) to upload to GCS"""
if not args:
parser.error('Missing target.')
if len(args) == 1 and args[0] == '-':
# Take stdin as a newline or null separated list of files.
if use_null_terminator:
return sys.stdin.read().split('\0')
return sys.stdin.read().splitlines()
return args
def create_archive(dirs: List[str]) -> str:
"""Given a list of directories, compress them all into one tar file"""
# tarfile name cannot have a forward slash or else an error will be
# thrown
_, filename = tempfile.mkstemp(suffix='.tar.gz')
with tarfile.open(filename, 'w:gz') as tar:
for d in dirs:
tar.add(d)
return filename
def validate_archive_dirs(dirs: List[str]) -> bool:
"""Validate the list of directories"""
for d in dirs:
# We don't allow .. in paths in our archives.
if d == '..':
return False
# We only allow dirs.
if not os.path.isdir(d):
return False
# Symlinks must point to a target inside the dirs
if os.path.islink(d) and not any(
os.realpath(d).startswith(os.realpath(dir_prefix))
for dir_prefix in dirs):
return False
# We required that the subdirectories we are archiving are all just
# below cwd.
if d not in next(os.walk('.'))[1]:
return False
return True
def get_sha256sum(filename: str) -> str:
"""Get the sha256sum of the file"""
sha = hashlib.sha256()
with open(filename, 'rb') as f:
while True:
# Read in 1mb chunks, so it doesn't all have to be loaded into
# memory.
chunk = f.read(1024 * 1024)
if not chunk:
break
sha.update(chunk)
return sha.hexdigest()
def upload_to_google_storage(file: str, base_url: str, object_name: str,
gsutil: Gsutil, force: bool, gzip: str,
dry_run: bool) -> str:
"""Upload file to GCS"""
file_url = '%s/%s' % (base_url, object_name)
if gsutil.check_call('ls', file_url)[0] == 0 and not force:
# File exists, check MD5 hash.
_, out, _ = gsutil.check_call_with_retries('ls', '-L', file_url)
etag_match = re.search(r'ETag:\s+\S+', out)
if etag_match:
raise Exception('File with url %s already exists' % file_url)
if dry_run:
return
print("Uploading %s as %s" % (file, file_url))
gsutil_args = ['-h', 'Cache-Control:public, max-age=31536000', 'cp', '-v']
if gzip:
gsutil_args.extend(['-z', gzip])
gsutil_args.extend([file, file_url])
code, _, err = gsutil.check_call_with_retries(*gsutil_args)
if code != 0:
raise Exception(
code, 'Encountered error on uploading %s to %s\n%s' %
(file, file_url, err))
pattern = re.escape(file_url) + '#(?P<generation>\d+)'
# The geneartion number is printed as part of the progress / status info
# which gsutil outputs to stderr to keep separated from any final output
# data.
for line in err.strip().splitlines():
m = re.search(pattern, line)
if m:
return m.group('generation')
print('Warning: generation number could not be parsed from status'
f'info: {err}')
return MISSING_GENERATION_MSG
def construct_deps_blob(bucket: str, object_name: str, file: str,
generation: str) -> dict:
"""Output a blob hint that would need be added to a DEPS file"""
return {
'path': {
'dep_type':
'gcs',
'bucket':
bucket,
'objects': [{
'object_name': object_name,
'sha256sum': get_sha256sum(file),
'size_bytes': os.path.getsize(file),
'generation': int(generation),
}],
}
}
def main():
parser = optparse.OptionParser(USAGE_STRING)
parser.add_option('-b',
'--bucket',
help='Google Storage bucket to upload to.')
parser.add_option('-p',
'--prefix',
help='Prefix that goes before object-name (i.e. in '
'between bucket and object name).')
parser.add_option('-o',
'--object-name',
help='Optional object name of uploaded tar file. '
'If empty, the sha256sum will be the object name.')
parser.add_option('-d',
'--dry-run',
action='store_true',
help='Check if file already exists on GS without '
'uploading it and output DEP blob.')
parser.add_option('-c',
'--config',
action='store_true',
help='Alias for "gsutil config". Run this if you want '
'to initialize your saved Google Storage '
'credentials. This will create a read-only '
'credentials file in ~/.boto.depot_tools.')
parser.add_option('-e', '--boto', help='Specify a custom boto file.')
parser.add_option('-f',
'--force',
action='store_true',
help='Force upload even if remote file exists.')
parser.add_option('-g',
'--gsutil_path',
default=GSUTIL_DEFAULT_PATH,
help='Path to the gsutil script.')
parser.add_option('-0',
'--use_null_terminator',
action='store_true',
help='Use \\0 instead of \\n when parsing '
'the file list from stdin. This is useful if the input '
'is coming from "find ... -print0".')
parser.add_option('-z',
'--gzip',
metavar='ext',
help='For files which end in <ext> gzip them before '
'upload. '
'ext is a comma-separated list')
(options, args) = parser.parse_args()
# Enumerate our inputs.
input_filenames = get_targets(args, parser, options.use_null_terminator)
# Allow uploading the entire directory
if len(input_filenames) == 1 and input_filenames[0] in ('.', './'):
input_filenames = next(os.walk('.'))[1]
if len(input_filenames) > 1 or (len(input_filenames) == 1
and os.path.isdir(input_filenames[0])):
if not validate_archive_dirs(input_filenames):
parser.error(
'Only directories just below cwd are valid entries. '
'Entries cannot contain .. and entries can not be symlinks. '
'Entries was %s' % input_filenames)
return 1
file = create_archive(input_filenames)
else:
file = input_filenames[0]
object_name = options.object_name
if not object_name:
object_name = get_sha256sum(file)
if options.prefix:
object_name = f'{options.prefix}/{object_name}'
# Make sure we can find a working instance of gsutil.
if os.path.exists(GSUTIL_DEFAULT_PATH):
gsutil = Gsutil(GSUTIL_DEFAULT_PATH, boto_path=options.boto)
else:
gsutil = None
for path in os.environ["PATH"].split(os.pathsep):
if os.path.exists(path) and 'gsutil' in os.listdir(path):
gsutil = Gsutil(os.path.join(path, 'gsutil'),
boto_path=options.boto)
if not gsutil:
parser.error('gsutil not found in %s, bad depot_tools checkout?' %
GSUTIL_DEFAULT_PATH)
# Passing in -g/--config will run our copy of GSUtil, then quit.
if options.config:
print('===Note from depot_tools===')
print('If you do not have a project ID, enter "0" when asked for one.')
print('===End note from depot_tools===')
print()
gsutil.check_call('version')
return gsutil.call('config')
assert '/' not in options.bucket, "Slashes not allowed in bucket name"
base_url = f'gs://{options.bucket}'
generation = upload_to_google_storage(file, base_url, object_name, gsutil,
options.force, options.gzip,
options.dry_run)
print(
json.dumps(construct_deps_blob(options.bucket, object_name, file,
generation),
indent=2))
if __name__ == '__main__':
try:
sys.exit(main())
except KeyboardInterrupt:
sys.stderr.write('interrupted\n')
sys.exit(1)
|