#!/usr/bin/python import sys import optparse import os import os.path import shutil import subprocess from ConfigParser import SafeConfigParser ######## #Stages# ######## def recollectData(): """ Copy all files and directories to temp file """ #os.removedirs(options.workPath) os.makedirs(options.workPath) for nmS,src in options.bareSources.iteritems(): dst = os.path.join(options.workPath,nmS)#complete destination path if os.path.isdir(src): shutil.copytree(src, dst) continue elif os.path.isfile(src): shutil.copy(src, dst) continue else: #If not file and not dir ERROR print >> sys.stderr , "Error, Cannot find %s in system"% source sys.exit(1) def createRPM(): """ Create an rpm with the rpm -ba specfile.spec command """ #Create and put file in the working directory. specFile = """ Name: %s Version: %s Release: %s Summary: Sumary Group: System Environment/Base License: GPL Source: %%{name}-%%{version}.tar.gz Buildroot: %%{_tmppath}/%%{name}-%%{version}-%%{release}-root-%%(%%{__id_u} -n) BuildArch: %s provides: %%{name} %%description Description %%prep %%setup -q %%build # Nothing to see here. %%install # Nothing to see here. """ % (options.pkgName, options.pkgVer, options.pkgRel, options.arch) #Create the files section fileSec = "%%files\n %%doc %s.spec " % options.pkgName for name in options.bareSources.iterkeys(): fileSec = fileSec + "%s " % name # Add the files section the the spec file. specFile = specFile + fileSec + "\n" # Put the spec file in the directory. specfd = open( os.path.join(options.workPath,options.pkgName+".spec"), 'w' ) specfd.write(specFile) specfd.close() #make a tar file and put it in SOURCES options.sourceDir = "/usr/src/redhat/SOURCES" tarCommand = [ "tar", "zcvvf", "%s/%s-%s.tar.gz"%(options.sourceDir, options.pkgName, options.pkgVer), "%s"%options.workDir] p = subprocess.Popen(tarCommand, stdout=subprocess.PIPE, stdin=subprocess.PIPE, cwd=options.tempDir) p.wait() #execute rpmbuild rpmCommand = [ "rpmbuild", "-ba", "%s/%s.spec"%(options.workPath, options.pkgName)] p = subprocess.Popen(rpmCommand, stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE) p.wait() def createRepo(): """ Create repo and put in destdir/repodir """ destdir = options.pconf.get('default', 'destdir') options.repodir = os.path.join(destdir, "repository") if not os.path.exists(options.repodir):os.makedirs(options.repodir) #retrieve the rpm from the /usr/src/redhat/RPMS shutil.copy("/usr/src/redhat/RPMS/%s/%s-%s-%s.%s.rpm"%(options.arch, options.pkgName, options.pkgVer, options.pkgRel, options.arch), options.repodir) #run createrepo createrepoCommand = ["createrepo", "-q", "%s"%options.repodir] p = subprocess.Popen(createrepoCommand, stdin=subprocess.PIPE, stdout=subprocess.PIPE, cwd=options.repodir) p.wait() def addRepo(): """ Add created repo to the yum conf file specified in the pungi conf file. """ #lets handle this with a config parser. options.yconf.add_section(options.repoName) options.yconf.set(options.repoName, 'name', options.repoName) options.yconf.set(options.repoName, 'baseurl', 'file://%s'% options.repodir) options.yconf.set(options.repoName, 'enabled', '1') options.yconf.set(options.repoName, 'gpgcheck', "0") options.yconf.write(open(options.yumConf, 'w')) def pungiConfFiles(): """ Modify the relnotepkgs, relnotefilere, relnotedirre accordingly """ #create the re variables. options.pconf.set('default','relnotepkgs', options.pkgName) relnotedirre = "" relnotefilere = "" for nmS, src in options.bareSources.iteritems(): if os.path.isdir(src): relnotedirre = "%s %s"%(relnotedirre, nmS) if os.path.isfile(src): relnotefilere = "%s %s"%(relnotefilere, nmS) #write to file if not relnotedirre == "": options.pconf.set('default','relnotedirre', relnotedirre) if not relnotefilere == "": options.pconf.set('default','relnotefilere', relnotefilere) options.pconf.write(open(options.pungiConf, 'w')) def manifestPkgs(): """ Add the created package to the manifest. """ manifest = options.pconf.get('default', 'manifest') fd = open(manifest, 'r+') lines = fd.readlines() try: lines.index(options.pkgName+'\n') except: lines.append(options.pkgName+'\n') fd.writelines(lines) fd.close() def clean(): """ Clean temp files. """ #Delete temporary file shutil.rmtree(options.tempDir, True) def revert(): """ Revert the changes from the pungi config files """ # Take out the relnotedirre and relnotefilere stuff relnotedirre = options.pconf.get('default', 'relnotedirre').split() relnotefilere = options.pconf.get('default', 'relnotefilere').split() for key in options.bareSources.iterkeys(): try: relnotedirre.remove(key) except: try: relnotefilere.remove(key) except: pass options.pconf.remove_option('default', 'relnotedirre') if len(relnotedirre) > 0: options.pconf.set('default', 'relnotedirre', relnotedirre) options.pconf.remove_option('default', 'relnotedirre') if len(relnotefilere) > 0: options.pconf.set('default', 'relnotefilere', relnotefilere) # Take out the relnotepkgs stuff relnotepkgs = options.pconf.get('default', 'relnotepkgs').split() for elem in relnotepkgs: try: relnotepkgs.remove(options.pkgName) except: pass options.pconf.remove_option('default', 'relnotepkgs') if len(relnotepkgs) > 0: options.pconf.set('default', 'relnotepkgs', relnotepkgs) # Take out the yum config stuff options.yconf.remove_section(options.repoName) # Write the configurations options.pconf.write(open(options.pungiConf, 'w')) options.yconf.write(open(options.yumConf, 'w')) def main(): #If revert just do it and exit. if options.revert: revert() sys.exit(0) #Recollect all the files recollectData() #Make the rpm package createRPM() #Make the very small repo createRepo() #Add the repo to the yum config addRepo() #Add the files and directories to the pungi config. pungiConfFiles() #Add packages to manifes manifestPkgs() #remember to clean up clean() if __name__ == "__main__": # Get user options. parser = optparse.OptionParser() parser.add_option( "-s","--source", action="append", dest="sources", type="string", help="This script takes local files and directories. \ --source=/path/to/dir. ") parser.add_option( "--pungi-conf", dest="pungiConf", type="string", default="/etc/pungi/pungi.conf", help="Pungi configuration file that will be modified.") parser.add_option( "--yum-conf", dest="yumConf", type="string", default="/etc/pungi/yum.conf.f7.x86_64", help="Yum configuration file that will be modified.") parser.add_option( "--name", dest="pkgName", type="string", default="name", help="It specifies the name of the package.") parser.add_option( "--version", dest="pkgVer", type="string", default="0.0.0", help="Package Version.") parser.add_option( "--release", dest="pkgRel", type="string", default="01", help="Package Release.") parser.add_option( "--arch", "-a", dest="arch", type="string", default="noarch", help="Package Architecture. noarch is recomended.") parser.add_option( "--repo", "-r", dest="repoName", type="string", default="additionals", help="The repository name. It will be located in the pungi workdir.") parser.add_option( "--revert", action="store_true", dest="revert", default=False, help="Is used to undo the changes that this scripts does to\ the yum config file and the pungi config file.") (options, args) = parser.parse_args() if options.sources == None: parser.print_help() sys.exit(1) # Create some needed variables. options.workDir = "%s-%s"%(options.pkgName,options.pkgVer) options.tempDir = os.path.join("/tmp", "temp-rpm%d"%os.getpid()) options.workPath = os.path.join(options.tempDir, options.workDir) # Set the handlers for the configuration files options.pconf = SafeConfigParser() options.pconf.read(options.pungiConf) options.yconf = SafeConfigParser() options.yconf.read(options.yumConf) # We are eventually going to need the bare sources. options.bareSources = {} for source in options.sources: src = os.path.normpath(source)# without leading '/' nmS = os.path.basename(src)# Name of file or dir without path dst = os.path.join(options.workPath,nmS)#complete destination path options.bareSources[nmS] = src main()