util.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #!/usr/bin/env python3
  2. import contextlib
  3. import errno
  4. import json
  5. import os
  6. import platform
  7. import shutil
  8. import subprocess
  9. import sys
  10. from urllib.request import urlopen
  11. import zipfile
  12. # from lib.config import is_verbose_mode
  13. def is_verbose_mode():
  14. return False
  15. ELECTRON_DIR = os.path.abspath(
  16. os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
  17. )
  18. TS_NODE = os.path.join(ELECTRON_DIR, 'node_modules', '.bin', 'ts-node')
  19. SRC_DIR = os.path.abspath(os.path.join(__file__, '..', '..', '..', '..'))
  20. if sys.platform in ['win32', 'cygwin']:
  21. TS_NODE += '.cmd'
  22. @contextlib.contextmanager
  23. def scoped_cwd(path):
  24. cwd = os.getcwd()
  25. os.chdir(path)
  26. try:
  27. yield
  28. finally:
  29. os.chdir(cwd)
  30. def download(text, url, path):
  31. safe_mkdir(os.path.dirname(path))
  32. with open(path, 'wb') as local_file, urlopen(url) as web_file:
  33. print(f"Downloading {url} to {path}")
  34. info = web_file.info()
  35. if hasattr(info, 'getheader'):
  36. file_size = int(info.getheaders("Content-Length")[0])
  37. else:
  38. file_size = int(info.get("Content-Length")[0])
  39. downloaded_size = 0
  40. block_size = 4096
  41. ci = os.environ.get('CI') is not None
  42. while True:
  43. buf = web_file.read(block_size)
  44. if not buf:
  45. break
  46. downloaded_size += len(buf)
  47. local_file.write(buf)
  48. if not ci:
  49. percent = downloaded_size * 100. / file_size
  50. status = f"\r{text} {downloaded_size:10d} [{percent:3.1f}%]"
  51. print(status, end=' ')
  52. if ci:
  53. print(f"{text} done.")
  54. else:
  55. print()
  56. return path
  57. def make_zip(zip_file_path, files, dirs):
  58. safe_unlink(zip_file_path)
  59. if sys.platform == 'darwin':
  60. allfiles = files + dirs
  61. execute(['zip', '-r', '-y', zip_file_path] + allfiles)
  62. else:
  63. with zipfile.ZipFile(zip_file_path, "w",
  64. zipfile.ZIP_DEFLATED,
  65. allowZip64=True) as zip_file:
  66. for filename in files:
  67. zip_file.write(filename, filename)
  68. for dirname in dirs:
  69. for root, _, filenames in os.walk(dirname):
  70. for f in filenames:
  71. zip_file.write(os.path.join(root, f))
  72. zip_file.close()
  73. def rm_rf(path):
  74. try:
  75. shutil.rmtree(path)
  76. except OSError:
  77. pass
  78. def safe_unlink(path):
  79. try:
  80. os.unlink(path)
  81. except OSError as e:
  82. if e.errno != errno.ENOENT:
  83. raise
  84. def safe_mkdir(path):
  85. try:
  86. os.makedirs(path)
  87. except OSError as e:
  88. if e.errno != errno.EEXIST:
  89. raise
  90. def execute(argv, env=None, cwd=None):
  91. if env is None:
  92. env = os.environ
  93. if is_verbose_mode():
  94. print(' '.join(argv))
  95. try:
  96. output = subprocess.check_output(argv, stderr=subprocess.STDOUT,
  97. env=env, cwd=cwd)
  98. if is_verbose_mode():
  99. print(output)
  100. return output
  101. except subprocess.CalledProcessError as e:
  102. print(e.output)
  103. raise e
  104. def get_electron_branding():
  105. SOURCE_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..'))
  106. branding_file_path = os.path.join(
  107. SOURCE_ROOT, 'shell', 'app', 'BRANDING.json')
  108. with open(branding_file_path, encoding='utf-8') as file_in:
  109. return json.load(file_in)
  110. cached_electron_version = None
  111. def get_electron_version():
  112. global cached_electron_version
  113. if cached_electron_version is None:
  114. cached_electron_version = str.strip(execute([
  115. 'node',
  116. '-p',
  117. 'require("./script/lib/get-version").getElectronVersion()'
  118. ], cwd=ELECTRON_DIR).decode())
  119. return cached_electron_version
  120. def store_artifact(prefix, key_prefix, files):
  121. # Azure Storage
  122. azput(prefix, key_prefix, files)
  123. def azput(prefix, key_prefix, files):
  124. env = os.environ.copy()
  125. output = execute([
  126. 'node',
  127. os.path.join(os.path.dirname(__file__), 'azput.js'),
  128. '--prefix', prefix,
  129. '--key_prefix', key_prefix,
  130. ] + files, env)
  131. print(output)
  132. def get_out_dir():
  133. out_dir = 'Default'
  134. override = os.environ.get('ELECTRON_OUT_DIR')
  135. if override is not None:
  136. out_dir = override
  137. return os.path.join(SRC_DIR, 'out', out_dir)
  138. # NOTE: This path is not created by gn, it is used as a scratch zone by our
  139. # upload scripts
  140. def get_dist_dir():
  141. return os.path.join(get_out_dir(), 'gen', 'electron_dist')
  142. def get_electron_exec():
  143. out_dir = get_out_dir()
  144. if sys.platform == 'darwin':
  145. return f'{out_dir}/Electron.app/Contents/MacOS/Electron'
  146. if sys.platform == 'win32':
  147. return f'{out_dir}/electron.exe'
  148. if sys.platform == 'linux':
  149. return f'{out_dir}/electron'
  150. raise Exception(
  151. f"get_electron_exec: unexpected platform '{sys.platform}'")
  152. def get_buildtools_executable(name):
  153. buildtools = os.path.realpath(os.path.join(ELECTRON_DIR, '..', 'buildtools'))
  154. if sys.platform == 'darwin':
  155. chromium_platform = 'mac_arm64' if platform.machine() == 'arm64' else 'mac'
  156. elif sys.platform in ['win32', 'cygwin']:
  157. chromium_platform = 'win'
  158. elif sys.platform in ['linux', 'linux2']:
  159. chromium_platform = 'linux64'
  160. else:
  161. raise Exception(f"Unsupported platform: {sys.platform}")
  162. if name == 'clang-format':
  163. chromium_platform += '-format'
  164. path = os.path.join(buildtools, chromium_platform, name)
  165. if sys.platform == 'win32':
  166. path += '.exe'
  167. return path
  168. def get_depot_tools_executable(name):
  169. buildtools = os.path.realpath(
  170. os.path.join(ELECTRON_DIR, '..', 'third_party', 'depot_tools'))
  171. path = os.path.join(buildtools, name)
  172. if sys.platform == 'win32':
  173. path += '.bat'
  174. return path
  175. def get_linux_binaries():
  176. return [
  177. 'chrome-sandbox',
  178. 'chrome_crashpad_handler',
  179. get_electron_branding()['project_name'],
  180. 'libEGL.so',
  181. 'libGLESv2.so',
  182. 'libffmpeg.so',
  183. 'libvk_swiftshader.so',
  184. ]