Simplify download function.

This commit is contained in:
Cheng Zhao 2013-06-20 23:10:00 +08:00
parent 993c7ad650
commit 9bcb677012
2 changed files with 18 additions and 11 deletions

View file

@ -1,20 +1,27 @@
#!/usr/bin/env python
import atexit
import errno
import shutil
import tarfile
import tempfile
import urllib2
import os
def download(text, url, filename):
directory = tempfile.mkdtemp(prefix='atom-shell-')
def tempdir(prefix=''):
directory = tempfile.mkdtemp(prefix=prefix)
atexit.register(shutil.rmtree, directory)
return directory
web_file = urllib2.urlopen(url)
file_size = int(web_file.info().getheaders("Content-Length")[0])
downloaded_size = 0
block_size = 128
with open(os.path.join(directory, filename), 'w+') as local_file:
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
while True:
buf = web_file.read(block_size)
if not buf:
@ -29,8 +36,6 @@ def download(text, url, filename):
print
return directory
def extract_tarball(tarball_path, member, path):
with tarfile.open(tarball_path) as tarball:

View file

@ -23,12 +23,14 @@ def main():
return 0
url, filename = get_node_url(args.url, args.version)
directory = download('Download node', url, filename)
directory = tempdir(prefix='atom-shell-')
path = os.path.join(directory, filename)
download('Download node', url, path)
if IS_POSIX:
root_name = 'node-{0}-{1}-x86'.format(args.version, sys.platform)
member = os.path.join(root_name, 'bin', 'node')
extract_tarball(os.path.join(directory, filename), member, directory)
extract_tarball(path, member, directory)
node_path = os.path.join(directory, member)
copy_node(node_path)