CVS: packaging get.py,NONE,1.1
"Chris Liechti" <[email protected]> Wed, 31 Dec 2008 00:15:20 +0000
| Newsgroups | gmane.comp.hardware.texas-instruments.msp430.gcc.cvs |
|---|---|
| Message-ID | <[email protected]> |
Update of /cvsroot/mspgcc/packaging In directory 23jxhf1.ch3.sourceforge.com:/tmp/cvs-serv31686 Added Files: get.py Log Message: a really simplified download tool for the poor w/o wget --- NEW FILE: get.py --- """\ Simple Internet download tool. For those who don't have wget ;-) (C) 2008 <[email protected]> """ import sys import urllib2 import urlparse import posixpath import time def write_bar(fileobj, offset, size): # calcualte how far the download is level = offset*60/size # calculate the width of done and todo parts of the bar dots = '='*level fill = ' '*(60-level) # write bar and percentage, overprint itself (\r) fileobj.write('\r [%s%s] %3d%%' % (dots, fill, offset*100.0/size)) # make sure output is shown in case of buffered terminals. fileobj.flush() def download_file(url): sys.stderr.write('%s:\n' % (url,)) # check URL and find filename urlpath = urlparse.urlparse(url)[2] filename = posixpath.basename(urlpath) # open the URL and destination, find out size source = urllib2.urlopen(url) size = int(source.info().getheader('Content-Length')) destination = open(filename, 'wb') # copy blocks from source to destination, in small blocks, progress bar blocksize = 32*1024 for offset in xrange(0, size, blocksize): destination.write(source.read(blocksize)) write_bar(sys.stderr, offset, size) # make sure 100% is displayed write_bar(sys.stderr, size, size) # finalize output and close all source.close() destination.close() # done, return number of bytes return size if __name__ == '__main__': import optparse parser = optparse.OptionParser(usage="""\ %prog [options] URL [URL ...] Simple HTTP dowloader. """) (options, args) = parser.parse_args() # sanity checks if len(args) != 1: parser.error("expected exactly one URL as parameter") # download files, measure time size = 0 start_time = time.time() for url in args: size += download_file(url) end_time = time.time() # finalize output duration = end_time - start_time if duration == 0: duration = 0.010 # ensure no zero division sys.stderr.write('\n\ndownloaded %dkB in %.1fs (%.1fkB/s)\n' % ( size/1024, duration, (size/1024) / duration )) sys.stderr.flush() ------------------------------------------------------------------------------