#!/usr/bin/python
-import sys, subprocess, datetime
-f = open('/var/log/emerge.log', 'r')
+import sys, subprocess, datetime, os, re
-a=0
-package_name = "wine"
-lista = []
+PORTDIR = "/usr/portage/"
+LOGFILE = "/var/log/emerge.log"
-for line in f:
- a += 1
- if package_name in line:
- if (">>>" in line) and ("emerge" in line):
+green_start = "\033[32m"
+color_stop = "\033[m"
- string = line.partition(">>>")
-
- time = float(string[0].strip().strip(":"))
+packages = []
- if (":::" in line) and ("completed emerge" in line):
- string = line.partition(":::")
- time2 = float(string[0].strip().strip(":"))
+class package:
+ def __init__(self, name):
+ self.name = name
- print "Started emerging", package_name, "at",
+ self.versions = []
- date = datetime.date.fromtimestamp(time)
- emerge_time = time2-time
- date = (str(date.day) + "/" + str(date.month)
- + "/" + str(date.year))
- print date
+ def add_version(self, version, emerge_time, emerge_date):
- print "Emerge time:", int(emerge_time), "seconds"
- print
+ self.versions.append((version, emerge_time, emerge_date))
- lista.append([date, int(emerge_time)])
-g = open('times', 'w')
+ def print_versions(self):
+ for p in self.versions:
+ print("\t" + self.name + p[0] + " >>> " +
+ self.time(p[1]) + " >>> " +
+ green_start + self.date(p[2]) + color_stop)
-for i in lista:
- line = str(i[0]) + " " + str(i[1]) + "\n"
- g.write(line)
-f.close()
-g.close()
+ def time(self, time):
+
+ days = time/(3600*24)
+ hours = (days - int(days))*24
+ minutes = (hours - int(hours))*60
+ seconds = (minutes - int(minutes))*60
+
+ days = int(days)
+ hours = int(hours)
+ minutes = int(minutes)
+ seconds = int(round(seconds))
+
+ pdays = str()
+ phours = str()
+ pminutes = str()
+ pseconds = str()
+
+ if days > 0:
+ pdays = (green_start + str(days) + color_stop + " day")
+ if days != 1:
+ pdays += "s"
+
+ if hours > 0:
+ phours = (green_start + str(hours) + color_stop + " hour")
+ if hours != 1:
+ phours += "s"
+
+ if minutes > 0:
+ pminutes = (green_start + str(minutes) + color_stop + " minute")
+ if minutes != 1:
+ pminutes += "s"
+
+ pseconds = (green_start + str(seconds) + color_stop + " second")
+ if seconds != 1:
+ pseconds += "s"
+
+ return (pdays + phours + pminutes + pdays)
+
+
+ def date(self, emerge_date):
+ date = datetime.datetime.fromtimestamp(emerge_date)
+
+ year = str(date.year)
+ month = str(date.month)
+ day = str(date.day)
+ hour = str(date.hour)
+ minute = str(date.minute)
+ second = str(date.second)
+
+ date = "{:%d.%m.%Y %H:%M:%S}".format(date)
+
+ return date
+
+
+
+def open_log():
+ """Attempt to open the LOGFILE."""
+
+ try:
+ f = open(LOGFILE, 'r')
+ except IOError as detail:
+ print detail
+ sys.exit(1)
+ finally:
+ return f
+
+
+
+def search_log_for_package(user_package):
+
+ log = open_log()
+
+ packages.insert(0, package(user_package))
+
+ for line in log:
+ if ((">>>" in line) and ("emerge" in line)):
+ if user_package in line:
+ version = line.partition(user_package)[2].partition(' ')[0]
+ digit = version.strip('-')[0].isdigit()
+
+ if digit:
+ time_string = line.partition(">>>")
+ start_time = float(time_string[0].strip().strip(':'))
+
+ elif ((":::" in line) and ("completed emerge" in line)):
+ if user_package in line:
+ if digit:
+ time_string = line.partition(":::")
+ stop_time = float(time_string[0].strip().strip(':'))
+
+ emerge_time = stop_time - start_time
+
+ packages[0].add_version(version, emerge_time, start_time)
+
+
+
+def print_stuff():
+ for p in packages:
+ p.print_versions()
+
+
+
+def main(status, user_package=None):
+
+ if status == "package":
+ search_log_for_package(user_package)
+ print_stuff()
+
+
+ elif status == "current":
+ pass
+
+ elif status == "pretended":
+ pass
+
+
+
+
+if __name__ == "__main__":
+
+ if len(sys.argv) == 1:
+ main("current")
+ sys.exit(1)
+
+ elif sys.argv[1] == "-p":
+ main("pretended")
+ sys.exit(1)
+
+ elif len(sys.argv) > 1:
+ main("package", sys.argv[1])
+