summaryrefslogtreecommitdiffstats
path: root/proxysolver
blob: 5cd51fab89104d30b8b12bff18a49d38d9be0003 (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
#!/usr/bin/env python3
#
# This script is in the public domain
#
# Author: Johannes Schauer Marin Rodrigues <josch@mister-muffin.de>
#
# thin layer around /usr/lib/apt/solvers/apt, so that we can capture the solver
# result
#
# we set Debug::EDSP::WriteSolution=yes so that Install stanzas also come with
# Package and Version fields. That way, we do not also have to parse the EDSP
# request and spend time matching ID numbers

import subprocess
import sys
import os
import getpass

if not os.path.exists("/usr/lib/apt/solvers/apt"):
    print(
        """Error: ERR_NO_SOLVER
Message: The external apt solver doesn't exist. You must install the apt-utils package.
"""
    )
    exit()

fname = os.environ.get("APT_EDSP_DUMP_FILENAME")
if fname is None:
    print(
        """Error: ERR_NO_FILENAME
Message: You have to set the environment variable APT_EDSP_DUMP_FILENAME
 to a valid filename to store the dump of EDSP solver input in.
 For example with: export APT_EDSP_DUMP_FILENAME=/tmp/dump.edsp
"""
    )
    exit()

try:
    with open(fname, "w") as f:
        with subprocess.Popen(
            ["/usr/lib/apt/solvers/apt", "-oDebug::EDSP::WriteSolution=yes"],
            stdin=sys.stdin.fileno(),
            stdout=subprocess.PIPE,
            bufsize=0,  # unbuffered
            text=True,  # open in text mode
        ) as p:
            for line in p.stdout:
                print(line, end="")
                f.write(line)
except (FileNotFoundError, PermissionError) as e:
    print(
        """Error: ERR_CREATE_FILE
Message: Writing EDSP solver input to file '%s' failed as it couldn't be created!
"""
        % fname
    )