Created
October 12, 2015 00:23
-
-
Save ptomulik/2d988ae760b992da62f8 to your computer and use it in GitHub Desktop.
Download and untar file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env python | |
def download_and_untar(url, **kw): | |
import os | |
import tarfile | |
from io import BytesIO | |
try: from urllib.request import urlopen | |
except ImportError: from urllib import urlopen | |
# Options | |
try: strip_components = kw['strip_components'] | |
except KeyError: strip_components = 0 | |
# Download the tar file | |
tar = tarfile.open(fileobj = BytesIO(urlopen(url).read())) | |
members = [m for m in tar.getmembers() if len(m.name.split(os.sep)) > strip_components] | |
if strip_components > 0: | |
for m in members: | |
m.name = os.path.join(*(m.name.split(os.sep)[strip_components:])) | |
tar.extractall(members = members) | |
tar.close() | |
url = "https://github.com/CxxTest/cxxtest/archive/master.tar.gz" | |
download_and_untar(url, strip_components = 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment