~mvo/python-apt/debian-sid-mirrored

175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
1
#  Copyright (c) 2005-2010 Canonical
2
#
3
#  Author: Michael Vogt <michael.vogt@ubuntu.com>
4
#
5
#  This program is free software; you can redistribute it and/or
6
#  modify it under the terms of the GNU General Public License as
7
#  published by the Free Software Foundation; either version 2 of the
8
#  License, or (at your option) any later version.
9
#
10
#  This program is distributed in the hope that it will be useful,
11
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
#  GNU General Public License for more details.
14
#
15
#  You should have received a copy of the GNU General Public License
16
#  along with this program; if not, write to the Free Software
17
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
#  USA
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
19
"""Classes for working with locally available Debian packages."""
175.1.213 by Michael Vogt
* tests/test_debs/*.deb, tests/test_debfile.py:
20
import apt
415.2.1 by Kiwinote
Merge gdebi changes
21
import apt_inst
22
import apt_pkg
23
import gzip
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
24
import os
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
25
import sys
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
26
217.3.170 by Julian Andres Klode
apt: Use apt_pkg.gettext instead of Python's gettext.
27
from apt_pkg import gettext as _
415.2.1 by Kiwinote
Merge gdebi changes
28
from StringIO import StringIO
196.1.3 by Ben Finney
Fixes to blank lines, to conform with PEP 8.
29
175.1.65 by Michael Vogt
* apt/debfile.py:
30
class NoDebArchiveException(IOError):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
31
    """Exception which is raised if a file is no Debian archive."""
32
175.1.65 by Michael Vogt
* apt/debfile.py:
33
class DebPackage(object):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
34
    """A Debian Package (.deb file)."""
175.1.65 by Michael Vogt
* apt/debfile.py:
35
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
36
    # Constants for comparing the local package file with the version
37
    # in the cache
38
    (VERSION_NONE, 
39
     VERSION_OUTDATED, 
40
     VERSION_SAME, 
41
     VERSION_NEWER) = range(4)
415.2.1 by Kiwinote
Merge gdebi changes
42
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
43
    debug = 0
44
45
    def __init__(self, filename=None, cache=None):
175.1.213 by Michael Vogt
* tests/test_debs/*.deb, tests/test_debfile.py:
46
        if cache is None:
47
            cache = apt.Cache()
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
48
        self._cache = cache
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
49
        self._debfile = None
50
        self.pkgname = ""
415.2.1 by Kiwinote
Merge gdebi changes
51
        self._sections = {}
175.1.220 by Michael Vogt
* apt/debfile.py:
52
        self._need_pkgs = []
563 by Michael Vogt
cleanup based on feedback from juliank
53
        self._check_was_run = False
175.1.221 by Michael Vogt
merged from debian-sid, fix crash in Dsc file handling
54
        self._failure_string = ""
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
55
        self._multiarch = None
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
56
        if filename:
57
            self.open(filename)
175.1.65 by Michael Vogt
* apt/debfile.py:
58
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
59
    def open(self, filename):
60
        """ open given debfile """
175.1.219 by Michael Vogt
apt/debfile.py: improve logging
61
        self._dbg(3, "open '%s'" % filename)
175.1.213 by Michael Vogt
* tests/test_debs/*.deb, tests/test_debfile.py:
62
        self._need_pkgs = []
63
        self._installed_conflicts = set()
64
        self._failure_string = ""
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
65
        self.filename = filename
508 by Julian Andres Klode
all: Fix all instances of ResourceWarning about unclosed files
66
        self._debfile = apt_inst.DebFile(self.filename)
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
67
        control = self._debfile.control.extractdata("control")
175.1.231 by Michael Vogt
merged patch from Samuel Lidén Borell to fix crash if there utf8
68
        self._sections = apt_pkg.TagSection(control)
175.1.65 by Michael Vogt
* apt/debfile.py:
69
        self.pkgname = self._sections["Package"]
563 by Michael Vogt
cleanup based on feedback from juliank
70
        self._check_was_run = False
71
        
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
72
    def __getitem__(self, key):
73
        return self._sections[key]
74
75
    @property
76
    def filelist(self):
77
        """return the list of files in the deb."""
78
        files = []
79
        try:
80
            self._debfile.data.go(lambda item, data: files.append(item.name))
81
        except SystemError:
549.1.41 by Michael Vogt
* apt/debfile.py:
82
            return [_("List of files for '%s' could not be read") %
83
                    self.filename]
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
84
        return files
85
549.1.41 by Michael Vogt
* apt/debfile.py:
86
    @property
87
    def control_filelist(self):
88
        """ return the list of files in control.tar.gt """
89
        control = []
90
        try:
91
            self._debfile.control.go(lambda item, data: control.append(item.name))
92
        except SystemError:
93
            return [_("List of control files for '%s' could not be read") %
94
                    self.filename]
95
        return sorted(control)
96
97
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
98
    # helper that will return a pkgname with a multiarch suffix if needed
549.2.2 by Michael Vogt
make the tests pass
99
    def _maybe_append_multiarch_suffix(self, pkgname, 
100
                                       in_conflict_checking=False):
101
        # trivial cases
102
        if not self._multiarch:
103
            return pkgname
104
        elif self._cache.is_virtual_package(pkgname):
105
            return pkgname
549.1.33 by Michael Vogt
* apt/debfile.py:
106
        elif (pkgname in self._cache and 
107
              self._cache[pkgname].candidate.architecture == "all"):
549.2.2 by Michael Vogt
make the tests pass
108
            return pkgname
109
        # now do the real multiarch checking
110
        multiarch_pkgname = "%s:%s" % (pkgname, self._multiarch)
111
        # the upper layers will handle this
112
        if not multiarch_pkgname in self._cache:
113
            return multiarch_pkgname
114
        # now check the multiarch state
115
        cand = self._cache[multiarch_pkgname].candidate._cand
116
        #print pkgname, multiarch_pkgname, cand.multi_arch
117
        # the default is to add the suffix, unless its a pkg that can satify
118
        # foreign dependencies
119
        if cand.multi_arch & cand.MULTI_ARCH_FOREIGN:
120
            return pkgname
121
        # for conflicts we need a special case here, any not multiarch enabled
122
        # package has a implicit conflict
123
        if (in_conflict_checking and 
124
            not (cand.multi_arch & cand.MULTI_ARCH_SAME)):
125
            return pkgname
126
        return multiarch_pkgname
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
127
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
128
    def _is_or_group_satisfied(self, or_group):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
129
        """Return True if at least one dependency of the or-group is satisfied.
130
131
        This method gets an 'or_group' and analyzes if at least one dependency
132
        of this group is already satisfied.
133
        """
134
        self._dbg(2, "_checkOrGroup(): %s " % (or_group))
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
135
136
        for dep in or_group:
137
            depname = dep[0]
138
            ver = dep[1]
139
            oper = dep[2]
140
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
141
            # multiarch
142
            depname = self._maybe_append_multiarch_suffix(depname)
143
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
144
            # check for virtual pkgs
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
145
            if not depname in self._cache:
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
146
                if self._cache.is_virtual_package(depname):
415.2.1 by Kiwinote
Merge gdebi changes
147
                    self._dbg(3, "_is_or_group_satisfied(): %s is virtual dep" % depname)
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
148
                    for pkg in self._cache.get_providing_packages(depname):
149
                        if pkg.is_installed:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
150
                            return True
151
                continue
415.2.1 by Kiwinote
Merge gdebi changes
152
            # check real dependency
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
153
            inst = self._cache[depname].installed
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
154
            if inst is not None and apt_pkg.check_dep(inst.version, oper, ver):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
155
                return True
415.2.1 by Kiwinote
Merge gdebi changes
156
157
            # if no real dependency is installed, check if there is
158
            # a package installed that provides this dependency
159
            # (e.g. scrollkeeper dependecies are provided by rarian-compat)
160
            # but only do that if there is no version required in the 
161
            # dependency (we do not supprot versionized dependencies)
162
            if not oper:
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
163
                for ppkg in self._cache.get_providing_packages(
164
                    depname, include_nonvirtual=True):
415.2.1 by Kiwinote
Merge gdebi changes
165
                    if ppkg.is_installed:
166
                        self._dbg(3, "found installed '%s' that provides '%s'" % (ppkg.name, depname))
167
                        return True
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
168
        return False
202.1.1 by Julian Andres Klode
Cleanup: Remove whitespace at the end of line in all python codes.
169
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
170
    def _satisfy_or_group(self, or_group):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
171
        """Try to satisfy the or_group."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
172
        for dep in or_group:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
173
            depname, ver, oper = dep
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
174
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
175
            # multiarch
176
            depname = self._maybe_append_multiarch_suffix(depname)
177
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
178
            # if we don't have it in the cache, it may be virtual
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
179
            if not depname in self._cache:
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
180
                if not self._cache.is_virtual_package(depname):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
181
                    continue
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
182
                providers = self._cache.get_providing_packages(depname)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
183
                # if a package just has a single virtual provider, we
184
                # just pick that (just like apt)
185
                if len(providers) != 1:
186
                    continue
187
                depname = providers[0].name
202.1.1 by Julian Andres Klode
Cleanup: Remove whitespace at the end of line in all python codes.
188
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
189
            # now check if we can satisfy the deps with the candidate(s)
190
            # in the cache
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
191
            pkg = self._cache[depname]
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
192
            cand = self._cache._depcache.get_candidate_ver(pkg._pkg)
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
193
            if not cand:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
194
                continue
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
195
            if not apt_pkg.check_dep(cand.ver_str, oper, ver):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
196
                continue
197
198
            # check if we need to install it
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
199
            self._dbg(2, "Need to get: %s" % depname)
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
200
            self._need_pkgs.append(depname)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
201
            return True
202
203
        # if we reach this point, we failed
204
        or_str = ""
205
        for dep in or_group:
206
            or_str += dep[0]
415.2.1 by Kiwinote
Merge gdebi changes
207
            if ver and oper:
208
                or_str += " (%s %s)" % (dep[2], dep[1])
209
            if dep != or_group[len(or_group)-1]:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
210
                or_str += "|"
415.2.1 by Kiwinote
Merge gdebi changes
211
        self._failure_string += _("Dependency is not satisfiable: %s\n") % or_str
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
212
        return False
213
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
214
    def _check_single_pkg_conflict(self, pkgname, ver, oper):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
215
        """Return True if a pkg conflicts with a real installed/marked pkg."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
216
        # FIXME: deal with conflicts against its own provides
217
        #        (e.g. Provides: ftp-server, Conflicts: ftp-server)
415.2.1 by Kiwinote
Merge gdebi changes
218
        self._dbg(3, "_check_single_pkg_conflict() pkg='%s' ver='%s' oper='%s'" % (pkgname, ver, oper))
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
219
        pkg = self._cache[pkgname]
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
220
        if pkg.is_installed:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
221
            pkgver = pkg.installed.version
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
222
        elif pkg.marked_install:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
223
            pkgver = pkg.candidate.version
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
224
        else:
225
            return False
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
226
        #print "pkg: %s" % pkgname
227
        #print "ver: %s" % ver
228
        #print "pkgver: %s " % pkgver
229
        #print "oper: %s " % oper
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
230
        if (apt_pkg.check_dep(pkgver, oper, ver) and not
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
231
            self.replaces_real_pkg(pkgname, oper, ver)):
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
232
            self._failure_string += _("Conflicts with the installed package "
233
                                      "'%s'") % pkg.name
175.1.219 by Michael Vogt
apt/debfile.py: improve logging
234
            self._dbg(3, "conflicts with installed pkg '%s'" % pkg.name)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
235
            return True
236
        return False
237
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
238
    def _check_conflicts_or_group(self, or_group):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
239
        """Check the or-group for conflicts with installed pkgs."""
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
240
        self._dbg(2, "_check_conflicts_or_group(): %s " % (or_group))
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
241
        for dep in or_group:
242
            depname = dep[0]
243
            ver = dep[1]
244
            oper = dep[2]
245
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
246
            # FIXME: is this good enough? i.e. will apt always populate
247
            #        the cache with conflicting pkgnames for our arch?
549.2.2 by Michael Vogt
make the tests pass
248
            depname = self._maybe_append_multiarch_suffix(
249
                depname, in_conflict_checking=True)
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
250
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
251
            # check conflicts with virtual pkgs
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
252
            if not depname in self._cache:
202.1.1 by Julian Andres Klode
Cleanup: Remove whitespace at the end of line in all python codes.
253
                # FIXME: we have to check for virtual replaces here as
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
254
                #        well (to pass tests/gdebi-test8.deb)
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
255
                if self._cache.is_virtual_package(depname):
256
                    for pkg in self._cache.get_providing_packages(depname):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
257
                        self._dbg(3, "conflicts virtual check: %s" % pkg.name)
258
                        # P/C/R on virtal pkg, e.g. ftpd
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
259
                        if self.pkgname == pkg.name:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
260
                            self._dbg(3, "conflict on self, ignoring")
261
                            continue
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
262
                        if self._check_single_pkg_conflict(pkg.name, ver, 
263
                                                           oper):
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
264
                            self._installed_conflicts.add(pkg.name)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
265
                continue
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
266
            if self._check_single_pkg_conflict(depname, ver, oper):
267
                self._installed_conflicts.add(depname)
268
        return bool(self._installed_conflicts)
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
269
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
270
    @property
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
271
    def conflicts(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
272
        """List of package names conflicting with this package."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
273
        key = "Conflicts"
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
274
        try:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
275
            return apt_pkg.parse_depends(self._sections[key])
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
276
        except KeyError:
277
            return []
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
278
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
279
    @property
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
280
    def depends(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
281
        """List of package names on which this package depends on."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
282
        depends = []
283
        # find depends
415.2.1 by Kiwinote
Merge gdebi changes
284
        for key in "Depends", "Pre-Depends":
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
285
            try:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
286
                depends.extend(apt_pkg.parse_depends(self._sections[key]))
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
287
            except KeyError:
288
                pass
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
289
        return depends
290
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
291
    @property
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
292
    def provides(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
293
        """List of virtual packages which are provided by this package."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
294
        key = "Provides"
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
295
        try:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
296
            return apt_pkg.parse_depends(self._sections[key])
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
297
        except KeyError:
298
            return []
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
299
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
300
    @property
175.20.6 by Sebastian Heinlein
DebPackage: turn get(Provides|Replaces|Depends|Conflicts)() into attributes
301
    def replaces(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
302
        """List of packages which are replaced by this package."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
303
        key = "Replaces"
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
304
        try:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
305
            return apt_pkg.parse_depends(self._sections[key])
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
306
        except KeyError:
307
            return []
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
308
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
309
    def replaces_real_pkg(self, pkgname, oper, ver):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
310
        """Return True if a given non-virtual package is replaced.
311
312
        Return True if the deb packages replaces a real (not virtual)
313
        packages named (pkgname, oper, ver).
314
        """
415.2.1 by Kiwinote
Merge gdebi changes
315
        self._dbg(3, "replaces_real_pkg() %s %s %s" % (pkgname, oper, ver))
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
316
        pkg = self._cache[pkgname]
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
317
        if pkg.is_installed:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
318
            pkgver = pkg.installed.version
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
319
        elif pkg.marked_install:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
320
            pkgver = pkg.candidate.version
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
321
        else:
322
            pkgver = None
323
        for or_group in self.replaces:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
324
            for (name, ver, oper) in or_group:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
325
                if (name == pkgname and apt_pkg.check_dep(pkgver, oper, ver)):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
326
                    self._dbg(3, "we have a replaces in our package for the "
327
                                 "conflict against '%s'" % (pkgname))
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
328
                    return True
329
        return False
330
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
331
    def check_conflicts(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
332
        """Check if there are conflicts with existing or selected packages.
333
334
        Check if the package conflicts with a existing or to be installed
335
        package. Return True if the pkg is OK.
336
        """
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
337
        res = True
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
338
        for or_group in self.conflicts:
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
339
            if self._check_conflicts_or_group(or_group):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
340
                #print "Conflicts with a exisiting pkg!"
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
341
                #self._failure_string = "Conflicts with a exisiting pkg!"
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
342
                res = False
343
        return res
344
415.2.1 by Kiwinote
Merge gdebi changes
345
    def check_breaks_existing_packages(self):
346
        """ 
347
        check if installing the package would break exsisting 
348
        package on the system, e.g. system has:
349
        smc depends on smc-data (= 1.4)
350
        and user tries to installs smc-data 1.6
351
        """
352
        # show progress information as this step may take some time
353
        size = float(len(self._cache))
175.1.215 by Michael Vogt
tests/test_debfile.py: add fixture dpkg-status file to make tests work
354
        steps = max(int(size/50), 1)
415.2.1 by Kiwinote
Merge gdebi changes
355
        debver = self._sections["Version"]
549.1.27 by Michael Vogt
handle architecture-specific conflicts correctly (LP: #829138)
356
        debarch = self._sections["Architecture"]
175.1.212 by Michael Vogt
* apt/debfile.py:
357
        # store what we provide so that we can later check against that
358
        provides = [ x[0][0] for x in self.provides]
415.2.1 by Kiwinote
Merge gdebi changes
359
        for (i, pkg) in enumerate(self._cache):
360
            if i%steps == 0:
361
                self._cache.op_progress.update(float(i)/size*100.0)
362
            if not pkg.is_installed:
363
                continue
364
            # check if the exising dependencies are still satisfied
365
            # with the package
366
            ver = pkg._pkg.current_ver
367
            for dep_or in pkg.installed.dependencies:
368
                for dep in dep_or.or_dependencies:
369
                    if dep.name == self.pkgname:
370
                        if not apt_pkg.check_dep(debver, dep.relation, dep.version):
371
                            self._dbg(2, "would break (depends) %s" % pkg.name)
372
                            # TRANSLATORS: the first '%s' is the package that breaks, the second the dependency that makes it break, the third the relation (e.g. >=) and the latest the version for the releation
373
                            self._failure_string += _("Breaks existing package '%(pkgname)s' dependency %(depname)s (%(deprelation)s %(depversion)s)") % {
374
                                'pkgname' : pkg.name, 
375
                                'depname' : dep.name, 
376
                                'deprelation' : dep.relation, 
377
                                'depversion' : dep.version}
378
                            self._cache.op_progress.done()
379
                            return False
380
            # now check if there are conflicts against this package on
381
            # the existing system
382
            if "Conflicts" in ver.depends_list:
383
                for conflicts_ver_list in ver.depends_list["Conflicts"]:
384
                    for c_or in conflicts_ver_list:
549.1.27 by Michael Vogt
handle architecture-specific conflicts correctly (LP: #829138)
385
                        if c_or.target_pkg.name == self.pkgname and c_or.target_pkg.architecture == debarch:
415.2.1 by Kiwinote
Merge gdebi changes
386
                            if apt_pkg.check_dep(debver, c_or.comp_type, c_or.target_ver):
387
                                self._dbg(2, "would break (conflicts) %s" % pkg.name)
388
				# TRANSLATORS: the first '%s' is the package that conflicts, the second the packagename that it conflicts with (so the name of the deb the user tries to install), the third is the relation (e.g. >=) and the last is the version for the relation
175.1.212 by Michael Vogt
* apt/debfile.py:
389
                                self._failure_string += _("Breaks existing package '%(pkgname)s' conflict: %(targetpkg)s (%(comptype)s %(targetver)s)") % {
415.2.1 by Kiwinote
Merge gdebi changes
390
                                    'pkgname' : pkg.name, 
391
                                    'targetpkg' : c_or.target_pkg.name, 
392
                                    'comptype' : c_or.comp_type, 
393
                                    'targetver' : c_or.target_ver }
394
                                self._cache.op_progress.done()
395
                                return False
175.1.236 by Michael Vogt
merged from lp:~kiwinote/python-apt/reinstall-same-file
396
                        if (c_or.target_pkg.name in provides and
397
                            self.pkgname != pkg.name):
175.1.212 by Michael Vogt
* apt/debfile.py:
398
                            self._dbg(2, "would break (conflicts) %s" % provides)
399
                            self._failure_string += _("Breaks existing package '%(pkgname)s' that conflict: '%(targetpkg)s'. But the '%(debfile)s' provides it via: '%(provides)s'") % {
400
                                'provides' : ",".join(provides),
401
                                'debfile'  : self.filename,
402
                                'targetpkg' : c_or.target_pkg.name,
403
                                'pkgname' : pkg.name }
404
                            self._cache.op_progress.done()
405
                            return False
415.2.1 by Kiwinote
Merge gdebi changes
406
        self._cache.op_progress.done()
407
        return True
408
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
409
    def compare_to_version_in_cache(self, use_installed=True):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
410
        """Compare the package to the version available in the cache.
411
412
        Checks if the package is already installed or availabe in the cache
413
        and if so in what version, returns one of (VERSION_NONE,
414
        VERSION_OUTDATED, VERSION_SAME, VERSION_NEWER).
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
415
        """
415.2.1 by Kiwinote
Merge gdebi changes
416
        self._dbg(3, "compare_to_version_in_cache")
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
417
        pkgname = self._sections["Package"]
418
        debver = self._sections["Version"]
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
419
        self._dbg(1, "debver: %s" % debver)
420
        if pkgname in self._cache:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
421
            if use_installed and self._cache[pkgname].installed:
422
                cachever = self._cache[pkgname].installed.version
175.1.211 by Michael Vogt
apt/debfile.py: fix bug in compare_to_version_in_cache with use_installed=True
423
            elif not use_installed and self._cache[pkgname].candidate:
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
424
                cachever = self._cache[pkgname].candidate.version
415.2.5 by Kiwinote
Don't query cache[].candidate.version when no cache[].candidate is available
425
            else:
426
                return self.VERSION_NONE
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
427
            if cachever is not None:
415.2.1 by Kiwinote
Merge gdebi changes
428
                cmp = apt_pkg.version_compare(cachever, debver)
429
                self._dbg(1, "CompareVersion(debver,instver): %s" % cmp)
430
                if cmp == 0:
431
                    return self.VERSION_SAME
432
                elif cmp < 0:
433
                    return self.VERSION_NEWER
434
                elif cmp > 0:
435
                    return self.VERSION_OUTDATED
436
        return self.VERSION_NONE
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
437
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
438
    def check(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
439
        """Check if the package is installable."""
175.1.219 by Michael Vogt
apt/debfile.py: improve logging
440
        self._dbg(3, "check")
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
441
563 by Michael Vogt
cleanup based on feedback from juliank
442
        self._check_was_run = True
443
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
444
        # check arch
415.2.1 by Kiwinote
Merge gdebi changes
445
        if not "Architecture" in self._sections:
446
            self._dbg(1, "ERROR: no architecture field")
447
            self._failure_string = _("No Architecture field in the package")
448
            return False
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
449
        arch = self._sections["Architecture"]
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
450
        if  arch != "all" and arch != apt_pkg.config.find("APT::Architecture"):
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
451
            if arch in apt_pkg.get_architectures():
452
                self._multiarch = arch
453
                self.pkgname = "%s:%s" % (self.pkgname, self._multiarch)
454
                self._dbg(1, "Found multiarch arch: '%s'" % arch)
455
            else:
456
                self._dbg(1, "ERROR: Wrong architecture dude!")
457
                self._failure_string = _("Wrong architecture '%s'") % arch
458
                return False
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
459
460
        # check version
415.2.1 by Kiwinote
Merge gdebi changes
461
        if self.compare_to_version_in_cache() == self.VERSION_OUTDATED:
462
            if self._cache[self.pkgname].installed:
463
                # the deb is older than the installed
464
                self._failure_string = _("A later version is already installed")
465
                return False
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
466
467
        # FIXME: this sort of error handling sux
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
468
        self._failure_string = ""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
469
470
        # check conflicts
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
471
        if not self.check_conflicts():
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
472
            return False
473
415.2.1 by Kiwinote
Merge gdebi changes
474
        # check if installing it would break anything on the 
475
        # current system
476
        if not self.check_breaks_existing_packages():
477
            return False
478
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
479
        # try to satisfy the dependencies
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
480
        if not self._satisfy_depends(self.depends):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
481
            return False
482
483
        # check for conflicts again (this time with the packages that are
484
        # makeed for install)
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
485
        if not self.check_conflicts():
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
486
            return False
487
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
488
        if self._cache._depcache.broken_count > 0:
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
489
            self._failure_string = _("Failed to satisfy all dependencies "
490
                                     "(broken cache)")
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
491
            # clean the cache again
492
            self._cache.clear()
493
            return False
494
        return True
495
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
496
    def satisfy_depends_str(self, dependsstr):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
497
        """Satisfy the dependencies in the given string."""
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
498
        return self._satisfy_depends(apt_pkg.parse_depends(dependsstr))
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
499
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
500
    def _satisfy_depends(self, depends):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
501
        """Satisfy the dependencies."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
502
        # turn off MarkAndSweep via a action group (if available)
503
        try:
217.3.13 by Julian Andres Klode
* Update the code to use the new classes.
504
            _actiongroup = apt_pkg.ActionGroup(self._cache._depcache)
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
505
        except AttributeError:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
506
            pass
507
        # check depends
508
        for or_group in depends:
509
            #print "or_group: %s" % or_group
415.2.1 by Kiwinote
Merge gdebi changes
510
            #print "or_group satified: %s" % self._is_or_group_satisfied(or_group)
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
511
            if not self._is_or_group_satisfied(or_group):
512
                if not self._satisfy_or_group(or_group):
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
513
                    return False
514
        # now try it out in the cache
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
515
        for pkg in self._need_pkgs:
516
            try:
517
                self._cache[pkg].mark_install(from_user=False)
549.1.34 by Michael Vogt
pyflakes cleanup, use apt_pkg.gettext in aptsources too
518
            except SystemError:
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
519
                self._failure_string = _("Cannot install '%s'") % pkg
520
                self._cache.clear()
521
                return False
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
522
        return True
523
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
524
    @property
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
525
    def missing_deps(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
526
        """Return missing dependencies."""
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
527
        self._dbg(1, "Installing: %s" % self._need_pkgs)
563 by Michael Vogt
cleanup based on feedback from juliank
528
        if not self._check_was_run:
564 by Michael Vogt
cleanup
529
            raise AttributeError("property only available after check() was run")
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
530
        return self._need_pkgs
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
531
202.1.2 by Julian Andres Klode
* apt/debfile.py, apt/package.py: Use @property
532
    @property
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
533
    def required_changes(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
534
        """Get the changes required to satisfy the dependencies.
535
536
        Returns: a tuple with (install, remove, unauthenticated)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
537
        """
538
        install = []
539
        remove = []
540
        unauthenticated = []
563 by Michael Vogt
cleanup based on feedback from juliank
541
        if not self._check_was_run:
564 by Michael Vogt
cleanup
542
            raise AttributeError("property only available after check() was run")
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
543
        for pkg in self._cache:
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
544
            if pkg.marked_install or pkg.marked_upgrade:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
545
                install.append(pkg.name)
546
                # check authentication, one authenticated origin is enough
547
                # libapt will skip non-authenticated origins then
548
                authenticated = False
204.1.7 by Julian Andres Klode
* apt/: Adjust modules to use Package.{installed,candidate}.*
549
                for origin in pkg.candidate.origins:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
550
                    authenticated |= origin.trusted
551
                if not authenticated:
552
                    unauthenticated.append(pkg.name)
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
553
            if pkg.marked_delete:
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
554
                remove.append(pkg.name)
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
555
        return (install, remove, unauthenticated)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
556
421 by Michael Vogt
apt/debfile.py: make to_{hex,strish} staticmethods
557
    @staticmethod
175.1.230 by Michael Vogt
* apt/debfile.py:
558
    def to_hex(in_data):
415.2.1 by Kiwinote
Merge gdebi changes
559
        hex = ""
560
        for (i, c) in enumerate(in_data):
561
            if i%80 == 0:
562
                hex += "\n"
563
            hex += "%2.2x " % ord(c)
564
        return hex
565
421 by Michael Vogt
apt/debfile.py: make to_{hex,strish} staticmethods
566
    @staticmethod
175.1.230 by Michael Vogt
* apt/debfile.py:
567
    def to_strish(in_data):
415.2.1 by Kiwinote
Merge gdebi changes
568
        s = ""
549.1.25 by Michael Vogt
apt/debfile.py: add py3 compat for to_strish()
569
        # py2 compat, in_data is type string
570
        if type(in_data) == str:
571
            for c in in_data:
572
                if ord(c) < 10 or ord(c) > 127:
573
                    s += " "
574
                else:
575
                    s += c
576
        # py3 compat, in_data is type bytes
577
        else:
578
            for b in in_data:
579
                if b < 10 or b > 127:
580
                    s += " "
581
                else:
582
                    s += chr(b)
415.2.1 by Kiwinote
Merge gdebi changes
583
        return s
584
        
585
    def _get_content(self, part, name, auto_decompress=True, auto_hex=True):
175.1.258 by Michael Vogt
* apt/debfile.py, tests/test_debfile.py:
586
        if name.startswith("./"):
587
            name = name[2:]
464 by Julian Andres Klode
* apt/debfile.py:
588
        data = part.extractdata(name)
415.2.1 by Kiwinote
Merge gdebi changes
589
        # check for zip content
590
        if name.endswith(".gz") and auto_decompress:
591
            io = StringIO(data)
592
            gz = gzip.GzipFile(fileobj=io)
593
            data = _("Automatically decompressed:\n\n")
594
            data += gz.read()
595
        # auto-convert to hex
596
        try:
597
            data = unicode(data, "utf-8")
549.1.34 by Michael Vogt
pyflakes cleanup, use apt_pkg.gettext in aptsources too
598
        except Exception:
415.2.1 by Kiwinote
Merge gdebi changes
599
            new_data = _("Automatically converted to printable ascii:\n")
600
            new_data += self.to_strish(data)
601
            return new_data
602
        return data
603
604
    def control_content(self, name):
605
        """ return the content of a specific control.tar.gz file """
415.2.4 by Kiwinote
Don't depend on python-debian
606
        try:
464 by Julian Andres Klode
* apt/debfile.py:
607
            return self._get_content(self._debfile.control, name)
608
        except LookupError:
609
            return ""
415.2.1 by Kiwinote
Merge gdebi changes
610
611
    def data_content(self, name):
612
        """ return the content of a specific control.tar.gz file """
415.2.4 by Kiwinote
Don't depend on python-debian
613
        try:
464 by Julian Andres Klode
* apt/debfile.py:
614
            return self._get_content(self._debfile.data, name)
615
        except LookupError:
616
            return ""
415.2.1 by Kiwinote
Merge gdebi changes
617
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
618
    def _dbg(self, level, msg):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
619
        """Write debugging output to sys.stderr."""
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
620
        if level <= self.debug:
621
            print >> sys.stderr, msg
622
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
623
    def install(self, install_progress=None):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
624
        """Install the package."""
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
625
        if install_progress is None:
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
626
            return os.spawnlp(os.P_WAIT, "dpkg", "dpkg", "-i", self.filename)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
627
        else:
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
628
            try:
629
                install_progress.start_update()
630
            except AttributeError:
631
                install_progress.startUpdate()
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
632
            res = install_progress.run(self.filename)
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
633
            try:
634
                install_progress.finish_update()
635
            except AttributeError:
636
                install_progress.finishUpdate()
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
637
            return res
638
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
639
class DscSrcPackage(DebPackage):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
640
    """A locally available source package."""
641
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
642
    def __init__(self, filename=None, cache=None):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
643
        DebPackage.__init__(self, None, cache)
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
644
        self.filename = filename
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
645
        self._depends = []
646
        self._conflicts = []
175.10.195 by Michael Vogt
* apt/debfile.py:
647
        self._installed_conflicts = set()
357 by Julian Andres Klode
Some stylistic changes.
648
        self.pkgname = ""
649
        self.binaries = []
175.1.209 by Michael Vogt
merged lp:~kiwinote/python-apt/merge-gdebi-changes, this port the
650
        if self.filename is not None:
651
            self.open(self.filename)
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
652
653
    @property
654
    def depends(self):
655
        """Return the dependencies of the package"""
656
        return self._depends
657
658
    @property
659
    def conflicts(self):
660
        """Return the dependencies of the package"""
661
        return self._conflicts
662
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
663
    def open(self, file):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
664
        """Open the package."""
665
        depends_tags = ["Build-Depends", "Build-Depends-Indep"]
666
        conflicts_tags = ["Build-Conflicts", "Build-Conflicts-Indep"]
667
        fobj = open(file)
217.3.13 by Julian Andres Klode
* Update the code to use the new classes.
668
        tagfile = apt_pkg.TagFile(fobj)
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
669
        try:
338 by Julian Andres Klode
python/tagfile.cc: Implement the iterator protocol in TagFile.
670
            for sec in tagfile:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
671
                for tag in depends_tags:
217.2.2 by Julian Andres Klode
* python/tag.cc: Support 'key in mapping' for TagSections
672
                    if not tag in sec:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
673
                        continue
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
674
                    self._depends.extend(apt_pkg.parse_src_depends(sec[tag]))
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
675
                for tag in conflicts_tags:
217.2.2 by Julian Andres Klode
* python/tag.cc: Support 'key in mapping' for TagSections
676
                    if not tag in sec:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
677
                        continue
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
678
                    self._conflicts.extend(apt_pkg.parse_src_depends(sec[tag]))
217.2.2 by Julian Andres Klode
* python/tag.cc: Support 'key in mapping' for TagSections
679
                if 'Source' in sec:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
680
                    self.pkgname = sec['Source']
217.2.2 by Julian Andres Klode
* python/tag.cc: Support 'key in mapping' for TagSections
681
                if 'Binary' in sec:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
682
                    self.binaries = sec['Binary'].split(', ')
217.2.2 by Julian Andres Klode
* python/tag.cc: Support 'key in mapping' for TagSections
683
                if 'Version' in sec:
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
684
                    self._sections['Version'] = sec['Version']
685
        finally:
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
686
            del tagfile
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
687
            fobj.close()
688
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
689
        s = _("Install Build-Dependencies for "
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
690
              "source package '%s' that builds %s\n") % (self.pkgname,
691
              " ".join(self.binaries))
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
692
        self._sections["Description"] = s
563 by Michael Vogt
cleanup based on feedback from juliank
693
        self._check_was_run = False
694
        
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
695
    def check(self):
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
696
        """Check if the package is installable.."""
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
697
        if not self.check_conflicts():
698
            for pkgname in self._installed_conflicts:
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
699
                if self._cache[pkgname]._pkg.essential:
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
700
                    raise Exception(_("An essential package would be removed"))
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
701
                self._cache[pkgname].mark_delete()
563 by Michael Vogt
cleanup based on feedback from juliank
702
        # properties are ok now
703
        self._check_was_run = True
415.2.1 by Kiwinote
Merge gdebi changes
704
        # FIXME: a additional run of the check_conflicts()
705
        #        after _satisfy_depends() should probably be done
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
706
        return self._satisfy_depends(self.depends)
175.1.65 by Michael Vogt
* apt/debfile.py:
707
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
708
def _test():
709
    """Test function"""
710
    from apt.cache import Cache
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
711
    from apt.progress.base import InstallProgress
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
712
713
    cache = Cache()
714
715
    vp = "www-browser"
415.2.3 by Kiwinote
Merge cache.get_providers_for() into cache.get_providing_packages() and update debfile.py to use this
716
    print "%s virtual: %s" % (vp, cache.is_virtual_package(vp))
217.3.2 by Julian Andres Klode
* apt/*.py: Initial rename work for Bug#481061
717
    providers = cache.get_providing_packages(vp)
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
718
    print "Providers for %s :" % vp
719
    for pkg in providers:
720
        print " %s" % pkg.name
202.1.1 by Julian Andres Klode
Cleanup: Remove whitespace at the end of line in all python codes.
721
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
722
    d = DebPackage(sys.argv[1], cache)
723
    print "Deb: %s" % d.pkgname
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
724
    if not d.check():
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
725
        print "can't be satified"
202.1.33 by Julian Andres Klode
* apt/debfile.py: Do not use mixedCase anymore
726
        print d._failure_string
727
    print "missing deps: %s" % d.missing_deps
728
    print d.required_changes
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
729
415.2.1 by Kiwinote
Merge gdebi changes
730
    print d.filelist
731
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
732
    print "Installing ..."
549.2.1 by Michael Vogt
first cut of multiarch support, tests still fail though
733
    ret = d.install(InstallProgress())
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
734
    print ret
735
736
    #s = DscSrcPackage(cache, "../tests/3ddesktop_0.2.9-6.dsc")
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
737
    #s.check_dep()
175.20.5 by Sebastian Heinlein
Merge the Debfile class from dpkg into the one from the debfile package.
738
    #print "Missing deps: ",s.missingDeps
739
    #print "Print required changes: ", s.requiredChanges
740
741
    s = DscSrcPackage(cache=cache)
742
    d = "libc6 (>= 2.3.2), libaio (>= 0.3.96) | libaio1 (>= 0.3.96)"
217.3.32 by Julian Andres Klode
apt, aptsources, doc: Update to use the new names.
743
    print s._satisfy_depends(apt_pkg.parse_depends(d))
202.1.7 by Julian Andres Klode
* apt/debfile: Fix and cleanup
744
745
if __name__ == "__main__":
746
    _test()