xref: /trunk/test/odf-validate/gate.py (revision 0e16a31d78a652c56c5c0fc79c752f48b9048df9)
1#**************************************************************
2#
3#  Licensed to the Apache Software Foundation (ASF) under one
4#  or more contributor license agreements.  See the NOTICE file
5#  distributed with this work for additional information
6#  regarding copyright ownership.  The ASF licenses this file
7#  to you under the Apache License, Version 2.0 (the
8#  "License"); you may not use this file except in compliance
9#  with the License.  You may obtain a copy of the License at
10#
11#    http://www.apache.org/licenses/LICENSE-2.0
12#
13#  Unless required by applicable law or agreed to in writing,
14#  software distributed under the License is distributed on an
15#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16#  KIND, either express or implied.  See the License for the
17#  specific language governing permissions and limitations
18#  under the License.
19#
20#**************************************************************
21
22# usage: gate.py <tag> [--stages s,...] [--modes ext,strict] [--fixture-modes v00,v10,v11,ext,strict] [--prev DIR] [--jobs FILE] [--limit N] [--port P]
23#                [--build mod,...] [--libs lib.dylib,...] [--stall SECONDS] [--force] [--crosscheck]
24# The corpus regression gate as one resumable run in runs/gate-<tag>/. Stages, in order:
25#   build     rebuild the modules (--build) and deliver them to the solver
26#   refresh   copy the rebuilt libraries (--libs) from the solver into the app copy and verify them
27#   corpus    save the job list in ODF 1.2 Extended (ext) and strict (strict) mode with the app copy
28#   validate  validate.py and the checkers on the saved packages; kinds compared with baseline/ and --prev;
29#             a checker that did not run, a shall finding of idcheck/pkgcheck/doctypecheck, or more formula findings than --prev fails
30#   dump      reload every saved package and dump a fixed property set (dump7.py), with a stall watchdog
31#   diff      dumps (diff7.py) and packages (cmp7.py) against --prev; differences not in gate-volatile.tsv are unexplained
32#   fixtures  gate-fixtures.tsv saved in five version modes, reload checks, validation; results against --prev
33# --jobs freezes the inputs (source<TAB>output lines; outputs are rewritten); default: --prev's list, else make-jobs.sh.
34# Exit status 0 only if every stage that ran passed.
35import argparse, collections, json, os, re, shutil, socket, subprocess, sys, threading, time
36
37D = os.path.dirname(os.path.abspath(__file__))
38PY = [sys.executable, "-I"]
39STAGES = ["build", "refresh", "corpus", "validate", "dump", "diff", "fixtures"]
40FIX_MODES = ["v00", "v10", "v11", "ext", "strict"]
41DEFAULT_LIBS = "libxo.dylib,libsw.dylib,libsc.dylib,libsd.dylib,libsfx.dylib"
42
43
44def listening(port):
45    with socket.socket() as s:
46        s.settimeout(0.5)
47        return s.connect_ex(("localhost", port)) == 0
48
49
50def kill_office(profile):
51    subprocess.run(["pkill", "-f", "profile_" + profile], capture_output=True)
52    time.sleep(2)
53
54
55def run(cmd, log=None, timeout=None, cwd=None, env=None):
56    """Runs cmd, appending its output to log; returns the exit code (124 on timeout)."""
57    out = open(log, "a") if log else subprocess.DEVNULL
58    try:
59        return subprocess.run(cmd, stdout=out, stderr=subprocess.STDOUT, timeout=timeout, cwd=cwd, env=env).returncode
60    except subprocess.TimeoutExpired:
61        return 124
62    finally:
63        if log:
64            out.close()
65
66
67def read_jobs(path):
68    return [tuple(l.rstrip("\n").split("\t")) for l in open(path) if l.strip()]
69
70
71def rewrite_jobs(jobs, outdir):
72    """Same sources, outputs moved into outdir."""
73    return [(src, os.path.join(outdir, os.path.basename(out))) for src, out in jobs]
74
75
76def read_volatile(path=os.path.join(D, "gate-volatile.tsv")):
77    return {l.split("\t")[0]: l.rstrip("\n").split("\t")[1] for l in open(path) if l.strip() and not l.startswith("#")}
78
79
80def classify(diff, volatile):
81    """diff: diff7.py's result; returns (identical, volatile differing, unexplained differing)."""
82    differing = sorted(k for k, v in diff.items() if v["ndiff"])
83    vol = [k for k in differing if k in volatile]
84    return len(diff) - len(differing), vol, [k for k in differing if k not in volatile]
85
86
87def run_with_stall(cmd, progress, stall, poll=2.0, log=None):
88    """Runs cmd until it exits or progress() has not changed for `stall` seconds (then kills it).
89    Returns ('exit', returncode) or ('stalled', None)."""
90    out = open(log, "a") if log else subprocess.DEVNULL
91    p = subprocess.Popen(cmd, stdout=out, stderr=subprocess.STDOUT)
92    last, since = progress(), time.time()
93    try:
94        while p.poll() is None:
95            time.sleep(poll)
96            now = progress()
97            if now != last:
98                last, since = now, time.time()
99            elif time.time() - since > stall:
100                p.kill(); p.wait()
101                return "stalled", None
102        return "exit", p.returncode
103    finally:
104        if log:
105            out.close()
106
107
108def bridge_dead(path):
109    """True for a dump that only records that the office died under an earlier document."""
110    try:
111        d = json.load(open(path))
112    except Exception:
113        return False
114    return isinstance(d, dict) and (str(d.get("error", "")).startswith("Binary URP bridge") or str(d.get("body_error", "")).startswith("Binary URP bridge"))
115
116
117def dump_dir(src, dest, profile, port, stall, log, tool=None, poll=2.0):
118    """Reloads every package in src into dest/*.json. A document that stalls the office or kills it gets an error
119    marker only after `stall` seconds without progress, never because the whole batch took long.
120    tool(port, dest, files) -> argv replaces dump7.py under the office (for tests); then no office is started."""
121    os.makedirs(dest, exist_ok=True)
122    files = sorted(os.path.join(src, f) for f in os.listdir(src) if not f.startswith("."))
123    count = lambda: len([f for f in os.listdir(dest) if f.endswith(".json")])
124    stalls = 0
125    retried = 0
126    for attempt in range(4):
127        while count() < len(files):
128            if tool is None and not listening(port):
129                if run([os.path.join(D, "start.sh"), profile, str(port)], log) != 0:
130                    return {"error": "office did not start", "stalls": stalls}
131            before = count()
132            marker = os.path.join(dest, ".current")
133            argv = tool(port, dest, files) if tool else [os.path.join(D, "runpy.sh"), os.path.join(D, "dump7.py"), str(port), dest] + files
134            how, rc = run_with_stall(argv, lambda: (count(), os.path.getmtime(marker) if os.path.exists(marker) else 0), stall, poll, log=log)
135            if how == "stalled" or rc != 0:
136                cur = open(marker).read().strip() if os.path.exists(marker) else ""
137                if cur and not os.path.exists(os.path.join(dest, os.path.basename(cur) + ".json")):
138                    reason = ("HANG: no progress for %ds" % stall) if how == "stalled" else "CRASH: rc=%s" % rc
139                    json.dump({"error": reason}, open(os.path.join(dest, os.path.basename(cur) + ".json"), "w"))
140                    stalls += 1
141                if tool is None:
142                    kill_office(profile)
143            elif count() == before:
144                return {"error": "dump7.py made no progress", "stalls": stalls}
145        # dump7.py records "bridge disposed" for every document after an office crash; those are not dumps, so redo them
146        gone = [f for f in os.listdir(dest) if f.endswith(".json") and bridge_dead(os.path.join(dest, f))]
147        if not gone or attempt == 3:
148            break
149        if tool is None:
150            kill_office(profile)
151        for f in gone:
152            os.remove(os.path.join(dest, f))
153        retried += len(gone)
154    if tool is None:
155        kill_office(profile)
156    res = {"documents": count(), "stalls": stalls}
157    if retried:
158        res["bridge_retried"] = retried
159    return res
160
161
162def count_done(done):
163    ok = fail = 0
164    if os.path.exists(done):
165        for l in open(done):
166            ok += l.rstrip("\n").endswith("\tOK")
167            fail += "\tFAIL" in l
168    return ok, fail
169
170
171class Gate:
172    def __init__(self, a):
173        self.a = a
174        self.run = os.path.join(D, "runs", "gate-" + a.tag)
175        os.makedirs(self.run, exist_ok=True)
176        self.res_path = os.path.join(self.run, "results.json")
177        self.res = json.load(open(self.res_path)) if os.path.exists(self.res_path) else {}
178        self.modes = a.modes.split(",")
179        self.fix_modes = a.fixture_modes.split(",")
180        self.prev = os.path.abspath(a.prev) if a.prev else None
181
182    def save(self):
183        json.dump(self.res, open(self.res_path, "w"), indent=1)
184
185    def log(self, name):
186        return os.path.join(self.run, name + ".log")
187
188    def prev_path(self, *candidates):
189        for c in candidates:
190            p = os.path.join(self.prev, c) if self.prev else None
191            if p and os.path.exists(p):
192                return p
193        return None
194
195    # ---- stages
196    def build(self):
197        mods = [m for m in self.a.build.split(",") if m]
198        if not mods:
199            return {"skipped": "no --build modules"}
200        env = os.environ.get("ODFV_BUILD_ENV") or sorted(__import__("glob").glob(os.path.join(D, "..", "..", "main", "*Env.Set.sh")))[-1]
201        res = {}
202        for m in mods:
203            cmd = "cd %s/../../main && source %s && cd %s && perl $SOLARENV/bin/build.pl && perl $SOLARENV/bin/deliver.pl" % (D, env, m)
204            res[m] = run(["/bin/zsh", "-c", cmd], self.log("build_" + m))
205        return {"ok": all(v == 0 for v in res.values()), "rc": res}
206
207    def refresh(self):
208        libs = [l for l in self.a.libs.split(",") if l]
209        out = subprocess.run([os.path.join(D, "refresh-app.sh")] + libs, capture_output=True, text=True)
210        return {"ok": out.returncode == 0, "output": out.stdout.strip().splitlines()}
211
212    def jobs(self):
213        if self.a.jobs:
214            return read_jobs(self.a.jobs)
215        pj = self.prev_path("jobs.tsv", "ext/jobs.tsv")
216        if pj:
217            return read_jobs(pj)
218        out = subprocess.run([os.path.join(D, "make-jobs.sh"), "OUT"], capture_output=True, text=True, check=True).stdout
219        return [tuple(l.split("\t")) for l in out.splitlines()]
220
221    def corpus(self):
222        jobs = self.jobs()
223        if self.a.limit:
224            jobs = jobs[:self.a.limit]
225        with open(os.path.join(self.run, "jobs.tsv"), "w") as f:
226            f.writelines("%s\t%s\n" % j for j in jobs)
227        procs, res = {}, {}
228        for i, mode in enumerate(self.modes):
229            md = os.path.join(self.run, mode)
230            os.makedirs(os.path.join(md, "out"), exist_ok=True)
231            with open(os.path.join(md, "jobs.tsv"), "w") as f:
232                f.writelines("%s\t%s\n" % j for j in rewrite_jobs(jobs, os.path.join(md, "out")))
233            open(os.path.join(md, "done.tsv"), "a").close()
234            procs[mode] = subprocess.Popen([os.path.join(D, "drive.sh"), mode, str(self.a.port + i), os.path.join(md, "jobs.tsv"), os.path.join(md, "done.tsv")])
235        for mode, p in procs.items():
236            p.wait()
237            md = os.path.join(self.run, mode)
238            ok, fail = count_done(os.path.join(md, "done.tsv"))
239            res[mode] = {"rc": p.returncode, "ok": ok, "fail": fail, "jobs": len(jobs), "packages": len(os.listdir(os.path.join(md, "out")))}
240            pd = self.prev_path(mode + "/done.tsv")
241            if pd and not self.a.limit:
242                res[mode]["prev_ok_fail"] = count_done(pd)
243        res["ok"] = all(r["rc"] == 0 and r["ok"] + r["fail"] == r["jobs"] and r["ok"] > 0 for r in res.values() if isinstance(r, dict))
244        if not self.a.limit:
245            res["ok"] = res["ok"] and all(tuple(r["prev_ok_fail"]) == (r["ok"], r["fail"]) for r in res.values() if isinstance(r, dict) and "prev_ok_fail" in r)
246        return res
247
248    def prev_validate(self, *keys):
249        """The validate-result dict of --prev at keys, or None when it has none."""
250        pf = self.prev_path("results.json")
251        d = json.load(open(pf)) if pf else {}
252        for k in keys:
253            d = d.get(k) if isinstance(d, dict) else None
254        return d if isinstance(d, dict) else None
255
256    def shall_problems(self, r, prev):
257        """Checker results that must fail the gate: a checker that did not run, a Part 1/3 finding, a formula finding that grew."""
258        bad = []
259        for n in ("ids", "pkg", "doctype", "formulas"):
260            if r.get("shall_" + n) is None:
261                bad.append("%s checker did not run" % n)
262        for n in ("ids", "pkg", "doctype"):
263            if r.get("shall_" + n):
264                bad.append("%d shall findings in %s" % (r["shall_" + n], n))
265        pf = (prev or {}).get("shall_formulas")
266        if not self.a.limit and r.get("shall_formulas") is not None and pf is not None and r["shall_formulas"] > pf:
267            bad.append("shall_formulas %d > %d in --prev" % (r["shall_formulas"], pf))
268        return bad
269
270    def validate_dir(self, md, out, kinds_base, label):
271        """validate.py + report.py + the checkers on out/; returns a result dict."""
272        r = {}
273        log = os.path.join(md, "validate.log")
274        run(PY + [os.path.join(D, "validate.py"), out, os.path.join(md, "val.json")], log)
275        val = json.load(open(os.path.join(md, "val.json")))
276        r["errors"] = {v: len(e) for v, e in val["errors"].items()}
277        r["packages"] = len(val["packages"])
278        for v in ("12", "14", "12x", "13", "13x"):
279            kp = os.path.join(md, "kinds%s.json" % ("" if v == "12" else v))
280            run(PY + [os.path.join(D, "report.py"), os.path.join(md, "val.json"), kp, v], os.path.join(md, "report%s.log" % v))
281            r["kinds" + v] = len(json.load(open(kp)))
282        for name, script, pat in (("ids", "idcheck.py", r"(\d+) errors"), ("pkg", "pkgcheck.py", r"(\d+) shall"),
283                                  ("doctype", "doctypecheck.py", r"(\d+) shall"), ("formulas", "formulacheck.py", r"(\d+) shall")):
284            o = subprocess.run(PY + [os.path.join(D, script), out, os.path.join(md, name + ".json")], capture_output=True, text=True)
285            m = re.search(pat, o.stdout)
286            r["shall_" + name] = int(m.group(1)) if m and o.returncode == 0 else None
287        base = kinds_base
288        if base and os.path.exists(base):
289            r["vs_baseline"] = run(PY + [os.path.join(D, "compare.py"), base, os.path.join(md, "kinds.json")], os.path.join(md, "compare_base.log"))
290        return r
291
292    def validate(self):
293        res = {}
294        for mode in self.modes:
295            md = os.path.join(self.run, mode)
296            r = self.validate_dir(md, os.path.join(md, "out"), os.path.join(D, "baseline", mode + ".kinds.json"), mode)
297            pk = self.prev_path(mode + "/kinds.json")
298            r["problems"] = self.shall_problems(r, self.prev_validate("validate", mode))
299            if not self.a.limit and not pk and "vs_baseline" not in r:
300                r["problems"].append("no baseline/%s.kinds.json and no --prev kinds to compare with" % mode)
301            if pk and not self.a.limit:
302                r["vs_prev"] = run(PY + [os.path.join(D, "compare.py"), pk, os.path.join(md, "kinds.json")], os.path.join(md, "compare_prev.log"))
303                r["vs_prev_reverse"] = run(PY + [os.path.join(D, "compare.py"), os.path.join(md, "kinds.json"), pk], os.path.join(md, "compare_prev_rev.log"))
304            res[mode] = r
305        res["ok"] = all(r.get("vs_baseline", 0) == 0 and r.get("vs_prev", 0) == 0 and r.get("vs_prev_reverse", 0) == 0 and not r["problems"] for r in res.values())
306        return res
307
308    def dump(self):
309        res, threads = {}, []
310        for i, mode in enumerate(self.modes):
311            def one(mode=mode, i=i):
312                md = os.path.join(self.run, mode)
313                res[mode] = dump_dir(os.path.join(md, "out"), os.path.join(md, "dump"), "gate_dump_%s_%s" % (self.a.tag, mode),
314                                     self.a.port + 10 + i, self.a.stall, self.log("dump_" + mode))
315            t = threading.Thread(target=one); t.start(); threads.append(t)
316        for t in threads:
317            t.join()
318        res["ok"] = all("error" not in r for r in res.values())
319        for mode in self.modes:
320            md = os.path.join(self.run, mode, "dump")
321            res[mode]["bridge_errors"] = sorted(f for f in os.listdir(md) if f.endswith(".json") and bridge_dead(os.path.join(md, f)))
322            res["ok"] = res["ok"] and not res[mode]["bridge_errors"]
323            res[mode]["hang_markers"] = sorted(f for f in os.listdir(md) if f.endswith(".json") and "error" in json.load(open(os.path.join(md, f))) and
324                                               str(json.load(open(os.path.join(md, f)))["error"]).startswith(("HANG", "CRASH")))
325        return res
326
327    def diff(self):
328        if not self.prev:
329            return {"skipped": "no --prev"}
330        volatile, res = read_volatile(), {}
331        for mode in self.modes:
332            md = os.path.join(self.run, mode)
333            old = self.prev_path(mode + "/dump", "dump_" + mode)
334            oldrun = self.prev_path(mode + "/out")
335            r = {}
336            if old:
337                # compare only the documents this run saved (a --limit run saves fewer)
338                sub = os.path.join(md, "prev-dump")
339                shutil.rmtree(sub, ignore_errors=True); os.makedirs(sub)
340                for f in os.listdir(os.path.join(md, "dump")):
341                    if f.endswith(".json") and os.path.exists(os.path.join(old, f)):
342                        os.symlink(os.path.join(old, f), os.path.join(sub, f))
343                dj = os.path.join(md, "diff_dump.json")
344                run(PY + [os.path.join(D, "diff7.py"), sub, os.path.join(md, "dump"), dj], os.path.join(md, "diff_dump.log"))
345                same, vol, unexplained = classify(json.load(open(dj)), volatile)
346                r["dump"] = {"compared": len(os.listdir(sub)), "identical": same, "volatile": vol, "unexplained": unexplained}
347            if oldrun:
348                stage = os.path.join(self.run, "cmp-" + mode)
349                shutil.rmtree(stage, ignore_errors=True); os.makedirs(os.path.join(stage, "out"))
350                for f in os.listdir(os.path.join(md, "out")):
351                    if os.path.exists(os.path.join(oldrun, f)):
352                        os.symlink(os.path.join(oldrun, f), os.path.join(stage, "out", f))
353                # cmp7 reads <run>/out for both sides: the old side is the subset linked above, the new side this run
354                cj = os.path.join(md, "cmp7.json")
355                cp = subprocess.run(PY + [os.path.join(D, "cmp7.py"), stage, md, mode, cj], capture_output=True, text=True)
356                open(os.path.join(md, "cmp7.log"), "w").write(cp.stdout + cp.stderr)
357                m = re.search(r"differing after env masks: (\d+); differing after intended-edit normalisation: (\d+)", cp.stdout)
358                if m:
359                    cmp = json.load(open(cj))
360                    differing = sorted(k for k, v in cmp.items() if v.get("norm_diff_streams") or v.get("names_diff") or v.get("missing_in_new"))
361                    r["packages"] = {"after_masks": int(m.group(1)), "volatile": [k for k in differing if k in volatile],
362                                     "unexplained": [k for k in differing if k not in volatile]}
363                else:
364                    r["packages"] = {"error": (cp.stderr or cp.stdout)[-200:], "unexplained": ["cmp7.py failed"]}
365            res[mode] = r
366        res["ok"] = all(("dump" not in r or not r["dump"]["unexplained"]) and ("packages" not in r or not r["packages"]["unexplained"]) for r in res.values())
367        return res
368
369    def fixtures(self):
370        entries = [l.rstrip("\n").split("\t") for l in open(os.path.join(D, "gate-fixtures.tsv")) if l.strip() and not l.startswith("#")]
371        def src_path(s):
372            return os.path.join(D, "..", "..", "main", s[4:]) if s.startswith("EXT:") else os.path.join(D, "fixtures", s)
373        def out_name(s):
374            b = os.path.splitext(os.path.basename(s))[0]
375            ext = {"doc": "odt", "docx": "odt", "odt": "odt", "sxw": "odt", "ods": "ods", "sxc": "ods", "odg": "odg", "sxd": "odg", "odp": "odp", "sxi": "odp"}[s.rsplit(".", 1)[1]]
376            return ("pf13-" if s.startswith("page-fill-1_3/") else "") + b + "." + ext
377        sources = sorted({e[0] for e in entries})
378        res, threads = {}, []
379        for i, mode in enumerate(self.fix_modes):
380            def one(mode=mode, i=i):
381                md = os.path.join(self.run, "fixtures", mode)
382                out = os.path.join(md, "out")
383                os.makedirs(out, exist_ok=True)
384                missing = [s for s in sources if not os.path.isfile(src_path(s))]
385                with open(os.path.join(md, "jobs.tsv"), "w") as f:
386                    f.writelines("%s\t%s\n" % (os.path.abspath(src_path(s)), os.path.join(out, out_name(s))) for s in sources if s not in missing)
387                open(os.path.join(md, "done.tsv"), "a").close()
388                rc = run([os.path.join(D, "drive.sh"), mode, str(self.a.port + 20 + i), os.path.join(md, "jobs.tsv"), os.path.join(md, "done.tsv")])
389                ok, fail = count_done(os.path.join(md, "done.tsv"))
390                r = {"drive_rc": rc, "saved": ok, "failed": fail, "missing": missing, "checks": {}}
391                prof, port = "gate_rt_%s_%s" % (self.a.tag, mode), self.a.port + 30 + i
392                run([os.path.join(D, "start.sh"), prof, str(port)], os.path.join(md, "rt.log"))
393                for s, ck in (e for e in entries if e[1] != "-"):
394                    doc = os.path.join(out, out_name(s))
395                    key = "%s|%s" % (os.path.basename(s), ck)
396                    if not os.path.isfile(doc):
397                        r["checks"][key] = {"error": "no saved copy"}; continue
398                    p = subprocess.run([os.path.join(D, "runpy.sh"), os.path.join(D, "roundtrip.py"), str(port), doc, os.path.join(D, "fixtures", ck)], capture_output=True, text=True, timeout=600)
399                    m = re.search(r"roundtrip: (\d+)/(\d+) match", p.stdout)
400                    r["checks"][key] = {"mismatches": sorted(re.findall(r"^MISMATCH (\S+?):", p.stdout, re.M)), "match": int(m.group(1)) if m else None, "total": int(m.group(2)) if m else None}
401                kill_office(prof)
402                r["validate"] = self.validate_dir(md, out, None, mode)
403                r["problems"] = self.shall_problems(r["validate"], self.prev_validate("fixtures", mode, "validate"))
404                res[mode] = r
405            t = threading.Thread(target=one); t.start(); threads.append(t)
406        for t in threads:
407            t.join()
408        pf = self.prev_path("results.json")
409        changed = []
410        if pf:
411            prev = json.load(open(pf)).get("fixtures", {})
412            for mode in self.fix_modes:
413                for k, c in res.get(mode, {}).get("checks", {}).items():
414                    pc = prev.get(mode, {}).get("checks", {}).get(k)
415                    if pc is not None and pc != c:
416                        changed.append("%s %s" % (mode, k))
417        res["changed_vs_prev"] = changed if pf else "no previous fixtures results"
418        res["ok"] = all(res[m]["drive_rc"] == 0 and res[m]["failed"] == 0 and not res[m]["missing"] and
419                        all("error" not in c and c["total"] for c in res[m]["checks"].values()) and not res[m]["problems"] for m in self.fix_modes) and not changed
420        return res
421
422
423def main():
424    ap = argparse.ArgumentParser()
425    ap.add_argument("tag")
426    ap.add_argument("--stages", default="refresh,corpus,validate,dump,diff,fixtures")
427    ap.add_argument("--modes", default="ext,strict")
428    ap.add_argument("--fixture-modes", default=",".join(FIX_MODES))
429    ap.add_argument("--prev")
430    ap.add_argument("--jobs")
431    ap.add_argument("--limit", type=int, default=0)
432    ap.add_argument("--port", type=int, default=2013)
433    ap.add_argument("--build", default="")
434    ap.add_argument("--libs", default=DEFAULT_LIBS)
435    ap.add_argument("--stall", type=int, default=300)
436    ap.add_argument("--force", action="store_true")
437    a = ap.parse_args()
438    g = Gate(a)
439    stages = [s for s in STAGES if s in a.stages.split(",") or (s == "build" and a.build)]
440    failed = []
441    for s in stages:
442        if s in g.res and not a.force:
443            print("%-9s already done (--force to rerun)" % s)
444        else:
445            t0 = time.time()
446            g.res[s] = getattr(g, s)()
447            g.res[s]["seconds"] = int(time.time() - t0)
448            g.save()
449        ok = g.res[s].get("ok", True)
450        print("%-9s %s  %s" % (s, "PASS" if ok else "FAIL", json.dumps({k: v for k, v in g.res[s].items() if k != "ok"})[:300]))
451        if not ok:
452            failed.append(s)
453    print("gate %s: %s" % (a.tag, "PASS" if not failed else "FAIL in " + ", ".join(failed)))
454    sys.exit(1 if failed else 0)
455
456
457if __name__ == "__main__":
458    main()
459