3 # Least Used PACkages v0.0.7a
4 # ---------------------------
6 # luizribeiro aaattt gmail dddooottt com
7 # venox @ irc.freenode.net
17 from operator import itemgetter
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"
32 cmd = os.popen(cmdLine)
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
44 propagateAccessTime(list, depslist, dep, accessTime)
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:
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
63 if opt in ("-a", "--ago"):
69 if opt in ("-d", "--nodeps"):
71 if opt in ("-n", "--notable"):
73 if opt in ("-q", "--quiet"):
75 if opt in ("-s", "--stripdeps"):
77 if opt in ("-y", "--days"):
79 if opt in ("-h", "--help"):
86 print "Fetching packages database from pacman... ",
88 pacmanOutput = sysexec("pacman -Ql")
89 outputLines = pacmanOutput.split('\n')
96 print "Parsing database... ",
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:
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
110 if not ltup.has_key(package) or ltup[package] < lastTime:
111 ltup[package] = lastTime
112 else: propagateAccessTime(ltup, deps, package, lastTime)
119 print "Sorting results... ",
121 ltup = sorted(ltup.items(), key=itemgetter(1))
128 print "\nSystem packages sorted by usage:"
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__":