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. |
kjellander_chromium
2015/01/28 11:05:09
This is a really bad assumption to make. There are
| |
42 for arg in args[1:]: | 44 return 0 |
43 # Package is named <folder>_<package_name>_<version> | 45 for arg in args[1:]: |
44 first_underscore = arg.find('_') | 46 local_zip = '%s/%s' % (SDK_EXTRAS_PATH, arg) |
45 last_underscore = arg.rfind('_') | 47 if not os.path.exists(local_zip): |
46 folder = arg[0:first_underscore] | 48 package_zip = '%s/%s' % (SDK_EXTRAS_BUCKET, arg) |
47 package = arg[first_underscore+1:last_underscore] | 49 subprocess.check_call([GSUTIL_PATH, '--force-version', '4.7', 'cp', |
48 # Package bucket is <SDK_EXTRAS_BUCKET>/<folder>_<package_name>_<version> | 50 package_zip, local_zip]) |
49 # and in that bucket will be the directory <folder>/<package_name> to cp. | 51 # Always clean dir and extract zip to ensure correct contents. |
50 package_bucket = '%s/%s/%s/%s' % (SDK_EXTRAS_BUCKET, arg, folder, package) | 52 clean_and_extract(arg) |
51 package_dir = '%s/%s/%s' % (SDK_EXTRAS_PATH, folder, package) | |
52 if not os.path.exists(package_dir): | |
53 os.makedirs(package_dir) | |
54 # rsync is only supported by gsutil version 4.x | |
55 cmd_lst = [GSUTIL_PATH, '--force-version', '4.6', '-m', 'rsync', '-r', | |
56 '-d', package_bucket, package_dir] | |
57 stdout, rc = GetCmdOutputAndStatus(cmd_lst) | |
58 success = (rc == 0) and success | |
59 if not success: | |
60 return 1 | |
61 return 0 | |
62 | 53 |
63 | 54 |
64 if __name__ == '__main__': | 55 if __name__ == '__main__': |
65 sys.exit(main(sys.argv)) | 56 sys.exit(main(sys.argv)) |
OLD | NEW |