1*4344e542STrevor Gross#!/usr/bin/env python3
2*4344e542STrevor Gross"""Create a tarball of intermediate output for inspection if tests fail.
3*4344e542STrevor Gross
4*4344e542STrevor GrossThis is useful for seeing what exactly `ctest` is running.
5*4344e542STrevor Gross"""
6*4344e542STrevor Gross
7*4344e542STrevor Grossimport os
8*4344e542STrevor Grossimport subprocess as sp
9*4344e542STrevor Grossimport sys
10*4344e542STrevor Gross
11*4344e542STrevor Grossfrom datetime import datetime, timezone
12*4344e542STrevor Grossfrom glob import glob
13*4344e542STrevor Grossfrom pathlib import Path
14*4344e542STrevor Gross
15*4344e542STrevor Gross
16*4344e542STrevor Grossdef main():
17*4344e542STrevor Gross    # Find the most recently touched file named "main.c" in the target
18*4344e542STrevor Gross    # directory. This will be libc-tests's `OUT_DIR`
19*4344e542STrevor Gross    marker_files = [Path(p) for p in glob("target/**/main.c", recursive=True)]
20*4344e542STrevor Gross    marker_files.sort(key=lambda path: path.stat().st_mtime)
21*4344e542STrevor Gross    build_dir = marker_files[0].parent
22*4344e542STrevor Gross    print(f"Located build directory '{build_dir}'")
23*4344e542STrevor Gross
24*4344e542STrevor Gross    # Collect all relevant Rust and C files
25*4344e542STrevor Gross    add_files = glob("**/*.rs", recursive=True, root_dir=build_dir)
26*4344e542STrevor Gross    add_files += glob("**/*.c", recursive=True, root_dir=build_dir)
27*4344e542STrevor Gross    file_list = "\n".join(add_files).encode()
28*4344e542STrevor Gross
29*4344e542STrevor Gross    now = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H%MZ")
30*4344e542STrevor Gross    archive_name = f"archive-{now}"
31*4344e542STrevor Gross    archive_path = f"{archive_name}.tar.gz"
32*4344e542STrevor Gross
33*4344e542STrevor Gross    sp.run(["tar", "czvf", archive_path, "-C", build_dir, "-T-"], input=file_list)
34*4344e542STrevor Gross
35*4344e542STrevor Gross    # If we are in GHA, set these env vars for future use
36*4344e542STrevor Gross    gh_env = os.getenv("GITHUB_ENV")
37*4344e542STrevor Gross    if gh_env is not None:
38*4344e542STrevor Gross        print("Updating CI environment")
39*4344e542STrevor Gross        with open(gh_env, "w+") as f:
40*4344e542STrevor Gross            f.write(f"ARCHIVE_NAME={archive_name}\n")
41*4344e542STrevor Gross            f.write(f"ARCHIVE_PATH={archive_path}\n")
42*4344e542STrevor Gross
43*4344e542STrevor Gross
44*4344e542STrevor Grossif __name__ == "__main__":
45*4344e542STrevor Gross    # FIXME(ci): remove after the bump to windoes-2025 GHA images
46*4344e542STrevor Gross    # Python <= 3.9 does not support the very helpful `root_dir` argument,
47*4344e542STrevor Gross    # and that is the version used by the Windows GHA images. Rather than
48*4344e542STrevor Gross    # using setup-python or doing something in the CI script, just find
49*4344e542STrevor Gross    # the newer version and relaunch if this happens to be run with an old
50*4344e542STrevor Gross    # version.
51*4344e542STrevor Gross    try:
52*4344e542STrevor Gross        glob("", root_dir="")
53*4344e542STrevor Gross    except TypeError:
54*4344e542STrevor Gross        if os.environ.get("CI") is None:
55*4344e542STrevor Gross            sys.exit(1)
56*4344e542STrevor Gross
57*4344e542STrevor Gross        # Find the next 3.1x Python version
58*4344e542STrevor Gross        dirs = sorted(list(Path(r"C:\hostedtoolcache\windows\Python").iterdir()))
59*4344e542STrevor Gross        usepy = next(x for x in dirs if r"\3.1" in str(x))
60*4344e542STrevor Gross        py = usepy.joinpath(r"x64\python.exe")
61*4344e542STrevor Gross        print(f"relaunching with {py}")
62*4344e542STrevor Gross        os.execvp(py, [__file__] + sys.argv)
63*4344e542STrevor Gross
64*4344e542STrevor Gross    main()
65