2013-06-20 15:10:00 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
2013-06-20 14:51:58 +00:00
|
|
|
import atexit
|
2013-06-29 03:36:02 +00:00
|
|
|
import contextlib
|
2013-06-20 15:10:00 +00:00
|
|
|
import errno
|
2013-06-20 14:51:58 +00:00
|
|
|
import shutil
|
2013-06-21 02:32:57 +00:00
|
|
|
import subprocess
|
|
|
|
import sys
|
2013-06-20 14:51:58 +00:00
|
|
|
import tarfile
|
|
|
|
import tempfile
|
|
|
|
import urllib2
|
2013-06-20 15:10:00 +00:00
|
|
|
import os
|
2013-06-20 15:23:22 +00:00
|
|
|
import zipfile
|
2013-06-20 14:51:58 +00:00
|
|
|
|
|
|
|
|
2013-06-20 15:10:00 +00:00
|
|
|
def tempdir(prefix=''):
|
|
|
|
directory = tempfile.mkdtemp(prefix=prefix)
|
2013-06-20 14:51:58 +00:00
|
|
|
atexit.register(shutil.rmtree, directory)
|
2013-06-20 15:10:00 +00:00
|
|
|
return directory
|
|
|
|
|
2013-06-20 14:51:58 +00:00
|
|
|
|
2013-06-29 03:36:02 +00:00
|
|
|
@contextlib.contextmanager
|
|
|
|
def scoped_cwd(path):
|
|
|
|
cwd = os.getcwd()
|
|
|
|
os.chdir(path)
|
|
|
|
try:
|
|
|
|
yield
|
|
|
|
finally:
|
|
|
|
os.chdir(cwd)
|
|
|
|
|
|
|
|
|
2013-06-20 15:10:00 +00:00
|
|
|
def download(text, url, path):
|
|
|
|
with open(path, 'w') as local_file:
|
|
|
|
web_file = urllib2.urlopen(url)
|
|
|
|
file_size = int(web_file.info().getheaders("Content-Length")[0])
|
|
|
|
downloaded_size = 0
|
|
|
|
block_size = 128
|
2013-06-20 14:51:58 +00:00
|
|
|
|
|
|
|
while True:
|
|
|
|
buf = web_file.read(block_size)
|
|
|
|
if not buf:
|
|
|
|
break
|
|
|
|
|
|
|
|
downloaded_size += len(buf)
|
|
|
|
local_file.write(buf)
|
|
|
|
|
|
|
|
percent = downloaded_size * 100. / file_size
|
|
|
|
status = "\r%s %10d [%3.1f%%]" % (text, downloaded_size, percent)
|
|
|
|
print status,
|
|
|
|
|
|
|
|
print
|
|
|
|
|
|
|
|
|
2013-06-20 15:23:22 +00:00
|
|
|
def extract_tarball(tarball_path, member, destination):
|
2013-06-20 14:51:58 +00:00
|
|
|
with tarfile.open(tarball_path) as tarball:
|
2013-06-20 15:23:22 +00:00
|
|
|
tarball.extract(member, destination)
|
|
|
|
|
|
|
|
|
|
|
|
def extract_zip(zip_path, destination):
|
2013-06-21 02:32:57 +00:00
|
|
|
if sys.platform == 'darwin':
|
|
|
|
# Use unzip command on Mac to keep symbol links in zip file work.
|
2013-11-18 03:41:44 +00:00
|
|
|
subprocess.check_output(['unzip', zip_path, '-d', destination])
|
2013-06-21 02:32:57 +00:00
|
|
|
else:
|
|
|
|
with zipfile.ZipFile(zip_path) as z:
|
|
|
|
z.extractall(destination)
|
2013-06-20 14:51:58 +00:00
|
|
|
|
2013-10-26 09:23:16 +00:00
|
|
|
def make_zip(zip_file_path, files, dirs):
|
2013-08-31 01:37:02 +00:00
|
|
|
safe_unlink(zip_file_path)
|
|
|
|
if sys.platform == 'darwin':
|
2013-10-26 09:23:16 +00:00
|
|
|
files += dirs
|
2013-11-18 03:41:44 +00:00
|
|
|
subprocess.check_output(['zip', '-r', '-y', zip_file_path] + files)
|
2013-08-31 01:37:02 +00:00
|
|
|
else:
|
2013-10-26 09:42:12 +00:00
|
|
|
zip_file = zipfile.ZipFile(zip_file_path, "w", zipfile.ZIP_DEFLATED)
|
2013-08-31 01:37:02 +00:00
|
|
|
for filename in files:
|
|
|
|
zip_file.write(filename, filename)
|
2013-10-26 09:23:16 +00:00
|
|
|
for dirname in dirs:
|
|
|
|
for root, _, filenames in os.walk(dirname):
|
|
|
|
for f in filenames:
|
|
|
|
zip_file.write(os.path.join(root, f))
|
2013-08-31 01:37:02 +00:00
|
|
|
zip_file.close()
|
|
|
|
|
2013-06-20 14:51:58 +00:00
|
|
|
|
2013-06-24 09:51:48 +00:00
|
|
|
def rm_rf(path):
|
|
|
|
try:
|
|
|
|
shutil.rmtree(path)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
2013-06-20 14:51:58 +00:00
|
|
|
def safe_unlink(path):
|
|
|
|
try:
|
|
|
|
os.unlink(path)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.ENOENT:
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
def safe_mkdir(path):
|
|
|
|
try:
|
|
|
|
os.makedirs(path)
|
|
|
|
except OSError as e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2013-08-12 07:01:05 +00:00
|
|
|
|
|
|
|
|
|
|
|
def get_atom_shell_version():
|
|
|
|
return subprocess.check_output(['git', 'describe', '--tags']).strip()
|