File size: 1,481 Bytes
2c3c408
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

import shlex
import sys
import subprocess
import threading
from pathlib import Path

target_script_name = "basic"
script_time_to_live = 2.0  # in seconds

if len(sys.argv) > 1:
    target_script_name = sys.argv[1]
if len(sys.argv) > 2:
    script_time_to_live = float(sys.argv[2])

e2e_root = Path(__file__).parent / "test_apps"

completed_process = None


def launch_sandbox_script(python_file_name: str) -> None:
    global completed_process

    command = f"{sys.executable} {shlex.quote(python_file_name)}.py"
    print(f"Launching command '{command}'...")
    try:
        completed_process = subprocess.run(
            command, shell=True, check=True, capture_output=True, cwd=str(e2e_root)
        )
    except subprocess.CalledProcessError as err:
        print(f"Process error: {err.stderr}")
        raise


thread = threading.Thread(
    target=launch_sandbox_script, args=(target_script_name,), daemon=False
)
thread.start()

print(
    f"Launching Python script in a sub-thread; we'll wait for it for {script_time_to_live} seconds..."
)
thread.join(timeout=script_time_to_live)
print("The wait is over.")

process_still_running = completed_process is None
process_was_able_to_run_without_errors = process_still_running

if process_was_able_to_run_without_errors:
    print("Python script is still running :-)")
else:
    print("Python script is no longer running :-/")

sys.exit(0 if process_was_able_to_run_without_errors else 1)