kozmetik..
This commit is contained in:
+34
-34
@@ -67,7 +67,7 @@ class Fetcher:
|
|||||||
self.percent = 0
|
self.percent = 0
|
||||||
self.rate = 0.0
|
self.rate = 0.0
|
||||||
self.progress = None
|
self.progress = None
|
||||||
self.existsize = 0
|
self.exist_size = 0
|
||||||
|
|
||||||
def fetch (self):
|
def fetch (self):
|
||||||
"""Return value: Fetched file's full path.."""
|
"""Return value: Fetched file's full path.."""
|
||||||
@@ -78,44 +78,44 @@ class Fetcher:
|
|||||||
if not os.access(self.filedest, os.W_OK):
|
if not os.access(self.filedest, os.W_OK):
|
||||||
self.err(_('Access denied to write to destination directory'))
|
self.err(_('Access denied to write to destination directory'))
|
||||||
|
|
||||||
archivefile = os.path.join(self.filedest, self.url.filename())
|
archive_file = os.path.join(self.filedest, self.url.filename())
|
||||||
|
|
||||||
if self.url.is_local_file():
|
if self.url.is_local_file():
|
||||||
self.fetchLocalFile(archivefile + '.part')
|
self.fetchLocalFile(archive_file + '.part')
|
||||||
else:
|
else:
|
||||||
self.fetchRemoteFile(archivefile + '.part')
|
self.fetchRemoteFile(archive_file + '.part')
|
||||||
|
|
||||||
if os.stat(archivefile + '.part').st_size == 0:
|
if os.stat(archive_file + '.part').st_size == 0:
|
||||||
os.remove(archivefile + '.part')
|
os.remove(archive_file + '.part')
|
||||||
self.err(_('A problem occured. Please check the archive address and/or permissions again.'))
|
self.err(_('A problem occured. Please check the archive address and/or permissions again.'))
|
||||||
else:
|
else:
|
||||||
move(archivefile + '.part', archivefile)
|
move(archive_file + '.part', archive_file)
|
||||||
|
|
||||||
return archivefile
|
return archive_file
|
||||||
|
|
||||||
def _do_grab(self, fileURI, dest, totalsize):
|
def _do_grab(self, fileURI, dest, total_size):
|
||||||
symbols = [' B/s', 'KB/s', 'MB/s', 'GB/s']
|
symbols = [' B/s', 'KB/s', 'MB/s', 'GB/s']
|
||||||
bs, tt, = 1024, int(time())
|
bs, tt, = 1024, int(time())
|
||||||
s_time = time()
|
s_time = time()
|
||||||
Tdiff = lambda: time() - s_time
|
Tdiff = lambda: time() - s_time
|
||||||
downloadedsize = existsize = self.existsize
|
downloaded_size = exist_size = self.exist_size
|
||||||
symbol, depth = 'B/s', 0
|
symbol, depth = 'B/s', 0
|
||||||
st = time()
|
st = time()
|
||||||
chunk = fileURI.read(bs)
|
chunk = fileURI.read(bs)
|
||||||
downloadedsize += len(chunk)
|
downloaded_size += len(chunk)
|
||||||
|
|
||||||
if self.progress:
|
if self.progress:
|
||||||
p = self.progress(totalsize, existsize)
|
p = self.progress(total_size, exist_size)
|
||||||
self.percent = p.update(downloadedsize)
|
self.percent = p.update(downloaded_size)
|
||||||
self.complete = False
|
self.complete = False
|
||||||
|
|
||||||
while chunk:
|
while chunk:
|
||||||
dest.write(chunk)
|
dest.write(chunk)
|
||||||
chunk = fileURI.read(bs)
|
chunk = fileURI.read(bs)
|
||||||
downloadedsize += len(chunk)
|
downloaded_size += len(chunk)
|
||||||
ct = time()
|
ct = time()
|
||||||
if int(tt) != int(ct):
|
if int(tt) != int(ct):
|
||||||
self.rate = (downloadedsize - existsize) / (ct - st)
|
self.rate = (downloaded_size - exist_size) / (ct - st)
|
||||||
|
|
||||||
if self.percent:
|
if self.percent:
|
||||||
self.eta = '%02d:%02d:%02d' %\
|
self.eta = '%02d:%02d:%02d' %\
|
||||||
@@ -129,13 +129,13 @@ class Fetcher:
|
|||||||
tt = time()
|
tt = time()
|
||||||
|
|
||||||
if self.progress:
|
if self.progress:
|
||||||
if p.update(downloadedsize):
|
if p.update(downloaded_size):
|
||||||
self.percent = p.percent
|
self.percent = p.percent
|
||||||
if not self.complete:
|
if not self.complete:
|
||||||
ctx.ui.display_progress(filename = self.url.filename(),
|
ctx.ui.display_progress(filename = self.url.filename(),
|
||||||
percent = self.percent,
|
percent = self.percent,
|
||||||
totalsize = totalsize,
|
total_size = total_size,
|
||||||
downloadedsize = downloadedsize,
|
downloaded_size = downloaded_size,
|
||||||
rate = self.rate,
|
rate = self.rate,
|
||||||
eta = self.eta,
|
eta = self.eta,
|
||||||
symbol = symbol)
|
symbol = symbol)
|
||||||
@@ -144,26 +144,26 @@ class Fetcher:
|
|||||||
|
|
||||||
dest.close()
|
dest.close()
|
||||||
|
|
||||||
def fetchLocalFile (self, archivefile):
|
def fetchLocalFile (self, archive_file):
|
||||||
url = self.url
|
url = self.url
|
||||||
|
|
||||||
if not os.access(url.path(), os.F_OK):
|
if not os.access(url.path(), os.F_OK):
|
||||||
self.err(_('No such file or no permission to read'))
|
self.err(_('No such file or no permission to read'))
|
||||||
|
|
||||||
dest = open(archivefile, 'w')
|
dest = open(archive_file, 'w')
|
||||||
totalsize = os.path.getsize(url.path())
|
total_size = os.path.getsize(url.path())
|
||||||
fileObj = open(url.path())
|
fileObj = open(url.path())
|
||||||
self._do_grab(fileObj, dest, totalsize)
|
self._do_grab(fileObj, dest, total_size)
|
||||||
|
|
||||||
def fetchRemoteFile (self, archivefile):
|
def fetchRemoteFile (self, archive_file):
|
||||||
from httplib import HTTPException
|
from httplib import HTTPException
|
||||||
|
|
||||||
if os.path.exists(archivefile):
|
if os.path.exists(archive_file):
|
||||||
if self.scheme == 'http' or self.scheme == 'https' or self.scheme == 'ftp':
|
if self.scheme == 'http' or self.scheme == 'https' or self.scheme == 'ftp':
|
||||||
self.existsize = os.path.getsize(archivefile)
|
self.exist_size = os.path.getsize(archive_file)
|
||||||
dest = open(archivefile, 'ab')
|
dest = open(archive_file, 'ab')
|
||||||
else:
|
else:
|
||||||
dest = open(archivefile, 'wb')
|
dest = open(archive_file, 'wb')
|
||||||
|
|
||||||
uri = self.url.uri
|
uri = self.url.uri
|
||||||
|
|
||||||
@@ -185,15 +185,15 @@ class Fetcher:
|
|||||||
self.err(_('Cannot fetch %s; (%s): %s') % (uri, e.__class__.__name__, e))
|
self.err(_('Cannot fetch %s; (%s): %s') % (uri, e.__class__.__name__, e))
|
||||||
finally:
|
finally:
|
||||||
if flag:
|
if flag:
|
||||||
if os.stat(archivefile).st_size == 0:
|
if os.stat(archive_file).st_size == 0:
|
||||||
os.remove(archivefile)
|
os.remove(archive_file)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
totalsize = int(headers['Content-Length']) + self.existsize
|
total_size = int(headers['Content-Length']) + self.exist_size
|
||||||
except:
|
except:
|
||||||
totalsize = 0
|
total_size = 0
|
||||||
|
|
||||||
self._do_grab(fileObj, dest, totalsize)
|
self._do_grab(fileObj, dest, total_size)
|
||||||
|
|
||||||
def formatRequest(self, request):
|
def formatRequest(self, request):
|
||||||
if self.url.auth_info():
|
if self.url.auth_info():
|
||||||
@@ -206,10 +206,10 @@ class Fetcher:
|
|||||||
'ftp' : FTPRangeHandler
|
'ftp' : FTPRangeHandler
|
||||||
}
|
}
|
||||||
|
|
||||||
if self.existsize and range_handlers.has_key(self.scheme):
|
if self.exist_size and range_handlers.has_key(self.scheme):
|
||||||
opener = urllib2.build_opener(range_handlers.get(self.scheme)())
|
opener = urllib2.build_opener(range_handlers.get(self.scheme)())
|
||||||
urllib2.install_opener(opener)
|
urllib2.install_opener(opener)
|
||||||
request.add_header('Range', 'bytes=%d-' % self.existsize)
|
request.add_header('Range', 'bytes=%d-' % self.exist_size)
|
||||||
|
|
||||||
return request
|
return request
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user