override_sdk.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. #!/usr/bin/env python3
  2. # Copyright 2022 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. """Check out the Fuchsia SDK from a given GCS path. Should be used in a
  6. 'hooks_os' entry so that it only runs when .gclient's custom_vars includes
  7. 'fuchsia'."""
  8. import argparse
  9. import logging
  10. import os
  11. import platform
  12. import sys
  13. from common import GetHostOsFromPlatform, SDK_ROOT
  14. from update_images import DownloadAndUnpackFromCloudStorage, \
  15. MakeCleanDirectory
  16. def _GetHostArch():
  17. host_arch = platform.machine()
  18. # platform.machine() returns AMD64 on 64-bit Windows.
  19. if host_arch in ['x86_64', 'AMD64']:
  20. return 'amd64'
  21. elif host_arch == 'aarch64':
  22. return 'arm64'
  23. raise Exception('Unsupported host architecture: %s' % host_arch)
  24. def _GetTarballPath(gcs_tarball_prefix: str) -> str:
  25. """Get the full path to the sdk tarball on GCS"""
  26. platform = GetHostOsFromPlatform()
  27. arch = _GetHostArch()
  28. return f'{gcs_tarball_prefix}/{platform}-{arch}/gn.tar.gz'
  29. def main():
  30. parser = argparse.ArgumentParser()
  31. parser.add_argument('--verbose',
  32. '-v',
  33. action='store_true',
  34. help='Enable debug-level logging.')
  35. args = parser.parse_args()
  36. logging.basicConfig(level=logging.DEBUG if args.verbose else logging.INFO)
  37. # Exit if there's no SDK support for this platform.
  38. try:
  39. GetHostOsFromPlatform()
  40. except:
  41. logging.warning('Fuchsia SDK is not supported on this platform.')
  42. return 0
  43. sdk_override = os.path.join(os.path.dirname(__file__), 'sdk_override.txt')
  44. # Exit if there is no override file.
  45. if not os.path.isfile(sdk_override):
  46. return 0
  47. with open(sdk_override, 'r') as f:
  48. gcs_tarball_prefix = f.read()
  49. # Always re-download the SDK.
  50. logging.info('Downloading GN SDK...')
  51. MakeCleanDirectory(SDK_ROOT)
  52. DownloadAndUnpackFromCloudStorage(_GetTarballPath(gcs_tarball_prefix),
  53. SDK_ROOT)
  54. return 0
  55. if __name__ == '__main__':
  56. sys.exit(main())