Simplify download function.
This commit is contained in:
parent
993c7ad650
commit
9bcb677012
2 changed files with 18 additions and 11 deletions
|
@ -1,20 +1,27 @@
|
||||||
|
#!/usr/bin/env python
|
||||||
|
|
||||||
import atexit
|
import atexit
|
||||||
|
import errno
|
||||||
import shutil
|
import shutil
|
||||||
import tarfile
|
import tarfile
|
||||||
import tempfile
|
import tempfile
|
||||||
import urllib2
|
import urllib2
|
||||||
|
import os
|
||||||
|
|
||||||
|
|
||||||
def download(text, url, filename):
|
def tempdir(prefix=''):
|
||||||
directory = tempfile.mkdtemp(prefix='atom-shell-')
|
directory = tempfile.mkdtemp(prefix=prefix)
|
||||||
atexit.register(shutil.rmtree, directory)
|
atexit.register(shutil.rmtree, directory)
|
||||||
|
return directory
|
||||||
|
|
||||||
|
|
||||||
|
def download(text, url, path):
|
||||||
|
with open(path, 'w') as local_file:
|
||||||
web_file = urllib2.urlopen(url)
|
web_file = urllib2.urlopen(url)
|
||||||
file_size = int(web_file.info().getheaders("Content-Length")[0])
|
file_size = int(web_file.info().getheaders("Content-Length")[0])
|
||||||
downloaded_size = 0
|
downloaded_size = 0
|
||||||
block_size = 128
|
block_size = 128
|
||||||
|
|
||||||
with open(os.path.join(directory, filename), 'w+') as local_file:
|
|
||||||
while True:
|
while True:
|
||||||
buf = web_file.read(block_size)
|
buf = web_file.read(block_size)
|
||||||
if not buf:
|
if not buf:
|
||||||
|
@ -29,8 +36,6 @@ def download(text, url, filename):
|
||||||
|
|
||||||
print
|
print
|
||||||
|
|
||||||
return directory
|
|
||||||
|
|
||||||
|
|
||||||
def extract_tarball(tarball_path, member, path):
|
def extract_tarball(tarball_path, member, path):
|
||||||
with tarfile.open(tarball_path) as tarball:
|
with tarfile.open(tarball_path) as tarball:
|
||||||
|
|
|
@ -23,12 +23,14 @@ def main():
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
url, filename = get_node_url(args.url, args.version)
|
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:
|
if IS_POSIX:
|
||||||
root_name = 'node-{0}-{1}-x86'.format(args.version, sys.platform)
|
root_name = 'node-{0}-{1}-x86'.format(args.version, sys.platform)
|
||||||
member = os.path.join(root_name, 'bin', 'node')
|
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)
|
node_path = os.path.join(directory, member)
|
||||||
|
|
||||||
copy_node(node_path)
|
copy_node(node_path)
|
||||||
|
|
Loading…
Reference in a new issue