]> git.donarmstrong.com Git - dak.git/blob - dak/ls.py
Merge branch 'master' of /srv/ftp.debian.org/git/dak
[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   -f, --format=control-suite use same format as control-suite for output
58
59 ARCH, COMPONENT and SUITE can be comma (or space) separated lists, e.g.
60     --architecture=amd64,i386"""
61     sys.exit(exit_code)
62
63 ################################################################################
64
65 def main ():
66     cnf = Config()
67
68     Arguments = [('a', "architecture", "Ls::Options::Architecture", "HasArg"),
69                  ('b', "binarytype", "Ls::Options::BinaryType", "HasArg"),
70                  ('c', "component", "Ls::Options::Component", "HasArg"),
71                  ('f', "format", "Ls::Options::Format", "HasArg"),
72                  ('g', "greaterorequal", "Ls::Options::GreaterOrEqual"),
73                  ('G', "greaterthan", "Ls::Options::GreaterThan"),
74                  ('r', "regex", "Ls::Options::Regex"),
75                  ('s', "suite", "Ls::Options::Suite", "HasArg"),
76                  ('S', "source-and-binary", "Ls::Options::Source-And-Binary"),
77                  ('h', "help", "Ls::Options::Help")]
78     for i in [ "architecture", "binarytype", "component", "format",
79                "greaterorequal", "greaterthan", "regex", "suite",
80                "source-and-binary", "help" ]:
81         if not cnf.has_key("Ls::Options::%s" % (i)):
82             cnf["Ls::Options::%s" % (i)] = ""
83
84     packages = apt_pkg.parse_commandline(cnf.Cnf, Arguments, sys.argv)
85     Options = cnf.subtree("Ls::Options")
86
87     if Options["Help"]:
88         usage()
89     if not packages:
90         utils.fubar("need at least one package name as an argument.")
91
92     session = DBConn().session()
93
94     # If cron.daily is running; warn the user that our output might seem strange
95     if os.path.exists(os.path.join(cnf["Dir::Lock"], "daily.lock")):
96         utils.warn("Archive maintenance is in progress; database inconsistencies are possible.")
97
98     # Handle buildd maintenance helper options
99     if Options["GreaterOrEqual"] or Options["GreaterThan"]:
100         if Options["GreaterOrEqual"] and Options["GreaterThan"]:
101             utils.fubar("-g/--greaterorequal and -G/--greaterthan are mutually exclusive.")
102         if not Options["Suite"]:
103             Options["Suite"] = "unstable"
104
105     # Parse -a/--architecture, -c/--component and -s/--suite
106     (con_suites, con_architectures, con_components, check_source) = \
107                  utils.parse_args(Options)
108
109     if Options["BinaryType"]:
110         if Options["BinaryType"] != "udeb" and Options["BinaryType"] != "deb":
111             utils.fubar("Invalid binary type.  'udeb' and 'deb' recognised.")
112         con_bintype = "AND b.type = '%s'" % (Options["BinaryType"])
113         # REMOVE ME TRAMP
114         if Options["BinaryType"] == "udeb":
115             check_source = 0
116     else:
117         con_bintype = ""
118
119     if Options["Regex"]:
120         comparison_operator = "~"
121     else:
122         comparison_operator = "="
123
124     if Options["Source-And-Binary"]:
125         new_packages = []
126         for package in packages:
127             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),
128                                 {'package': package})
129             new_packages.extend([ i[0] for i in q.fetchall() ])
130             if package not in new_packages:
131                 new_packages.append(package)
132         packages = new_packages
133
134     results = 0
135     for package in packages:
136         q = session.execute("""
137 SELECT b.package, b.version, a.arch_string, su.suite_name, c.name, m.name
138   FROM binaries b, architecture a, suite su, bin_associations ba,
139        files f, files_archive_map af, component c, maintainer m
140  WHERE b.package %s :package AND a.id = b.architecture AND su.id = ba.suite
141    AND b.id = ba.bin AND b.file = f.id AND af.file_id = f.id AND su.archive_id = af.archive_id
142    AND af.component_id = c.id AND b.maintainer = m.id %s %s %s
143 """ % (comparison_operator, con_suites, con_architectures, con_bintype), {'package': package})
144         ql = q.fetchall()
145         if check_source:
146             q = session.execute("""
147 SELECT s.source, s.version, 'source', su.suite_name, c.name, m.name
148   FROM source s, suite su, src_associations sa, files f, files_archive_map af,
149        component c, maintainer m
150  WHERE s.source %s :package AND su.id = sa.suite AND s.id = sa.source
151    AND s.file = f.id AND af.file_id = f.id AND af.archive_id = su.archive_id AND af.component_id = c.id
152    AND s.maintainer = m.id %s
153 """ % (comparison_operator, con_suites), {'package': package})
154             if not Options["Architecture"] or con_architectures:
155                 ql.extend(q.fetchall())
156             else:
157                 ql = q.fetchall()
158         d = {}
159         highver = {}
160         for i in ql:
161             results += 1
162             (pkg, version, architecture, suite, component, maintainer) = i
163             if component != "main":
164                 suite = "%s/%s" % (suite, component)
165             if not d.has_key(pkg):
166                 d[pkg] = {}
167             highver.setdefault(pkg,"")
168             if not d[pkg].has_key(version):
169                 d[pkg][version] = {}
170                 if apt_pkg.version_compare(version, highver[pkg]) > 0:
171                     highver[pkg] = version
172             if not d[pkg][version].has_key(suite):
173                 d[pkg][version][suite] = []
174             d[pkg][version][suite].append(architecture)
175
176         packages = d.keys()
177         packages.sort()
178
179         # Calculate optimal column sizes
180         sizes = [10, 13, 10]
181         for pkg in packages:
182             versions = d[pkg].keys()
183             for version in versions:
184                 suites = d[pkg][version].keys()
185                 for suite in suites:
186                        sizes[0] = max(sizes[0], len(pkg))
187                        sizes[1] = max(sizes[1], len(version))
188                        sizes[2] = max(sizes[2], len(suite))
189         fmt = "%%%is | %%%is | %%%is | "  % tuple(sizes)
190
191         for pkg in packages:
192             versions = d[pkg].keys()
193             versions.sort(apt_pkg.version_compare)
194             for version in versions:
195                 suites = d[pkg][version].keys()
196                 suites.sort()
197                 for suite in suites:
198                     arches = d[pkg][version][suite]
199                     arches.sort(utils.arch_compare_sw)
200                     if Options["Format"] == "": #normal
201                         sys.stdout.write(fmt % (pkg, version, suite))
202                         sys.stdout.write(", ".join(arches))
203                         sys.stdout.write('\n')
204                     elif Options["Format"] in [ "control-suite", "heidi" ]:
205                         for arch in arches:
206                             sys.stdout.write("%s %s %s\n" % (pkg, version, arch))
207             if Options["GreaterOrEqual"]:
208                 print "\n%s (>= %s)" % (pkg, highver[pkg])
209             if Options["GreaterThan"]:
210                 print "\n%s (>> %s)" % (pkg, highver[pkg])
211
212     if not results:
213         sys.exit(1)
214
215 #######################################################################################
216
217 if __name__ == '__main__':
218     main()