sqlmap/lib/utils/progress.py

106 lines
3.3 KiB
Python
Raw Normal View History

2019-03-21 16:00:09 +03:00
#!/usr/bin/env python2
2008-10-15 19:38:22 +04:00
"""
2019-01-05 23:38:52 +03:00
Copyright (c) 2006-2019 sqlmap developers (http://sqlmap.org/)
2017-10-11 15:50:46 +03:00
See the file 'LICENSE' for copying permission
2008-10-15 19:38:22 +04:00
"""
2018-07-05 16:13:51 +03:00
import time
2008-10-15 19:38:22 +04:00
from lib.core.common import dataToStdout
2019-05-06 01:54:21 +03:00
from lib.core.convert import getUnicode
2010-03-12 15:46:26 +03:00
from lib.core.data import conf
from lib.core.data import kb
2008-10-15 19:38:22 +04:00
class ProgressBar(object):
2008-10-15 19:38:22 +04:00
"""
This class defines methods to update and draw a progress bar
"""
2010-03-12 15:46:26 +03:00
def __init__(self, minValue=0, maxValue=10, totalWidth=None):
self._progBar = "[]"
2012-12-21 18:06:03 +04:00
self._min = int(minValue)
self._max = int(maxValue)
2013-12-17 01:00:47 +04:00
self._span = max(self._max - self._min, 0.001)
2012-12-21 18:06:03 +04:00
self._width = totalWidth if totalWidth else conf.progressWidth
self._amount = 0
2018-07-05 16:13:51 +03:00
self._start = None
2008-10-15 19:38:22 +04:00
self.update()
def _convertSeconds(self, value):
2008-10-15 19:38:22 +04:00
seconds = value
2019-01-22 04:29:52 +03:00
minutes = seconds // 60
2008-10-15 19:38:22 +04:00
seconds = seconds - (minutes * 60)
return "%.2d:%.2d" % (minutes, seconds)
def update(self, newAmount=0):
"""
This method updates the progress bar
"""
2012-12-21 18:06:03 +04:00
if newAmount < self._min:
newAmount = self._min
elif newAmount > self._max:
newAmount = self._max
2008-10-15 19:38:22 +04:00
2012-12-21 18:06:03 +04:00
self._amount = newAmount
2008-10-15 19:38:22 +04:00
# Figure out the new percent done, round to an integer
2012-12-21 18:06:03 +04:00
diffFromMin = float(self._amount - self._min)
percentDone = (diffFromMin / float(self._span)) * 100.0
2008-10-15 19:38:22 +04:00
percentDone = round(percentDone)
2013-12-17 01:00:47 +04:00
percentDone = min(100, int(percentDone))
2008-10-15 19:38:22 +04:00
# Figure out how many hash bars the percentage should be
2018-07-05 16:13:51 +03:00
allFull = self._width - len("100%% [] %s/%s (ETA 00:00)" % (self._max, self._max))
2008-10-15 19:38:22 +04:00
numHashes = (percentDone / 100.0) * allFull
numHashes = int(round(numHashes))
# Build a progress bar with an arrow of equal signs
if numHashes == 0:
self._progBar = "[>%s]" % (" " * (allFull - 1))
2008-10-15 19:38:22 +04:00
elif numHashes == allFull:
self._progBar = "[%s]" % ("=" * allFull)
2008-10-15 19:38:22 +04:00
else:
self._progBar = "[%s>%s]" % ("=" * (numHashes - 1), " " * (allFull - numHashes))
2008-10-15 19:38:22 +04:00
# Add the percentage at the beginning of the progress bar
2010-06-02 16:45:40 +04:00
percentString = getUnicode(percentDone) + "%"
self._progBar = "%s %s" % (percentString, self._progBar)
2008-10-15 19:38:22 +04:00
2018-07-05 16:13:51 +03:00
def progress(self, newAmount):
"""
This method saves item delta time and shows updated progress bar with calculated eta
"""
2018-07-05 16:13:51 +03:00
if self._start is None or newAmount > self._max:
self._start = time.time()
2013-05-09 23:20:17 +04:00
eta = None
else:
2018-07-05 16:13:51 +03:00
delta = time.time() - self._start
eta = (self._max - self._min) * (1.0 * delta / newAmount) - delta
self.update(newAmount)
self.draw(eta)
2013-05-09 23:20:17 +04:00
def draw(self, eta=None):
2008-10-15 19:38:22 +04:00
"""
This method draws the progress bar if it has changed
"""
2018-07-05 16:13:51 +03:00
dataToStdout("\r%s %d/%d%s" % (self._progBar, self._amount, self._max, (" (ETA %s)" % (self._convertSeconds(int(eta)) if eta is not None else "??:??"))))
if self._amount >= self._max:
if not conf.liveTest:
dataToStdout("\r%s\r" % (" " * self._width))
kb.prependFlag = False
else:
dataToStdout("\n")
2008-10-15 19:38:22 +04:00
def __str__(self):
"""
This method returns the progress bar string
"""
return getUnicode(self._progBar)