File indexing completed on 2024-11-17 04:55:14
0001 #!/usr/bin/env python 0002 0003 import atexit 0004 import contextlib 0005 import errno 0006 import platform 0007 import re 0008 import shutil 0009 import ssl 0010 import subprocess 0011 import sys 0012 import tarfile 0013 import tempfile 0014 import urllib2 0015 import os 0016 import zipfile 0017 0018 from config import is_verbose_mode 0019 0020 0021 def get_host_arch(): 0022 """Returns the host architecture with a predictable string.""" 0023 host_arch = platform.machine() 0024 0025 # Convert machine type to format recognized by gyp. 0026 if re.match(r'i.86', host_arch) or host_arch == 'i86pc': 0027 host_arch = 'ia32' 0028 elif host_arch in ['x86_64', 'amd64']: 0029 host_arch = 'x64' 0030 elif host_arch.startswith('arm'): 0031 host_arch = 'arm' 0032 0033 # platform.machine is based on running kernel. It's possible to use 64-bit 0034 # kernel with 32-bit userland, e.g. to give linker slightly more memory. 0035 # Distinguish between different userland bitness by querying 0036 # the python binary. 0037 if host_arch == 'x64' and platform.architecture()[0] == '32bit': 0038 host_arch = 'ia32' 0039 0040 return host_arch 0041 0042 0043 def tempdir(prefix=''): 0044 directory = tempfile.mkdtemp(prefix=prefix) 0045 atexit.register(shutil.rmtree, directory) 0046 return directory 0047 0048 0049 @contextlib.contextmanager 0050 def scoped_cwd(path): 0051 cwd = os.getcwd() 0052 os.chdir(path) 0053 try: 0054 yield 0055 finally: 0056 os.chdir(cwd) 0057 0058 0059 @contextlib.contextmanager 0060 def scoped_env(key, value): 0061 origin = '' 0062 if key in os.environ: 0063 origin = os.environ[key] 0064 os.environ[key] = value 0065 try: 0066 yield 0067 finally: 0068 os.environ[key] = origin 0069 0070 0071 def download(text, url, path): 0072 safe_mkdir(os.path.dirname(path)) 0073 with open(path, 'wb') as local_file: 0074 if hasattr(ssl, '_create_unverified_context'): 0075 ssl._create_default_https_context = ssl._create_unverified_context 0076 0077 web_file = urllib2.urlopen(url) 0078 file_size = int(web_file.info().getheaders("Content-Length")[0]) 0079 downloaded_size = 0 0080 block_size = 128 0081 0082 ci = os.environ.get('CI') == '1' 0083 0084 while True: 0085 buf = web_file.read(block_size) 0086 if not buf: 0087 break 0088 0089 downloaded_size += len(buf) 0090 local_file.write(buf) 0091 0092 if not ci: 0093 percent = downloaded_size * 100. / file_size 0094 status = "\r%s %10d [%3.1f%%]" % (text, downloaded_size, percent) 0095 print status, 0096 0097 if ci: 0098 print "%s done." % (text) 0099 else: 0100 print 0101 return path 0102 0103 0104 def extract_tarball(tarball_path, member, destination): 0105 with tarfile.open(tarball_path) as tarball: 0106 tarball.extract(member, destination) 0107 0108 0109 def extract_zip(zip_path, destination): 0110 if sys.platform == 'darwin': 0111 # Use unzip command on Mac to keep symbol links in zip file work. 0112 execute(['unzip', zip_path, '-d', destination]) 0113 else: 0114 with zipfile.ZipFile(zip_path) as z: 0115 z.extractall(destination) 0116 0117 def make_zip(zip_file_path, files, dirs): 0118 safe_unlink(zip_file_path) 0119 if sys.platform == 'darwin': 0120 files += dirs 0121 execute(['zip', '-r', '-y', zip_file_path] + files) 0122 else: 0123 zip_file = zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED) 0124 for filename in files: 0125 zip_file.write(filename, filename) 0126 for dirname in dirs: 0127 for root, _, filenames in os.walk(dirname): 0128 for f in filenames: 0129 zip_file.write(os.path.join(root, f)) 0130 zip_file.close() 0131 0132 0133 def rm_rf(path): 0134 try: 0135 shutil.rmtree(path) 0136 except OSError: 0137 pass 0138 0139 0140 def safe_unlink(path): 0141 try: 0142 os.unlink(path) 0143 except OSError as e: 0144 if e.errno != errno.ENOENT: 0145 raise 0146 0147 0148 def safe_mkdir(path): 0149 try: 0150 os.makedirs(path) 0151 except OSError as e: 0152 if e.errno != errno.EEXIST: 0153 raise 0154 0155 0156 def execute(argv, env=os.environ): 0157 if is_verbose_mode(): 0158 print ' '.join(argv) 0159 try: 0160 output = subprocess.check_output(argv, stderr=subprocess.STDOUT, env=env) 0161 if is_verbose_mode(): 0162 print output 0163 return output 0164 except subprocess.CalledProcessError as e: 0165 print e.output 0166 raise e 0167 0168 0169 def execute_stdout(argv, env=os.environ): 0170 if is_verbose_mode(): 0171 print ' '.join(argv) 0172 try: 0173 subprocess.check_call(argv, env=env) 0174 except subprocess.CalledProcessError as e: 0175 print e.output 0176 raise e 0177 else: 0178 execute(argv, env) 0179 0180 0181 def atom_gyp(): 0182 SOURCE_ROOT = os.path.abspath(os.path.join(__file__, '..', '..', '..')) 0183 gyp = os.path.join(SOURCE_ROOT, 'atom.gyp') 0184 with open(gyp) as f: 0185 obj = eval(f.read()); 0186 return obj['variables'] 0187 0188 0189 def get_atom_shell_version(): 0190 return 'v' + atom_gyp()['version%'] 0191 0192 0193 def parse_version(version): 0194 if version[0] == 'v': 0195 version = version[1:] 0196 0197 vs = version.split('.') 0198 if len(vs) > 4: 0199 return vs[0:4] 0200 else: 0201 return vs + ['0'] * (4 - len(vs)) 0202 0203 0204 def s3put(bucket, access_key, secret_key, prefix, key_prefix, files): 0205 env = os.environ.copy() 0206 BOTO_DIR = os.path.abspath(os.path.join(__file__, '..', '..', '..', 'vendor', 0207 'boto')) 0208 env['PYTHONPATH'] = os.path.pathsep.join([ 0209 env.get('PYTHONPATH', ''), 0210 os.path.join(BOTO_DIR, 'build', 'lib'), 0211 os.path.join(BOTO_DIR, 'build', 'lib.linux-x86_64-2.7')]) 0212 0213 boto = os.path.join(BOTO_DIR, 'bin', 's3put') 0214 args = [ 0215 sys.executable, 0216 boto, 0217 '--bucket', bucket, 0218 '--access_key', access_key, 0219 '--secret_key', secret_key, 0220 '--prefix', prefix, 0221 '--key_prefix', key_prefix, 0222 '--grant', 'public-read' 0223 ] + files 0224 0225 execute(args, env)