~mvo/software-center/size-calculation-via-aptdaemon-5.2

1
2
3
4
5
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
# Copyright (C) 2009 Canonical
#
# Authors:
#  Michael Vogt
#
# Parts taken from gnome-app-install:utils.py (also written by Michael Vogt)
#
# 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; version 3.
#
# 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.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA

import apt
import apt_pkg
import logging
import os

from gi.repository import GObject
from gi.repository import Gio

# we need this to get the size calculation done asyncronously and reliable
from aptdaemon.client import AptClient
from defer import inline_callbacks, return_value

from softwarecenter.db.pkginfo import PackageInfo, _Version
from softwarecenter.enums import PkgStates

LOG = logging.getLogger(__name__)



class GtkMainIterationProgress(apt.progress.base.OpProgress):
    """Progress that just runs the main loop"""
    def update(self, percent=0):
        context = GObject.main_context_default()
        while context.pending():
            context.iteration()


def convert_package_argument(f):
    """ decorator converting _Package argument to Package object from cache """
    def _converted(self, pkg, *args):
        try:
            if type(pkg) is not apt_pkg.Package:
                if type(pkg) is str:
                    pkg = self._cache[pkg]
                else:
                    pkg = self._cache[pkg.name]
        except Exception as e:
            logging.exception(e)
            pkg = None
        return f(self, pkg, *args)
    return _converted


def pkg_downloaded(pkg_version):
    filename = os.path.basename(pkg_version.filename)
    # FIXME: use relative path here
    return os.path.exists("/var/cache/apt/archives/" + filename)


class AptCacheVersion(_Version):
    def __init__(self, version):
        self.ver = version

    @property
    def description(self):
        return self.ver.description

    @property
    def summary(self):
        return self.ver.summary

    @property
    def size(self):
        return self.ver.size

    @property
    def installed_size(self):
        return self.ver.installed_size

    @property
    def version(self):
        return self.ver.version

    @property
    def origins(self):
        return self.ver.origins

    @property
    def downloadable(self):
        return self.ver.downloadable

    @property
    def not_automatic(self):
        priority = self.ver.policy_priority
        if priority <= 100 and self.ver.downloadable:
            return True
        return False


