]> git.decadent.org.uk Git - dak.git/blob - dak/ls.py
Merge branch 'master' into dbtests
[dak.git] / dak / ls.py
1 #!/usr/bin/env python
2
3 """
4 Display information about package(s) (suite, version, etc.)
5
6 @contact: Debian FTP Master <ftpmaster@debian.org>
7 @copyright: 2000, 2001, 2002, 2003, 2004, 2005, 2006  James Troup <james@nocrew.org>
8 @license: GNU General Public License version 2 or later
9
10 """
11 # This program is free software; you can redistribute it and/or modify
12 # it under the terms of the GNU General Public License as published by
13 # the Free Software Foundation; either version 2 of the License, or
14 # (at your option) any later version.
15
16 # This program is distributed in the hope that it will be useful,
17 # but WITHOUT ANY WARRANTY; without even the implied warranty of
18 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 # GNU General Public License for more details.
20
21 # You should have received a copy of the GNU General Public License
22 # along with this program; if not, write to the Free Software
23 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24
25 ################################################################################
26
27 # <aj> ooo, elmo has "special powers"
28 # <neuro> ooo, does he have lasers that shoot out of his eyes?
29 # <aj> dunno
30 # <aj> maybe he can turn invisible? that'd sure help with improved transparency!
31
32 ################################################################################
33
34 import os
35 import sys
36 import apt_pkg
37
38 from daklib.config import Config
39 from daklib.dbconn import *
40 from daklib import utils
41
42 ################################################################################
43
44 def usage (exit_code=0):
45     print """Usage: dak ls [OPTION] PACKAGE[...]
46 Display information about PACKAGE(s).
47
48   -a, --architecture=ARCH    only show info for ARCH(s)
49   -b, --binary-type=TYPE     only show info for binary TYPE
50   -c, --component=COMPONENT  only show info for COMPONENT(s)
51   -g, --greaterorequal       show buildd 'dep-wait pkg >= {highest version}' info
52   -G, --greaterthan          show buildd 'dep-wait pkg >> {highest version}' info
53   -h, --help                 show this help and exit
54   -r, --regex                treat PACKAGE as a regex
55   -s, --suite=SUITE          only show info for this suite
56   -S, --source-and-binary    show info for the binary children of source pkgs
57
58 ARCH, COMPONENT and SUITE can be comma (or space) separated lists, e.g.
59     --architecture=amd64,i386"""
60     sys.exit(exit_code)
61
62 ################################################################################
63
64 def main ():
65     cnf = Config()
66
67     Arguments = [('a', "architecture", "Ls::Options::Architecture", "HasArg"),
68                  ('b', "binarytype", "Ls::Options::BinaryType", "HasArg"),
69                  ('c', "component", "Ls::Options::Component", "HasArg"),
70                  ('f', "format", "Ls::Options::Format", "HasArg"),
71                  ('g', "greaterorequal", "Ls::Options::GreaterOrEqual"),
72                  ('G', "greaterthan", "Ls::Options::GreaterThan"),
73                  ('r', "regex", "Ls::Options::Regex"),
74                  ('s', "suite", "Ls::Options::Suite", "HasArg"),
75                  ('S', "source-and-binary", "Ls::Options::Source-And-Binary"),
76                  ('h', "help", "Ls::Options::Help")]
77     for i in [ "architecture", "binarytype", "component", "format",
78                "greaterorequal", "greaterthan", "regex", "suite",
79                "source-and-binary", "help" ]:
80         if not cnf.has_key("Ls::Options::%s" % (i)):
81             cnf["Ls::Options::%s" % (i)] = ""
82
83     packages = apt_pkg.ParseCommandLine(cnf.Cnf, Arguments, sys.argv)
84     Options = cnf.SubTree("Ls::Options")
85
86     if Options["Help"]:
87         usage()
88     if not packages:
89         utils.fubar("need at least one package name as an argument.")
90
91     session = DBConn().session()
92
93     # If cron.daily is running; warn the user that our output might seem strange
94     if os.path.exists(os.path.join(cnf["Dir::Lock"], "daily.lock")):
95         utils.warn("Archive maintenance is in progress; database inconsistencies are possible.")
96
97     # Handle buildd maintenance helper options
98     if Options["GreaterOrEqual"] or Options["GreaterThan"]:
99         if Options["GreaterOrEqual"] and Options["GreaterThan"]:
100             utils.fubar("-g/--greaterorequal and -G/--greaterthan are mutually exclusive.")
101         if not Options["Suite"]:
102             Options["Suite"] = "unstable"
103
104     # Parse -a/--architecture, -c/--component and -s/--suite
105     (con_suites, con_architectures, con_components, check_source) = \
106                  utils.parse_args(Options)
107
108     if Options["BinaryType"]:
109         if Options["BinaryType"] != "udeb" and Options["BinaryType"] != "deb":
110             utils.fubar("Invalid binary type.  'udeb' and 'deb' recognised.")
111         con_bintype = "AND b.type = '%s'" % (Options["BinaryType"])
112         # REMOVE ME TRAMP
113         if Options["BinaryType"] == "udeb":
114             check_source = 0
115     else:
116         con_bintype = ""
117
118     if Options["Regex"]:
119         comparison_operator = "~"
120     else:
121         comparison_operator = "="
122
123     if Options["Source-And-Binary"]:
124         new_packages = []
125         for package in packages:
126             q = session.execute("SELECT DISTINCT b.package FROM binaries b, bin_associations ba, suite su, source s WHERE b.source = s.id AND su.id = ba.suite AND b.id = ba.bin AND s.source %s :package %s" % (comparison_operator, con_suites),
127                                 {'package': package})
128             new_packages.extend([ i[0] for i in q.fetchall() ])
129             if package not in new_packages:
130                 new_packages.append(package)
131         packages = new_packages
132
133     results = 0
134     for package in packages:
135         q = session.execute("""
136 SELECT b.package, b.version, a.arch_string, su.suite_name, c.name, m.name
137   FROM binaries b, architecture a, suite su, bin_associations ba,
138        files f, location l, component c, maintainer m
139  WHERE b.package %s :package AND a.id = b.architecture AND su.id = ba.suite
140    AND b.id = ba.bin AND b.file = f.id AND f.location = l.id
141    AND l.component = c.id AND b.maintainer = m.id %s %s %s
142 """ % (comparison_operator, con_suites, con_architectures, con_bintype), {'package': package})
143         ql = q.fetchall()
144         if check_source:
145             q = session.execute("""
146 SELECT s.source, s.version, 'source', su.suite_name, c.name, m.name
147   FROM source s, suite su, src_associations sa, files f, location l,
148        component c, maintainer m
149  WHERE s.source %s :package AND su.id = sa.suite AND s.id = sa.source
150    AND s.file = f.id AND f.location = l.id AND l.component = c.id
151    AND s.maintainer = m.id %s
152 """ % (comparison_operator, con_suites), {'package': package})
153             if not Options["Architecture"] or con_architectures:
154                 ql.extend(q.fetchall())
155             else:
156                 ql = q.fetchall()
157         d = {}
158         highver = {}
159         for i in ql:
160             results += 1
161             (pkg, version, architecture, suite, component, maintainer) = i
162             if component != "main":
163                 suite = "%s/%s" % (suite, component)
164             if not d.has_key(pkg):
165                 d[pkg] = {}
166             highver.setdefault(pkg,"")
167             if not d[pkg].has_key(version):
168                 d[pkg][version] = {}
169                 if apt_pkg.VersionCompare(version, highver[pkg]) > 0:
170                     highver[pkg] = version
171             if not d[pkg][version].has_key(suite):
172                 d[pkg][version][suite] = []
173             d[pkg][version][suite].append(architecture)
174
175         packages = d.keys()
176         packages.sort()
177         for pkg in packages:
178             versions = d[pkg].keys()
179             versions.sort(apt_pkg.VersionCompare)
180             for version in versions:
181                 suites = d[pkg][version].keys()
182                 suites.sort()
183                 for suite in suites:
184                     arches = d[pkg][version][suite]
185                     arches.sort(utils.arch_compare_sw)
186                     if Options["Format"] == "": #normal
187                         sys.stdout.write("%10s | %10s | %13s | " % (pkg, version, suite))
188                         sys.stdout.write(", ".join(arches))
189                         sys.stdout.write('\n')
190                     elif Options["Format"] in [ "control-suite", "heidi" ]:
191                         for arch in arches:
192                             sys.stdout.write("%s %s %s\n" % (pkg, version, arch))
193             if Options["GreaterOrEqual"]:
194                 print "\n%s (>= %s)" % (pkg, highver[pkg])
195             if Options["GreaterThan"]:
196                 print "\n%s (>> %s)" % (pkg, highver[pkg])
197
198     if not results:
199         sys.exit(1)
200
201 #######################################################################################
202
203 if __name__ == '__main__':
204     main()