basit progress
This commit is contained in:
+45
-8
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
# python standard library modules
|
# python standard library modules
|
||||||
import urlparse
|
import urlparse
|
||||||
import urllib
|
import urllib, urllib2
|
||||||
import os
|
import os
|
||||||
from sys import argv
|
from sys import argv
|
||||||
from sys import exit
|
from sys import exit
|
||||||
@@ -13,6 +13,12 @@ import pisiconfig
|
|||||||
class FetchError (Exception):
|
class FetchError (Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
|
||||||
|
class ProgressMeter:
|
||||||
|
def __init__ (self,):
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Fetcher:
|
class Fetcher:
|
||||||
"""Yet another Pisi tool for fetching files from various sources.."""
|
"""Yet another Pisi tool for fetching files from various sources.."""
|
||||||
def __init__(self, uri):
|
def __init__(self, uri):
|
||||||
@@ -35,7 +41,7 @@ class Fetcher:
|
|||||||
if os.access(self.filedest, os.W_OK) == False:
|
if os.access(self.filedest, os.W_OK) == False:
|
||||||
self.err("no perm to write destination")
|
self.err("no perm to write destination")
|
||||||
|
|
||||||
scheme_err = lambda: self.err("unexpected scheme (expecting file, http or ftp)")
|
scheme_err = lambda: self.err("unexpected scheme")
|
||||||
|
|
||||||
actions = {
|
actions = {
|
||||||
'file': self.fetchLocalFile,
|
'file': self.fetchLocalFile,
|
||||||
@@ -52,17 +58,48 @@ class Fetcher:
|
|||||||
copyfile(self.filepath, self.filedest + "/" + self.filename)
|
copyfile(self.filepath, self.filedest + "/" + self.filename)
|
||||||
|
|
||||||
def fetchRemoteFile (self):
|
def fetchRemoteFile (self):
|
||||||
try:
|
from httplib import HTTPException
|
||||||
file = urllib.urlopen(self.uri)
|
|
||||||
|
|
||||||
except IOError, e:
|
try:
|
||||||
self.err(e.strerror[1])
|
file = urllib2.urlopen(self.uri)
|
||||||
|
headers = file.info()
|
||||||
|
|
||||||
|
except ValueError, e:
|
||||||
|
self.err('%s' % (e, ))
|
||||||
|
except IOError, e:
|
||||||
|
self.err('%s' % (e, ))
|
||||||
|
except OSError, e:
|
||||||
|
self.err('%s' % (e, ))
|
||||||
|
except HTTPException, e:
|
||||||
|
self.err(('(%s): %s') % (e.__class__.__name__, e))
|
||||||
|
|
||||||
|
if not headers is None and not headers.has_key('Content-Length'):
|
||||||
|
self.err('file not found')
|
||||||
|
else: totalsize = int(headers['Content-Length'])
|
||||||
|
|
||||||
dest = open(self.filedest + "/" + self.filename , "w")
|
dest = open(self.filedest + "/" + self.filename , "w")
|
||||||
dest.write(file.read())
|
|
||||||
|
bs = 1024*4
|
||||||
|
size = 0
|
||||||
|
oldpercent = -1
|
||||||
|
#FIXME: güzel, flexible bir progress nesnesi hazırlamak lazım.
|
||||||
|
# şimdilik böyle kalsın, haftasonu hallederim..
|
||||||
|
chunk = file.read(bs)
|
||||||
|
size = size + len(chunk)
|
||||||
|
while chunk:
|
||||||
|
dest.write(chunk)
|
||||||
|
chunk = file.read(bs)
|
||||||
|
size = size + len(chunk)
|
||||||
|
percent = (size * 100) / totalsize
|
||||||
|
if size != totalsize and oldpercent != percent:
|
||||||
|
print "%%%d" % (percent)
|
||||||
|
oldpercent = percent
|
||||||
|
|
||||||
|
dest.close(); print "%100"
|
||||||
|
|
||||||
def err (self, error):
|
def err (self, error):
|
||||||
raise FetchError, error
|
raise FetchError(error)
|
||||||
|
|
||||||
|
|
||||||
def usage():
|
def usage():
|
||||||
print "Usage: %s URI" % argv[0]
|
print "Usage: %s URI" % argv[0]
|
||||||
|
|||||||
@@ -3,7 +3,7 @@
|
|||||||
|
|
||||||
# mkdir /var/tmp/pisi/cache
|
# mkdir /var/tmp/pisi/cache
|
||||||
# ./fetch_file file:///etc/passwd
|
# ./fetch_file file:///etc/passwd
|
||||||
# ./fetch http://www.uludag.org.tr/belgeler/gelistirici/gelistirici.pdf
|
# ./fetch http://cekirdek.uludag.org.tr/~meren/Imagi.mp3
|
||||||
# ./fetch_file ftp://ftp.uludag.org.tr/pub/md5summer.exe
|
# ./fetch_file ftp://ftp.uludag.org.tr/pub/md5summer.exe
|
||||||
# ls /var/tmp/pisi/cache
|
# ls /var/tmp/pisi/cache
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user