[Pypt-offline-general] SF.net SVN: pypt-offline: [120] trunk
Status: Beta
Brought to you by:
riteshsarraf
|
From: <rit...@us...> - 2007-01-15 21:56:25
|
Revision: 120
http://svn.sourceforge.net/pypt-offline/?rev=120&view=rev
Author: riteshsarraf
Date: 2007-01-14 05:12:16 -0800 (Sun, 14 Jan 2007)
Log Message:
-----------
* Merging changes from branch progressbar.
* For now we'll move with a cumulative progressbar.
Modified Paths:
--------------
trunk/pypt_core.py
trunk/pypt_progressbar.py
Modified: trunk/pypt_core.py
===================================================================
--- trunk/pypt_core.py 2007-01-14 09:46:02 UTC (rev 119)
+++ trunk/pypt_core.py 2007-01-14 13:12:16 UTC (rev 120)
@@ -1,5 +1,5 @@
import os, shutil, string, sys, urllib2, Queue, threading
-import pypt_progressbar, pypt_md5_check, pypt_variables, pypt_logger, progressbar
+import pypt_progressbar, pypt_md5_check, pypt_variables, pypt_logger, pypt_progressbar
'''This is the core module. It does the main job of downloading packages/update packages,\nfiguring out if the packages are in the local cache, handling exceptions and many more stuff'''
@@ -123,20 +123,17 @@
size = int(headers['Content-Length'])
data = open(file,'wb')
- log.msg("Downloading %s\n" % (file))
- prog = pypt_progressbar.myReportHook(size, number_of_threads)
- #widgets = ['Test: ', progressbar.Percentage(), ' ', progressbar.Bar(marker=progressbar.RotatingMarker()), ' ', progressbar.ETA(), ' ', progressbar.FileTransferSpeed()]
- #widgets = [CrazyFileTransferSpeed(),' <<<', Bar(), '>>> ', Percentage(),' ', ETA()]
- #pbar = progressbar.ProgressBar(widgets=widgets, maxval=size)
- #pbar.start()
+ progbar.addItem(size)
+
+ log.msg("Downloading %s - %d KB\n" % (file, size/1024))
while i < size:
data.write (temp.read(block_size))
+ increment = min(block_size, size - i)
i += block_size
counter += 1
- #pbar.update(i)
- prog.updateAmount(counter * block_size, thread_name)
- #pbar.finish()
- #print "\n"
+ progbar.updateValue(increment)
+ progbar.completed()
+ log.msg("%s\tdone.\n" % (file))
data.close()
temp.close()
@@ -247,7 +244,7 @@
try:
shutil.copy(os.path.join(path, file), dest_dir)
except shutil.Error:
- log.msg("%s available. Skipping Copy!\n\n" % (file, dest_dir))
+ log.verbose("%s already available in dest_dir. Skipping copy!!!\n\n" % (file))
return True
return False
@@ -334,6 +331,10 @@
arg_type - arg_type is basically used to identify wether it's a update download or upgrade download
'''
+ #INFO: For the Progress Bar
+ global progbar
+ progbar = pypt_progressbar.ProgressBar(width = 30)
+
if arg_type == 1:
#INFO: Oh! We're only downloading the update package list database
# Package Update database changes almost daily in Debian.
@@ -380,8 +381,8 @@
else:
#INFO: Thread Support
if pypt_variables.options.num_of_threads > 1:
- log.msg("WARNING: Threads is still in alpha stage. It's better to use just a single thread at the moment.\n")
- log.warn("Threads is still in alpha stage. It's better to use just a single thread at the moment.\n")
+ log.msg("WARNING: Threads is still in alpha stage. It's better to use just a single thread at the moment.\n\n")
+ log.warn("Threads is still in alpha stage. It's better to use just a single thread at the moment.\n\n")
NUMTHREADS = pypt_variables.options.num_of_threads
ziplock = threading.Lock()
@@ -607,7 +608,7 @@
if len(pypt_variables.errlist) == 0:
pass # Don't print if nothing failed.
else:
- log.err("The following files failed to be downloaded.\n")
+ log.err("\n\nThe following files failed to be downloaded.\n")
for error in pypt_variables.errlist:
log.err("%s failed.\n" % (error))
Modified: trunk/pypt_progressbar.py
===================================================================
--- trunk/pypt_progressbar.py 2007-01-14 09:46:02 UTC (rev 119)
+++ trunk/pypt_progressbar.py 2007-01-14 13:12:16 UTC (rev 120)
@@ -1,54 +1,63 @@
-import sys
+# A minor part of this code is taken from Nilton Volpato's progressbar implementation,
+# precisely the terminal width probe code.
+# Rest of the code was mostly from Dennis Lee Beiber
+import signal, sys
+from array import array
-class progressBar:
- def __init__(self, minValue = 0, maxValue = 10, totalWidth = 12, number_of_threads = 1):
- self.progBar = {}
- for thread in range(number_of_threads):
- self.progBar["Thread-" + str(thread+1)] = ""
- #self.progBar = "[]" # This holds the progress bar string
+if sys.platform == "win32":
+ pass
+else:
+ from fcntl import ioctl
+ import termios
+
+
+class ProgressBar(object):
+ def __init__(self, minValue = 0, maxValue = 0, width = None, fd = sys.stderr):
+ #width does NOT include the two places for [] markers
self.min = minValue
self.max = maxValue
- self.span = maxValue - minValue
- self.width = totalWidth
- self.amount = 0 # When amount == max, we are 100% done
- self.updateAmount(0) # Build progress bar string
-
- def updateAmount(self, newAmount = 0, thread_name = "Thread-1"):
- if newAmount < self.min: newAmount = self.min
- if newAmount > self.max: newAmount = self.max
- self.amount = newAmount
-
- # Figure out the new percent done, round to an integer
- diffFromMin = float(self.amount - self.min)
- percentDone = (diffFromMin / float(self.span)) * 100.0
- percentDone = round(percentDone)
- percentDone = int(percentDone)
-
- # Figure out how many hash bars the percentage should be
- allFull = self.width - 2
- numHashes = (percentDone / 100.0) * allFull
- numHashes = int(round(numHashes))
+ self.span = float(self.max - self.min)
+ self.fd = fd
+ self.signal_set = False
+ if width is None:
+ try:
+ self.handle_resize(None, None)
+ signal.signal(signal.SIGWINCH, self.handle_resize)
+ self.signal_set = True
+ except:
+ self.width = 79 #The standard
+ else:
+ self.width = width
+ self.value = self.min
+ self.items = 0 #count of items being tracked
+ self.complete = 0
- for name in self.progBar.keys():
- if name == thread_name:
- self.progBar[name] = "[" + '#'*numHashes + ' '*(allFull-numHashes) + "]"
-
- percentPlace = (len(self.progBar[name]) / 2) - len(str(percentDone))
- percentString = str(percentDone) + "%"
-
- self.progBar[name] = self.progBar[name][0:percentPlace] + percentString + self.progBar[name][percentPlace+len(percentString):] \
- + " " + str(newAmount/1024) + "KB of " + str(self.max/1024) + "KB"
-
- progress = ""
- keys = self.progBar.keys()
- keys.sort()
- for name in keys:
- progress += self.progBar[name] + " "
- #print self.progBar[name],
- #sys.stdout.write(self.progBar[name] + "\t")
- sys.stdout.write(progress + "\r")
- #sys.stdout.write("\r")
+ def handle_resize(self, signum, frame):
+ h,w=array('h', ioctl(self.fd,termios.TIOCGWINSZ,'\0'*8))[:2]
+ self.width = w
+
+ def updateValue(self, newValue):
+ #require caller to supply a value! newValue is the increment from last call
+ self.value = max(self.min, min(self.max, self.value + newValue))
+ self.display()
-def myReportHook(totalSize, number_of_threads):
- prog = progressBar(0,totalSize,50, number_of_threads)
- return prog
\ No newline at end of file
+ def completed(self):
+ self.complete = self.complete + 1
+ if self.signal_set:
+ signal.signal(signal.SIGWINCH, signal.SIG_DFL)
+ self.display()
+
+ def addItem(self, maxValue):
+ self.max = self.max + maxValue
+ self.span = float(self.max - self.min)
+ self.items = self.items + 1
+ self.display()
+
+ def display(self):
+ print "%3s/%3s items: %s\r" % (self.complete, self.items, str(self)),
+
+ def __str__(self):
+ #compute display fraction
+ percentFilled = ((self.value - self.min) / self.span)
+ widthFilled = int(self.width * percentFilled + 0.5)
+ return ("[" + "#"*widthFilled + " "*(self.width - widthFilled) + "]" + " %5.1f%% of %d KB" % (percentFilled * 100.0, self.max/1024))
This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.
|