OLD | NEW |
---|---|
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright 2014 The Chromium Authors. All rights reserved. | 2 # Copyright 2014 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 """Script to download sdk/extras packages on the bots from google storage. | 6 """Script to download sdk/extras packages on the bots from google storage. |
7 | 7 |
8 The script expects an argument that specifies the packet name in the following | 8 The script expects arguments that specify zips file in the google storage |
9 format: <dir_in_sdk_extras>_<package_name>_<version>. There will be a | 9 bucket named: <dir in SDK extras>_<package name>_<version>.zip. The file will |
10 correpsonding bucket in google storage with that name, and it will be downloaded | 10 be extracted in the android_tools/sdk/extras directory. |
11 to android_tools/sdk/extras/. | |
12 """ | 11 """ |
13 | 12 |
14 import os | 13 import os |
14 import shutil | |
15 import subprocess | 15 import subprocess |
16 import sys | 16 import sys |
17 import zipfile | |
17 | 18 |
18 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'android')) | 19 sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'android')) |
19 from pylib import constants | 20 from pylib import constants |
20 | 21 |
21 GSUTIL_PATH = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, | 22 GSUTIL_PATH = os.path.join(os.path.dirname(__file__), os.pardir, os.pardir, |
22 os.pardir, os.pardir, os.pardir, os.pardir, 'depot_tools', 'gsutil.py') | 23 os.pardir, os.pardir, os.pardir, os.pardir, 'depot_tools', 'gsutil.py') |
23 SDK_EXTRAS_BUCKET = 'gs://chrome-sdk-extras' | 24 SDK_EXTRAS_BUCKET = 'gs://chrome-sdk-extras' |
24 SDK_EXTRAS_PATH = os.path.join(constants.ANDROID_SDK_ROOT, 'extras') | 25 SDK_EXTRAS_PATH = os.path.join(constants.ANDROID_SDK_ROOT, 'extras') |
25 | 26 |
26 | 27 |
27 def GetCmdOutputAndStatus(cmd_lst): | 28 def clean_and_extract(zip_file): |
28 process = subprocess.Popen(cmd_lst, stdout=subprocess.PIPE) | 29 # Extract the directory name and package name from zip file name. The format |
29 stdout, _ = process.communicate() | 30 # of the zip file is <dir in sdk/extras>_<package_name>_<version>.zip. |
30 return stdout, process.returncode | 31 dir_name = zip_file[:zip_file.index('_')] |
32 package_name = zip_file[zip_file.index('_')+1:zip_file.rindex('_')] | |
33 local_dir = '%s/%s/%s' % (SDK_EXTRAS_PATH, dir_name, package_name) | |
34 if os.path.exists(local_dir): | |
35 shutil.rmtree(local_dir) | |
36 local_zip = '%s/%s' % (SDK_EXTRAS_PATH, zip_file) | |
37 with zipfile.ZipFile(local_zip) as z: | |
38 z.extractall(path=SDK_EXTRAS_PATH) | |
31 | 39 |
32 def is_android_buildbot_checkout(): | |
33 if not os.path.exists(GSUTIL_PATH) or not os.path.exists(SDK_EXTRAS_PATH): | |
34 return False | |
35 stdout, rc = GetCmdOutputAndStatus([GSUTIL_PATH, 'ls', SDK_EXTRAS_BUCKET]) | |
36 # If successfully read bucket, then this must be a bot with permissions | |
37 return not rc | |
38 | 40 |
39 def main(args): | 41 def main(args): |
40 if is_android_buildbot_checkout(): | 42 if not os.path.exists(GSUTIL_PATH) or not os.path.exists(SDK_EXTRAS_PATH): |
41 success = True | 43 # This is not a buildbot checkout. |
42 for arg in args[1:]: | 44 return 0 |
43 # Package is named <folder>_<package_name>_<version> | 45 success = True |
44 first_underscore = arg.find('_') | 46 for arg in args[1:]: |
45 last_underscore = arg.rfind('_') | 47 local_zip = '%s/%s' % (SDK_EXTRAS_PATH, arg) |
46 folder = arg[0:first_underscore] | 48 if not os.path.exists(local_zip): |
47 package = arg[first_underscore+1:last_underscore] | 49 # Package zip is named <folder>_<package_name>_<version>.zip. |
48 # Package bucket is <SDK_EXTRAS_BUCKET>/<folder>_<package_name>_<version> | 50 package_zip = '%s/%s' % (SDK_EXTRAS_BUCKET, arg) |
49 # and in that bucket will be the directory <folder>/<package_name> to cp. | 51 rc = subprocess.check_call([GSUTIL_PATH, '--force-version', '4.7', 'cp', |
cjhopman
2015/01/06 19:14:23
don't need to manually check returncode
nyquist
2015/01/06 19:14:55
Do you need an except CalledProcessError here, to
cjhopman
2015/01/06 19:16:42
I'd just let it go uncaught personally.
navabi
2015/01/06 19:25:25
If the zip file does not exist and we are unable t
| |
50 package_bucket = '%s/%s/%s/%s' % (SDK_EXTRAS_BUCKET, arg, folder, package) | 52 package_zip, local_zip]) |
51 package_dir = '%s/%s/%s' % (SDK_EXTRAS_PATH, folder, package) | 53 if rc: |
52 if not os.path.exists(package_dir): | 54 success = False |
53 os.makedirs(package_dir) | 55 continue |
54 # rsync is only supported by gsutil version 4.x | 56 # Always clean dir and extract zip to ensure correct contents. |
55 cmd_lst = [GSUTIL_PATH, '--force-version', '4.6', '-m', 'rsync', '-r', | 57 clean_and_extract(arg) |
56 '-d', package_bucket, package_dir] | 58 if not success: |
57 stdout, rc = GetCmdOutputAndStatus(cmd_lst) | 59 return 1 |
58 success = (rc == 0) and success | |
59 if not success: | |
60 return 1 | |
61 return 0 | |
62 | 60 |
63 | 61 |
64 if __name__ == '__main__': | 62 if __name__ == '__main__': |
65 sys.exit(main(sys.argv)) | 63 sys.exit(main(sys.argv)) |
OLD | NEW |