Skip to content

Instantly share code, notes, and snippets.

@mekanoe
Last active August 29, 2015 13:57
Show Gist options
  • Save mekanoe/9404781 to your computer and use it in GitHub Desktop.
Save mekanoe/9404781 to your computer and use it in GitHub Desktop.
ct tool
#!/usr/bin/env python
# ct - cURL then (un)tar
# for normal use, it replaces
# $ curl <tarball url> | tar -z?f -
# also has weird zip stuff that acts the same way.
# NOW WITH RPM+YUM SUPPORT
import posixpath
import subprocess
import sys
try:
import urlparse
except ImportError:
import urllib.parse as urlparse
try:
url = sys.argv[1]
except IndexError:
url = None
if url is None:
print("""ct - cURL then (un)tar
usage: ct <tarball url>""")
sys.exit(1)
# find the magic ending
base = posixpath.basename(urlparse.urlsplit(url).path)
file = base.split(".")[-1]
zip = False
rpm = False
caller = "curl --silent %s | tar -x%sf -"
if file == "gz" or file == "tgz":
type = "z"
elif file == "bz2" or file == "tbz2":
type = "j"
elif file == "xz" or file == "txz":
type = "J"
elif file == "lzma":
type = "lzma"
caller = "curl --silent %s | tar --lzma -xf -; echo \"%s\" > /dev/null"
elif file == "lz":
type = "lzip"
caller = "curl --silent %s | tar --lzip -xf -; echo \"%s\" > /dev/null"
elif file == "zip":
zip = True
elif file == "rpm":
rpm = True
else:
print("Unknown file ending, I have no idea how to handle this file.")
sys.exit(1)
if zip:
subprocess.call(("wget -q %s" % (url)).split(" "))
subprocess.call(("unzip -qq %s -d %s" % (base, ".".join(base.split(".")[:-1]))).split(" "))
subprocess.call(("rm %s" % (base)).split(" "))
elif rpm:
subprocess.call(("sudo yum localinstall -y %s" % (url)).split(" "))
else:
subprocess.call(caller % (url, type), shell=True)
if not rpm:
print("Extracted probably into %s." % ".".join(base.split(".")[:-1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment