[yum-git] plugins/tmprepo yum-utils.spec

James Antill james at linux.duke.edu
Mon Jul 21 15:14:05 UTC 2008


 plugins/tmprepo/tmprepo.py |   68 +++++++++++++++++++++++++++++++++++++++++----
 yum-utils.spec             |    1 
 2 files changed, 63 insertions(+), 6 deletions(-)

New commits:
commit 8adb0c1dc30b2d8b8ef31df990177cb5a0dbe604
Author: James Antill <james at and.org>
Date:   Mon Jul 21 11:12:17 2008 -0400

    Add _local_ directory support to tmprepo, requires createrepo.

diff --git a/plugins/tmprepo/tmprepo.py b/plugins/tmprepo/tmprepo.py
index ad8ca33..a3e9cdb 100644
--- a/plugins/tmprepo/tmprepo.py
+++ b/plugins/tmprepo/tmprepo.py
@@ -30,6 +30,8 @@ from yum import logginglevels
 import logging
 import urlgrabber.grabber
 import tempfile
+import os
+import shutil
 
 requires_api_version = '2.5'
 plugin_type = (TYPE_INTERACTIVE,)
@@ -54,18 +56,72 @@ def make_validate(log, gpgcheck):
 
     return tvalidate
 
+dnames = []
+class AutoCleanupDir:
+    """
+    Given a directory ... let it exist until "the end", and then clean it up.
+    """
+
+    def __init__(self, dname):
+        self.dname = dname
+        # Want it to live until python shutdown
+        dnames.append(self)
+
+    # Can't use __del__ as python doesn't dtrt. on exit
+    def cleanup(self):
+        shutil.rmtree(self.dname, ignore_errors=True)
+
+def close_hook(conduit):
+    global dnames
+    for dname in dnames:
+        dname.cleanup()
+    dnames = []
+
+def add_dir_repo(base, tmp_fname, trepo, log):
+    # Let people do it via. local directories ... we don't want this for
+    # HTTP because people _should_ just generate .repo files, but for
+    # local CDs of pkgs etc. we'll be nice.
+    trepo_path = trepo[len("file:"):]
+    trepo_data = AutoCleanupDir(tempfile.mkdtemp())
+    trepo_name = os.path.basename(os.path.dirname(trepo_path))
+    open(tmp_fname, "wb").write("""\
+[%(basename)s]
+name=Temp. for %(rbasename)s
+baseurl=file:%(dname)s
+enabled=1
+gpgcheck=1
+metadata_expire=0
+#  Make cost smaller, as we know it's "local" ... if this isn't good just create
+# your own .repo file. ... then you won't need to createrepo each run either.
+cost=500
+""" % {'basename'  : os.path.basename(tmp_fname),
+       'rbasename' : os.path.basename(trepo_name),
+       'dname'     : trepo_data.dname})
+    print "Creating repodata for directory:", os.path.basename(trepo_name)
+    os.spawnlp(os.P_WAIT, "createrepo", "createrepo", "--baseurl", trepo,
+               "--outputdir", trepo_data.dname, trepo_path)
+    AutoCleanupDir("%s/%s" % (base.conf.cachedir, os.path.basename(tmp_fname)))
+
 def add_repos(base, log, tmp_repos, tvalidate):
     """ Add temporary repos to yum. """
     # Don't use self._splitArg()? ... or require URLs without commas?
     for trepo in tmp_repos:
         tfo   = tempfile.NamedTemporaryFile()
         fname = tfo.name
-        grab = urlgrabber.grabber.URLGrabber()
-        try:
-            grab.urlgrab(trepo, fname)
-        except urlgrabber.grabber.URLGrabError, e:
-            log.warn("Failed to retrieve " + trepo)
-            continue
+        if trepo.startswith("/"):
+            trepo = "file:%s" % trepo
+        if trepo.startswith("file:") and trepo.endswith("/"):
+            if not os.path.isdir(trepo[len("file:"):]):
+                log.warn("Failed to find directory " + trepo[len("file:"):])
+                continue
+            add_dir_repo(base, fname, trepo)
+        else:
+            grab = urlgrabber.grabber.URLGrabber()
+            try:
+                grab.urlgrab(trepo, fname)
+            except urlgrabber.grabber.URLGrabError, e:
+                log.warn("Failed to retrieve " + trepo)
+                continue
 
         base.getReposFromConfigFile(fname, validate=tvalidate)
         added = True
diff --git a/yum-utils.spec b/yum-utils.spec
index b882dac..cbd45f4 100644
--- a/yum-utils.spec
+++ b/yum-utils.spec
@@ -231,6 +231,7 @@ each category, if any were specified.
 Summary: Yum plugin to add temporary repositories
 Group: System Environment/Base
 Requires: yum >= 3.2.11
+Requires: createrepo
 
 %description -n yum-tmprepo
 This plugin adds the option --tmprepo which takes a url to a .repo file



More information about the Yum-cvs-commits mailing list