]> git.decadent.org.uk Git - dak.git/blob - claire.py
claire section/component fix.
[dak.git] / claire.py
1 #!/usr/bin/env python
2
3 # 'Fix' stable to make debian-cd and dpkg -BORGiE users happy
4 # Copyright (C) 2000  James Troup <james@nocrew.org>
5 # $Id: claire.py,v 1.2 2001-01-25 06:00:07 troup Exp $
6
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
11
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
16
17 # You should have received a copy of the GNU General Public License
18 # along with this program; if not, write to the Free Software
19 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20
21 # "Look around... leaves are brown... and the sky's a hazy shade of winter,
22 #  Look around... leaves are brown... there's a patch of snow on the ground."
23 #                                         -- Simon & Garfunkel / 'A Hazy Shade'
24
25 ################################################################################
26
27 import os, pg, re, string, sys
28 import utils, db_access
29 import apt_pkg;
30
31 ################################################################################
32
33 re_strip_section_prefix = re.compile(r'.*/');
34
35 Cnf = None;
36 projectB = None;
37
38 ################################################################################
39
40 # Relativize an absolute symlink from 'src' -> 'dest' relative to 'root'.
41 # Returns fixed 'src'
42 def clean_symlink (src, dest, root):
43     src = string.replace(src, root, '', 1);
44     dest = string.replace(dest, root, '', 1);
45     dest = os.path.dirname(dest);
46     new_src = '';
47     for i in xrange(len(string.split(dest, '/'))):
48         new_src = new_src + '../';
49     return new_src + src
50
51 ################################################################################
52
53 def fix_component_section (component, section):
54     if component == "":
55         (None, component) = utils.extract_component_from_section(section);
56
57     # FIXME: ugly hacks to work around override brain damage
58     section = re_strip_section_prefix.sub('', section);
59     section = string.replace(string.lower(section), 'non-us', '');
60     if section != '':
61         section = section + '/';
62
63     return (component, section);
64
65 ################################################################################
66
67 def find_dislocated_stable(Cnf, projectB):
68     dislocated_files = {}
69
70     # Source
71     q = projectB.query("""
72 SELECT DISTINCT ON (f.id) c.name, sec.section, l.path, f.filename, f.id
73     FROM component c, override o, section sec, source s, files f, location l,
74          dsc_files df, suite su, src_associations sa, files f2, location l2
75     WHERE su.suite_name = 'stable' AND sa.suite = su.id AND sa.source = s.id
76       AND f2.id = s.file AND f2.location = l2.id AND df.source = s.id
77       AND f.id = df.file AND f.location = l.id AND o.package = s.source
78       AND sec.id = o.section AND NOT (f.filename ~ '^potato/')
79       AND l.component = c.id
80 UNION SELECT DISTINCT ON (f.id) null, sec.section, l.path, f.filename, f.id
81     FROM component c, override o, section sec, source s, files f, location l,
82          dsc_files df, suite su, src_associations sa, files f2, location l2
83     WHERE su.suite_name = 'stable' AND sa.suite = su.id AND sa.source = s.id
84       AND f2.id = s.file AND f2.location = l2.id AND df.source = s.id
85       AND f.id = df.file AND f.location = l.id AND o.package = s.source
86       AND sec.id = o.section AND NOT (f.filename ~ '^potato/')
87       AND NOT EXISTS (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);
88 """);
89     for i in q.getresult():
90         src = i[2]+i[3]
91         (component, section) = fix_component_section(i[0], i[1]);
92         dest = "%sdists/stable/%s/source/%s%s" % (Cnf["Dir::RootDir"], component, section, os.path.basename(i[3]));
93         src = clean_symlink(src, dest, Cnf["Dir::RootDir"]);
94         if not os.path.exists(dest):
95             if Cnf.Find("Claire::Options::Verbose"):
96                 print src+' -> '+dest
97             os.symlink(src, dest);
98         dislocated_files[i[4]] = dest;
99
100     return dislocated_files;
101
102     # TODO later when there's something to test it with!
103     # Binary
104     q = projectB.query("""
105 SELECT DISTINCT ON (f.id) c.name, a.arch_string, sec.section, b.package,
106                           b.version, l.path, f.filename, f.id
107     FROM architecture a, bin_associations ba, binaries b, component c, files f,
108          location l, override o, section sec, suite su
109     WHERE su.suite_name = 'stable' AND ba.suite = su.id AND ba.bin = b.id
110       AND f.id = b.file AND f.location = l.id AND o.package = b.package
111       AND sec.id = o.section AND NOT (f.filename ~ '^potato/')
112       AND b.architecture = a.id AND l.component = c.id
113 UNION SELECT DISTINCT ON (f.id) null, a.arch_string, sec.section, b.package,
114                           b.version, l.path, f.filename, f.id
115     FROM architecture a, bin_associations ba, binaries b, component c, files f,
116          location l, override o, section sec, suite su
117     WHERE su.suite_name = 'stable' AND ba.suite = su.id AND ba.bin = b.id
118       AND f.id = b.file AND f.location = l.id AND o.package = b.package
119       AND sec.id = o.section AND NOT (f.filename ~ '^potato/')
120       AND b.architecture = a.id AND NOT EXISTS
121         (SELECT l.path FROM location l WHERE l.component IS NOT NULL AND f.location = l.id);
122 """);
123     for i in q.getresult():
124         (component, section) = fix_component_section(i[0], i[2]);
125         architecture = i[1];
126         package = i[3]
127         version = utils.re_no_epoch.sub('', i[4]);
128         src = i[5]+i[6]
129        
130         dest = "%sdists/stable/%s/binary-%s/%s%s_%s.deb" % (Cnf["Dir::RootDir"], component, architecture, section, package, version);
131         src = clean_symlink(src, dest, Cnf["Dir::RootDir"]);
132         if not os.path.exists(dest):
133             if Cnf.Find("Claire::Options::Verbose"):
134                 print src+' -> '+dest
135             os.symlink(src, dest);
136         dislocated_files[i[7]] = dest;
137
138     return dislocated_files
139
140 ################################################################################
141
142 def main ():
143     global Cnf, projectB;
144
145     apt_pkg.init();
146     
147     Cnf = apt_pkg.newConfiguration();
148     apt_pkg.ReadConfigFileISC(Cnf,utils.which_conf_file());
149
150     Arguments = [('d',"debug","Claire::Options::Debug", "IntVal"),
151                  ('h',"help","Claire::Options::Help"),
152                  ('v',"verbose","Claire::Options::Verbose"),
153                  ('V',"version","Claire::Options::Version")];
154
155     apt_pkg.ParseCommandLine(Cnf,Arguments,sys.argv);
156
157     projectB = pg.connect('projectb', 'localhost');
158
159     db_access.init(Cnf, projectB);
160
161     find_dislocated_stable(Cnf, projectB);
162
163 #######################################################################################
164
165 if __name__ == '__main__':
166     main()
167