jenna 11.7 KB
Newer Older
J
James Troup 已提交
1 2 3
#!/usr/bin/env python

# Generate file list which is then fed to apt-ftparchive to generate Packages and Sources files
J
James Troup 已提交
4
# Copyright (C) 2000, 2001  James Troup <james@nocrew.org>
5
# $Id: jenna,v 1.10 2001-04-16 17:45:06 troup Exp $
J
James Troup 已提交
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

# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#######################################################################################

# BTAF: "GOD *DAMMIT*!!  What the FUCK happened to my free will??"
#
# -- http://www.angryflower.com/timelo.gif

#######################################################################################

import pg, string, os, sys
import apt_pkg
J
James Troup 已提交
31
import db_access, utils, claire
J
James Troup 已提交
32 33 34 35

projectB = None
Cnf = None

J
James Troup 已提交
36
def generate_src_list(suite, component, output, dislocated_files):
J
James Troup 已提交
37 38 39 40 41
    sources = {}

    suite_id = db_access.get_suite_id(suite);
    
    if component == "-":
42
        q = projectB.query("SELECT s.source, l.path, f.filename, f.id FROM source s, src_associations sa, location l, files f WHERE sa.source = s.id AND sa.suite = '%d' AND l.id = f.location AND s.file = f.id ORDER BY s.source, s.version"
J
James Troup 已提交
43 44
                           % (suite_id));
    else:
45
        q = projectB.query("SELECT s.source, l.path, f.filename, f.id FROM source s, src_associations sa, location l, component c, files f WHERE lower(c.name) = '%s' AND (c.id = l.component OR l.component = NULL) AND sa.source = s.id AND sa.suite = '%d' AND l.id = f.location AND s.file = f.id ORDER BY s.source, s.version"
J
James Troup 已提交
46 47 48
                           % (component, suite_id));
    entries = q.getresult();
    for entry in entries:
49
        (source, path, filename, file_id) = entry;
J
James Troup 已提交
50 51
        if dislocated_files.has_key(file_id):
            filename = dislocated_files[file_id];
J
James Troup 已提交
52
        else:
53 54
            filename = path + filename;
        if sources.has_key(source):
55
            sys.stderr.write("E: %s in %s / %s / source is duplicated.  Gravity wins!\n" % (source, suite, component));
56 57
        else:
            sources[source] = filename;
J
James Troup 已提交
58 59 60 61 62

    # Write the list of files out
    source_keys = sources.keys();
    source_keys.sort();
    for source in source_keys:
63
        output.write(sources[source]+'\n')
J
James Troup 已提交
64
    
65 66
#########################################################################################

J
James Troup 已提交
67
def generate_bin_list(suite, component, architecture, output, type, dislocated_files):
J
James Troup 已提交
68 69 70 71 72
    packages = {}

    suite_id = db_access.get_suite_id(suite);
    
    if component == "-":
73
        q = projectB.query("SELECT b.package, l.path, f.filename, f.id FROM architecture a, binaries b, bin_associations ba, location l, files f WHERE ( a.arch_string = '%s' OR a.arch_string = 'all' ) AND a.id = b.architecture AND ba.bin = b.id AND ba.suite = '%d' AND l.id = f.location AND b.file = f.id AND b.type = '%s' ORDER BY b.package, b.version, a.arch_string" % (architecture, suite_id, type));
J
James Troup 已提交
74
    else:
75
        q = projectB.query("SELECT b.package, l.path, f.filename, f.id FROM architecture a, binaries b, bin_associations ba, location l, component c, files f WHERE lower(c.name) = '%s' AND (c.id = l.component OR l.component = NULL) AND (a.arch_string = '%s' OR a.arch_string = 'all') AND a.id = b.architecture AND ba.bin = b.id AND ba.suite = '%d' AND l.id = f.location AND b.file = f.id AND b.type = '%s' ORDER BY b.package, b.version, a.arch_string" % (component, architecture, suite_id, type));
J
James Troup 已提交
76 77
    entries = q.getresult();
    for entry in entries:
