]> git.sthu.org Git - shutils.git/blob - eFindStaleFiles.py
zsh: Add starship support
[shutils.git] / eFindStaleFiles.py
1 #!/usr/bin/python
2 #shuber, 2011-04-11
3
4 __author__ = "Stefan Huber"
5 __email__ = "shuber@cosy.sbg.ac.at"
6
7
8 import os
9 import stat
10 import re
11 import sys
12
13 # portage (output module) and gentoolkit need special path modifications
14 sys.path.insert(0, "/usr/lib/portage/pym")
15 sys.path.insert(0, "/usr/lib/gentoolkit/pym")
16
17 import gentoolkit
18 import portage
19
20
21
22
23
24 def print_dbg(str):
25 sys.stderr.write("\033[0;34m" + str + "\033[m")
26
27
28 def stripSlash(f):
29 if len(f)>1 and f[-1]=="/":
30 return f[:-1]
31 return f
32
33
34 def getNontrackedFiles( directory, trackedFiles):
35
36 directory = stripSlash(directory)
37 print_dbg("Scan '" + directory + "'...\n")
38
39 if not os.access(directory, os.F_OK):
40 print_dbg(" not existing.\n")
41 elif os.path.islink(directory):
42 print_dbg(" symlink, skipping.\n")
43 else:
44
45 # The directory is not tracked -- yield it
46 if not directory in trackedFiles and directory!="/":
47 yield directory
48
49 else:
50 # So 'directory' is tracked -- get its content
51 for dirpath, dirnames, filenames in os.walk(directory):
52
53 # just interested in this directory
54 if dirpath!=directory:
55 continue
56
57 def topath(f):
58 return os.path.join(dirpath,f)
59
60 # check the files
61 for f in filenames:
62 if not topath(f) in trackedFiles:
63 yield topath(f)
64
65 # check the directories and, if necessary, start recursive scan
66 for d in dirnames:
67 if not topath(d) in trackedFiles:
68 yield topath(d)
69 else:
70 for f in getNontrackedFiles(topath(d), trackedFiles):
71 yield f
72
73 break
74
75
76
77 def getTrackedFiles( directory ):
78
79 directory = stripSlash(directory)
80 print_dbg("Get tracked files from '" + directory + "'...\n")
81
82 rxexp = "^" + directory
83 rx = re.compile(rxexp)
84
85 #Get all packages installed
86 root = "/"
87 vartree = portage.db[root]["vartree"]
88 allcpv = vartree.getallcpv()
89 no = 0
90
91 #Now, really get the files
92 result = set()
93 for cpv in allcpv:
94
95 no += 1
96 if no%100 == 0:
97 print_dbg("%d of %d packages done...\n" % (no, len(allcpv)))
98
99 #Get all files of this cpv
100 cpvsplit = cpv.split("/")
101 cat,pkg = cpvsplit[0:2]
102 db = portage.dblink(cat, pkg, root, vartree.settings)
103 cpvfiles = db.getcontents().keys()
104
105 #Check for all files of cpv, whether it matches regex
106 for f in cpvfiles:
107 if rx.search(f) or directory=="/":
108 yield f
109
110
111
112 if __name__ == "__main__":
113
114 # get directories to scan
115 dirs = sys.argv[1:]
116 if len(dirs) == 0:
117 dirs = [os.getcwd()]
118
119 # get the files tracked and which are in the directories
120 trackedfiles = set()
121 for d in dirs:
122 trackedfiles |= set(getTrackedFiles(d))
123
124 # get the non-tracked files
125 for d in dirs:
126 for f in getNontrackedFiles(d, trackedfiles):
127 print f
128