source: ogLive-Builder-Git/boottools/apt.py

filebeat-installer
Last change on this file was bfbc10a, checked in by Natalia Serrano <natalia.serrano@…>, 7 months ago

refs #773 do not use python apt module

  • Property mode set to 100644
File size: 1.6 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    opts_list = []
62    for k in opts: opts_list += ['-o', f'{k}={opts[k]}']
63    print ('about to install these packages: "{}"'.format (' '.join (pkgs)))
64    import subprocess
65    subprocess.run (['apt-get', '--yes', 'install'] + pkgs + opts_list)
Note: See TracBrowser for help on using the repository browser.