I've written a python version of the bash script above, which does the same thing, except that:
* it adds the following arguments: -i for interactive mode, and -d for number of seconds between updates.
* the TOTAL header comes after DONE.
* it's much faster than the bash script.
example usage:
Code: Select all
eprogress.py gedit
eprogress.py gedit -i -d2 # default delay is 5 seconds
eprogress.py \* # works like the bash script without args.
Btw, I think that it would be a better idea to use a list of known source extensions/object extensions instead of wildcards.
How many different extensions are there? (For objects I've personally only seen .o, and for source .c, .cpp.)
The script is probably a bit too large to post, but I don't have any webspace to use, so here goes.
Code: Select all
import portage, glob, sys, os, fnmatch, time
from optparse import OptionParser
def err(msg, fatal=True, ret=1):
print >>sys.stderr, "Error:", msg
if fatal:
sys.exit(ret)
if os.getuid() != 0:
err("You have to be root to run this script.")
parser = OptionParser(usage="%prog [options] progname",
version="%prog 0.0.1")
parser.add_option("-i", action="store_true",
dest="interactive", default=False,
help="Interactive mode")
parser.add_option("-d", type="int", dest="delay", default=5,
help="Number of seconds between updates in interactive mode")
options, args = parser.parse_args()
if len(args) != 1:
parser.print_help()
sys.exit(1)
else:
progname = args[0]
for cfg_file in "/etc/make.conf", "/etc/make.globals":
try:
portage_tmpdir = portage.getconfig(cfg_file)["PORTAGE_TMPDIR"]
break
except:
portage_tmpdir = False
if not portage_tmpdir:
err("Could not find PORTAGE_TMPDIR.")
portage_tmpdir += "/portage"
p = os.popen("clear")
clear = p.read()
if p.close() != None:
clear = False
os.chdir(portage_tmpdir)
#
header_format = "%40s%8s%8s%8s%9s"
result_format = "%40s%8s%8s%8s%7s"
# Borrowed from Gherald's bash script.
while True:
dirs = []
dirs_tmp = glob.glob("%s*/work/*" % progname)
for dir in dirs_tmp:
if os.path.isdir(dir):
dirs.append(dir)
if clear:
sys.stdout.write(clear)
for dir in dirs:
package = dir.split('/')[0]
source = []
object = []
print header_format % ("PACKAGE", "DONE", "TOTAL", "LEFT", "PERCENT")
for root, dirs, files in os.walk(dir):
for file in files:
ext = os.path.splitext(file)[1][1:].lower()
if fnmatch.fnmatch(ext, "c*"):
source.append(file)
elif fnmatch.fnmatch(ext, "o"):
object.append(file)
total = len(source)
left = total - len(object)
done = len(object)
try:
percent = done * 100 / total
except:
pass
print result_format % (package, done, total, left, percent)
if not options.interactive:
break
else:
time.sleep(options.delay)