util.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. #!/usr/bin/env python
  2. import atexit
  3. import contextlib
  4. import errno
  5. import platform
  6. import re
  7. import shutil
  8. import ssl
  9. import subprocess
  10. import sys
  11. import tarfile
  12. import tempfile
  13. import urllib2
  14. import os
  15. import zipfile
  16. from config import is_verbose_mode
  17. from env_util import get_vs_env
  18. BOTO_DIR = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'vendor',
  19. 'boto'))
  20. def get_host_arch():
  21. """Returns the host architecture with a predictable string."""
  22. host_arch = platform.machine()
  23. # Convert machine type to format recognized by gyp.
  24. if re.match(r'i.86', host_arch) or host_arch == 'i86pc':
  25. host_arch = 'ia32'
  26. elif host_arch in ['x86_64', 'amd64']:
  27. host_arch = 'x64'
  28. elif host_arch.startswith('arm'):
  29. host_arch = 'arm'
  30. # platform.machine is based on running kernel. It's possible to use 64-bit
  31. # kernel with 32-bit userland, e.g. to give linker slightly more memory.
  32. # Distinguish between different userland bitness by querying
  33. # the python binary.
  34. if host_arch == 'x64' and platform.architecture()[0] == '32bit':
  35. host_arch = 'ia32'
  36. return host_arch
  37. def tempdir(prefix=''):
  38. directory = tempfile.mkdtemp(prefix=prefix)
  39. atexit.register(shutil.rmtree, directory)
  40. return directory
  41. @contextlib.contextmanager
  42. def scoped_cwd(path):
  43. cwd = os.getcwd()
  44. os.chdir(path)
  45. try:
  46. yield
  47. finally:
  48. os.chdir(cwd)
  49. @contextlib.contextmanager
  50. def scoped_env(key, value):
  51. origin = ''
  52. if key in os.environ:
  53. origin = os.environ[key]
  54. os.environ[key] = value
  55. try:
  56. yield
  57. finally:
  58. os.environ[key] = origin
  59. def download(text, url, path):
  60. safe_mkdir(os.path.dirname(path))
  61. with open(path, 'wb') as local_file:
  62. if hasattr(ssl, '_create_unverified_context'):
  63. ssl._create_default_https_context = ssl._create_unverified_context
  64. web_file = urllib2.urlopen(url)
  65. file_size = int(web_file.info().getheaders("Content-Length")[0])
  66. downloaded_size = 0
  67. block_size = 128
  68. ci = os.environ.get('CI') == '1'
  69. while True:
  70. buf = web_file.read(block_size)
  71. if not buf:
  72. break
  73. downloaded_size += len(buf)
  74. local_file.write(buf)
  75. if not ci:
  76. percent = downloaded_size * 100. / file_size
  77. status = "\r%s %10d [%3.1f%%]" % (text, downloaded_size, percent)
  78. print status,
  79. if ci:
  80. print "%s done." % (text)
  81. else:
  82. print
  83. return path
  84. def extract_tarball(tarball_path, member, destination):
  85. with tarfile.open(tarball_path) as tarball:
  86. tarball.extract(member, destination)
  87. def extract_zip(zip_path, destination):
  88. if sys.platform == 'darwin':
  89. # Use unzip command on Mac to keep symbol links in zip file work.
  90. execute(['unzip', zip_path, '-d', destination])
  91. else:
  92. with zipfile.ZipFile(zip_path) as z:
  93. z.extractall(destination)
  94. def make_zip(zip_file_path, files, dirs):
  95. safe_unlink(zip_file_path)
  96. if sys.platform == 'darwin':
  97. files += dirs
  98. execute(['zip', '-r', '-y', zip_file_path] + files)
  99. else:
  100. zip_file = zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED)
  101. for filename in files:
  102. zip_file.write(filename, filename)
  103. for dirname in dirs:
  104. for root, _, filenames in os.walk(dirname):
  105. for f in filenames:
  106. zip_file.write(os.path.join(root, f))
  107. zip_file.close()
  108. def rm_rf(path):
  109. try:
  110. shutil.rmtree(path)
  111. except OSError:
  112. pass
  113. def safe_unlink(path):
  114. try:
  115. os.unlink(path)
  116. except OSError as e:
  117. if e.errno != errno.ENOENT:
  118. raise
  119. def safe_mkdir(path):
  120. try:
  121. os.makedirs(path)
  122. except OSError as e:
  123. if e.errno != errno.EEXIST:
  124. raise
  125. def execute(argv, env=os.environ):
  126. if is_verbose_mode():
  127. print ' '.join(argv)
  128. try:
  129. output = subprocess.check_output(argv, stderr=subprocess.STDOUT, env=env)
  130. if is_verbose_mode():
  131. print output
  132. return output
  133. except subprocess.CalledProcessError as e:
  134. print e.output
  135. raise e
  136. def execute_stdout(argv, env=os.environ):
  137. if is_verbose_mode():
  138. print ' '.join(argv)
  139. try:
  140. subprocess.check_call(argv, env=env)
  141. except subprocess.CalledProcessError as e:
  142. print e.output
  143. raise e
  144. else:
  145. execute(argv, env)
  146. def electron_gyp():
  147. SOURCE_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..'))
  148. gyp = os.path.join(SOURCE_ROOT, 'electron.gyp')
  149. with open(gyp) as f:
  150. obj = eval(f.read());
  151. return obj['variables']
  152. def get_electron_version():
  153. return 'v' + electron_gyp()['version%']
  154. def parse_version(version):
  155. if version[0] == 'v':
  156. version = version[1:]
  157. vs = version.split('.')
  158. if len(vs) > 4:
  159. return vs[0:4]
  160. else:
  161. return vs + ['0'] * (4 - len(vs))
  162. def boto_path_dirs():
  163. return [
  164. os.path.join(BOTO_DIR, 'build', 'lib'),
  165. os.path.join(BOTO_DIR, 'build', 'lib.linux-x86_64-2.7')
  166. ]
  167. def run_boto_script(access_key, secret_key, script_name, *args):
  168. env = os.environ.copy()
  169. env['AWS_ACCESS_KEY_ID'] = access_key
  170. env['AWS_SECRET_ACCESS_KEY'] = secret_key
  171. env['PYTHONPATH'] = os.path.pathsep.join(
  172. [env.get('PYTHONPATH', '')] + boto_path_dirs())
  173. boto = os.path.join(BOTO_DIR, 'bin', script_name)
  174. execute([sys.executable, boto] + list(args), env)
  175. def s3put(bucket, access_key, secret_key, prefix, key_prefix, files):
  176. args = [
  177. '--bucket', bucket,
  178. '--prefix', prefix,
  179. '--key_prefix', key_prefix,
  180. '--grant', 'public-read'
  181. ] + files
  182. run_boto_script(access_key, secret_key, 's3put', *args)
  183. def import_vs_env(target_arch):
  184. if sys.platform != 'win32':
  185. return
  186. if target_arch == 'ia32':
  187. vs_arch = 'amd64_x86'
  188. else:
  189. vs_arch = 'x86_amd64'
  190. env = get_vs_env('14.0', vs_arch)
  191. os.environ.update(env)