File: image-test.py

package info (click to toggle)
geeqie 1%3A2.5-8
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 16,780 kB
  • sloc: cpp: 110,189; xml: 11,497; sh: 3,681; awk: 124; perl: 88; python: 80; makefile: 23
file content (164 lines) | stat: -rwxr-xr-x 6,147 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#!/bin/python3
#**********************************************************************
# Copyright (C) 2023 - The Geeqie Team
#
# Author: Colin Clark
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#**********************************************************************

import os
import pathlib
import subprocess
import sys
import time
import traceback
from typing import Optional

MAX_GEEQIE_INIT_TIME_S = 10
MAX_REMOTE_CMD_TIME_S = 10
MAX_GEEQIE_SHUTDOWN_TIME_S = 10


class GeeqieTestError(Exception):
    """A custom exception type that forwards the geeqie exit code.

    Attributes:
        exit_code: An int with the exit code to return when image-test.py exits.
    """

    def __init__(self, description: str, geeqie_proc: Optional[subprocess.Popen]):
        """GeeqieTestError constructor.

        Args:
            description: A description of the failure state.
            geeqie_proc: The Popen wrapper for the geeqie process.  The exception
                message will be generic if this is not provided.
        """
        self._description = description
        if geeqie_proc is not None:
            self._geeqie_proc_provided = True
            self._proc_exit_code = geeqie_proc.poll()
        else:
            self._geeqie_proc_provided = False
            self._proc_exit_code = 1

        if self._proc_exit_code:
            self.exit_code = self._proc_exit_code
        else:
            self.exit_code = 1

    def __str__(self) -> str:
        if self._geeqie_proc_provided:
            if self._proc_exit_code is None:
                # geeqie process is still running.
                verb = "stalled"
            else:
                # geeqie process exited unexpectedly.
                verb = "crashed"

            return f"geeqie {verb} during: {self._description}"

        # Generic error.  This will automatically be preceded by the Exception
        # class name, "GeeqieTestError", so no extra decoration is needed.
        return self._description


def main(argv) -> int:
    geeqie_exe = argv[1]
    test_image_path = argv[2]

    # All geeqie commands start with this.
    geeqie_cmd_prefix = ["xvfb-run", "--auto-servernum", geeqie_exe]

    # Start geeqie and have it open the provided test image path.
    # TODO(xsdg): Note that killing the `xvfb-run` script will not forward that
    # signal to the geeqie process.  See
    # <https://unix.stackexchange.com/questions/291804/howto-terminate-xvfb-run-properly>.
    # So we should modify or replace xvfb-run to allow us to kill an errant geeqie process.
    geeqie_proc = subprocess.Popen(args=[*geeqie_cmd_prefix, test_image_path])

    # try/finally to ensure we clean up geeqie_proc
    try:
        start_time = time.monotonic()

        # Wait up to MAX_GEEQIE_INIT_TIME_S for remote to initialize.
        command_fifo_path = pathlib.Path(os.environ["XDG_CONFIG_HOME"], "geeqie/.command")
        while not command_fifo_path.exists():
            time.sleep(1)
            if time.monotonic() - start_time > MAX_GEEQIE_INIT_TIME_S:
                raise GeeqieTestError("init (before creating command fifo)", geeqie_proc)

        # We make sure Geeqie can stay alive for 2 seconds after initialization.
        time.sleep(2)
        if geeqie_proc.poll() is not None:
            raise GeeqieTestError("2-second post-init check", geeqie_proc)

        file_info_result = subprocess.run(
                args=[*geeqie_cmd_prefix, "--remote", "--get-file-info"],
                capture_output=True, text=True, timeout=MAX_REMOTE_CMD_TIME_S)

        # Check if Geeqie crashed (which would cause xvfb-run to terminate)
        time.sleep(1)
        if geeqie_proc.poll() is not None:
            raise GeeqieTestError("remote command processing", geeqie_proc)

        # Request shutdown
        subprocess.run(args=[*geeqie_cmd_prefix, "--remote", "--quit"],
                       timeout=MAX_REMOTE_CMD_TIME_S)

        # If there's a timeout, or the exit code isn't zero, flag it.
        try:
            if geeqie_proc.wait(MAX_GEEQIE_SHUTDOWN_TIME_S) != 0:
                raise GeeqieTestError("shutdown", geeqie_proc)
        except subprocess.TimeoutExpired:
            # Re-raise as shutdown error.
            raise GeeqieTestError("shutdown", geeqie_proc)

        # Check if file was recognized correctly or not
        if "Class: Unknown" in file_info_result.stdout:
            raise GeeqieTestError("file was not loaded correctly", geeqie_proc=None)

    except subprocess.TimeoutExpired as e:
        # This means one of the remote commands failed.  Re-raise as test error,
        # and include the original exception (to show which remote failed).
        raise GeeqieTestError("remote command timeout", geeqie_proc=None) from e

    finally:
        if geeqie_proc.poll() is None:
            # Stash the true error now, before we alter the state of geeqie_proc.
            exit_error = GeeqieTestError("shutdown", geeqie_proc)

            # geeqie is still running after we requested it to exit, so try SIGTERM.
            geeqie_proc.terminate()

            try:
                geeqie_proc.wait(timeout=15)
            except subprocess.TimeoutExpired:
                print("sending SIGKILL to geeqie")
                geeqie_proc.kill()

            raise exit_error

    return 0


if __name__ == "__main__":
    try:
        exit(main(sys.argv))

    except GeeqieTestError as e:
        traceback.print_exception(e)
        exit(e.exit_code)