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
sync  
James Troup 已提交
5
# $Id: heidi,v 1.3 2001-01-10 06:08:03 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

# 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":
J
sync  
James Troup 已提交
59 60
        projectB.query("DELETE FROM bin_associations WHERE suite = %s" % (suite_id));
        projectB.query("DELETE FROM src_associations WHERE suite = %s" % (suite_id));
J
James Troup 已提交
61 62 63 64 65
        action = "add";
        
    for line in file.readlines():
        split_line = string.split(string.strip(line[:-1]));
        if len(split_line) != 3:
J
sync  
James Troup 已提交
66
            sys.stderr.write("W: '%s' does not break into 'package version architecture'.\n" % (line[:-1]));
J
James Troup 已提交
67 68 69 70 71 72 73
            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

        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
J
sync  
James Troup 已提交
87
            q = projectB.query("SELECT id FROM src_associations WHERE suite = %s and source = %s" % (suite_id, id));
J
James Troup 已提交
88 89 90 91 92 93 94 95
            ql = q.getresult();
            if ql == []:
                assoication_id = None;
            else:
                assoication_id = ql[0][0];
            # Take action
            if action == "add":
                if assoication_id != None:
J
sync  
James Troup 已提交
96
                    sys.stderr.write("W: '%s~%s~%s' already exists in suite %s.\n" % (package, version, architecture, suite_id));
J
James Troup 已提交
97 98
                    continue;
                else:
J
sync  
James Troup 已提交
99
                    q = projectB.query("INSERT INTO src_associations (suite, source) VALUES (%s, %s)" % (suite_id, id));
J
James Troup 已提交
100 101
            elif action == "remove":
                if assoication_id == None:
J
sync  
James Troup 已提交
102
                    sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %s.\n" % (package, version, architecture, suite_id));
J
James Troup 已提交
103 104
                    continue;
                else:
J
sync  
James Troup 已提交
105
                    q = projectB.query("DELETE FROM src_associations WHERE id = %s" % (assoication_id));
J
James Troup 已提交
106 107
        else:
            # Find the existing assoications ID, if any
J
sync  
James Troup 已提交
108
            q = projectB.query("SELECT id FROM bin_associations WHERE suite = %s and bin = %s" % (suite_id, id));
J
James Troup 已提交
109 110 111 112 113 114 115 116
            ql = q.getresult();
            if ql == []:
                assoication_id = None;
            else:
                assoication_id = ql[0][0];
            # Take action
            if action == "add":
                if assoication_id != None:
J
sync  
James Troup 已提交
117
                    sys.stderr.write("W: '%s~%s~%s' already exists in suite %s.\n" % (package, version, architecture, suite_id));
J
James Troup 已提交
118 119
                    continue;
                else:
J
sync  
James Troup 已提交
120
                    q = projectB.query("INSERT INTO bin_associations (suite, bin) VALUES (%s, %s)" % (suite_id, id));
J
James Troup 已提交
121 122
            elif action == "remove":
                if assoication_id == None:
J
sync  
James Troup 已提交
123
                    sys.stderr.write("W: '%s~%s~%s' doesn't exist in suite %s.\n" % (package, version, architecture, suite_id));
J
James Troup 已提交
124 125
                    continue;
                else:
J
sync  
James Troup 已提交
126
                    q = projectB.query("DELETE FROM bin_associations WHERE id = %s" % (assoication_id));
J
James Troup 已提交
127 128 129 130 131
              
#######################################################################################

def get_list (suite_id):
    # List binaries
J
sync  
James Troup 已提交
132
    q = projectB.query("SELECT b.package, b.version, a.arch_string FROM binaries b, bin_associations ba, architecture a WHERE ba.suite = %s AND ba.bin = b.id AND b.architecture = a.id" % (suite_id));
J
James Troup 已提交
133 134
    ql = q.getresult();
    for i in ql:
J
sync  
James Troup 已提交
135
        print string.join(i);
J
James Troup 已提交
136 137

    # List source
J
sync  
James Troup 已提交
138
    q = projectB.query("SELECT s.source, s.version FROM source s, src_associations sa WHERE sa.suite = %s AND sa.source = s.id" % (suite_id));
J
James Troup 已提交
139 140
    ql = q.getresult();
    for i in ql:
J
sync  
James Troup 已提交
141
        print string.join(i) + " source";
J
James Troup 已提交
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

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

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:
J
sync  
James Troup 已提交
197
                process_file(utils.open_file(file,'r'), suite_id, action);
J
James Troup 已提交
198 199 200 201 202 203 204 205
        else:
            process_file(sys.stdin, suite_id, action);

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

if __name__ == '__main__':
    main()