summaryrefslogtreecommitdiffstats
path: root/scripts/yumcache
blob: 50ff0ba13eef7484d30943da276fa8613ed6cc92 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#!/usr/bin/python

from optparse import OptionParser

import os
import glob
import shutil
import sys
import tempfile
import yum

class CacheConf:
    """Dynamic yum configuration"""

    def __init__( self, repopath):
        self.fd, self.name = tempfile.mkstemp(".conf", "yum-", "/tmp")
	self.cachedir = tempfile.mkdtemp("", "yum-cache-", "/tmp")
        self.repopath = repopath

        self.yumconfstr = """
[main]
distroverpkg=redhat-release
cachedir=%s
gpgcheck=0

[cache]
baseurl=file://%s
enabled=1
""" % (self.cachedir, self.repopath)

        os.write(self.fd, self.yumconfstr)
        os.close(self.fd)

class YumCacheGenerator(yum.YumBase):
    def __init__(self, repopath):
        yum.YumBase.__init__(self)
        self.repopath = repopath
        self.config = CacheConf(repopath)
        self.doConfigSetup(self.config.name)
        os.unlink(self.config.name)

    def log(self, level, msg):
	pass

    def errorlog(self, level, msg):
        pass

    def filelog(self, level, msg):
        pass

    def write(self):
	self.repos.disableRepo('*')
	self.repos.enableRepo('cache')
        self.doRepoSetup()
        self.repos.populateSack(with='metadata', pickleonly=1)
        self.repos.populateSack(with='filelists', pickleonly=1)
 	for cache in glob.glob("%s/cache/*.sqlite" %( self.config.cachedir,)):
            shutil.move(cache, "%s/repodata" % (self.config.repopath))
        
        shutil.rmtree(self.config.cachedir)

    
def usage():
	print "yumcache <toppath>"

if __name__ == "__main__":
    parser = OptionParser()
    (options, args) = parser.parse_args()

    if len(args) != 1:
	usage()
        sys.exit(1)
    repopath = args[0] 

    if not os.path.exists("%s/repodata/repomd.xml" % (repopath)):
        sys.stderr.write("No metadata found in %s" % (repopath,))
        sys.exit(2)

    y = YumCacheGenerator(repopath)
    y.write()