updated on Wed Jan 25 16:08:47 UTC 2012
[aur-mirror.git] / lupac / lupac
blobb01bcd24d9a9e994778ab8e5879551274813f047
1 #!/usr/bin/python2
3 # Least Used PACkages v0.0.7a
4 # ---------------------------
5 # Luiz Ribeiro
6 # luizribeiro aaattt gmail dddooottt com
7 # venox @ irc.freenode.net
9 import os
10 import re
11 import datetime
12 import time
13 import sys
14 import getopt
15 import locale
17 from operator import itemgetter
19 def usage():
20         print "Usage: %s [OPTIONS]" % (sys.argv[0])
21         print "Lists the installed packages sorted by usage."
22         print "  -a <N>, --ago=<N>   lists only packages that have not been used for <N> days or more"
23         print "  -q, --quiet         don't display status messages"
24         print "  -n, --notable       don't display output in a fancy table"
25         print "  -d, --nodeps        don't check for dependencies while processing (faster, poor results)"
26         print "  -s, --stripdeps     don't show dependencies on the output list (doesn't work with -d)"
27         print "  -y, --days          shows for how long, in days, each package hasn't been used"
28         print "  -h, --help          shows this help screen"
31 def sysexec(cmdLine):
32         cmd = os.popen(cmdLine)
33         return cmd.read()
36 def propagateAccessTime(list, depslist, package, accessTime):
37         if not list.has_key(package) or list[package] < accessTime:
38                 list[package] = accessTime
39                 output = sysexec("LC_ALL=C pacman -Qi '%s' 2>>/dev/null| grep Depends | sed 's/^.*: //g;s/>[^ ]*//g;s/\ \+/\ /g'" % (package))
40                 deps = output.split('\n')[0]
41                 for dep in deps.split(' '):
42                         if not dep or dep == "None": continue
43                         depslist[dep] = True
44                         propagateAccessTime(list, depslist, dep, accessTime)
46 def main():
47         try:
48                 opts, args = getopt.getopt(sys.argv[1:], "hqndsya:", ["help", "quiet", "notable", "nodeps", "stripdeps", "days", "ago="])
49         except getopt.GetoptError:
50                 # print help information and exit:
51                 usage()
52                 sys.exit(2)
54         # default values        
55         a = 0                                   # list only packages that have not been used by at least 'a' days
56         quiet = False                   # display status messages
57         noTable = False                 # display results in a fancy table
58         noDeps = False                  # check for dependencies while processing
59         showDays = False                        # shows for how long packages have been unused, in days
60         stripDeps = False                       # strip dependencies from the output
62         for opt, arg in opts:
63                 if opt in ("-a", "--ago"):
64                         try:
65                                 a = int(arg)
66                         except ValueError:
67                                 usage()
68                                 sys.exit(2)
69                 if opt in ("-d", "--nodeps"):
70                         noDeps = True
71                 if opt in ("-n", "--notable"):
72                         noTable = True
73                 if opt in ("-q", "--quiet"):
74                         quiet = True
75                 if opt in ("-s", "--stripdeps"):
76                         stripDeps = True
77                 if opt in ("-y", "--days"):
78                         showDays = True
79                 if opt in ("-h", "--help"):
80                         usage()
81                         sys.exit()
83         ####
85         if not quiet:
86                 print "Fetching packages database from pacman... ",
87                 sys.stdout.flush()
88         pacmanOutput = sysexec("pacman -Ql")
89         outputLines = pacmanOutput.split('\n')
90         if not quiet:
91                 print "OK"
92         
93         ####
94         
95         if not quiet:
96                 print "Parsing database... ",
97                 sys.stdout.flush()
98         deps = {}                               # dependencies list
99         ltup = {}                               # packages list
100         gpn = 0                         # greatest package length (used to create the fancy table)
101         for line in outputLines:
102                 if line:
103                         # if the line is not empty
104                         (package, file) = line.split(' ', 1)
105                         if os.path.exists(file) and not os.path.isdir(file) and os.stat(file)[7] > 0:
106                                 lastTime = os.stat(file)[7]
107                                 if len(package) > gpn: gpn = len(package)
108                                 if not deps.has_key(package): deps[package] = False
109                                 if noDeps:
110                                         if not ltup.has_key(package) or ltup[package] < lastTime:
111                                                 ltup[package] = lastTime
112                                 else: propagateAccessTime(ltup, deps, package, lastTime)
113         if not quiet:
114                 print "OK"
115         
116         ####
117         
118         if not quiet:
119                 print "Sorting results... ",
120                 sys.stdout.flush()
121         ltup = sorted(ltup.items(), key=itemgetter(1))
122         if not quiet:
123                 print "OK"
124         
125         ####
126         
127         if not quiet:
128                 print "\nSystem packages sorted by usage:"
129         now = time.time()
130         timeAgo = now - a * 3600 * 24
131         for (package, lastAccess) in ltup:
132                 if timeAgo < lastAccess: continue
133                 if stripDeps and deps[package]: continue
134                 if noTable: space = ' '
135                 else: space = (gpn + 3 - len(package)) * ' '
136                 if showDays: print "%s%s%s days" % (package, space, int((now-lastAccess)/(3600*24)))
137                 else: print "%s%s%s" % (package, space, datetime.datetime.fromtimestamp(lastAccess).ctime())
139 if __name__ == "__main__":
140         main()