|
| 1 | +#!/usr/bin/env python3 |
| 2 | +"""Render the code-registration Theory/Runtype Details dialogs to PNGs. |
| 3 | +
|
| 4 | + tests/look/codereg_shot.py OUTDIR [--theme light|dark|both] [--tag NAME] |
| 5 | + [--codes orca,nwchem,...] [--scripts DIR] |
| 6 | +
|
| 7 | +Runs the real dialog scripts (unmodified) on a private Xvfb, grabs the |
| 8 | +frame once it is mapped, and writes OUTDIR/<tag>-<code>-<theory|runtype>-<theme>.png. |
| 9 | +Used to compare the dialogs against the C++ windows (#210). |
| 10 | +""" |
| 11 | +import argparse |
| 12 | +import os |
| 13 | +import socket |
| 14 | +import subprocess |
| 15 | +import sys |
| 16 | +import tempfile |
| 17 | +import time |
| 18 | + |
| 19 | +HERE = os.path.dirname(os.path.abspath(__file__)) |
| 20 | +ROOT = os.path.dirname(os.path.dirname(HERE)) |
| 21 | + |
| 22 | +# code -> (theory script, runtype script, category, theory, runtype) |
| 23 | +CASES = { |
| 24 | + "orca": ("orcatheory.py", "orcaruntype.py", "DFT", "RDFT", "Geometry"), |
| 25 | + "nwchem": ("nedtheory.py", "nedruntype.py", "SCF", "RHF", "Geometry"), |
| 26 | + "gaussian16": ("ged16theory.py", "ged16runtype.py", "DFT", "RDFT", |
| 27 | + "Geometry"), |
| 28 | + "mopac": ("mopactheory.py", "mopacruntype.py", "SE", "RPM7", "Energy"), |
| 29 | + "qe": ("qetheory.py", "qeruntype.py", "PW", "PW", "Energy"), |
| 30 | + "gromacs": ("gromacstheory.py", "gromacsruntype.py", "MD", "MD", "Energy"), |
| 31 | +} |
| 32 | + |
| 33 | +INNER = r''' |
| 34 | +import os, sys, subprocess |
| 35 | +sys.path.insert(0, os.environ["CODEREG_DIR"]) |
| 36 | +os.chdir(os.environ["CODEREG_DIR"]) |
| 37 | +import templates |
| 38 | +from templates import wx |
| 39 | +out = os.environ["SHOT_OUT"] |
| 40 | +def finalize(self): |
| 41 | + import time |
| 42 | + self.Fit() |
| 43 | + self.Show(True) |
| 44 | + flash = os.environ.get("SHOT_FLASH") |
| 45 | + if flash: |
| 46 | + panel = [c for c in self.GetChildren() |
| 47 | + if isinstance(c, templates.EccePanel)][0] |
| 48 | + panel.SetStatusText("Value out of range: hold the colour for the shot", |
| 49 | + flash) |
| 50 | + panel.colorTimer.Stop() |
| 51 | + panel.messageTimer.Stop() |
| 52 | + end = time.time() + 1.5 |
| 53 | + while time.time() < end: |
| 54 | + wx.GetApp().Yield() |
| 55 | + time.sleep(0.05) |
| 56 | + r = self.GetScreenRect() |
| 57 | + subprocess.run(["import", "-window", "root", out + ".root.png"]) |
| 58 | + subprocess.run(["convert", out + ".root.png", "-crop", |
| 59 | + "%dx%d+%d+%d" % (r.width, r.height, r.x, r.y), |
| 60 | + "+repage", out]) |
| 61 | + os.unlink(out + ".root.png") |
| 62 | +templates.EcceFrame.Finalize = finalize |
| 63 | +import runpy |
| 64 | +runpy.run_path(os.environ["SHOT_SCRIPT"], run_name="__main__") |
| 65 | +''' |
| 66 | + |
| 67 | + |
| 68 | +def main(): |
| 69 | + ap = argparse.ArgumentParser() |
| 70 | + ap.add_argument("outdir") |
| 71 | + ap.add_argument("--theme", default="light") |
| 72 | + ap.add_argument("--tag", default="after") |
| 73 | + ap.add_argument("--codes", default=",".join(CASES)) |
| 74 | + ap.add_argument("--scripts", default=os.path.join(ROOT, "scripts/codereg")) |
| 75 | + ap.add_argument("--flash", choices=("warning", "error"), default=None, |
| 76 | + help="show a status message with that flash colour") |
| 77 | + ap.add_argument("--fontsize", default=None, |
| 78 | + help="write FONTSIZE:<n> to the test preferences") |
| 79 | + o = ap.parse_args() |
| 80 | + os.makedirs(o.outdir, exist_ok=True) |
| 81 | + sys.path.insert(0, os.path.join(ROOT, "tests", "apps")) |
| 82 | + import xdisplay |
| 83 | + work = tempfile.mkdtemp(prefix="codereg-shot") |
| 84 | + os.makedirs(os.path.join(work, ".ECCE")) |
| 85 | + if o.fontsize: |
| 86 | + with open(os.path.join(work, ".ECCE", "EcceGlobal"), "w") as f: |
| 87 | + f.write("FONTSIZE:\t%s\n" % o.fontsize) |
| 88 | + sink = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) |
| 89 | + sink.bind(("127.0.0.1", 0)) |
| 90 | + port = str(sink.getsockname()[1]) |
| 91 | + status = 0 |
| 92 | + with xdisplay.Display(screen="1400x1000x24") as xv: |
| 93 | + time.sleep(1) |
| 94 | + themes = ["light", "dark"] if o.theme == "both" else [o.theme] |
| 95 | + for theme in themes: |
| 96 | + gtk = "Adwaita:dark" if theme == "dark" else "Adwaita" |
| 97 | + for code in o.codes.split(","): |
| 98 | + th, rt, cat, theory, run = CASES[code] |
| 99 | + for kind, script in (("theory", th), ("runtype", rt)): |
| 100 | + png = os.path.join(o.outdir, "%s-%s-%s-%s.png" |
| 101 | + % (o.tag, code, kind, theme)) |
| 102 | + restore = os.path.join(work, "restore.in") |
| 103 | + open(restore, "w").write("END_GUIValues\n") |
| 104 | + argv = [restore, port, "GUIValues", "Writable", |
| 105 | + "DebugOff", cat, theory, run, "shot", "0", "C1", |
| 106 | + "10", "1", "0", "5", "5", "6"] |
| 107 | + env = dict(os.environ, DISPLAY=xv.name, GTK_THEME=gtk, |
| 108 | + ECCE_REALUSERHOME=work, HOME=work, |
| 109 | + CODEREG_DIR=o.scripts, SHOT_OUT=png, |
| 110 | + SHOT_SCRIPT=os.path.join(o.scripts, script), |
| 111 | + PYTHONDONTWRITEBYTECODE="1", |
| 112 | + SHOT_FLASH=o.flash or "") |
| 113 | + r = subprocess.run([sys.executable, "-c", INNER] + argv, |
| 114 | + env=env, capture_output=True, text=True, errors="replace", |
| 115 | + timeout=120) |
| 116 | + if not os.path.exists(png): |
| 117 | + print("FAILED %s %s %s rc=%s\n%s%s" % (code, kind, theme, r.returncode, r.stdout, |
| 118 | + r.stderr[-1500:])) |
| 119 | + status = 1 |
| 120 | + else: |
| 121 | + print(png) |
| 122 | + return status |
| 123 | + |
| 124 | + |
| 125 | +if __name__ == "__main__": |
| 126 | + sys.exit(main()) |
0 commit comments