heidi 8.4 KB
Newer Older
J
James Troup 已提交
1 2 3 4
#!/usr/bin/env python

# Manipulate suite tags
# Copyright (C) 2000  James Troup <james@nocrew.org>
J
James Troup 已提交
5
# $Id: heidi,v 1.2 2000-12-05 04:27:48 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 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

# 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

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

# 8to6Guy: "Wow, Bob, You look rough!"
# BTAF: "Mbblpmn..."
# BTAF <.oO>: "You moron! This is what you get for staying up all night drinking vodka and salad dressing!"
# BTAF <.oO>: "This coffee I.V. drip is barely even keeping me awake! I need something with more kick! But what?"
# BTAF: "OMIGOD! I OVERDOSED ON HEROIN"
# CoWorker#n: "Give him air!!"
# CoWorker#n+1: "We need a syringe full of adrenaline!"
# CoWorker#n+2: "Stab him in the heart!"
# BTAF: "*YES!*"
# CoWorker#n+3: "Bob's been overdosing quite a bit lately..."
# CoWorker#n+4: "Third time this week."

# -- http://www.angryflower.com/8to6.gif

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

# Adds or removes packages from a suite.  Takes the list of files
# either from stdin or as a command line argument.  Special action
# "set", will reset the suite (!) and add all packages from scratch. 

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

import os, pg, string, sys;
import apt_pkg;
import utils, db_access;

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

Cnf = None;
projectB = None;

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

def process_file (file, suite_id, action):

    if action == "set":
        projectB.query("DELETE FROM bin_associations WHERE suite = %d" % (suite_id));
        projectB.query("DELETE FROM src_associations WHERE suite = %d" % (suite_id));
        action = "add";
        
    for line in file.readlines():
        split_line = string.split(string.strip(line[:-1]));
        if len(split_line) != 3:
            sys.stderr.write("W: '%s' does not break into 'package version architecture'.\n");
            continue;
        
        (package, version, architecture) = split_line;

        if architecture == "source":
            q = projectB.query("SELECT id FROM source WHERE source = '%s' AND version = '%s'" % (package, version))
        else:
J
James Troup 已提交
74
            q = projectB.query("SELECT b.id FROM binaries b, architecture a WHERE b.package = '%s' AND b.version = '%s' AND (a.arch_string = '%s' OR a.arch_string = 'all') AND b.architecture = a.id" % (package, version, architecture))
J
James Troup 已提交
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

        ql = q.getresult();
        if ql == []:
            sys.stderr.write("W: Couldn't find '%s~%s~%s'.\n" % (package, version, architecture));
            continue;
        if len(ql) > 1:
            sys.stderr.write("E: Found more than one match for '%s~%s~%s'.\n" % (package, version, architecture));
            continue;
        id = ql[0][0];

        if architecture == "source": 
            # Find the existing assoications ID, if any
            q = projectB.query("SELECT id FROM src_associations WHERE suite = %d and source = %d" % (suite_id, id));
            ql = q.getresult();
            if ql == []:
                assoication_id = None;
            else:
                assoication_id = ql[0][0];
            # Take action
            if action == "add":
                if assoication_id != None:
                    sys.stderr.write("W: '%s~%s~%s' already exists in suite %d.\n" % (package, version, architecture, suite_id));
                    continue;
                else:
                    q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%d, %d)" % (suite_id, id));
            elif action == "remove":
                if assoication_id == None:
                    sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %d.\n" % (package, version, architecture, suite_id));
                    continue;
                else:
                    q = projectB.query("DELETE FROM src_associations WHERE id = %d" % (assoication_id));
        else:
            # Find the existing assoications ID, if any
            q = projectB.query("SELECT id FROM bin_associations WHERE suite = %d and bin = %d" % (suite_id, id));
            ql = q.getresult();
            if ql == []:
                assoication_id = None;
            else:
                assoication_id = ql[0][0];
            # Take action
            if action == "add":
                if assoication_id != None:
                    sys.stderr.write("W: '%s~%s~%s' already exists in suite %d.\n" % (package, version, architecture, suite_id));
                    continue;
                else:
                    q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%d, %d)" % (suite_id, id));
            elif action == "remove":
                if assoication_id == None:
                    sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %d.\n" % (package, version, architecture, suite_id));
                    continue;
                else:
                    q = projectB.query("DELETE FROM bin_associations WHERE id = %d" % (assoication_id));
              
#######################################################################################

def get_list (suite_id):
    # List binaries
    q = projectB.query("SELECT b.package, b.version, a.arch_string FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %d AND ba.bin = b.id AND b.architecture = a.id" % (suite_id));
    ql = q.getresult();
    for i in ql:
        print "%s %s %s" % (i[0], i[1], i[2]);

    # List source
    q = projectB.query("SELECT s.source, s.version FROM source s, src_associations sa WHERE sa.suite = %d AND sa.source = s.id" % (suite_id));
    ql = q.getresult();
    for i in ql:
        print "%s %s source" % (i[0], i[1]);

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

def main ():
    global Cnf, projectB;

    apt_pkg.init();
    
    Cnf = apt_pkg.newConfiguration();
    apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());

    Arguments = [('a',"add","Heidi::Options::Add", "HasArg"),
                 ('d',"debug","Heidi::Options::Debug", "IntVal"),
                 ('h',"help","Heidi::Options::Help"),
                 ('l',"list","Heidi::Options::List","HasArg"),
                 ('r',"remove", "Heidi::Options::Remove", "HasArg"),
                 ('s',"set", "Heidi::Options::Set", "HasArg"),
                 ('v',"version","Heidi::Options::Version")];

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

    projectB = pg.connect('projectb', 'localhost');

    db_access.init(Cnf, projectB);

    action = None;

    for i in ("add", "list", "remove", "set"):
        suite = Cnf["Heidi::Options::%s" % (i)];
        if suite !="":
            if not Cnf.has_key("Suite::%s" % (suite)):
                sys.stderr.write("Unknown suite %s.\n" %(suite));
                sys.exit(2);
            else:
                suite_id = db_access.get_suite_id(suite);
                if action != None:
                    sys.stderr.write("Can only do one action at a time.\n");
                    sys.exit(2);
                action = i;

    # Need an action...
    if action == None:
        sys.stderr.write("No action specified.\n");
        sys.exit(2);

    # Safety/Sanity check
    if action == "set" and suite != "testing":
        sys.stderr.write("Will not reset a suite other than testing...\n");
        sys.exit(2);

    if action == "list":
        get_list(suite_id);
    else:
        if file_list != []:
            for file in file_list:
                process_file(utils.open_file(file_list[0],'r'), suite_id, action);
        else:
            process_file(sys.stdin, suite_id, action);

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

if __name__ == '__main__':
    main()