diff --git a/pylorax/__init__.py b/pylorax/__init__.py index 58b6a084..7d925c17 100644 --- a/pylorax/__init__.py +++ b/pylorax/__init__.py @@ -24,6 +24,10 @@ version = (0, 1) __all__ = ['discinfo', 'treeinfo'] +import os +import shutil +import tempfile + import discinfo import treeinfo @@ -39,3 +43,120 @@ def show_version(prog): prog = 'pylorax' print "%s version %d.%d" % (prog, version[0], version[1],) + +def collectRepos(args): + """collectRepos(args) + + Get the main repo (the first one) and then build a list of all remaining + repos in the list. Sanitize each repo URL for proper yum syntax. + + """ + + if args is None or args == []: + return '', [] + + repolist = [] + for repospec in args: + if repospec.startswith('/'): + repolist.append("file://%s" % (repospec,)) + elif repospec.startswith('http://') or repospec.startswith('ftp://'): + repolist.append(repospec) + + repo = repolist[0] + extrarepos = [] + + if len(repolist) > 1: + for extra in repolist[1:]: + extrarepos.append(extra) + + return repo, extrarepos + +def initializeDirs(output): + """initializeDirs(output) + + Create directories used for image generation. The only required + parameter is the main output directory specified by the user. + + """ + + if not os.path.isdir(output): + os.makedirs(output, mode=0755) + + tmpdir = tempfile.gettempdir() + buildinstdir = tempfile.mkdtemp('XXXXXX', 'buildinstall.tree.', tmpdir) + treedir = tempfile.mkdtemp('XXXXXX', 'treedir.', tmpdir) + cachedir = tempfile.mkdtemp('XXXXXX', 'yumcache.' tmpdir) + + return buildinstdir, treedir, cachedir + +def writeYumConf(cachedir=None, repo=None, extrarepos=[], mirrorlist=[]): + """writeYumConf(cachedir=None, repo=None, [extrarepos=[], mirrorlist=[]]) + + Generate a temporary yum.conf file for image generation. The required + parameters are the cachedir that yum should use and the main repo to use. + + Optional parameters are a list of extra repositories to add to the + yum.conf file. The mirrorlist parameter is a list of yum mirrorlists + that should be added to the yum.conf file. + + Returns the path to the temporary yum.conf file on success, None of failure. + """ + + if cachedir is None or repo is None: + return None + + tmpdir = tempfile.gettempdir() + (f, yumconf) = tempfile.mkstemp('XXXXXX', 'yum.conf', tmpdir) + + f.write("[main]\n") + f.write("cachedir=%s\n" % (cachedir,)) + f.write("keepcache=0\n") + f.write("gpgcheck=0\n") + f.write("plugins=0\n") + f.write("reposdir=\n") + f.write("tsflags=nodocs\n\n") + f.write("[anacondarepo]\n") + f.write("name=anaconda repo\n") + f.write("baseurl=%s\n" % (repo,)) + f.write("enabled=1\n\n") + + if extrarepos != []: + n = 1 + for extra in extrarepos: + f.write("[anaconda-extrarepo-%d]\n" % (n,) + f.write("name=anaconda extra repo %d\n" % (n,) + f.write("baseurl=%s\n" % (extra,)) + f.write("enabled=1\n") + n += 1 + + if mirrorlist != []: + n = 1 + for mirror in mirrorlist: + f.write("[anaconda-mirrorlistrepo-%d]\n" % (n,) + f.write("name=anaconda mirrorlist repo %d\n" % (n,) + f.write("mirrorlist=%s\n" % (extra,)) + f.write("enabled=1\n") + n += 1 + + f.close() + return yumconf + +def cleanup(trash=[]) + """cleanup(trash) + + Given a list of things to remove, cleanup() will remove them if it can. + Never fails, just tries to remove things and returns regardless of + failures removing things. + + """ + + if trash is []: + return + + for item in trash: + if os.path.isdir(item): + shutil.rmtree(item, ignore_errors=True) + else: + os.unlink(item) + + return