__main__.py 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. import argparse
  2. import os
  3. import subprocess
  4. import sys
  5. from .runner import REPO, ROOTFS, Userland
  6. from .session import KINDS, Session, Unsupported
  7. FEDORA_PACKAGES = REPO / "tools/desktops/rootfs-fedora.txt"
  8. ARCH_PACKAGES = REPO / "tools/desktops/rootfs-arch.txt"
  9. def setup(which):
  10. """the rootfs a session runs in where its programs are not installed natively"""
  11. script = str(REPO / "tools/distro_rootfs.sh")
  12. image, packages, install = {
  13. "fedora": ("fedora:44", FEDORA_PACKAGES, ["dnf", "install", "-y", "--setopt=install_weak_deps=False"]),
  14. "arch": ("archlinux:latest", ARCH_PACKAGES, ["pacman", "-Syu", "--noconfirm", "--needed", "--disable-sandbox"]),
  15. }[which]
  16. root = ROOTFS[which]
  17. if not root.is_dir():
  18. subprocess.run([script, "fetch", image, str(root)], check=True)
  19. if which == "arch":
  20. # proot has no free space to report
  21. subprocess.run(["sed", "-i", "s/^CheckSpace/#CheckSpace/", str(root / "etc/pacman.conf")], check=True)
  22. names = packages.read_text().split()
  23. subprocess.run([script, "run", str(root), *install, *names], check=True)
  24. # the session bus ignores uids without a passwd entry
  25. uid, gid = os.getuid(), os.getgid()
  26. subprocess.run([script, "run", str(root), "sh", "-c",
  27. f"getent group {gid} >/dev/null || groupadd -g {gid} lwe; "
  28. f"getent passwd {uid} >/dev/null || useradd -u {uid} -g {gid} -M -d /tmp -s /bin/bash lwe"], check=True)
  29. def main():
  30. parser = argparse.ArgumentParser(prog="desktop_session.py", description="Headless desktop sessions for tests")
  31. sub = parser.add_subparsers(dest="command", required=True)
  32. sub.add_parser("list", help="the sessions and where each would run")
  33. sub.add_parser("setup", help="create the rootfs for sessions that are not installed natively").add_argument(
  34. "which", choices=("fedora", "arch"))
  35. start = sub.add_parser("start", help="start a session")
  36. start.add_argument("name", choices=sorted(KINDS))
  37. start.add_argument("--size", default="1920x1080")
  38. start.add_argument("--outputs", type=int, default=1)
  39. for name, text in (("env", "shell exports for a program under test"), ("stop", "stop a session or all"),
  40. ("status", "whether a session runs")):
  41. sub.add_parser(name, help=text).add_argument("name", nargs="?" if name == "stop" else None, default="all")
  42. tool = sub.add_parser("tool", help="run a program in a session's userland: swaymsg, hyprctl, wmctrl, grim")
  43. tool.add_argument("name")
  44. tool.add_argument("argv", nargs=argparse.REMAINDER)
  45. shot = sub.add_parser("shot", help="screenshot of a session")
  46. shot.add_argument("name")
  47. shot.add_argument("path")
  48. args = parser.parse_args()
  49. if args.command == "list":
  50. for name, userland in Session.available().items():
  51. print(f"{name:14} {userland or 'not available (run setup)'}")
  52. elif args.command == "setup":
  53. setup(args.which)
  54. elif args.command == "start":
  55. width, height = (int(part) for part in args.size.split("x"))
  56. session = Session.of(args.name, size=(width, height), outputs=args.outputs).start()
  57. print(f"{args.name} is up in {session.userland.name}, use: eval \"$(python3 tools/desktop_session.py env {args.name})\"")
  58. elif args.command == "stop":
  59. for name in (KINDS if args.name in ("all", None) else [args.name]):
  60. Session.of(name).stop()
  61. elif args.command == "status":
  62. print("running" if Session.of(args.name).is_running() else "stopped")
  63. elif args.command == "env":
  64. print(Session.load(args.name).shell_exports(), end="")
  65. elif args.command == "tool":
  66. session = Session.load(args.name)
  67. result = session.run(args.argv)
  68. sys.stdout.write(result.stdout)
  69. sys.stderr.write(result.stderr)
  70. sys.exit(result.returncode)
  71. elif args.command == "shot":
  72. try:
  73. Session.load(args.name).shot(args.path)
  74. except Unsupported as error:
  75. sys.exit(f"{error}")
  76. if __name__ == "__main__":
  77. main()