From 6c4f9793d90e759522507a0bb4ef638e6c3215c1 Mon Sep 17 00:00:00 2001 From: Ahmed Kamal Date: Fri, 9 Feb 2007 23:50:26 +0200 Subject: Initial code commit for server side code --- server.py | 155 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100755 server.py diff --git a/server.py b/server.py new file mode 100755 index 0000000..c7bfe4c --- /dev/null +++ b/server.py @@ -0,0 +1,155 @@ +#!/usr/bin/python -t +# -*- mode: Python; indent-tabs-mode: nil; -*- +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program; if not, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + +import errno, os, sys +import fnmatch, re +import rpmUtils.transaction, rpmUtils.miscutils +import commands + +#### import Utils + +DEBUG = False +#### Utils.setdebug(DEBUG) + +SUFFIX='drpm' + +def genDeltaRPM(ts, newrpm, oldrpm): + (f1,n1,e1,v1,r1) = newrpm + (f2,n2,e2,v2,r2) = oldrpm + hdr = rpmUtils.miscutils.hdrFromPackage(ts,f1) + arch = hdr['arch'] + print 'Generating delta rpm for %s' % n1 + v12 = "_".join([v1,v2]) + r12 = "_".join([r1,r2]) + deltaRPMName= '%s.%s.%s' % ("-".join([n1,v12,r12]), arch, SUFFIX) + deltaCommand = 'makedeltarpm %s %s %s' % (f2, f1, deltaRPMName) + if DEBUG: + print "DEBUG " + deltaCommand + (code, out) = commands.getstatusoutput(deltaCommand) + if code: + raise Exception("genDeltaRPM: exitcode was %s - Reported Error: %s" % (code, out)) + +def pruneRepo(keep,whitelist,srcdir): + ts = rpmUtils.transaction.initReadOnlyTransaction() + changed = False + + # Create list of .rpm files. + # We don't use "glob", so sub-directories are supported. + print 'Expiring (keep=%d):' % keep, srcdir + srcfiles = [] + for root, dirs, files in os.walk(srcdir): + for f in fnmatch.filter(files,'*.rpm'): + srcfiles.append(os.path.join(root,f)) + if not len(srcfiles): + print ' Nothing found.' + return changed + assert srcfiles[0].startswith(srcdir) + + # Create map: rpm %name -> list of tuples (filename,name,e,v,r) + newestsrcrpms = {} + for f in srcfiles: + hdr = rpmUtils.miscutils.hdrFromPackage(ts,f) + n = hdr['name'] + v = hdr['version'] + r = hdr['release'] + e = hdr['epoch'] + if e is None: + e = 0 + newestsrcrpms.setdefault(n,[]) + newestsrcrpms[n].append((f,n,e,v,r)) + + # Now purge old src.rpm unless their %name matches a white-list pattern. + for l in newestsrcrpms.values(): + x = len(l) + + if x > 1: + # White-listing. + (f,n,e,v,r) = l[0] + keepthis = False + for r in whitelist: + if re.compile(r).search(n): + keepthis = True + break + if keepthis: + print ' Skipping',n + continue + + def sortByEVR(fnevr1, fnevr2): + (f1,n1,e1,v1,r1) = fnevr1 + (f2,n2,e2,v2,r2) = fnevr2 + rc = rpmUtils.miscutils.compareEVR((e1,v1,r1),(e2,v2,r2)) + if rc == 0: + return 0 + if rc > 0: + return -1 + if rc < 0: + return 1 + + l.sort(sortByEVR) # highest first in list + # Generate delta rpm + genDeltaRPM(ts, l[0],l[1]) + + oldies = [] + if len(l) > abs(keep): + oldies = l[keep:] + for (f,n,e,v,r) in oldies: + print ' Removing', os.path.basename(f) + srcfiles.remove(f) + if not DEBUG: + os.remove(f) + print "not removing\n" + changed = True + + if not len(srcfiles): + print 'WARNING: No .rpms left. Stopping here.' + return changed + + # Examine binary repository directories and remove everything which + # is missing its corresponding src.rpm. + return changed + + +def main(bin_rpm_path): + assert rpmUtils.miscutils.compareEVR((1,2,3),(1,2,0)) > 0 + assert rpmUtils.miscutils.compareEVR((0,1,2),(0,1,2)) == 0 + assert rpmUtils.miscutils.compareEVR((1,2,3),(4,0,99)) < 0 + + #### keep = (dist == 'development') and 1 or 2 + keep = 2 + #### whitelist = cfg.repoprune_keepdict[dist] + whitelist = "" + + return pruneRepo(keep,whitelist,bin_rpm_path) + + +if __name__ == '__main__': + if len(sys.argv) < 2: + print 'Usage: %s \n' % os.path.basename(sys.argv[0]) + sys.exit(errno.EINVAL) + bin_rpm_path = sys.argv[1] + + #### cfg = Utils.load_config_module(sys.argv[1]) + + #### Utils.signer_gid_check(cfg.signersgid) + #### os.umask(cfg.signersumask) + + #### for dist in sys.argv[2:]: + #### if not cfg.archdict.has_key(dist): + #### print "No distribution release named '%s' found" % dist + #### sys.exit(errno.EINVAL) + main(bin_rpm_path) + sys.exit(0) -- cgit