Skip to content

Instantly share code, notes, and snippets.

@briarfox
Created June 5, 2014 19:49
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save briarfox/f707126dfdd5ec257eab to your computer and use it in GitHub Desktop.
Save briarfox/f707126dfdd5ec257eab to your computer and use it in GitHub Desktop.
get-ShellistaExt.py
'''get-ShellistaExt'''
import urllib2,os,sys,zipfile
def main(line):
'''Gets a file from a link.'''
args = [line,]
if args == None:
return
if len(args)==1:
file_name,ext = os.path.splitext(args[0].split('/')[-1])
try:
u = urllib2.urlopen(args[0])
except :
print 'Invalid url'
f = open('ShellistaExt.zip', 'wb')
meta = u.info()
file_size = int(meta.getheaders("Content-Length")[0])
print "Downloading: %s Bytes: %s" % (file_name+ext, file_size)
file_size_dl = 0
block_sz = 8192
while True:
#pr
buffer = u.read(block_sz)
if not buffer:
break
file_size_dl += len(buffer)
f.write(buffer)
status = r"%10d [%3.2f%%]" % (file_size_dl, file_size_dl * 100. / file_size)
status = status + chr(8)*(len(status)+1)
print status,
f.close()
else:
print 'Error downloading file'
def doZip(line):
"""unzip a zip archive"""
# filename with optional destination
args = [line]
if args is None:
return
elif not (1 <= len(args) <= 2):
print "unzip: Usage: unzip file [destination]"
else:
filename = os.path.abspath(args[0])
if not os.path.isfile(filename):
print "unzip: %s: No such file" % args[0]
else:
# PK magic marker check
f = open(filename)
try:
pk_check = f.read(2)
except Exception:
pk_check = ''
finally:
f.close()
if pk_check != 'PK':
print "unzip: %s: does not appear to be a zip file" % args[0]
else:
if (os.path.basename(filename).lower().endswith('.zip')):
altpath = os.path.splitext(os.path.basename(filename))[0]
else:
altpath = os.path.basename(filename) + '_unzipped'
altpath = os.path.join(os.path.dirname(filename), altpath)
location = (args[1:2] or [altpath])[0]
if (os.path.exists(location)) and not (os.path.isdir(location)):
print "unzip: %s: destination is not a directory" % location
return
elif not os.path.exists(location):
os.makedirs(location)
zipfp = open(filename, 'rb')
try:
zipf = zipfile.ZipFile(zipfp)
# check for a leading directory common to all files and remove it
dirnames = [os.path.join(os.path.dirname(x), '') for x in zipf.namelist()]
common_dir = os.path.commonprefix(dirnames or ['/'])
# Check to make sure there aren't 2 or more sub directories with the same prefix
if not common_dir.endswith('/'):
common_dir = os.path.join(os.path.dirname(common_dir), '')
for name in zipf.namelist():
data = zipf.read(name)
fn = name
if common_dir:
if fn.startswith(common_dir):
fn = fn.split(common_dir, 1)[-1]
elif fn.startswith('/' + common_dir):
fn = fn.split('/' + common_dir, 1)[-1]
fn = fn.lstrip('/')
fn = os.path.join(location, fn)
dirf = os.path.dirname(fn)
if not os.path.exists(dirf):
os.makedirs(dirf)
if fn.endswith('/'):
# A directory
if not os.path.exists(fn):
os.makedirs(fn)
else:
fp = open(fn, 'wb')
try:
fp.write(data)
finally:
fp.close()
except Exception:
zipfp.close()
print "unzip: %s: zip file is corrupt" % args[0]
return
finally:
zipfp.close()
if __name__ == '__main__':
main('https://codeload.github.com/briarfox/ShellistaExt/zip/master')
doZip('ShellistaExt.zip')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment