File: emulated-g-ir-tool.in

package info (click to toggle)
gobject-introspection 1.84.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 72,336 kB
  • sloc: ansic: 562,269; python: 23,692; xml: 16,240; yacc: 1,711; perl: 1,624; sh: 1,139; lex: 510; cpp: 487; makefile: 182; javascript: 15; lisp: 1
file content (73 lines) | stat: -rw-r--r-- 1,864 bytes parent folder | download
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
#!/usr/bin/python3
# Copyright 2023 Collabora Ltd.
# Copyright 2024 Simon McVittie
# SPDX-License-Identifier: MIT

import os
import subprocess
import sys
import sysconfig
from typing import NoReturn


CAN_RUN = '@CAN_RUN@'
DEB_HOST_ARCH = '@DEB_HOST_ARCH@'
DEB_HOST_GNU_TYPE = '@DEB_HOST_GNU_TYPE@'
DEB_HOST_MULTIARCH = '@DEB_HOST_MULTIARCH@'
TOOL = '@TOOL@'
TOOL_PATH = '@TOOL_PATH@'

ME = f'{DEB_HOST_GNU_TYPE}-{TOOL}'


class CrossGirTool:
    def __init__(
        self,
        argv: list[str],
    ) -> None:
        self.argv = argv

    def can_run(self) -> bool:
        python_arch = sysconfig.get_config_var('MULTIARCH')

        if python_arch == DEB_HOST_MULTIARCH:
            # Common case: if python3 is already an executable of the desired
            # architecture, then trivially we can run it
            return True

        try:
            completed = subprocess.run(
                [CAN_RUN],
                check=False,
                stdout=subprocess.PIPE,
                stderr=subprocess.DEVNULL,
            )
        except OSError:
            return False
        else:
            return completed.stdout == b'ok\n' and completed.returncode == 0

    def get_wrapper(self) -> str:
        return f'{DEB_HOST_GNU_TYPE}-cross-exe-wrapper'

    def exec(self) -> NoReturn:
        exe_wrapper = []

        if not self.can_run():
            print(
                (f'{ME}: Cannot run {DEB_HOST_ARCH} executables '
                 + 'directly, using cross-exe-wrapper'),
                file=sys.stderr,
            )
            exe_wrapper = [self.get_wrapper()]

        argv = exe_wrapper + [TOOL_PATH] + sys.argv[1:]
        try:
            os.execvp(argv[0], argv)
        except OSError:
            print(f'{ME}: Unable to run: {argv}')
            raise


if __name__ == '__main__':
    CrossGirTool(sys.argv).exec()