78
        (package, path, filename, file_id) = entry;
J
James Troup 已提交
79 80
        if dislocated_files.has_key(file_id):
            filename = dislocated_files[file_id];
81 82 83 84 85 86
        else:
            filename = path + filename;
        if packages.has_key(package):
            sys.stderr.write("E: %s in %s / %s / %s / %s is duplicated.  Gravity wins!" % (package, suite, component, architecture, type));
        else:
            packages[package] = filename;
J
James Troup 已提交
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
    # Write the list of files out
    package_keys = packages.keys();
    package_keys.sort();
    for package in package_keys:
        output.write(packages[package]+'\n')

#########################################################################################

##########
# <elmo> I'm doing it in python btw.. nothing against your monster
#        SQL, but the python wins in terms of speed and readiblity
# <aj> bah
# <aj> you suck!!!!!
# <elmo> sorry :(
# <aj> you are not!!!
# <aj> you mock my SQL!!!!
# <elmo> you want have contest of skillz??????
# <aj> all your skillz are belong to my sql!!!!
# <elmo> yo momma are belong to my python!!!!
# <aj> yo momma was SQLin' like a pig last night!
##########

# If something has moved from one component to another we need to
# clean out the old versions here.  The rest of jenna won't do this
# because it works on a per-component level for flexibility.

def clean_suite (suite):
    print "Cleaning out packages for %s..." % (suite)
    
    suite_id = db_access.get_suite_id(suite);
    q = projectB.query("""
SELECT b.id, b.package, a.arch_string, b.version, l.path, f.filename, c.name
  FROM binaries b, bin_associations ba, files f, location l, architecture a, component c
  WHERE ba.suite = %s AND ba.bin = b.id AND b.file = f.id AND
        f.location = l.id AND l.component = c.id AND b.architecture = a.id
UNION
SELECT s.id, s.source, 'source', s.version, l.path, f.filename, c.name
  FROM source s, src_associations sa, files f, location l, component c
  WHERE sa.suite = %s AND sa.source = s.id AND s.file = f.id AND
        f.location = l.id AND l.component = c.id;""" % (suite_id, suite_id));
    ql = q.getresult();
    d = {};
    for i in ql:
        (id, package, architecture, version, path, filename, component) = i;
        filename = path + filename;
        if architecture == "source":
            delete_table = "src_associations";
            delete_col = "source";
        else:
            delete_table = "bin_associations";
            delete_col = "bin";
        key = "%s~%s" % (package, architecture);
J
James Troup 已提交
140
        if os.path.exists(filename):
141 142 143 144 145
            if d.has_key(key):
                (other_version, other_component, other_id) = d[key];
                if apt_pkg.VersionCompare(version, other_version) != 1:
                    (keep_version, keep_component) = (other_version, other_component)
                    (delete_id, delete_version, delete_component) = (id, version, component)
J
James Troup 已提交
146
                else:
147 148 149 150 151 152 153 154 155 156
                    (keep_version, keep_component) = (version, component)
                    (delete_id, delete_version, delete_component) = (other_id, other_version, other_component)
                    d[key] = (version, component, id);
                if not Cnf.Find("Suite::%s::Untouchable" % (suite)):
                    print "deleting %s on %s (%s [%s]) in favour of newer version %s [%s]..." \
                          % (package, architecture, delete_version, delete_component, keep_version, keep_component);
                    projectB.query("DELETE FROM %s WHERE suite = %s AND %s = %s" % (delete_table, suite_id, delete_col, delete_id));
                else:
                    print "[untouchable] would delete %s on %s (%s [%s]) in favour of newer version %s [%s]..." \
                          % (package, architecture, delete_version, delete_component, keep_version, keep_component);
J
James Troup 已提交
157
            else:
158
                d[key] = (version, component, id);
J
James Troup 已提交
159 160 161
        else:
            if not Cnf.Find("Suite::%s::Untouchable" % (suite)):
                sys.stderr.write("WARNING: deleting %s because it doesn't exist.\n" % (filename));
