You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
3.9 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #!/usr/bin/env python
  2. # Copyright (c) 2017 The WebRTC project authors. All Rights Reserved.
  3. #
  4. # Use of this source code is governed by a BSD-style license
  5. # that can be found in the LICENSE file in the root of the source
  6. # tree. An additional intellectual property rights grant can be found
  7. # in the file PATENTS. All contributing project authors may
  8. # be found in the AUTHORS file in the root of the source tree.
  9. """Utilities for all our deps-management stuff."""
  10. from __future__ import absolute_import
  11. from __future__ import division
  12. from __future__ import print_function
  13. import os
  14. import shutil
  15. import subprocess
  16. import sys
  17. import tarfile
  18. import time
  19. import zipfile
  20. def RunSubprocessWithRetry(cmd):
  21. """Invokes the subprocess and backs off exponentially on fail."""
  22. for i in range(5):
  23. try:
  24. subprocess.check_call(cmd)
  25. return
  26. except subprocess.CalledProcessError as exception:
  27. backoff = pow(2, i)
  28. print('Got %s, retrying in %d seconds...' % (exception, backoff))
  29. time.sleep(backoff)
  30. print('Giving up.')
  31. raise exception
  32. def DownloadFilesFromGoogleStorage(path, auto_platform=True):
  33. print('Downloading files in %s...' % path)
  34. extension = 'bat' if 'win32' in sys.platform else 'py'
  35. cmd = ['download_from_google_storage.%s' % extension,
  36. '--bucket=chromium-webrtc-resources',
  37. '--directory', path]
  38. if auto_platform:
  39. cmd += ['--auto_platform', '--recursive']
  40. subprocess.check_call(cmd)
  41. # Code partially copied from
  42. # https://cs.chromium.org#chromium/build/scripts/common/chromium_utils.py
  43. def RemoveDirectory(*path):
  44. """Recursively removes a directory, even if it's marked read-only.
  45. Remove the directory located at *path, if it exists.
  46. shutil.rmtree() doesn't work on Windows if any of the files or directories
  47. are read-only, which svn repositories and some .svn files are. We need to
  48. be able to force the files to be writable (i.e., deletable) as we traverse
  49. the tree.
  50. Even with all this, Windows still sometimes fails to delete a file, citing
  51. a permission error (maybe something to do with antivirus scans or disk
  52. indexing). The best suggestion any of the user forums had was to wait a
  53. bit and try again, so we do that too. It's hand-waving, but sometimes it
  54. works. :/
  55. """
  56. file_path = os.path.join(*path)
  57. print('Deleting `{}`.'.format(file_path))
  58. if not os.path.exists(file_path):
  59. print('`{}` does not exist.'.format(file_path))
  60. return
  61. if sys.platform == 'win32':
  62. # Give up and use cmd.exe's rd command.
  63. file_path = os.path.normcase(file_path)
  64. for _ in range(3):
  65. print('RemoveDirectory running %s' % (' '.join(
  66. ['cmd.exe', '/c', 'rd', '/q', '/s', file_path])))
  67. if not subprocess.call(['cmd.exe', '/c', 'rd', '/q', '/s', file_path]):
  68. break
  69. print(' Failed')
  70. time.sleep(3)
  71. return
  72. else:
  73. shutil.rmtree(file_path, ignore_errors=True)
  74. def UnpackArchiveTo(archive_path, output_dir):
  75. extension = os.path.splitext(archive_path)[1]
  76. if extension == '.zip':
  77. _UnzipArchiveTo(archive_path, output_dir)
  78. else:
  79. _UntarArchiveTo(archive_path, output_dir)
  80. def _UnzipArchiveTo(archive_path, output_dir):
  81. print('Unzipping {} in {}.'.format(archive_path, output_dir))
  82. zip_file = zipfile.ZipFile(archive_path)
  83. try:
  84. zip_file.extractall(output_dir)
  85. finally:
  86. zip_file.close()
  87. def _UntarArchiveTo(archive_path, output_dir):
  88. print('Untarring {} in {}.'.format(archive_path, output_dir))
  89. tar_file = tarfile.open(archive_path, 'r:gz')
  90. try:
  91. tar_file.extractall(output_dir)
  92. finally:
  93. tar_file.close()
  94. def GetPlatform():
  95. if sys.platform.startswith('win'):
  96. return 'win'
  97. if sys.platform.startswith('linux'):
  98. return 'linux'
  99. if sys.platform.startswith('darwin'):
  100. return 'mac'
  101. raise Exception("Can't run on platform %s." % sys.platform)
  102. def GetExecutableExtension():
  103. return '.exe' if GetPlatform() == 'win' else ''