]> git.pld-linux.org Git - projects/pld-ftp-admin.git/blame - modules/ftptree.py
- sign pkgs using external rpm binary
[projects/pld-ftp-admin.git] / modules / ftptree.py
CommitLineData
098f4a50
MM
1# vi: encoding=utf-8 ts=8 sts=4 sw=4 et
2
ce470bd9 3import os, config, string, urllib, re, rpm
81e9387d 4from common import fileexists, noarchcachedir
151d31cc 5from baseftptree import BasePkg, BaseFtpTree
098f4a50
MM
6errnum=0
7
85f3481a 8quietmode=False
0a108b7f 9
76c9cb26 10class SomeError(Exception):
db126e0b
ER
11 def __init__(self):
12 return
13
14 def __str__(self):
15 print "","An Error occured!"
16
098f4a50
MM
17def bailoutonerror():
18 if not errnum == 0:
19 print "%d error(s) encountered... aborting" % errnum
76c9cb26 20 raise SomeError
098f4a50 21
d9b3388c 22def pinfo(msg):
0767aa5e 23 print 'INFO: ' + msg
d9b3388c 24
098f4a50
MM
25def perror(msg):
26 global errnum
27 errnum=errnum+1
492b6398 28 print 'ERR: ' + msg
098f4a50 29
796b7867 30def pwarning(msg):
492b6398 31 print 'WARN: ' + msg
796b7867 32
a0b52be0
AM
33def rm(file, test=False):
34 if test:
35 if not os.path.exists(file):
36 pinfo("TEST os.remove(%s): file doesn't exists" % file)
37 else:
2d70dbd8
AM
38 try:
39 os.remove(file)
40 except OSError, e:
41 pinfo("os.remove(%s): %s" % (file, e))
8911f226 42 #raise
a0b52be0
AM
43
44def mv(src, dst, test=False):
45 fsrc = src
46 fdst = dst+'/'+src.split('/')[-1]
47 if test:
48 if not os.path.exists(src):
49 pinfo("TEST os.rename(%s, %s): source doesn't exists" % (fsrc, fdst))
50 else:
51 try:
52 os.rename(fsrc, fdst)
53 except OSError, e:
aee80850 54 pinfo("os.rename(%s, %s): %s" % (fsrc, fdst, e))
a0b52be0 55 raise
098f4a50 56
151d31cc 57class Pkg(BasePkg):
85f3481a
MM
58 def __init__(self, nvr, tree):
59 BasePkg.__init__(self, nvr, tree)
8911f226
ER
60 self.name = string.join(nvr.split('-')[:-2], '-')
61 self.version = nvr.split('-')[-2]
62 self.release = nvr.split('-')[-1]
63 self.marked4removal = False
64 self.marked4moving = False
65 self.marked4movingpool = []
66 self.errors = []
67 self.warnings = []
098f4a50 68
d7667ebe
MM
69 def __cmp__(self, pkg):
70 if self.name > pkg.name:
71 return 1
72 elif self.name < pkg.name:
73 return -1
74 else:
75 return rpm.labelCompare(('0', self.version, self.release),
76 ('0', pkg.version, pkg.release))
77
54ff0049
ER
78
79 # unfortunately can't do new Pkg(NVR), and have no "tree" in this pkg context
80 # so this static function
81 def is_debuginfo(self, nvr):
82 """
83 returns true if NVR is debuginfo package and separate debuginfo is enabled
84 """
85 if not config.separate_debuginfo:
86 return False
87 pkg = nvr.split('-')[:-2]
88 return pkg[-1] == 'debuginfo'
89
14085d11
MM
90 def mark4moving(self):
91 if not self.marked4moving:
f301e305
MM
92 # Only one pkg in this pool can be marked for moving
93 for pkg in self.marked4movingpool:
94 pkg.unmark4moving()
14085d11
MM
95 self.tree.marked4moving.append(self)
96 self.marked4moving=True
97
f301e305
MM
98 def unmark4moving(self):
99 if self.marked4moving:
100 self.tree.marked4moving.remove(self)
101 self.marked4moving=False
102
14085d11
MM
103 def mark4removal(self):
104 if not self.marked4removal:
105 self.tree.marked4removal.append(self)
106 self.marked4removal=True
098f4a50 107
85f3481a
MM
108 def error(self, msg):
109 self.errors.append(msg)
110 if not quietmode:
111 perror('%s %s' % (self.nvr, msg))
112
113 def warning(self, msg):
114 self.warnings.append(msg)
115 if not quietmode:
116 pwarning('%s %s' % (self.nvr, msg))
117
bb2cb325
MM
118 def load(self, content=None):
119 BasePkg.load(self, content)
098f4a50 120 if self.info.has_key('move'):
14085d11 121 self.mark4moving()
098f4a50
MM
122
123 def writeinfo(self):
8911f226 124 f = open(self.tree.basedir+'/SRPMS/.metadata/'+self.nvr+'.src.rpm.info', 'w')
098f4a50
MM
125 for bid in self.build.keys():
126 f.write("info:build:%s:requester:%s\ninfo:build:%s:requester_email:%s\n" % (bid, self.build[bid].requester, bid, self.build[bid].requester_email))
127 for key in self.info.keys():
128 f.write("info:%s:%s\n" % (key, string.join(self.info[key], ':')))
151d31cc
MM
129 for arch in self.files.keys():
130 for rpm in self.files[arch]:
098f4a50
MM
131 f.write("file:%s:%s\n" % (arch, rpm))
132
8911f226
ER
133 def remove(self, test = False):
134 """
135 Remove package from ftp
136 """
151d31cc
MM
137 for arch in self.files.keys():
138 for rpm in self.files[arch]:
54ff0049
ER
139 if self.is_debuginfo(rpm):
140 rm(self.tree.basedir + '/' + arch + '/debuginfo/' + rpm, test)
141 else:
142 rm(self.tree.basedir + '/' + arch + '/RPMS/' + rpm, test)
8911f226
ER
143 if arch == 'noarch':
144 if fileexists(noarchcachedir + rpm + '.filelist'):
145 rm(noarchcachedir + rpm + '.filelist', test)
146 if fileexists(noarchcachedir + rpm + '.reqlist'):
147 rm(noarchcachedir + rpm + '.reqlist', test)
148 rm(self.tree.basedir + '/SRPMS/.metadata/' + self.nvr + '.src.rpm.info', test)
098f4a50 149
a0b52be0 150 def move(self, dsttree, test=False):
85f3481a 151 if dsttree.has_key(self.nvr):
8911f226 152 movedany = False
151d31cc 153 for arch in self.files.keys():
85f3481a 154 if arch in dsttree[self.nvr].files.keys():
77b616ee
AM
155 msg = ""
156 if test:
157 msg = "TEST "
158 pinfo("%sArch %s for %s is already present in dest tree; removing from srctree" % (msg, arch, self.nvr))
151d31cc 159 for rpm in self.files[arch]:
54ff0049
ER
160 if self.is_debuginfo(rpm):
161 rm(self.tree.basedir + '/' + arch + '/debuginfo/' + rpm, test)
162 else:
163 rm(self.tree.basedir + '/' + arch + '/RPMS/' + rpm, test)
098f4a50 164 else:
8911f226
ER
165 movedany = True
166 dsttree[self.nvr].files[arch] = self.files[arch]
151d31cc 167 for rpm in self.files[arch]:
54ff0049
ER
168 if self.is_debuginfo(rpm):
169 mv(self.tree.basedir + '/' + arch + '/debuginfo/' + rpm, dsttree.basedir + '/' + arch + '/debuginfo/', test)
170 else:
171 mv(self.tree.basedir + '/' + arch + '/RPMS/' + rpm, dsttree.basedir + '/' + arch + '/RPMS/', test)
a0b52be0 172 if not test and movedany:
098f4a50 173 for bid in self.build.keys():
8911f226 174 dsttree[self.nvr].build[bid] = self.build[bid]
85f3481a 175 dsttree[self.nvr].writeinfo()
8911f226 176 rm(self.tree.basedir + '/SRPMS/.metadata/' + self.nvr + '.src.rpm.info', test)
098f4a50 177 else:
8911f226 178 # move files
151d31cc
MM
179 for arch in self.files.keys():
180 for rpm in self.files[arch]:
54ff0049
ER
181 if self.is_debuginfo(rpm):
182 mv(self.tree.basedir + '/' + arch + '/debuginfo/' + rpm, dsttree.basedir + '/' + arch + '/debuginfo/', test)
183 else:
184 mv(self.tree.basedir + '/' + arch + '/RPMS/' + rpm, dsttree.basedir + '/' + arch + '/RPMS/', test)
098f4a50 185
8911f226
ER
186 # move metadata
187 mv(self.tree.basedir + '/SRPMS/.metadata/' + self.nvr + '.src.rpm.info', dsttree.basedir + '/SRPMS/.metadata/', test)
098f4a50 188
151d31cc 189class FtpTree(BaseFtpTree):
098f4a50 190 def __init__(self, tree, loadall=False):
151d31cc 191 BaseFtpTree.__init__(self, tree)
8911f226
ER
192 self.loadedpkgs = {}
193 self.marked4removal = []
194 self.marked4moving = []
195 self.pkgnames = []
d9b3388c 196 self.__loadpkgnames()
098f4a50 197 if loadall:
d9b3388c 198 for pkgname in self.pkgnames:
8911f226 199 self.loadedpkgs[pkgname] = Pkg(pkgname, self)
796b7867 200 # Tests:
8911f226 201 self.do_checkbuild = True
d9b3388c 202
098f4a50
MM
203 def __getitem__(self, key):
204 if self.loadedpkgs.has_key(key):
205 return self.loadedpkgs[key]
206 elif key in self.pkgnames:
207 pkg=Pkg(key, self)
208 self.loadedpkgs[key]=pkg
209 return pkg
210 else:
211 raise KeyError, key
796b7867 212
098f4a50
MM
213 def has_key(self, key):
214 if key in self.pkgnames:
215 return True
216 else:
217 return False
796b7867 218
1610d209
MM
219 def keys(self):
220 return self.pkgnames
098f4a50 221
d7667ebe
MM
222 def values(self):
223 return self.loadedpkgs.values()
224
67b5bf38 225 def checktree(self, dsttree):
85f3481a
MM
226 self.__checkbuild(self.loadedpkgs.values())
227 self.__checkarchs(dsttree, self.loadedpkgs.values())
0767aa5e 228
9b00920e 229 def testmove(self, dsttree):
67b5bf38
MM
230 self.__checkbuild(self.marked4moving)
231 self.__checkarchs(dsttree, self.marked4moving)
a0b52be0 232
8911f226
ER
233 self.__rmolderfromsrc(test = True)
234 self.__rmotherfromdst(dsttree, test = True)
a0b52be0
AM
235
236 for pkg in self.marked4moving:
8911f226 237 pkg.move(dsttree, test = True)
9b00920e 238
098f4a50 239 def movepkgs(self, dsttree):
f053c9e6 240 if self.do_checkbuild:
67b5bf38 241 self.__checkbuild(self.marked4moving)
098f4a50 242 bailoutonerror()
67b5bf38 243 self.__checkarchs(dsttree, self.marked4moving)
098f4a50
MM
244 bailoutonerror()
245 self.__rmolderfromsrc()
246 self.__rmotherfromdst(dsttree)
247
14085d11 248 for pkg in self.marked4moving:
098f4a50
MM
249 pkg.move(dsttree)
250
14085d11 251 def removepkgs(self):
6bc6286e 252 if self.do_checkbuild:
67b5bf38 253 self.__checkbuild(self.marked4removal)
6bc6286e 254 bailoutonerror()
14085d11
MM
255 for pkg in self.marked4removal:
256 pkg.remove()
257
258 def mark4removal(self, wannabepkgs):
259 self.__mark4something(wannabepkgs, Pkg.mark4removal)
260
261 def mark4moving(self, wannabepkgs):
262 self.__mark4something(wannabepkgs, Pkg.mark4moving)
263
264
265 # Internal functions below
266
d9b3388c
MM
267 def __loadpkgnames(self):
268 def checkfiletype(name):
269 if name[-13:]=='.src.rpm.info':
270 return True
271 else:
272 return False
8911f226
ER
273 list = filter(checkfiletype, os.listdir(self.basedir+'/SRPMS/.metadata'))
274 self.pkgnames = map((lambda x: x[:-13]), list)
d9b3388c 275
14085d11 276 def __mark4something(self, wannabepkgs, markfunction):
098f4a50
MM
277 def chopoffextension(pkg):
278 found=pkg.find('.src.rpm')
279 if found==-1:
280 return pkg
281 else:
282 return pkg[:found]
283 for wannabepkg in wannabepkgs:
284 pkgname=chopoffextension(wannabepkg)
285 if pkgname in self.pkgnames:
286 if not pkgname in self.loadedpkgs.keys():
287 self.loadedpkgs[pkgname]=Pkg(pkgname, self)
14085d11 288 markfunction(self.loadedpkgs[pkgname])
098f4a50 289 else:
492b6398 290 perror('%s not found in source tree' % pkgname)
098f4a50 291 bailoutonerror()
14085d11 292
67b5bf38 293 def __checkbuild(self, marked):
8911f226
ER
294 """
295 Checks queue file if all arches are built
296
297 Reads config.builderqueue to grab the info
298 """
299 f = urllib.urlopen(config.builderqueue)
300 requests = {}
301 reid = re.compile(r'^.*id=(.*) pri.*$')
302 regb = re.compile(r'^group:.*$|builders:.*$', re.M)
098f4a50 303 for i in re.findall(regb, f.read()):
8911f226
ER
304 if i[0] == 'g':
305 id = reid.sub(r'\1', i)
306 requests[id] = ""
098f4a50 307 elif i[0]=='b':
8911f226 308 requests[id] = requests[id] + i
098f4a50 309 f.close()
8911f226 310
6bc6286e 311 for pkg in marked:
098f4a50
MM
312 for bid in pkg.build.keys():
313 if requests.has_key(bid) and not requests[bid].find('?') == -1:
85f3481a 314 pkg.error("(buildid %s) building not finished" % bid)
098f4a50 315
67b5bf38 316 def __checkarchs(self, dsttree, marked):
8911f226
ER
317 """
318 Checks marked pkgs it is built on all archs.
319 """
67b5bf38 320 for pkg in marked:
4802753d 321 if len(pkg.files.keys()) <= 1:
85f3481a 322 pkg.error('has only src.rpm built')
4802753d 323 continue
8911f226
ER
324 otherpkgnames = self.__find_other_pkgs(pkg, dsttree)
325
326 # check if we're not removing some archs
327 if otherpkgnames:
328 curarchs = []
329 missingarchs = []
796b7867
MM
330 for somepkg in otherpkgnames:
331 curarchs.extend(Pkg(somepkg, dsttree).files.keys())
332 for arch in curarchs:
333 if arch not in pkg.files.keys():
334 missingarchs.append(arch)
335 if missingarchs:
85f3481a 336 pkg.error('moving would remove archs: %s' % missingarchs)
8911f226
ER
337 else:
338 # warn if a package isn't built for all archs
339 if (config.separate_noarch and 'noarch' in pkg.files.keys() and len(pkg.files.keys()) == 2):
492b6398 340 continue
8911f226
ER
341 elif len(pkg.files.keys()) != len(config.ftp_archs) + 1:
342 missingarchs = []
492b6398
MM
343 for arch in config.ftp_archs:
344 if arch not in pkg.files.keys():
345 missingarchs.append(arch)
85f3481a 346 pkg.warning('not built for archs: %s' % missingarchs)
098f4a50 347
a0b52be0 348 def __rmolderfromsrc(self, test=False):
14085d11 349 for pkg in self.marked4moving:
8911f226 350 olderpkgnames = self.__find_older_pkgs(pkg)
098f4a50 351 for i in olderpkgnames:
a0b52be0 352 Pkg(i, self).remove(test)
098f4a50 353
8911f226 354 def __rmotherfromdst(self, dsttree, test = False):
14085d11 355 for pkg in self.marked4moving:
8911f226 356 pkgnames = self.__find_other_pkgs(pkg, dsttree)
098f4a50 357 for i in pkgnames:
a0b52be0 358 Pkg(i, dsttree).remove(test)
098f4a50 359
d9b3388c 360 # Used more than once filter functions
d9b3388c 361 def __find_other_pkgs(self, pkg, tree):
8911f226
ER
362 escapedpkgname = pkg.name.replace('.', '\.').replace('+', '\+')
363 ziewre = re.compile(escapedpkgname + '-[^-]*-[^-]*$')
d9b3388c 364 def filter_other_pkgs(x):
85f3481a 365 if ziewre.match(x) and not x == pkg.nvr:
d9b3388c
MM
366 return True
367 else:
368 return False
369 return filter(filter_other_pkgs, tree.pkgnames)
370
371 def __find_older_pkgs(self, pkg):
372 def filter_older_pkgs(x):
8911f226 373 c = x.split('-')
ce470bd9
MM
374 rc = rpm.labelCompare(('0', pkg.version, pkg.release),
375 ('0', c[-2], c[-1]))
376 if rc == 1: # pkg > x
d9b3388c 377 return True
d9b3388c
MM
378 else:
379 return False
380 return filter(filter_older_pkgs, self.__find_other_pkgs(pkg, self))
381
This page took 0.091605 seconds and 4 git commands to generate.