162 163 164
                projectB.query("DELETE FROM %s WHERE suite = %s AND %s = %s" % (delete_table, suite_id, delete_col, delete_id));
            else:
                sys.stderr.write("WARNING: [untouchable] would delete %s because it doesn't exist.\n" % (filename));
J
James Troup 已提交
165

166
#########################################################################################
J
James Troup 已提交
167 168 169

def main():
    global Cnf, projectB;
J
James Troup 已提交
170
    dislocated_files = {};
J
James Troup 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
    
    apt_pkg.init();
    
    Cnf = apt_pkg.newConfiguration();
    apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());

    Arguments = [('a',"architecture","Jenna::Options::Architecture", "HasArg"),
                 ('c',"component","Jenna::Options::Component", "HasArg"),
                 ('d',"debug","Jenna::Options::Debug", "IntVal"),
                 ('h',"help","Jenna::Options::Help"),
                 ('s',"suite", "Jenna::Options::Suite", "HasArg"),
                 ('v',"verbose","Jenna::Options::Verbose"),
                 ('V',"version","Jenna::Options::Version")];

    apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);

187
    projectB = pg.connect(Cnf["DB::Name"], Cnf["DB::Host"], int(Cnf["DB::Port"]));
J
James Troup 已提交
188 189 190 191 192 193
    db_access.init(Cnf, projectB);

    if Cnf["Jenna::Options::Suite"] == "":
        Cnf["Jenna::Options::Suite"] = string.join(Cnf.SubTree("Suite").List());
    for suite in string.split(Cnf["Jenna::Options::Suite"]):
        suite = string.lower(suite);
J
James Troup 已提交
194
        if suite == 'stable':
195
            print "Undoing dislocation..."
J
James Troup 已提交
196
            dislocated_files = claire.find_dislocated_stable(Cnf, projectB);
197 198 199
        else:
            dislocated_files = {};
        clean_suite(suite);
J
James Troup 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
        components = Cnf["Jenna::Options::Component"];
        if not Cnf.has_key("Suite::%s::Components" % (suite)):
            components = "-";
        if components == "":
            components = string.join(Cnf.SubTree("Suite::%s::Components" % (suite)).List());
        for component in string.split(components):
            component = string.lower(component)
            architectures = Cnf["Jenna::Options::Architecture"];
            if architectures == "":
                architectures = string.join(Cnf.SubTree("Suite::%s::Architectures" % (suite)).List());
            for architecture in string.split(architectures):
                architecture = string.lower(architecture)
                if architecture == "all":
                    continue
                if architecture == "source":
                    print "Processing dists/%s/%s/%s..." % (suite, component, architecture);
                    output = utils.open_file("%s/%s_%s_%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w")
J
James Troup 已提交
217
                    generate_src_list(suite, component, output, dislocated_files);
J
James Troup 已提交
218 219 220 221
                    output.close();
                else:
                    print "Processing dists/%s/%s/binary-%s..." % (suite, component, architecture);
                    output = utils.open_file("%s/%s_%s_binary-%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w");
J
James Troup 已提交
222
                    generate_bin_list(suite, component, architecture, output, "deb", dislocated_files);
J
James Troup 已提交
223
                    output.close();
224
                    if component == "main" and (suite == "unstable" or suite == "testing"): # FIXME: must be a cleaner way to say debian-installer is main only?
J
James Troup 已提交
225 226
                        print "Processing dists/%s/%s/debian-installer/binary-%s..." % (suite,component, architecture);
                        output = utils.open_file("%s/%s_%s_debian-installer_binary-%s.list" % (Cnf["Dir::ListsDir"], suite, component, architecture), "w");
J
James Troup 已提交
227
                        generate_bin_list(suite, component, architecture, output, "udeb", dislocated_files);
J
James Troup 已提交
228 229
                        output.close();

230 231
#########################################################################################

J
James Troup 已提交
232 233
if __name__ == '__main__':
    main()