update-prebuilts.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2016 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. #
  17. """Update the prebuilt RenderScript from the build server."""
  18. from __future__ import print_function
  19. import argparse
  20. import inspect
  21. import os
  22. import shutil
  23. import subprocess
  24. import sys
  25. THIS_DIR = os.path.realpath(os.path.dirname(__name__))
  26. ANDROID_DIR = os.path.realpath(os.path.join(THIS_DIR, '../..'))
  27. BRANCH = 'aosp-master'
  28. def android_path(*args):
  29. return os.path.join(ANDROID_DIR, *args)
  30. class ArgParser(argparse.ArgumentParser):
  31. def __init__(self):
  32. super(ArgParser, self).__init__(
  33. description=inspect.getdoc(sys.modules[__name__]))
  34. self.add_argument(
  35. 'build_number', metavar='BUILD',
  36. help='Build number to pull from the build server.')
  37. self.add_argument(
  38. '-a', '--android_branch', default=BRANCH,
  39. help='The Android branch to pull from build server, default: ' + BRANCH + '.')
  40. self.add_argument(
  41. '-b', '--bug', type=int,
  42. help='Bug to reference in commit message.')
  43. self.add_argument(
  44. '--use-current-branch', action='store_true',
  45. help='Do not repo start a new branch for the update.')
  46. def host_to_build_host(host):
  47. """Gets the build host name for an NDK host tag.
  48. The Windows builds are done from Linux.
  49. """
  50. return {
  51. 'darwin': 'mac',
  52. 'linux': 'linux',
  53. 'windows': 'linux',
  54. }[host]
  55. def build_name(host):
  56. """Gets the build name for a given host.
  57. The build name is either "linux" or "darwin", with any Windows builds
  58. coming from "linux".
  59. """
  60. return {
  61. 'darwin': 'darwin',
  62. 'linux': 'linux',
  63. 'windows': 'linux',
  64. }[host]
  65. def manifest_name(build_number):
  66. """Returns the manifest file name for a given build.
  67. >>> manifest_name('1234')
  68. 'manifest_1234.xml'
  69. """
  70. return 'manifest_{}.xml'.format(build_number)
  71. def package_name(build_number, host):
  72. """Returns the file name for a given package configuration.
  73. >>> package_name('1234', 'linux')
  74. 'renderscript-1234-linux-x86.tar.bz2'
  75. """
  76. return 'renderscript-{}-{}-x86.tar.bz2'.format(build_number, host)
  77. def download_build(host, build_number, android_branch, download_dir):
  78. filename = package_name(build_number, host)
  79. return download_file(host, build_number, android_branch, filename, download_dir)
  80. def download_manifest(host, build_number, android_branch, download_dir):
  81. filename = manifest_name(build_number)
  82. return download_file(host, build_number, android_branch, filename, download_dir)
  83. def download_file(host, build_number, android_branch, pkg_name, download_dir):
  84. url_base = 'https://android-build-uber.corp.google.com'
  85. path = 'builds/{build_branch}-{build_host}-{build_name}/{build_num}'.format(
  86. build_branch=android_branch,
  87. build_host=host_to_build_host(host),
  88. build_name='renderscript',
  89. build_num=build_number)
  90. url = '{}/{}/{}'.format(url_base, path, pkg_name)
  91. TIMEOUT = '60' # In seconds.
  92. out_file_path = os.path.join(download_dir, pkg_name)
  93. with open(out_file_path, 'w') as out_file:
  94. print('Downloading {} to {}'.format(url, out_file_path))
  95. subprocess.check_call(
  96. ['sso_client', '--location', '--request_timeout', TIMEOUT, url],
  97. stdout=out_file)
  98. return out_file_path
  99. def extract_package(package, install_dir):
  100. cmd = ['tar', 'xf', package, '-C', install_dir]
  101. print('Extracting {}...'.format(package))
  102. subprocess.check_call(cmd)
  103. def update_renderscript(host, build_number, android_branch, use_current_branch, download_dir, bug):
  104. host_tag = host + '-x86'
  105. prebuilt_dir = android_path('prebuilts/renderscript/host', host_tag)
  106. os.chdir(prebuilt_dir)
  107. if not use_current_branch:
  108. subprocess.check_call(
  109. ['repo', 'start', 'update-renderscript-{}'.format(build_number), '.'])
  110. package = download_build(host, build_number, android_branch, download_dir)
  111. manifest = download_manifest(host, build_number, android_branch, download_dir)
  112. install_subdir = 'current'
  113. extract_package(package, prebuilt_dir)
  114. shutil.rmtree(install_subdir)
  115. shutil.move('renderscript-{}'.format(build_number), install_subdir)
  116. shutil.copy(manifest, install_subdir)
  117. print('Adding files to index...')
  118. subprocess.check_call(['git', 'add', install_subdir])
  119. print('Committing update...')
  120. message_lines = [
  121. 'Update prebuilt RenderScript to build {}.'.format(build_number),
  122. '',
  123. 'Built from {build_branch}, build {build_number}.'.format(
  124. build_branch=android_branch,
  125. build_number=build_number),
  126. ]
  127. if bug is not None:
  128. message_lines.append('')
  129. message_lines.append('Bug: http://b/{}'.format(bug))
  130. message = '\n'.join(message_lines)
  131. subprocess.check_call(['git', 'commit', '-m', message])
  132. def main():
  133. args = ArgParser().parse_args()
  134. download_dir = os.path.realpath('.download')
  135. if os.path.isdir(download_dir):
  136. shutil.rmtree(download_dir)
  137. os.makedirs(download_dir)
  138. try:
  139. hosts = ('darwin', 'linux', 'windows')
  140. for host in hosts:
  141. update_renderscript(host, args.build_number, args.android_branch,
  142. args.use_current_branch, download_dir, args.bug)
  143. finally:
  144. shutil.rmtree(download_dir)
  145. if __name__ == '__main__':
  146. main()