#!/usr/bin/python -t # 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 Library 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 os import sys import rpmUtils import smtplib import shutil import string ts = rpmUtils.transaction.initReadOnlyTransaction() DEBUG = True def debugprint(msg): if DEBUG: print msg def find_files(path): """returns a dict of filetypes and paths to those files""" filedict = {} filedict['srpm'] = [] filedict['rpm'] = [] filedict['debuginfo'] = [] for root, dirs, files in os.walk(path): for file in files: # match the files to what list they should be in if file.endswith('.rpm'): if file.find('debuginfo') != -1: which = 'debuginfo' elif file.endswith('.src.rpm'): which = 'srpm' else: which = 'rpm' else: which = 'other' if which == 'srpm' or which == 'rpm' or which == 'debuginfo': fullfile = os.path.join(root, file) filedict[which].append(fullfile) return filedict def naevr(pkg): """return nevra from the package srpm""" hdr = rpmUtils.miscutils.hdrFromPackage(ts, pkg) name = hdr['name'] ver = hdr['version'] rel = hdr['release'] arch = hdr['arch'] epoch = hdr['epoch'] if epoch is None: epoch = 0 sourcerpm = hdr['sourcerpm'] return (name, arch, epoch, ver, rel, sourcerpm) def main(sourcedir, destdir, dest_flat): files = find_files(sourcedir) if not os.path.exists(destdir): os.makedirs(destdir) for package in files['srpm'] + files['rpm'] + files['debuginfo']: if dest_flat: dest_file = os.path.join(destdir, os.path.basename(package)) else: (n,a,e,v,r,srpm) = naevr(package) if package.endswith('.src.rpm'): sub_path = os.path.join("%s" % n, "%s-%s" % (v, r)) sub_path = os.path.join(sub_path, "SRPM") else: t = srpm.split('-') srpm = t[0] sub_path = os.path.join("%s" % srpm, "%s-%s" % (v, r)) sub_path = os.path.join(sub_path, "%s" % a) dest_file = os.path.join(destdir, sub_path, os.path.basename(package)) if os.path.exists(dest_file): debugprint('Deleting %s' % dest_file) os.unlink(dest_file) debugprint('Copying %s to %s' % (os.path.basename(package), dest_file)) if not os.path.exists(os.path.dirname(dest_file)): os.makedirs(os.path.dirname(dest_file)) shutil.copy(package, dest_file) # Create repo metadata print "Making Repository Metadata" rpdata = os.path.join(destdir, 'repodata') debugprint('removing tree %s' % rpdata) shutil.rmtree(rpdata, ignore_errors=True) cmd = '/usr/bin/createrepo %s' % destdir debugprint(cmd) result = os.system(cmd) if __name__ == '__main__': me = os.getcwd() if len(sys.argv) < 3: print "Usage:\nrepoconv.py source [-flat] dest\n\n" sys.exit(1) source = sys.argv[1] if not os.path.exists(source): print "Source directory does not exist." sys.exit(1) if sys.argv[2].lower() == '-flat': dest_flat = True dest = sys.argv[3] else: dest_flat = False dest = sys.argv[2] main(source, dest, dest_flat) os.chdir(me)