This is an automated email from the ASF dual-hosted git repository.

jimjag pushed a commit to branch odf13-devel
in repository https://gitbox.apache.org/repos/asf/openoffice.git

commit 8fc0957a5e80986bcd9d78839b1ed87a703e85e5
Author: Jim Jagielski <[email protected]>
AuthorDate: Fri Oct 30 09:56:45 2026 -0400

    test/odf-validate: add the foreign ODF 1.3 corpus script, the 1.3 import 
smoke test and the prompt check
---
 test/odf-validate/fixtures/t13/prompt_check.py   |  79 +++++
 test/odf-validate/smoke13-known.tsv              |   4 +
 test/odf-validate/smoke13.py                     | 141 +++++++++
 test/odf-validate/tools13/README.md              |  57 ++++
 test/odf-validate/tools13/make-foreign-corpus.py | 358 +++++++++++++++++++++++
 5 files changed, 639 insertions(+)

diff --git a/test/odf-validate/fixtures/t13/prompt_check.py 
b/test/odf-validate/fixtures/t13/prompt_check.py
new file mode 100644
index 0000000000..b871122ad2
--- /dev/null
+++ b/test/odf-validate/fixtures/t13/prompt_check.py
@@ -0,0 +1,79 @@
+#**************************************************************
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+#**************************************************************
+
+# usage: runpy.sh fixtures/t13/prompt_check.py <port> 
<package.odt|ods|odp|odg> <outdir>
+# Loads copies of one ODF 1.2 package whose office:version and 
manifest:version are rewritten to 1.2, 1.3 and 1.4, with an
+# interaction handler, and reports whether the "created with a newer version" 
request (FutureDocumentVersionProductUpdateRequest)
+# was raised, counted as a call of the handler (the documents used raise no 
other request). Expected: none for 1.2 and 1.3, one for
+# 1.4 (the negative control). Needs Load/ShowOfficeUpdateDialog = true.
+import os, re, sys, zipfile
+import uno, unohelper
+from com.sun.star.task import XInteractionHandler
+from unohelp import connect, desktop, pv
+
+port, src, out = int(sys.argv[1]), os.path.abspath(sys.argv[2]), 
os.path.abspath(sys.argv[3])
+os.makedirs(out, exist_ok=True)
+
+
+class Handler(unohelper.Base, XInteractionHandler):
+    # getRequest() cannot be used from pyuno here (it raises, the request 
being an exception value), so a call counts
+    def __init__(self):
+        self.calls = 0
+
+    def handle(self, request):
+        self.calls += 1
+        for c in request.getContinuations():
+            if 
c.queryInterface(uno.getTypeByName("com.sun.star.task.XInteractionApprove")):
+                c.select()
+                return
+
+
+def rewrite(version, dst):
+    zi, zo = zipfile.ZipFile(src), zipfile.ZipFile(dst, "w")
+    for it in zi.infolist():
+        d = zi.read(it.filename)
+        if it.filename.endswith(".xml") and (it.filename == 
"META-INF/manifest.xml" or it.filename.rsplit("/", 1)[-1] in ("content.xml", 
"styles.xml", "meta.xml", "settings.xml")):
+            t = d.decode("utf-8")
+            attr = "manifest:version" if it.filename == 
"META-INF/manifest.xml" else "office:version"
+            t = re.sub(r'(%s=")[0-9.]+(")' % attr, r"\g<1>%s\g<2>" % version, 
t)
+            d = t.encode("utf-8")
+        zo.writestr(it, d, compress_type=it.compress_type)
+    zo.close()
+
+
+ctx = connect(port)
+desk = desktop(ctx)
+ext = os.path.splitext(src)[1]
+results = {}
+for version, want in (("1.2", False), ("1.3", False), ("1.4", True)):
+    dst = os.path.join(out, "v" + version.replace(".", "") + ext)
+    rewrite(version, dst)
+    h = Handler()
+    doc = desk.loadComponentFromURL(uno.systemPathToFileUrl(dst), "_blank", 0,
+            (pv("Hidden", True), pv("MacroExecutionMode", 0), pv("ReadOnly", 
True), pv("UpdateDocMode", 0), pv("InteractionHandler", h)))
+    loaded = doc is not None
+    if loaded:
+        doc.close(True)
+    got = h.calls > 0
+    results[version] = (loaded, got, want)
+    print("%s: version %s, loaded=%s, update request raised=%s (handler calls: 
%d)" % ("PASS" if loaded and got == want else "FAIL", version, loaded, got, 
h.calls))
+print("prompt_check: %d/%d pass" % (sum(1 for l, g, w in results.values() if l 
and g == w), len(results)))
+sys.exit(0 if all(l and g == w for l, g, w in results.values()) else 1)
diff --git a/test/odf-validate/smoke13-known.tsv 
b/test/odf-validate/smoke13-known.tsv
new file mode 100644
index 0000000000..384c071e0b
--- /dev/null
+++ b/test/odf-validate/smoke13-known.tsv
@@ -0,0 +1,4 @@
+# Twin pairs (1.2 and 1.3 output of the same producer) whose reload dumps may 
differ. name<TAB>reason. Read by smoke13.py --known.
+AddInChart.ods volatile Calc formula (NOW, TODAY or RAND), as in 
gate-volatile.tsv
+CurrentTime.ods        volatile Calc formula (NOW, TODAY or RAND), as in 
gate-volatile.tsv
+fields.odt     the producer writes draw:style-name on the master page in 1.3 
(a drawing-page style with draw:opacity="0%"); AOO reads it, so BackColor is 
transparent white instead of white
diff --git a/test/odf-validate/smoke13.py b/test/odf-validate/smoke13.py
new file mode 100644
index 0000000000..0fac5fdea2
--- /dev/null
+++ b/test/odf-validate/smoke13.py
@@ -0,0 +1,141 @@
+#**************************************************************
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+#**************************************************************
+
+# usage: smoke13.py <tag> [--root DIR] [--port N] [--stall SECONDS] [--known 
FILE]
+# Import smoke test for ODF 1.3: reloads every document of a foreign corpus 
(tools13/make-foreign-corpus.py) saved in
+# 1.3 and in 1.3 extended mode next to its 1.2 twin (same input, same 
producer), dumps a fixed property set of each
+# (dump7.py, with the gate's stall watchdog) and compares the dumps of every 
twin pair (diff7.py).
+# Fails on a document that hangs or kills the office, on a twin pair that 
differs without an entry in --known
+# (TSV: name<TAB>reason, like gate-volatile.tsv), and when the built-in 
controls misbehave: an unchanged copy must
+# compare equal and a copy with one changed word must compare different.
+import argparse, collections, importlib.util, json, os, re, shutil, 
subprocess, sys, zipfile
+
+D = os.path.dirname(os.path.abspath(__file__))
+spec = importlib.util.spec_from_file_location("gate", D + "/gate.py")
+gate = importlib.util.module_from_spec(spec)
+spec.loader.exec_module(gate)
+PAIRS = (("v12", "v13"), ("v12ext", "v13ext"))
+
+
+def mutate(src, dst):
+    """Copy of an ODF text package with one word appended to the first 
paragraph that has text; False if none."""
+    zi, zo = zipfile.ZipFile(src), zipfile.ZipFile(dst, "w")
+    done = False
+    for it in zi.infolist():
+        d = zi.read(it.filename)
+        if it.filename == "content.xml" and not done:
+            t = d.decode("utf-8")
+            t2, n = re.subn(r">([A-Za-z][^<>]{2,})</text:p>", r">\1 
mutated</text:p>", t, count=1)
+            if n:
+                d, done = t2.encode("utf-8"), True
+        zo.writestr(it, d, compress_type=it.compress_type)
+    zo.close()
+    return done
+
+
+def dumps(src, dest, profile, port, stall, log):
+    r = gate.dump_dir(src, dest, profile, port, stall, log)
+    if "error" in r:
+        sys.exit("dump of %s failed: %s" % (src, r))
+    return r
+
+
+def diff(old, new, out):
+    subprocess.run([sys.executable, "-I", os.path.join(D, "diff7.py"), old, 
new, out], capture_output=True, check=True)
+    return json.load(open(out))
+
+
+def main():
+    ap = argparse.ArgumentParser()
+    ap.add_argument("tag")
+    ap.add_argument("--root", default=os.path.join(D, "runs", "phase3", 
"foreign"))
+    ap.add_argument("--port", type=int, default=2794)
+    ap.add_argument("--stall", type=int, default=300)
+    ap.add_argument("--known")
+    ap.add_argument("--resume", action="store_true", help="reuse the dumps 
already in the run directory of this tag")
+    a = ap.parse_args()
+    run = os.path.join(D, "runs", "phase3", "smoke-" + a.tag)
+    if os.path.exists(run) and not a.resume:
+        sys.exit("%s exists, use a new tag or --resume" % run)
+    os.makedirs(run, exist_ok=True)
+    log = os.path.join(run, "dump.log")
+    known = {}
+    if a.known:
+        known = {l.split("\t")[0]: l.rstrip("\n").split("\t")[1] for l in 
open(a.known) if l.strip() and not l.startswith("#")}
+    problems, report = [], {}
+    stats = {}
+    for mode in sorted({m for p in PAIRS for m in p}):
+        src = os.path.join(a.root, mode)
+        if not os.path.isdir(src):
+            sys.exit("no %s" % src)
+        stats[mode] = dumps(src, os.path.join(run, "dump", mode), "smoke13", 
a.port, a.stall, log)
+    for mode, r in stats.items():
+        errs = []
+        for f in os.listdir(os.path.join(run, "dump", mode)):
+            if f.endswith(".json"):
+                d = json.load(open(os.path.join(run, "dump", mode, f)))
+                if isinstance(d, dict) and (d.get("error") or 
d.get("body_error")):
+                    errs.append((f[:-5], d.get("error") or 
d.get("body_error")))
+        report[mode] = {"documents": r["documents"], "stalls": r["stalls"], 
"errors": errs}
+        problems += ["%s: %s: %s" % (mode, n, e) for n, e in errs]
+    # controls: pick a text document that dumped with characters in it
+    base = None
+    for f in sorted(x for x in os.listdir(os.path.join(run, "dump", "v12")) if 
x.endswith(".json")):
+        d = json.load(open(os.path.join(run, "dump", "v12", f)))
+        if d.get("type") == "writer" and (d.get("body") or {}).get("chars", 0) 
> 20 and f.endswith(".odt.json"):
+            base = os.path.join(a.root, "v12", f[:-5])
+            break
+    ctl = {"old": os.path.join(run, "ctl", "old"), "new": os.path.join(run, 
"ctl", "new")}
+    for p in ctl.values():
+        os.makedirs(p, exist_ok=True)
+    if base is None:
+        problems.append("controls: no text document with characters in the 
corpus")
+    else:
+        shutil.copy2(base, ctl["old"] + "/ctl-same.odt")
+        shutil.copy2(base, ctl["new"] + "/ctl-same.odt")
+        shutil.copy2(base, ctl["old"] + "/ctl-mut.odt")
+        if not mutate(base, ctl["new"] + "/ctl-mut.odt"):
+            problems.append("controls: could not change a word in " + base)
+        for k in ctl:
+            dumps(ctl[k], os.path.join(run, "dump", "ctl-" + k), "smoke13", 
a.port, a.stall, log)
+        c = diff(os.path.join(run, "dump", "ctl-old"), os.path.join(run, 
"dump", "ctl-new"), os.path.join(run, "diff-ctl.json"))
+        if c["ctl-same.odt"]["ndiff"] != 0:
+            problems.append("controls: an unchanged copy compares different 
(%d)" % c["ctl-same.odt"]["ndiff"])
+        if c["ctl-mut.odt"]["ndiff"] == 0:
+            problems.append("controls: a copy with a changed word compares 
equal (the comparison cannot fail)")
+        report["controls"] = {"base": os.path.basename(base), "same_ndiff": 
c["ctl-same.odt"]["ndiff"], "mutated_ndiff": c["ctl-mut.odt"]["ndiff"]}
+    for old, new in PAIRS:
+        dd = diff(os.path.join(run, "dump", old), os.path.join(run, "dump", 
new), os.path.join(run, "diff-%s-%s.json" % (old, new)))
+        ident = sum(1 for v in dd.values() if not v["ndiff"])
+        differing = sorted(k for k, v in dd.items() if v["ndiff"])
+        unexplained = [k for k in differing if k not in known]
+        report["%s vs %s" % (old, new)] = {"compared": len(dd), "identical": 
ident, "different": len(differing), "unexplained": unexplained}
+        problems += ["%s vs %s: %s differs (%d paths, first: %s)" % (old, new, 
k, dd[k]["ndiff"], dd[k]["diffs"][0][:3]) for k in unexplained]
+    json.dump({"report": report, "problems": problems}, open(os.path.join(run, 
"result.json"), "w"), indent=1, default=str)
+    for k, v in report.items():
+        print(k, {x: (y if not isinstance(y, list) else len(y)) for x, y in 
v.items()})
+    for p in problems:
+        print("PROBLEM", p)
+    print("smoke13:", "FAIL" if problems else "PASS")
+    sys.exit(1 if problems else 0)
+
+
+main()
diff --git a/test/odf-validate/tools13/README.md 
b/test/odf-validate/tools13/README.md
new file mode 100644
index 0000000000..99afb09f1f
--- /dev/null
+++ b/test/odf-validate/tools13/README.md
@@ -0,0 +1,57 @@
+# tools13: foreign ODF 1.2 / 1.3 corpus
+
+`make-foreign-corpus.py` converts the same input documents with another 
popular Open Source Office suite
+(headless) once per output version, giving ODF 1.2 / 1.3 twins for an import 
smoke test.
+
+## Version values (observed)
+
+The output version comes from the profile setting 
`/org.openoffice.Office.Common/Save/ODF`, property
+`DefaultVersion`, written to the private profile's 
`user/registrymodifications.xcu`. Found black-box by converting one
+small document per value (`--probe`) and reading `office:version` / 
`manifest:version` and the declared namespaces back
+from the result. "Ext" means loext, calcext, officeooo, drawooo, tableooo and 
the ooo-ms-interop `field` namespace are
+declared.
+
+| value | office:version | extension namespaces |
+|---|---|---|
+| 1 | none (ODF 1.0) | no |
+| 2 | 1.1 (manifest has no version) | no |
+| 4 | 1.2 | no |
+| 8, 9 | 1.2 | yes |
+| 10 | 1.3 | no |
+| 11 | 1.3 | yes |
+| 12 | 1.4 | no |
+| 13 | 1.4 | yes |
+| 0, 3, 5, 6, 7, 14-20 | 1.4 | yes (acts as the default) |
+
+Modes used: `v12` = 4, `v12ext` = 8, `v13` = 10, `v13ext` = 11. Observed on 
one build (26.8.1.1); re-run `--probe`
+for another one. Plain-mode outputs still carry the legacy `ooo`, `oooc`, 
`ooow`, `rpt`, `formx` namespaces.
+
+## Run
+
+    python3 -I make-foreign-corpus.py <path to the other suite's soffice 
binary>
+
+Defaults: 55 documents chosen from `runs/gate-t16f/jobs.tsv` (see the comment 
above `QUOTA` in the script;
+`--list-selection` prints them), output root 
`test/odf-validate/runs/phase3/foreign/`. Other options: `--inputs LIST`
+(one path per line, first tab column used), `--root`, `--modes`, `--batch`, 
`--timeout`,
+`--probe WORKDIR [--probe-range 0-20]` for the value table only.
+
+Outputs: `<root>/<mode>/<name>.<odt|ods|odp|odg>` (legacy inputs map to their 
ODF type, e.g. `x.doc` -> `x.odt`; two
+inputs that would give the same output name are refused), 
`<root>/manifest.tsv` (input, mode, output, office:version,
+manifest:version, ok/fail, note), `<root>/logs/`, and one private profile per 
mode (`<root>/foreign-prof-<mode>`).
+Every output is checked for the expected `office:version` (in content, styles, 
meta, settings) and
+`manifest:version`; plain modes must declare no extension namespaces, ext 
modes must. Any mismatch or missing
+output gives a non-zero exit. Timed-out batches are retried file by file.
+
+Process safety: only processes whose command line carries one of its own 
profile URLs are ever killed, and the
+script refuses to start if such a process already exists.
+
+## Use
+
+A later import smoke test loads `<root>/v13/X` and `<root>/v12/X` (and the 
`ext` pair) and compares what the importer
+produced from each, so a 1.3-specific import difference shows up against the 
1.2 twin of the same source.
+
+## Clean-room note
+
+Only the other suite's output files and its public behaviour are observed. Its 
source code and its installed
+resource/configuration files are not read, and its name is not used in files 
here; the binary path is a command
+line argument only.
diff --git a/test/odf-validate/tools13/make-foreign-corpus.py 
b/test/odf-validate/tools13/make-foreign-corpus.py
new file mode 100644
index 0000000000..035bf70105
--- /dev/null
+++ b/test/odf-validate/tools13/make-foreign-corpus.py
@@ -0,0 +1,358 @@
+#!/usr/bin/env python3
+#**************************************************************
+#
+#  Licensed to the Apache Software Foundation (ASF) under one
+#  or more contributor license agreements.  See the NOTICE file
+#  distributed with this work for additional information
+#  regarding copyright ownership.  The ASF licenses this file
+#  to you under the Apache License, Version 2.0 (the
+#  "License"); you may not use this file except in compliance
+#  with the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing,
+#  software distributed under the License is distributed on an
+#  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+#  KIND, either express or implied.  See the License for the
+#  specific language governing permissions and limitations
+#  under the License.
+#
+#**************************************************************
+
+# Black-box corpus builder: converts the same inputs with another office 
suite's headless binary once per output
+# ODF version (1.2, 1.2 extended, 1.3, 1.3 extended), so an import smoke test 
can load each 1.3 file next to its
+# 1.2 twin. Only observes output files; never reads that suite's source or 
installed configuration.
+#
+#   python3 -I make-foreign-corpus.py <soffice binary> [--inputs LIST] [--root 
DIR] [--modes v12,v13,...]
+#   python3 -I make-foreign-corpus.py <soffice binary> --probe WORKDIR 
[--probe-range 0-20]   (value table only)
+import argparse, os, re, signal, subprocess, sys, time, zipfile
+from urllib.parse import quote
+
+HERE = os.path.dirname(os.path.abspath(__file__))
+TEST = os.path.dirname(HERE)  # test/odf-validate
+JOBS = os.path.join(TEST, "runs", "gate-t16f", "jobs.tsv")
+DEFAULT_ROOT = os.path.join(TEST, "runs", "phase3", "foreign")
+
+# Observed black-box (--probe, one small .odt converted per value; 
office:version and manifest:version read back
+# from the result, extension namespaces = 
loext/calcext/officeooo/drawooo/tableooo/field declared on the root):
+#   1 -> 1.0 (no version attribute)   2 -> 1.1   4 -> 1.2 plain   8 -> 1.2 + 
extensions   9 -> 1.2 + extensions
+#   10 -> 1.3 plain   11 -> 1.3 + extensions   12 -> 1.4 plain   13 -> 1.4 + 
extensions
+#   0, 3, 5, 6, 7 and 14.. all wrote 1.4 + extensions (the default).
+DEFAULT_VERSION_V12 = 4
+DEFAULT_VERSION_V12EXT = 8
+DEFAULT_VERSION_V13 = 10
+DEFAULT_VERSION_V13EXT = 11
+
+# mode -> (DefaultVersion value, expected office:version, whether extension 
namespaces are expected)
+MODES = {
+    "v12": (DEFAULT_VERSION_V12, "1.2", False),
+    "v12ext": (DEFAULT_VERSION_V12EXT, "1.2", True),
+    "v13": (DEFAULT_VERSION_V13, "1.3", False),
+    "v13ext": (DEFAULT_VERSION_V13EXT, "1.3", True),
+}
+OUT_EXT = {"odt": "odt", "sxw": "odt", "doc": "odt", "docx": "odt", "rtf": 
"odt",
+           "ods": "ods", "sxc": "ods", "xls": "ods", "xlsx": "ods",
+           "odp": "odp", "sxi": "odp", "ppt": "odp", "pptx": "odp", "odg": 
"odg", "sxd": "odg"}
+
+# Selection of the built-in list: per source extension a quota, filled 
round-robin across source directories
+# (largest file first within a directory, 2 KB..2 MB) so the spread covers 
many components and not one folder.
+# Quotas total 56 (<= 60); 55 are filled on this tree (the two testinput.odp 
share a name, so one is dropped):
+# native ODF odt 14, ods 9, odp 3, odg 3 (every ods/odp/odg source in the 
list), legacy
+# doc 6, xls 6, ppt 2, sxw 5, sxc 5, sxi 2. docx/rtf are left out: the aim is 
ODF 1.2/1.3 twins of ODF/legacy
+# office sources, and OOXML import adds noise unrelated to ODF 1.3 import. The 
xmlsecurity signed-document
+# variants are near duplicates, so the per-directory round-robin lets at most 
a few of them in.
+QUOTA = [("odt", 14), ("ods", 9), ("odp", 4), ("odg", 3), ("doc", 6), ("xls", 
6), ("ppt", 2),
+         ("sxw", 5), ("sxc", 5), ("sxi", 2)]
+MIN_SIZE, MAX_SIZE = 2048, 2 * 1024 * 1024
+
+EXT_URI_RX = 
re.compile(r"documentfoundation|openoffice\.org/20(?:09|10)/|ooo-ms-interop:xmlns:field")
+
+
+def builtin_selection(jobs=JOBS):
+    seen, byext = set(), {}
+    with open(jobs, encoding="utf8") as fh:
+        for line in fh:
+            p = line.split("\t", 1)[0].strip()
+            if not p or p in seen or not os.path.isfile(p):
+                continue
+            seen.add(p)
+            ext = p.rsplit(".", 1)[-1].lower()
+            sz = os.path.getsize(p)
+            if ext in OUT_EXT and MIN_SIZE <= sz <= MAX_SIZE:
+                byext.setdefault(ext, []).append((p, sz))
+    chosen, used = [], set()
+    for ext, quota in QUOTA:
+        dirs = {}
+        for p, sz in byext.get(ext, []):
+            dirs.setdefault(os.path.dirname(p), []).append((-sz, p))
+        queues = [[p for _, p in sorted(v)] for _, v in sorted(dirs.items())]
+        got = 0
+        while got < quota and any(queues):
+            for q in queues:
+                if not q or got >= quota:
+                    continue
+                p = q.pop(0)
+                key = (os.path.splitext(os.path.basename(p))[0], OUT_EXT[ext])
+                if key in used:
+                    continue
+                used.add(key)
+                chosen.append(p)
+                got += 1
+    return chosen
+
+
+def read_list(path):
+    out = []
+    with open(path, encoding="utf8") as fh:
+        for line in fh:
+            p = line.split("\t", 1)[0].strip()
+            if p and not p.startswith("#"):
+                out.append(os.path.abspath(p))
+    return out
+
+
+def profile_url(d):
+    return "file://" + quote(d)
+
+
+def write_profile(d, value):
+    os.makedirs(os.path.join(d, "user"), exist_ok=True)
+    with open(os.path.join(d, "user", "registrymodifications.xcu"), "w", 
encoding="utf8") as fh:
+        fh.write('<?xml version="1.0" encoding="UTF-8"?>\n'
+                 '<oor:items xmlns:oor="http://openoffice.org/2001/registry"; 
xmlns:xs="http://www.w3.org/2001/XMLSchema";'
+                 ' xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";>\n'
+                 '<item 
oor:path="/org.openoffice.Office.Common/Save/ODF"><prop 
oor:name="DefaultVersion" oor:op="fuse">'
+                 '<value>%d</value></prop></item>\n</oor:items>\n' % value)
+
+
+def own_pids(profile_dirs):
+    # Processes whose command line carries exactly one of our profile URLs (as 
a whole argument).
+    toks = ["-env:UserInstallation=" + profile_url(d) for d in profile_dirs]
+    r = subprocess.run(["ps", "-axww", "-o", "pid=,command="], 
capture_output=True, text=True)
+    pids = []
+    for line in r.stdout.splitlines():
+        line = line.strip()
+        if not line:
+            continue
+        pid, _, cmd = line.partition(" ")
+        pid = int(pid)
+        if pid == os.getpid():
+            continue
+        for t in toks:
+            if re.search(re.escape(t) + r"(?:\s|$)", cmd):
+                pids.append(pid)
+                break
+    return pids
+
+
+def kill_own(profile_dirs):
+    pids = own_pids(profile_dirs)
+    for sig in (signal.SIGTERM, signal.SIGKILL):
+        for pid in pids:
+            try:
+                os.kill(pid, sig)
+            except ProcessLookupError:
+                pass
+        deadline = time.time() + 8
+        while time.time() < deadline and own_pids(profile_dirs):
+            time.sleep(0.5)
+        pids = own_pids(profile_dirs)
+        if not pids:
+            return
+
+
+def run_convert(soffice, prof, outext, outdir, files, timeout, log):
+    cmd = [soffice, "-env:UserInstallation=" + profile_url(prof), 
"--headless", "--norestore",
+           "--convert-to", outext, "--outdir", outdir] + files
+    with open(log, "ab") as lf:
+        lf.write(("\n$ " + " ".join(cmd) + "\n").encode())
+        lf.flush()
+        p = subprocess.Popen(cmd, stdout=lf, stderr=subprocess.STDOUT, 
stdin=subprocess.DEVNULL)
+        try:
+            p.wait(timeout=timeout)
+            timed_out = False
+        except subprocess.TimeoutExpired:
+            timed_out = True
+    # soffice.bin may outlive the launcher briefly; only our own profile is 
touched
+    t0 = time.time()
+    while own_pids([prof]) and time.time() - t0 < 20 and not timed_out:
+        time.sleep(0.5)
+    if timed_out or own_pids([prof]):
+        kill_own([prof])
+    return timed_out
+
+
+def inspect(path):
+    # -> (office:version of content.xml, manifest:version, declared extension 
namespace prefixes, problems)
+    problems, vers, ext_ns, mv = [], {}, set(), None
+    try:
+        z = zipfile.ZipFile(path)
+        for n in ("content.xml", "styles.xml", "meta.xml", "settings.xml"):
+            if n not in z.namelist():
+                continue
+            head = z.read(n)[:20000].decode("utf8", "replace")
+            m = re.search(r'office:version="([^"]*)"', head)
+            vers[n] = m.group(1) if m else None
+            if n in ("content.xml", "styles.xml"):
+                for pre, uri in re.findall(r'xmlns:([\w.-]+)="([^"]*)"', head):
+                    if EXT_URI_RX.search(uri):
+                        ext_ns.add(pre)
+        m = re.search(r'manifest:version="([^"]*)"', 
z.read("META-INF/manifest.xml").decode("utf8", "replace"))
+        mv = m.group(1) if m else None
+    except Exception as e:  # unreadable output is a failure, reported by the 
caller
+        return None, None, set(), ["unreadable: %s" % e]
+    cv = vers.get("content.xml")
+    if len(set(vers.values())) > 1:
+        problems.append("office:version differs between parts: %s" % vers)
+    return cv, mv, ext_ns, problems
+
+
+def probe(soffice, work, rng):
+    lo, hi = rng
+    src = os.path.join(work, "probe-input")
+    os.makedirs(src, exist_ok=True)
+    doc = os.path.join(src, "probe.odt")
+    # minimal ODF 1.2 text document built here, so the probe needs no outside 
file
+    with zipfile.ZipFile(doc, "w") as z:
+        z.writestr("mimetype", "application/vnd.oasis.opendocument.text", 
compress_type=zipfile.ZIP_STORED)
+        z.writestr("META-INF/manifest.xml",
+                   '<?xml version="1.0" encoding="UTF-8"?>\n<manifest:manifest 
xmlns:manifest="urn:oasis:names:tc:opendocument:xmlns:manifest:1.0" 
manifest:version="1.2">'
+                   '<manifest:file-entry manifest:full-path="/" 
manifest:media-type="application/vnd.oasis.opendocument.text"/>'
+                   '<manifest:file-entry manifest:full-path="content.xml" 
manifest:media-type="text/xml"/></manifest:manifest>')
+        z.writestr("content.xml",
+                   '<?xml version="1.0" 
encoding="UTF-8"?>\n<office:document-content 
xmlns:office="urn:oasis:names:tc:opendocument:xmlns:office:1.0"'
+                   ' 
xmlns:text="urn:oasis:names:tc:opendocument:xmlns:text:1.0" 
office:version="1.2"><office:body><office:text>'
+                   
'<text:p>probe</text:p></office:text></office:body></office:document-content>')
+    dirs = [os.path.join(work, "probe-prof-%d" % n) for n in range(lo, hi + 1)]
+    if own_pids(dirs):
+        sys.exit("refusing to start: an office using one of the probe profiles 
is running")
+    print("value\toffice:version\tmanifest:version\textension namespaces")
+    for n, prof in zip(range(lo, hi + 1), dirs):
+        write_profile(prof, n)
+        out = os.path.join(work, "probe-out-%d" % n)
+        os.makedirs(out, exist_ok=True)
+        f = os.path.join(out, "probe.odt")
+        if os.path.exists(f):
+            os.remove(f)
+        run_convert(soffice, prof, "odt", out, [doc], 300, os.path.join(work, 
"probe.log"))
+        if not os.path.exists(f):
+            print("%d\tNO OUTPUT" % n)
+            continue
+        cv, mv, ext, _ = inspect(f)
+        print("%d\t%s\t%s\t%s" % (n, cv, mv, ",".join(sorted(ext)) or "-"))
+
+
+def main():
+    ap = argparse.ArgumentParser(description=__doc__, 
formatter_class=argparse.RawDescriptionHelpFormatter)
+    ap.add_argument("soffice", help="the other suite's soffice binary")
+    ap.add_argument("--inputs", help="file with one input path per line (first 
tab column used; '#' comments)")
+    ap.add_argument("--root", default=DEFAULT_ROOT, help="output root (default 
%(default)s)")
+    ap.add_argument("--modes", default=",".join(MODES), help="comma list of 
modes (default all)")
+    ap.add_argument("--batch", type=int, default=10, help="files per soffice 
call (default %(default)s)")
+    ap.add_argument("--timeout", type=int, default=300, help="seconds per 
soffice call (default %(default)s)")
+    ap.add_argument("--list-selection", action="store_true", help="print the 
built-in selection and exit")
+    ap.add_argument("--probe", metavar="WORKDIR", help="only print the 
DefaultVersion value table, using WORKDIR")
+    ap.add_argument("--probe-range", default="0-20")
+    a = ap.parse_args()
+    soffice = os.path.abspath(a.soffice)
+    if a.list_selection:
+        print("\n".join(builtin_selection()))
+        return 0
+    if not os.access(soffice, os.X_OK):
+        sys.exit("not executable: %s" % soffice)
+    if a.probe:
+        lo, hi = map(int, a.probe_range.split("-"))
+        probe(soffice, os.path.abspath(a.probe), (lo, hi))
+        return 0
+
+    modes = a.modes.split(",")
+    for m in modes:
+        if m not in MODES:
+            sys.exit("unknown mode %s" % m)
+    root = os.path.abspath(a.root)
+    profs = {m: os.path.join(root, "foreign-prof-" + m) for m in modes}
+    running = own_pids(list(profs.values()))
+    if running:
+        sys.exit("refusing to start: office process(es) %s already use one of 
our profile directories" % running)
+
+    inputs = read_list(a.inputs) if a.inputs else builtin_selection()
+    inputs = [p for p in inputs if os.path.isfile(p)]
+    keys = {}
+    for p in inputs:
+        ext = p.rsplit(".", 1)[-1].lower()
+        if ext not in OUT_EXT:
+            sys.exit("unsupported input type: %s" % p)
+        k = (os.path.splitext(os.path.basename(p))[0], OUT_EXT[ext])
+        if k in keys:
+            sys.exit("output name collision %s.%s: %s and %s" % (k[0], k[1], 
keys[k], p))
+        keys[k] = p
+    if not inputs:
+        sys.exit("no input files")
+    os.makedirs(os.path.join(root, "logs"), exist_ok=True)
+    print("%d inputs, modes %s, root %s" % (len(inputs), ",".join(modes), 
root), flush=True)
+
+    rows, short = [], False
+    for m in modes:
+        value, want, want_ext = MODES[m]
+        write_profile(profs[m], value)
+        outdir = os.path.join(root, m)
+        os.makedirs(outdir, exist_ok=True)
+        log = os.path.join(root, "logs", m + ".log")
+        todo = {}  # outext -> [(input, expected output)]
+        for p in inputs:
+            oe = OUT_EXT[p.rsplit(".", 1)[-1].lower()]
+            o = os.path.join(outdir, os.path.splitext(os.path.basename(p))[0] 
+ "." + oe)
+            if os.path.exists(o):
+                os.remove(o)  # a stale file must not pass for a fresh 
conversion
+            todo.setdefault(oe, []).append((p, o))
+        for oe, items in todo.items():
+            for i in range(0, len(items), a.batch):
+                chunk = items[i:i + a.batch]
+                to = run_convert(soffice, profs[m], oe, outdir, [p for p, _ in 
chunk], a.timeout, log)
+                missing = [(p, o) for p, o in chunk if not os.path.exists(o)]
+                if (to or missing) and len(chunk) > 1:
+                    print("  %s: %d of %d missing after batch%s; retrying one 
by one" %
+                          (m, len(missing), len(chunk), " (timeout)" if to 
else ""), flush=True)
+                    for p, o in missing:
+                        run_convert(soffice, profs[m], oe, outdir, [p], 
a.timeout, log)
+        n_out = n_ok = 0
+        for p in inputs:
+            oe = OUT_EXT[p.rsplit(".", 1)[-1].lower()]
+            o = os.path.join(outdir, os.path.splitext(os.path.basename(p))[0] 
+ "." + oe)
+            if not os.path.exists(o):
+                rows.append((p, m, o, "", "", "fail", "no output"))
+                continue
+            n_out += 1
+            cv, mv, ext, probs = inspect(o)
+            if cv != want:
+                probs.append("office:version %s, expected %s" % (cv, want))
+            if mv != want:
+                probs.append("manifest:version %s, expected %s" % (mv, want))
+            if want_ext and not ext:
+                probs.append("no extension namespaces declared")
+            if not want_ext and ext:
+                probs.append("unexpected extension namespaces %s" % 
",".join(sorted(ext)))
+            if not probs:
+                n_ok += 1
+            rows.append((p, m, o, cv or "", mv or "", "fail" if probs else 
"ok", "; ".join(probs)))
+        print("%s (DefaultVersion %d): %d/%d outputs, %d verified as 
office:version %s" %
+              (m, value, n_out, len(inputs), n_ok, want), flush=True)
+        if n_out < len(inputs) or n_ok < n_out:
+            short = True
+    with open(os.path.join(root, "manifest.tsv"), "w", encoding="utf8") as fh:
+        
fh.write("input\tmode\toutput\toffice:version\tmanifest:version\tstatus\tnote\n")
+        for r in rows:
+            fh.write("\t".join(r) + "\n")
+    bad = [r for r in rows if r[5] != "ok"]
+    for r in bad[:20]:
+        print("FAIL %s [%s]: %s" % (r[0], r[1], r[6]))
+    if own_pids(list(profs.values())):
+        print("warning: office processes of ours still running, killing them")
+        kill_own(list(profs.values()))
+    print("manifest: %s (%d rows, %d not ok)" % (os.path.join(root, 
"manifest.tsv"), len(rows), len(bad)))
+    return 1 if (short or bad) else 0
+
+
+if __name__ == "__main__":
+    sys.exit(main())

Reply via email to