summaryrefslogtreecommitdiffstats
path: root/scripts/pkgorder
blob: bc80c7876c39ee1a41fb88ebc4cc8f72dd615766 (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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/python
#
# pkgorder
#
# Copyright (C) 2005  Red Hat, Inc.  All rights reserved.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Author(s): Paul Nasrat <pnasrat@redhat.com>
#

import os.path
import glob
import rpm
import rpmUtils
import shutil
import string
import sys
import yum

sys.path.append("/usr/lib/anaconda")
sys.path.append("/usr/lib/booty")

import anaconda_log
import logging
logger = logging.getLogger("anaconda")
handler = logging.StreamHandler()
handler.setLevel(logging.ERROR)
logger.addHandler(handler)

from optparse import OptionParser
import yum

class PackageOrderer(yum.YumBase):
    def __init__(self, arch=None):
        yum.YumBase.__init__(self)
        self.arch = arch

    def _transactionDataFactory(self):
        return yum.transactioninfo.SortableTransactionData()

    def doFileLogSetup(self, uid, logfile):
        pass

    def doLoggingSetup(self, debuglevel, errorlevel):
        pass

    def setup(self, fn="/etc/yum.conf", root="/", excludes=[]):
        self.doConfigSetup(fn, root, init_plugins = False)
        self.conf.cache = 0
#         if hasattr(self.repos, 'sqlite'):
#             self.repos.sqlite = False
#             self.repos._selectSackType()
        exclude = self.conf.exclude
        exclude.extend(excludes)
        self.conf.exclude = exclude
        cachedir = yum.misc.getCacheDir()
        self.repos.setCacheDir(cachedir) 
        self.repos.setCache(0) 
        self.doRepoSetup()

        self.doSackSetup(rpmUtils.arch.getArchList(self.arch))
        self.doTsSetup()
        self.doGroupSetup()
        self.repos.populateSack('enabled', 'filelists')

    def getDownloadPkgs(self):
        pass

#XXX: sigh
processed = {}
def processTransaction(ds):
    del ds.ts
    ds.initActionTs()
    ds.populateTs(keepold=0)
    ds.ts.check()
    ds.ts.order()
    for (hdr, path) in ds.ts.ts.getKeys():
        fname = os.path.basename(path)
        fpattern = "%s*" % fname.rsplit('.', 2)[0]
        printMatchingPkgs(fpattern)

def printMatchingPkgs(fpattern):
    global processed

    if os.path.isdir("%s/%s/RPMS" % (toppath, product)):
        matches = glob.glob("%s/%s/RPMS/%s" % (toppath, product, fpattern))
    elif os.path.isdir("%s/%s" %(toppath, product)):
        matches = glob.glob("%s/%s/%s" % (toppath, product, fpattern))
    else:
        matches = glob.glob("%s/%s" % (toppath, fpattern))

    for match in matches:
        mname = os.path.basename(match)
        if processed.has_key(mname): continue
        processed[mname] = True
        print mname

def addPackages(ds, pkgLst):
    ds.initActionTs()
    for pkg in pkgLst:
        ds.install(pattern=pkg)
    ds.resolveDeps()
    processTransaction(ds)

def addGroups(ds, groupLst):
    ds.initActionTs()
    map(ds.selectGroup, filter(lambda x: ds.comps.has_group(x), groupLst))
    ds.resolveDeps()
    processTransaction(ds)

def createConfig(toppath):
    yumconfstr = """
[main]
distroverpkg=redhat-release
gpgcheck=0
reposdir=/dev/null
exclude=*debuginfo*

[anaconda]
name=Anaconda
baseurl=file://%s
enabled=1
""" % (toppath)
    
    try:
        (fd, path) = tempfile.mkstemp("", "yum-conf-", toppath)
    except (OSError, IOError), e:
        print >> sys.stderr, "Error writing to %s" % (toppath,)
        sys.exit(1)
    os.write(fd, yumconfstr)
    os.close(fd)
    return path

def usage():
    print >> sys.stderr, "pkgorder <toppath> <arch> <productpath>"
    print >> sys.stderr, "<arch>: use rpm architecture for tree, eg i686"

if __name__ == "__main__":
    import tempfile
    parser = OptionParser()
    parser.add_option("--debug", action="store_true", dest="debug", default=False)
    parser.add_option("--file", action="store", dest="file")
    parser.add_option("--product", action="store", dest="productPath", )
    parser.add_option("--exclude", action="append", dest="excludeList",
                      default=[])

    (options, args) = parser.parse_args()
     
    if len(args) != 3:
	usage()
        sys.exit(1)

    (toppath, arch, product) = args
    config = createConfig(toppath)

    # Boo.
    if arch == "i386":
        arch = "i686"

    # print out kernel related packages first
    #printMatchingPkgs("kernel-*")        

    if os.environ.has_key('TMPDIR'):
        testpath = "%s/pkgorder-%d" %(os.environ['TMPDIR'],os.getpid(),)
    else:
        testpath = "/tmp/pkgorder-%d" %(os.getpid(),)

    os.system("mkdir -p %s/var/lib/rpm" %(testpath,))
    
    ds = PackageOrderer(arch=arch)
    ds.setup(fn=config, excludes=options.excludeList, root = testpath)
    
    # hack, hack, hack... make sure iscsi ends up on disc1 (#208832)
    addPackages(ds, ["kernel-*","mkinitrd","mdadm"])

    addGroups(ds, ["core", "base", "text-internet"])

    addGroups(ds, ["base-x", "dial-up",
                   "graphical-internet", "editors", 
                   "gnome-desktop", "sound-and-video", "printing",
                   "fonts", "hardware-support", "admin-tools",
                   "java", "legacy-fonts"])

    addGroups(ds, ["office", "games", "graphics", "authoring-and-publishing"])

    addGroups(ds, ["web-server", "ftp-server", "sql-server",
                   "mysql", "server-cfg", "dns-server",
                   "smb-server"])

    addGroups(ds, ["kde-desktop", "development-tools", "development-libs",
                   "gnome-software-development", "eclipse",
                   "x-software-development",
                   "java-development", "kde-software-development",
                   "mail-server", "network-server", "legacy-network-server"])

    addGroups(ds, ["news-server", "legacy-software-development", 
                   "engineering-and-scientific"])

    #Everthing else but kernels
    for po in ds.pkgSack.returnPackages():
        if po.name.find("kernel") == -1:
            member = ds.tsInfo.addInstall(po)

    ds.resolveDeps()
    processTransaction(ds)
    os.unlink(config)
    shutil.rmtree(testpath)