Gentoo Archives: gentoo-portage-dev

From: Pavel Kazakov <nullishzero@g.o>
To: gentoo-portage-dev@l.g.o
Subject: Re: [gentoo-portage-dev] [PATCH] Add an emaint module that can scan for failed merges and that can fix failed merges.
Date: Thu, 20 Feb 2014 00:08:18
Message-Id: 53054774.8020805@gentoo.org
In Reply to: Re: [gentoo-portage-dev] [PATCH] Add an emaint module that can scan for failed merges and that can fix failed merges. by Alec Warner
1 Hey Alec,
2
3 Thanks for the comments--good stuff.
4
5 On 02/19/2014 02:32 PM, Alec Warner wrote:
6 > On Wed, Feb 19, 2014 at 12:31 PM, Pavel Kazakov <nullishzero@g.o
7 > <mailto:nullishzero@g.o>> wrote:
8 >
9 > ---
10 > pym/portage/emaint/modules/merges/__init__.py | 20 ++++++++
11 > pym/portage/emaint/modules/merges/merges.py | 70
12 > +++++++++++++++++++++++++++
13 > 2 files changed, 90 insertions(+)
14 > create mode 100644 pym/portage/emaint/modules/merges/__init__.py
15 > create mode 100644 pym/portage/emaint/modules/merges/merges.py
16 >
17 > diff --git a/pym/portage/emaint/modules/merges/__init__.py
18 > b/pym/portage/emaint/modules/merges/__init__.py
19 > new file mode 100644
20 > index 0000000..2cd79af
21 > --- /dev/null
22 > +++ b/pym/portage/emaint/modules/merges/__init__.py
23 > @@ -0,0 +1,20 @@
24 > +# Copyright 2005-2014 Gentoo Foundation
25 > +# Distributed under the terms of the GNU General Public License v2
26 > +
27 > +"""Scan for failed merges and fix them.
28 > +"""
29 >
30 >
31 > """Scan for failed merges fix them."""
32 All the other modules put the second """ on a newline, so I wanted to be
33 consistent. Should I still make the change?
34 >
35 >
36 > +
37 > +
38 > +module_spec = {
39 > + 'name': 'merges',
40 > + 'description': __doc__,
41 > + 'provides':{
42 >
43 >
44 > 'module1' ?
45 All the other modules did it this way, so I wanted consistency.
46
47 >
48 >
49 > + 'module1': {
50 > + 'name': "merges",
51 > + 'class': "MergesHandler",
52 > + 'description': __doc__,
53 > + 'functions': ['check', 'fix',],
54 > + 'func_desc': {}
55 > + }
56 > + }
57 > + }
58 > diff --git a/pym/portage/emaint/modules/merges/merges.py
59 > b/pym/portage/emaint/modules/merges/merges.py
60 > new file mode 100644
61 > index 0000000..b243082
62 > --- /dev/null
63 > +++ b/pym/portage/emaint/modules/merges/merges.py
64 > @@ -0,0 +1,70 @@
65 > +# Copyright 2005-2014 Gentoo Foundation
66 > +# Distributed under the terms of the GNU General Public License v2
67 > +
68 > +import portage
69 > +from portage import os
70 > +from portage.const import PRIVATE_PATH, VDB_PATH
71 > +from portage.util import writemsg
72 > +
73 > +import shutil
74 > +import sys
75 > +
76 > +if sys.hexversion >= 0x3000000:
77 > + # pylint: disable=W0622
78 > + long = int
79 >
80 >
81 > What is this little guy? Can we just do this in a library someplace?
82 Checks if python version is greater than or equal to 3, but I probably
83 don't even need it since I'm not using longs.
84 >
85 >
86 > +
87 > +class MergesHandler(object):
88 > +
89 > + short_desc = "Remove failed merges"
90 > +
91 >
92 >
93 > @staticmethod decorator?
94 Yeah, I copied legacy emaint code from other modules, but I can switch.
95 >
96 > + def name():
97 > + return "merges"
98 > + name = staticmethod(name)
99 > +
100 > + def __init__(self):
101 >
102 >
103 > Generally you want to be able to change these later, so you might do
104 > something like:
105 >
106 > def __init__(self, eroot=None, vardb=None, vardb_path=None):
107 > self._eroot = error or portage.settings['EROOT']
108 > ... and so forth.
109 >
110 > Also..why can't self._vardb_path be queried from the vardb?
111 To be honest, I noticed the other modules assigning such variables to
112 self, but they could all just as easily be calculated without needing to
113 be instance member variables.
114 >
115 >
116 > + self._eroot = portage.settings['EROOT']
117 > + self._vardb = portage.db[self._eroot]["vartree"].dbapi
118 > + self._vardb_path = os.path.join(self._eroot,
119 > VDB_PATH) + os.path.sep
120 > +
121 > + def can_progressbar(self, func):
122 > + return True
123 > +
124 > + def _failed_packages(self, onProgress):
125 > + for cat in os.listdir(self._vardb_path):
126 >
127 > os.listdir(os.path.join(...)) ?
128 Bad habits die hard ;/. I need to update the rest of my path
129 concatenation to use os.path.join().
130 >
131 > + pkgs = os.listdir(self._vardb_path + cat)
132 > + maxval = len(pkgs)
133 > + for i, pkg in enumerate(pkgs):
134 > + if onProgress:
135 > + onProgress(maxval, i+1)
136 > +
137 > + if '-MERGING-' in pkg:
138 > + yield cat + os.path.sep + pkg
139 > +
140 > + def check(self, **kwargs):
141 > + onProgress = kwargs.get('onProgress', None)
142 > + failed_pkgs = []
143 > + for pkg in self._failed_packages(onProgress):
144 > + failed_pkgs.append(pkg)
145 > +
146 > + errors = ["'%s' failed to merge." % x for x in
147 > failed_pkgs]
148 > + return errors
149 > +
150 > + def fix(self, **kwargs):
151 > + onProgress = kwargs.get('onProgress', None)
152 > + tracking_path = os.path.join(self._eroot,
153 > PRIVATE_PATH, 'failed-merges');
154 > + try:
155 > + with open(tracking_path, 'w') as tracking_file:
156 >
157 >
158 > is this unicode safe?
159 I can do a unicode encode to make sure it is.
160 >
161 >
162 > + for failed_pkg in
163 > self._failed_packages(onProgress):
164 > +
165 > tracking_file.write(failed_pkg + '\n')
166 > + pkg_path =
167 > self._vardb_path + failed_pkg
168 >
169 >
170 > os.path.join(...)
171 >
172 >
173 > + # Delete failed
174 > merge directory
175 > + # XXX: Would be a
176 > good idea to attempt try removing
177 > + # package contents
178 > to prevent orphaned files
179 >
180 >
181 > # XXX is terrible style. I realize a bunch of code does that, and it is
182 > stupid.
183 > # use
184 > # TODO: foo
185 Fair enough, I'll switch over to TODO.
186 >
187 >
188 >
189 >
190 > + shutil.rmtree(pkg_path)
191 > + # Re-emerge package
192 >
193 > + pkg_name = '=' +
194 > failed_pkg.replace('-MERGING-', '')
195 > +
196 > features='FEATURES="-collision-detect -protect-owned"'
197 > + emerge_cmd="emerge
198 > --verbose --oneshot --complete-graph=y"
199 > + os.system('%s %s %s'
200 > % (features, emerge_cmd, pkg_name))
201 >
202 >
203 > This is a security vulnerability :)
204 Yikes ;/
205 >
206 > -A
207 >
208 >
209 > + except Exception as ex:
210 > + writemsg('Unable to fix failed package: %s'
211 > % str(ex))
212 > --
213 > 1.8.3.2
214 >
215 >
216 >
217
218 Regards,
219 Pavel

Replies