Gentoo Archives: gentoo-portage-dev

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