source: ogLive-Builder-Git/boottools/apt.py @ 2e092b0

build-browserdeps-vadimfilebeat-installerimprove-versionlgromero-testsmainpull-from-cloning-engine
Last change on this file since 2e092b0 was ffc24fb, checked in by Natalia Serrano <natalia.serrano@…>, 8 months ago

refs #596 fix bug when removing packages

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#!/usr/bin/python3
2
3import apt
4import apt.progress.base
5import apt_pkg
6import shutil
7import os
8
9def clean():
10    apt_cache = apt.Cache()
11
12    apt_pkg.init()
13    cache_dir = apt_pkg.config.find_dir ('Dir::Cache::archives')
14
15    for fn in os.listdir (cache_dir):
16        file_path = os.path.join (cache_dir, fn)
17        try:
18            if os.path.isfile (file_path):
19                os.unlink (file_path)
20            elif os.path.isdir (file_path):
21                shutil.rmtree (file_path)
22        except Exception as e:
23            print (f'Failed to delete {file_path}. Reason: {e}')
24
25def autoremove():
26    apt_cache = apt.Cache()
27    for pkg in apt_cache:
28       if pkg.is_installed and pkg.is_auto_removable:
29           pkg.mark_delete()
30    try:
31        apt_cache.commit()
32    except Exception as e:
33        print ('installation failed: {}'.format (str (e)))
34
35def remove (pkgs):
36    apt_cache = apt.Cache()
37    for p in pkgs:
38        if p in apt_cache:
39            apt_cache[p].mark_delete()
40    try:
41        apt_cache.commit()
42    except Exception as e:
43        print ('removal failed: {}'.format (str (e)))
44
45def update():
46    apt_cache = apt.Cache()
47    apt_cache.update()
48
49def upgrade():
50    apt_cache = apt.Cache()
51    apt_cache.upgrade()
52
53def cache_search (pkgs):
54    apt_cache = apt.Cache()
55    res = {}
56    for p in pkgs:
57        res[p] = True if p in apt_cache else False
58    return res
59
60def install (pkgs, opts={}):
61    apt_pkg.init()
62
63    if opts:
64        apt_pkg.init_config()
65        for k in opts:
66            apt_pkg.config.set (k, opts[k])
67
68    cache = apt_pkg.Cache()
69    sl = apt_pkg.SourceList()
70    sl.read_main_list()
71    cache.update (apt.progress.base.AcquireProgress(), sl)
72
73    _to_install = []
74    dep_cache = apt_pkg.DepCache(cache)
75    for p in pkgs:
76        package = cache[p]
77        if not package:
78            print (f'package "{p}" not found')
79            continue
80        _to_install.append (p)
81        dep_cache.mark_install(package)
82
83    if _to_install:
84        print ('about to install these packages: "{}"'.format (' '.join (_to_install)))
85        fetcher = apt_pkg.Acquire()
86        install_progress = apt.progress.base.InstallProgress()
87        dep_cache.commit(fetcher, install_progress)
Note: See TracBrowser for help on using the repository browser.