Gentoo Archives: gentoo-portage-dev

From: Matt Turner <mattst88@g.o>
To: gentoo-portage-dev@l.g.o
Cc: Matt Turner <mattst88@g.o>
Subject: [gentoo-portage-dev] [PATCH gentoolkit 1/2] eclean: Rewrite findPackages()
Date: Fri, 21 Feb 2020 05:29:53
Message-Id: 20200221052945.972092-1-mattst88@gentoo.org
1 I found the original code to be nearly incomprehensible. Instead of
2 populating a dict of potential binpkgs to remove and then removing from
3 the to-be-removed list, just selectively add to-be-removed packages.
4
5 Signed-off-by: Matt Turner <mattst88@g.o>
6 ---
7 I switched from tabs to spaces in the process. I can revert back if
8 desired.
9
10 pym/gentoolkit/eclean/search.py | 189 ++++++++++++++++----------------
11 1 file changed, 94 insertions(+), 95 deletions(-)
12
13 diff --git a/pym/gentoolkit/eclean/search.py b/pym/gentoolkit/eclean/search.py
14 index 58bd97e..831ba39 100644
15 --- a/pym/gentoolkit/eclean/search.py
16 +++ b/pym/gentoolkit/eclean/search.py
17 @@ -489,98 +489,97 @@ class DistfilesSearch(object):
18
19
20 def findPackages(
21 - options,
22 - exclude=None,
23 - destructive=False,
24 - time_limit=0,
25 - package_names=False,
26 - pkgdir=None,
27 - port_dbapi=portage.db[portage.root]["porttree"].dbapi,
28 - var_dbapi=portage.db[portage.root]["vartree"].dbapi
29 - ):
30 - """Find all obsolete binary packages.
31 -
32 - XXX: packages are found only by symlinks.
33 - Maybe i should also return .tbz2 files from All/ that have
34 - no corresponding symlinks.
35 -
36 - @param options: dict of options determined at runtime
37 - @param exclude: an exclusion dict as defined in
38 - exclude.parseExcludeFile class.
39 - @param destructive: boolean, defaults to False
40 - @param time_limit: integer time value as returned by parseTime()
41 - @param package_names: boolean, defaults to False.
42 - used only if destructive=True
43 - @param pkgdir: path to the binary package dir being checked
44 - @param port_dbapi: defaults to portage.db[portage.root]["porttree"].dbapi
45 - can be overridden for tests.
46 - @param var_dbapi: defaults to portage.db[portage.root]["vartree"].dbapi
47 - can be overridden for tests.
48 -
49 - @rtype: dict
50 - @return clean_me i.e. {'cat/pkg-ver.tbz2': [filepath],}
51 - """
52 - if exclude is None:
53 - exclude = {}
54 - clean_me = {}
55 - # create a full package dictionary
56 -
57 - # now do an access test, os.walk does not error for "no read permission"
58 - try:
59 - test = os.listdir(pkgdir)
60 - del test
61 - except EnvironmentError as er:
62 - if options['ignore-failure']:
63 - exit(0)
64 - print( pp.error("Error accessing PKGDIR." ), file=sys.stderr)
65 - print( pp.error("(Check your make.conf file and environment)."), file=sys.stderr)
66 - print( pp.error("Error: %s" %str(er)), file=sys.stderr)
67 - exit(1)
68 -
69 - # if portage supports FEATURES=binpkg-multi-instance, then
70 - # cpv_all can return multiple instances per cpv, where
71 - # instances are distinguishable by some extra attributes
72 - # provided by portage's _pkg_str class
73 - bin_dbapi = portage.binarytree(pkgdir=pkgdir, settings=var_dbapi.settings).dbapi
74 - for cpv in bin_dbapi.cpv_all():
75 - mtime = int(bin_dbapi.aux_get(cpv, ['_mtime_'])[0])
76 - if time_limit and mtime >= time_limit:
77 - # time-limit exclusion
78 - continue
79 - # dict is cpv->[pkgs] (supports binpkg-multi-instance)
80 - clean_me.setdefault(cpv, []).append(cpv)
81 -
82 - # keep only obsolete ones
83 - if destructive and package_names:
84 - cp_all = dict.fromkeys(var_dbapi.cp_all())
85 - else:
86 - cp_all = {}
87 - for cpv in list(clean_me):
88 - if exclDictMatchCP(exclude,portage.cpv_getkey(cpv)):
89 - # exclusion because of the exclude file
90 - del clean_me[cpv]
91 - continue
92 - if not destructive and port_dbapi.cpv_exists(cpv):
93 - # exclusion because pkg still exists (in porttree)
94 - del clean_me[cpv]
95 - continue
96 - if destructive and var_dbapi.cpv_exists(cpv):
97 - buildtime = var_dbapi.aux_get(cpv, ['BUILD_TIME'])[0]
98 - clean_me[cpv] = [pkg for pkg in clean_me[cpv]
99 - # only keep path if BUILD_TIME is identical with vartree
100 - if bin_dbapi.aux_get(pkg, ['BUILD_TIME'])[0] != buildtime]
101 - if not clean_me[cpv]:
102 - # nothing we can clean for this package
103 - del clean_me[cpv]
104 - continue
105 - if portage.cpv_getkey(cpv) in cp_all and port_dbapi.cpv_exists(cpv):
106 - # exclusion because of --package-names
107 - del clean_me[cpv]
108 -
109 - # the getname method correctly supports FEATURES=binpkg-multi-instance,
110 - # allowing for multiple paths per cpv (the API used here is also compatible
111 - # with older portage which does not support binpkg-multi-instance)
112 - for cpv, pkgs in clean_me.items():
113 - clean_me[cpv] = [bin_dbapi.bintree.getname(pkg) for pkg in pkgs]
114 -
115 - return clean_me
116 + options,
117 + exclude=None,
118 + destructive=False,
119 + time_limit=0,
120 + package_names=False,
121 + pkgdir=None,
122 + port_dbapi=portage.db[portage.root]["porttree"].dbapi,
123 + var_dbapi=portage.db[portage.root]["vartree"].dbapi
124 + ):
125 + """Find obsolete binary packages.
126 +
127 + @param options: dict of options determined at runtime
128 + @type options: dict
129 + @param exclude: exclusion dict (as defined in the exclude.parseExcludeFile class)
130 + @type exclude: dict, optional
131 + @param destructive: binpkg is obsolete if not installed (default: `False`)
132 + @type destructive: bool, optional
133 + @param time_limit: binpkg is obsolete if older than time value as returned by parseTime()
134 + @type time_limit: int, optional
135 + @param package_names: exclude all binpkg versions if package is installed
136 + (used with `destructive=True`) (default: `False`)
137 + @type package_names: bool, optional
138 + @param pkgdir: path to the binpkg cache (PKGDIR)
139 + @type pkgdir: str
140 + @param port_dbapi: defaults to portage.db[portage.root]["porttree"].dbapi
141 + Can be overridden for tests.
142 + @param var_dbapi: defaults to portage.db[portage.root]["vartree"].dbapi
143 + Can be overridden for tests.
144 +
145 + @return binary packages to remove. e.g. {'cat/pkg-ver': [filepath]}
146 + @rtype: dict
147 + """
148 + if exclude is None:
149 + exclude = {}
150 +
151 + # Access test, os.walk does not error for "no read permission"
152 + try:
153 + test = os.listdir(pkgdir)
154 + del test
155 + except EnvironmentError as er:
156 + if options['ignore-failure']:
157 + exit(0)
158 + print(pp.error("Error accessing PKGDIR."), file=sys.stderr)
159 + print(pp.error("(Check your make.conf file and environment)."), file=sys.stderr)
160 + print(pp.error("Error: %s" % str(er)), file=sys.stderr)
161 + exit(1)
162 +
163 + # Create a dictionary of all installed packages
164 + if destructive and package_names:
165 + installed = dict.fromkeys(var_dbapi.cp_all())
166 + else:
167 + installed = {}
168 +
169 + # Dictionary of binary packages to clean. Organized as cpv->[pkgs] in order
170 + # to support FEATURES=binpkg-multi-instance.
171 + dead_binpkgs = {}
172 +
173 + # Create a dictionary of all binary packages whose mtime is older than
174 + # time_limit, if set. These packages will be considered for removal.
175 + bin_dbapi = portage.binarytree(pkgdir=pkgdir, settings=var_dbapi.settings).dbapi
176 + for cpv in bin_dbapi.cpv_all():
177 + cp = portage.cpv_getkey(cpv)
178 +
179 + # Exclude per --exclude-file=...
180 + if exclDictMatchCP(exclude, cp):
181 + continue
182 +
183 + # Exclude if binpkg is obsolete per --time-limit=...
184 + if time_limit:
185 + mtime = int(bin_dbapi.aux_get(cpv, ['_mtime_']))
186 + if mtime >= time_limit:
187 + continue
188 +
189 + # Exclude if binpkg exists in the porttree and not --deep
190 + if not destructive and port_dbapi.cpv_exists(cpv):
191 + continue
192 +
193 + if destructive and var_dbapi.cpv_exists(cpv):
194 + # Exclude if an instance of the package is installed due to
195 + # the --package-names option.
196 + if cp in installed and port_dbapi.cpv_exists(cpv):
197 + continue
198 +
199 + # Exclude if BUILD_TIME of binpkg is same as vartree
200 + buildtime = var_dbapi.aux_get(cpv, ['BUILD_TIME'])[0]
201 + if buildtime == bin_dbapi.aux_get(cpv, ['BUILD_TIME'])[0]:
202 + continue
203 +
204 + binpkg_path = bin_dbapi.bintree.getname(cpv)
205 + dead_binpkgs.setdefault(cpv, []).append(binpkg_path)
206 +
207 + return dead_binpkgs
208 +
209 +# vim: set ts=4 sw=4 tw=79:
210 --
211 2.24.1

Replies