class AptCache(PackageInfo):
    """
    A apt cache that opens in the background and keeps the UI alive
    """

    # dependency types we are about
    DEPENDENCY_TYPES = ("PreDepends", "Depends")
    RECOMMENDS_TYPES = ("Recommends",)
    SUGGESTS_TYPES = ("Suggests",)
    ENHANCES_TYPES = ("Enhances",)
    PROVIDES_TYPES = ("Provides",)

    # stamp file to monitor (provided by update-notifier via
    # APT::Update::Post-Invoke-Success)
    APT_FINISHED_STAMP = "/var/lib/update-notifier/dpkg-run-stamp"

    LANGPACK_PKGDEPENDS = "/usr/share/language-selector/data/pkg_depends"

    def __init__(self):
        PackageInfo.__init__(self)
        self._cache = None
        self._ready = False
        self._timeout_id = None
        # setup monitor watch for install/remove changes
        self.apt_finished_stamp = Gio.File.new_for_path(
            self.APT_FINISHED_STAMP)
        self.apt_finished_monitor = self.apt_finished_stamp.monitor_file(0,
            None)
        self.apt_finished_monitor.connect(
            "changed", self._on_apt_finished_stamp_changed)
        # this is fast, so ok
        self._language_packages = self._read_language_pkgs()
        # query the totalize on install using aptdaemon
        self.aptd_client = AptClient()

    @staticmethod
    def version_compare(a, b):
        return apt_pkg.version_compare(a, b)

    @staticmethod
    def upstream_version_compare(a, b):
        return apt_pkg.version_compare(apt_pkg.upstream_version(a),
                                       apt_pkg.upstream_version(b))

    @staticmethod
    def upstream_version(v):
        return apt_pkg.upstream_version(v)

    def is_installed(self, pkgname):
        # use the lowlevel cache here, twice as fast
        lowlevel_cache = self._cache._cache
        return (pkgname in lowlevel_cache and
                lowlevel_cache[pkgname].current_ver is not None)

    def is_upgradable(self, pkgname):
        # use the lowlevel cache here, twice as fast
        if not pkgname in self._cache:
            return False
        return self._cache[pkgname].is_upgradable

    def is_available(self, pkgname):
        return (pkgname in self._cache and
                self._cache[pkgname].candidate)

    def get_installed(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].is_installed):
            return None
        return AptCacheVersion(self._cache[pkgname].installed)

    def get_candidate(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return None
        return AptCacheVersion(self._cache[pkgname].candidate)

    def get_versions(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return []
        return [AptCacheVersion(v) for v in self._cache[pkgname].versions]

    def get_section(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return ''
        return self._cache[pkgname].candidate.section

    def get_summary(self, pkgname):
        if (pkgname not in self._cache or
        not self._cache[pkgname].candidate):
            return ''
        return self._cache[pkgname].candidate.summary

    def get_description(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return ''
        return self._cache[pkgname].candidate.description

    def get_website(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return ''
        return self._cache[pkgname].candidate.homepage

    def get_installed_files(self, pkgname):
        if (pkgname not in self._cache):
            return []
        return self._cache[pkgname].installed_files

    def get_size(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return 0
        return self._cache[pkgname].candidate.size

    def get_installed_size(self, pkgname):
        if (pkgname not in self._cache or
            not self._cache[pkgname].candidate):
            return 0
        return self._cache[pkgname].candidate.installed_size

    @property
    def ready(self):
        return self._ready

    def get_license(self, name):
        return None

    def open(self):
        """ (re)open the cache, this sends cache-invalid, cache-ready signals
        """
        LOG.info("aptcache.open()")
        self._ready = False
        self.emit("cache-invalid")
        from softwarecenter.utils import ExecutionTime
        with ExecutionTime("open the apt cache (in event loop)"):
            if self._cache == None:
                self._cache = apt.Cache(GtkMainIterationProgress())
            else:
                self._cache.open(GtkMainIterationProgress())
        self._ready = True
        self.emit("cache-ready")
        if self._cache.broken_count > 0:
            self.emit("cache-broken")

    # implementation specific code

    # temporarely return a full apt.Package so that the tests and the
    # code keeps working for now, this needs to go away eventually
    # and get replaced with the abstract _Package class
    #def __getitem__(self, key):
    #    return self._cache[key]

    def __iter__(self):
        return self._cache.__iter__()

    def __contains__(self, k):
        return self._cache.__contains__(k)

    def _on_apt_finished_stamp_changed(self, monitor, afile, other_file,
        event):
        if not event == Gio.FileMonitorEvent.CHANGES_DONE_HINT:
            return
        if self._timeout_id:
            GObject.source_remove(self._timeout_id)
            self._timeout_id = None
        self._timeout_id = GObject.timeout_add_seconds(10, self.open)

    def _get_rdepends_by_type(self, pkg, type, onlyInstalled):
        rdeps = set()
        # make sure this is a apt.Package object
        try:
            pkg = self._cache[pkg.name]
        except KeyError:
            LOG.error("package %s not found in AptCache" % str(pkg))
            return rdeps
        for rdep in pkg._pkg.rev_depends_list:
            dep_type = rdep.dep_type_untranslated
            if dep_type in type:
                rdep_name = rdep.parent_pkg.name
                if rdep_name in self._cache and (not onlyInstalled or
                (onlyInstalled and self._cache[rdep_name].is_installed)):
                    rdeps.add(rdep.parent_pkg.name)
        return rdeps

    def _installed_dependencies(self, pkg_name, all_deps=None):
        """ recursively return all installed dependencies of a given pkg """
        #print "_installed_dependencies", pkg_name, all_deps
        if not all_deps:
            all_deps = set()
        if pkg_name not in self._cache:
            return all_deps
        cur = self._cache[pkg_name]._pkg.current_ver
        if not cur:
            return all_deps
        for t in self.DEPENDENCY_TYPES + self.RECOMMENDS_TYPES:
            try:
                for dep in cur.depends_list[t]:
                    dep_name = dep[0].target_pkg.name
                    if not dep_name in all_deps:
                        all_deps.add(dep_name)
                        all_deps |= self._installed_dependencies(dep_name,
                            all_deps)
            except KeyError:
                pass
        return all_deps

    def get_installed_automatic_depends_for_pkg(self, pkg):
        """ Get the installed automatic dependencies for this given package
            only.

            Note that the package must be marked for removal already for
            this to work
            Not: unused
        """
        installed_auto_deps = set()
        deps = self._installed_dependencies(pkg.name)
        for dep_name in deps:
            try:
                pkg = self._cache[dep_name]
            except KeyError:
                continue
            else:
                if (pkg.is_installed and
                    pkg.is_auto_removable):
                    installed_auto_deps.add(dep_name)
        return installed_auto_deps

    def get_all_origins(self):
        """
        return a set of the current channel origins from the apt.Cache itself
        """
        origins = set()
        for pkg in self._cache:
            if not pkg.candidate:
                continue
            for item in pkg.candidate.origins:
                context = GObject.main_context_default()
                while context.pending():
                    context.iteration()
                if item.origin:
                    origins.add(item.origin)
        return origins

    def get_origins(self, pkgname):
        """
        return package origins from apt.Cache
        """
        if not pkgname in self._cache or not self._cache[pkgname].candidate:
            return
        origins = set()
        for origin in self._cache[pkgname].candidate.origins:
            if origin.origin:
                origins.add(origin)
        return origins

    def get_origin(self, pkgname):
        """
        return a uniqe origin for the given package name. currently
        this will use
        """
        if not pkgname in self._cache or not self._cache[pkgname].candidate:
            return None
        origins = set([origin.origin for origin in self.get_origins(pkgname)])
        if len(origins) > 1:
            LOG.warn("more than one origin '%s'" % origins)
            return None
        if not origins:
            return None
        # we support only a single origin (but its fine if that is available
        # on multiple mirrors). lowercase as the server excepts it this way
        origin_str = origins.pop()
        return origin_str.lower()

    def component_available(self, distro_codename, component):
        """ check if the given component is enabled """
        # FIXME: test for more properties here?
        for it in self._cache._cache.file_list:
            if (it.component != "" and
                it.component == component and
                it.archive != "" and
                it.archive == distro_codename):
                return True
        return False

    @convert_package_argument
    def _get_depends_by_type(self, pkg, types):
        version = pkg.installed
        if version == None:
            version = pkg.candidate
        return version.get_dependencies(*types)

    def _get_depends_by_type_str(self, pkg, *types):
        def not_in_list(list, item):
            for i in list:
                if i == item:
                    return False
            return True
        deps = self._get_depends_by_type(pkg, *types)
        deps_str = []
        for dep in deps:
            for dep_ in dep.or_dependencies:
                if not_in_list(deps_str, dep_.name):
                    deps_str.append(dep_.name)
        return deps_str

    # FIXME: there are cleaner ways to do this than below

    # pkg relations
    def _get_depends(self, pkg):
        return self._get_depends_by_type_str(pkg, self.DEPENDENCY_TYPES)

    def _get_recommends(self, pkg):
        return self._get_depends_by_type_str(pkg, self.RECOMMENDS_TYPES)

    def _get_suggests(self, pkg):
        return self._get_depends_by_type_str(pkg, self.SUGGESTS_TYPES)

    def _get_enhances(self, pkg):
        return self._get_depends_by_type_str(pkg, self.ENHANCES_TYPES)

    @convert_package_argument
    def _get_provides(self, pkg):
        # note: can use ._cand, because pkg has been converted to apt.Package
        provides_list = pkg.candidate._cand.provides_list
        provides = []
        for provided in provides_list:
            provides.append(provided[0])  # the package name
        return provides

    # reverse pkg relations
    def _get_rdepends(self, pkg):
        return self._get_rdepends_by_type(pkg, self.DEPENDENCY_TYPES, False)

    def _get_rrecommends(self, pkg):
        return self._get_rdepends_by_type(pkg, self.RECOMMENDS_TYPES, False)

    def _get_rsuggests(self, pkg):
        return self._get_rdepends_by_type(pkg, self.SUGGESTS_TYPES, False)

    def _get_renhances(self, pkg):
        return self._get_rdepends_by_type(pkg, self.ENHANCES_TYPES, False)

    @convert_package_argument
    def _get_renhances_lowlevel_apt_pkg(self, pkg):
        """ takes a apt_pkg.Package and returns a list of pkgnames that
            enhance this package - this is needed to support enhances
            for virtual packages
        """
        renhances = []
        for dep in pkg.rev_depends_list:
            if dep.dep_type_untranslated == "Enhances":
                renhances.append(dep.parent_pkg.name)
        return renhances

    def _get_rprovides(self, pkg):
        return self._get_rdepends_by_type(pkg, self.PROVIDES_TYPES, False)

    # installed reverse pkg relations
    def get_packages_removed_on_remove(self, pkg):
        return self._get_rdepends_by_type(pkg, self.DEPENDENCY_TYPES, True)

    def get_packages_removed_on_install(self, pkg):
        depends = set()
        deps_remove = self._try_install_and_get_all_deps_removed(pkg)
        for depname in deps_remove:
            if self._cache[depname].is_installed:
                depends.add(depname)
        return depends

    def _get_installed_rrecommends(self, pkg):
        return self._get_rdepends_by_type(pkg, self.RECOMMENDS_TYPES, True)

    def _get_installed_rsuggests(self, pkg):
        return self._get_rdepends_by_type(pkg, self.SUGGESTS_TYPES, True)

    def _get_installed_renhances(self, pkg):
        return self._get_rdepends_by_type(pkg, self.ENHANCES_TYPES, True)

    def _get_installed_rprovides(self, pkg):
        return self._get_rdepends_by_type(pkg, self.PROVIDES_TYPES, True)

    # language pack stuff
    def _is_language_pkg(self, addon):
        # a simple "addon in self._language_packages" is not enough
        for template in self._language_packages:
            if addon.startswith(template):
                return True
        return False

    def _read_language_pkgs(self):
        language_packages = set()
        if not os.path.exists(self.LANGPACK_PKGDEPENDS):
            return language_packages
        for line in open(self.LANGPACK_PKGDEPENDS):
            line = line.strip()
            if line.startswith('#'):
                continue
            try:
                (cat, code, dep_pkg, language_pkg) = line.split(':')
            except ValueError:
                continue
            language_packages.add(language_pkg)
        return language_packages

    # these are used for calculating the total size
    @convert_package_argument
    def _get_changes_without_applying(self, pkg):
        try:
            if pkg.installed == None:
                pkg.mark_install()
            else:
                pkg.mark_delete()
        except SystemError:
            # TODO: ideally we now want to display an error message
            #       and block the install button
            LOG.warning("broken packages encountered while getting deps for %s"
                      % pkg.name)
            return {}
        changes_tmp = self._cache.get_changes()
        changes = {}
        for change in changes_tmp:
            if change.marked_install or change.marked_reinstall:
                changes[change.name] = PkgStates.INSTALLING
            elif change.marked_delete:
                changes[change.name] = PkgStates.REMOVING
            elif change.marked_upgrade:
                changes[change.name] = PkgStates.UPGRADING
            else:
                changes[change.name] = PkgStates.UNKNOWN
        self._cache.clear()
        return changes

    def _try_install_and_get_all_deps_installed(self, pkg):
        """ Return all dependencies of pkg that will be marked for install """
        changes = self._get_changes_without_applying(pkg)
        installing_deps = []
        for change in changes.keys():
            if change != pkg.name and changes[change] == PkgStates.INSTALLING:
                installing_deps.append(change)
        return installing_deps

    def _try_install_and_get_all_deps_removed(self, pkg):
        """ Return all dependencies of pkg that will be marked for remove"""
        changes = self._get_changes_without_applying(pkg)
        removing_deps = []
        for change in changes.keys():
            if change != pkg.name and changes[change] == PkgStates.REMOVING:
                removing_deps.append(change)
        return removing_deps

    def _set_candidate_release(self, pkg, archive_suite):
        # Check if the package is provided in the release
        for version in pkg.versions:
            if [origin for origin in version.origins
                if origin.archive == archive_suite]:
                break
        else:
            return False
        res = pkg._pcache._depcache.set_candidate_release(
            pkg._pkg, version._cand, archive_suite)
        return res

    def _on_total_size_calculation_done(self, trans, space):
        self.emit(
            "query-total-size-on-install-done", trans.download, trans.space)

    def _on_trans_simulate_error(self, error):
        LOG.exception("simulate failed")

    def _on_trans_commit_packages_ready(self, trans):
        trans.connect("space-changed", self._on_total_size_calculation_done)
        try:
            trans.simulate(reply_handler=lambda: True,
                           error_handler=self._on_trans_simulate_error)
        except:
            LOG.exception("simulate failed for '%s'" % pkgname)

    def query_total_size_on_install(self, pkgname,
                                    addons_install=[], addons_remove=[],
                                    archive_suite=""):
        if not pkgname in self._cache:
            self.emit("query-total-size-on-install-done", (0, 0))

        # ensure the syntax is right
        if archive_suite:
            pkgname = pkgname + "/" + archive_suite

        # and simulate the install/remove via aptdaemon
        install = [pkgname] + addons_install
        remove = addons_remove
        reinstall = purge = upgrade = downgrade = []

        # do this async
        try:
            self.aptd_client.commit_packages(
                install, reinstall, remove, purge, upgrade, downgrade,
                # wait
                False,
                # reply and error handlers
                self._on_trans_commit_packages_ready,
                self._on_trans_simulate_error)
        except:
            LOG.exception(
                "getting commit_packages trans failed for '%s'" % pkgname)

    def get_all_deps_upgrading(self, pkg):
        # note: this seems not to be used anywhere
        changes = self._get_changes_without_applying(pkg)
        upgrading_deps = []
        for change in changes.keys():
            if change != pkg.name and changes[change] == PkgStates.UPGRADING:
                upgrading_deps.append(change)
        return upgrading_deps

    # determine the addons for a given package
    def get_addons(self, pkgname, ignore_installed=True):
        """ get the list of addons for the given pkgname

            The optional parameter "ignore_installed" controls if the output
            should be filtered and pkgs already installed should be ignored
            in the output (e.g. useful for testing).

            :return: a tuple of pkgnames (recommends, suggests)
        """
        logging.debug("get_addons for '%s'" % pkgname)

        def _addons_filter(addon):
            """ helper for get_addons that filters out unneeded ones """
            # we don't know about this one (prefectly legal for suggests)
            if not addon in self._cache:
                LOG.debug("not in cache %s" % addon)
                return False
            # can happen via "lonley" depends
            if addon == pkg.name:
                LOG.debug("circular %s" % addon)
                return False
            # child pkg is addon of parent pkg, not the other way around.
            if addon == '-'.join(pkgname.split('-')[:-1]):
                LOG.debug("child > parent %s" % addon)
                return False
            # get the pkg
            addon_pkg = self._cache[addon]
            # we don't care for essential or important (or refrences
            # to ourself)
            if (addon_pkg.essential or
                addon_pkg._pkg.important):
                LOG.debug("essential or important %s" % addon)
                return False
            # we have it in our dependencies already
            if addon in deps:
                LOG.debug("already a dependency %s" % addon)
                return False
            # its a language-pack, language-selector should deal with it
            if self._is_language_pkg(addon):
                LOG.debug("part of language pkg rdepends %s" % addon)
                return False
            # something on the system depends on it
            rdeps = self.get_packages_removed_on_remove(addon_pkg)
            if rdeps and ignore_installed:
                LOG.debug("already has a installed rdepends %s" % addon)
                return False
            # looks good
            return True
        #----------------------------------------------------------------

        def _addons_filter_slow(addon):
            """ helper for get_addons that filters out unneeded ones """
            # this addon would get installed anyway (e.g. via indirect
            # dependency) so it would be misleading to show it
            if addon in all_deps_if_installed:
                LOG.debug("would get installed automatically %s" % addon)
                return False
            return True
        #----------------------------------------------------------------
        # deb file, or pkg needing source, etc
        if (not pkgname in self._cache or
            not self._cache[pkgname].candidate):
            return ([], [])

        # initial setup
        pkg = self._cache[pkgname]

        # recommended addons
        addons_rec = self._get_recommends(pkg)
        LOG.debug("recommends: %s" % addons_rec)
        # suggested addons and renhances
        addons_sug = self._get_suggests(pkg)
        LOG.debug("suggests: %s" % addons_sug)
        renhances = self._get_renhances(pkg)
        LOG.debug("renhances: %s" % renhances)
        addons_sug += renhances
        provides = self._get_provides(pkg)
        LOG.debug("provides: %s" % provides)
        for provide in provides:
            virtual_aptpkg_pkg = self._cache._cache[provide]
            renhances = self._get_renhances_lowlevel_apt_pkg(
                virtual_aptpkg_pkg)
            LOG.debug("renhances of %s: %s" % (provide, renhances))
            addons_sug += renhances
            context = GObject.main_context_default()
            while context.pending():
                context.iteration()

        # get more addons, the idea is that if a package foo-data
        # just depends on foo we want to get the info about
        # "recommends, suggests, enhances" for foo-data as well
        #
        # FIXME: find a good package where this is actually the case and
        #        replace the existing test
        #        (arduino-core -> avrdude -> avrdude-doc) with that
        # FIXME2: if it turns out we don't have good/better examples,
        #         kill it
        deps = self._get_depends(pkg)
        for dep in deps:
            if dep in self._cache:
                pkgdep = self._cache[dep]
                if len(self._get_rdepends(pkgdep)) == 1:
                    # pkg is the only known package that depends on pkgdep
                    pkgdep_rec = self._get_recommends(pkgdep)
                    LOG.debug("recommends from lonley dependency %s: %s" % (
                            pkgdep, pkgdep_rec))
                    addons_rec += pkgdep_rec
                    pkgdep_sug = self._get_suggests(pkgdep)
                    LOG.debug("suggests from lonley dependency %s: %s" % (
                            pkgdep, pkgdep_sug))
                    addons_sug += pkgdep_sug
                    pkgdep_enh = self._get_renhances(pkgdep)
                    LOG.debug("renhances from lonley dependency %s: %s" % (
                            pkgdep, pkgdep_enh))
                    addons_sug += pkgdep_enh

            context = GObject.main_context_default()
            while context.pending():
                context.iteration()

        # remove duplicates from suggests (sets are great!)
        addons_sug = list(set(addons_sug) - set(addons_rec))

        # filter out stuff we don't want
        addons_rec = filter(_addons_filter, addons_rec)
        addons_sug = filter(_addons_filter, addons_sug)

        # this is not integrated into the filter above, as it is quite
        # expensive to run this call, so we only run it if we actually have
        # addons
        if addons_rec or addons_sug:
            # now get all_deps if the package would be installed
            try:
                all_deps_if_installed = \
                    self._try_install_and_get_all_deps_installed(pkg)
            except:
                # if we have broken packages, then we return no addons
                LOG.warn(
                    "broken packages encountered while getting deps for %s" %
                    pkgname)
                return ([], [])
            # filter out stuff we don't want
            addons_rec = filter(_addons_filter_slow, addons_rec)
            addons_sug = filter(_addons_filter_slow, addons_sug)

        return (addons_rec, addons_sug)

if __name__ == "__main__":
    c = AptCache()
    c.open()
    print("deps of unrar")
    print(c._installed_dependencies(c["unrar"].name))

    print("unused deps of 4g8")
    pkg = c._cache["4g8"]
    pkg.mark_delete()
    print(c.get_installed_automatic_depends_for_pkg(pkg))

    pkg = c["unace"]
    print(c.get_installed_automatic_depends_for_pkg(pkg))
    print(c.get_packages_removed_on_remove(pkg))
    print(c._get_installed_rrecommends(pkg))
    print(c._get_installed_rsuggests(pkg))

    print("deps of gimp")
    pkg = c["gimp"]
    print(c._get_depends(pkg))
    print(c._get_recommends(pkg))
    print(c._get_suggests(pkg))
    print(c._get_enhances(pkg))
    print(c._get_provides(pkg))

    print("rdeps of gimp")
    print(c._get_rdepends(pkg))
    print(c._get_rrecommends(pkg))
    print(c._get_rsuggests(pkg))
    print(c._get_renhances(pkg))
    print(c._get_rprovides(pkg))