summaryrefslogtreecommitdiff
path: root/bin/clean_old_injected_packages
blob: 03d4a94491d90c8056ed06630a7099639b867780 (plain)
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
#!/usr/bin/python

import sys
sys.path.insert(0, "/usr/lib/matter")
sys.path.insert(0, "/usr/lib/entropy/lib")

import os

from entropy.server.interfaces import Server
from entropy.i18n import _

from matter.spec import SpecParser
import matter.binpms.entropysrv  # plugin registration


PARTICLES_DIR = "/particles"


if __name__ == "__main__":

    # collect particles
    particles = []
    for root, dirs, files in os.walk(PARTICLES_DIR):
        for name in files:
            if name.endswith(".particle"):
                particles.append(os.path.join(root, name))

    packages = set()
    for particle in particles:
        with open(particle, "r") as f:
            spec = SpecParser(f)
            data = spec.parse()
            if not data:
                continue

            if data.get("build-only", "no") != "yes":
                continue

            drop_injected = data.get("drop-old-injected", "no") == "yes"
            if not drop_injected:
                continue
            for p_packages in data.get("packages", []):
                packages.update(p_packages)

    packages = sorted(packages)

    srv = Server()
    for repository in srv.repositories():

        repo = srv.open_repository(repository)
        for package in packages:

            latest_package_id, _mrc = repo.atomMatch(package)
            if latest_package_id == -1:
                continue

            package_ids, _mrc = repo.atomMatch(package, multiMatch=True)
            if not package_ids:
                continue

            key_slots = set()
            for package_id in package_ids:
                key, slot = repo.retrieveKeySlot(package_id)
                key_slots.add((key, slot))

            key_slot_package_ids = set()
            for key, slot in key_slots:
                ks_package_ids = [x for x in repo.searchKeySlot(key, slot) \
                                      if repo.isInjected(x)]
                key_slot_package_ids.update(ks_package_ids)

            key_slot_package_ids.discard(latest_package_id)
            if key_slot_package_ids:
                key_slot_package_ids = sorted(key_slot_package_ids)
                atoms = [repo.retrieveAtom(x) for x in key_slot_package_ids]
                atoms.sort()

                latest_atom = repo.retrieveAtom(latest_package_id)
                srv.output("%s is the latest package" % (latest_atom))
                srv.output("removing old packages:")
                for atom in atoms:
                    srv.output("  %s" % (atom,))
                resp = srv.ask_question("Really remove?")
                if resp != _("Yes"):
                    continue

                srv.remove_packages(
                    repository, key_slot_package_ids)
                srv.commit_repositories()

    srv.destroy()