xref: /trunk/test/odf-validate/convert.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: convert.py <port> <jobs.tsv> <done.tsv> [limit] [--version-only]
23# jobs.tsv: "<source>\t<output.od?>" per line; done.tsv gets START/OK/FAIL lines and is resumable
24import sys, os, uno
25from unohelp import connect, odf_version, desktop, load, pv, FILTERS
26
27port, jobs, donelog = int(sys.argv[1]), sys.argv[2], sys.argv[3]
28ctx = connect(port)
29print("DefaultVersion =", odf_version(ctx), flush=True)
30if "--version-only" in sys.argv:
31    sys.exit(0)
32limit = int(sys.argv[4]) if len(sys.argv) > 4 else 10**9
33desk = desktop(ctx)
34done = {l.split("\t")[0] for l in open(donelog)} if os.path.exists(donelog) else set()
35n = 0
36with open(donelog, "a") as log:
37    for line in open(jobs):
38        src, out = line.rstrip("\n").split("\t")
39        if src in done:
40            continue
41        if n >= limit:
42            break
43        n += 1
44        log.write(src + "\tSTART\n"); log.flush()
45        try:
46            doc = load(desk, src)
47            os.makedirs(os.path.dirname(out), exist_ok=True)
48            doc.storeToURL(uno.systemPathToFileUrl(out), (pv("FilterName", FILTERS[out.rsplit(".", 1)[1]]),))
49            doc.close(True)
50            log.write(src + "\tOK\n")
51        except Exception as e:
52            log.write(src + "\tFAIL " + repr(e).replace("\n", " ")[:200] + "\n")
53            log.flush()
54            if "disposed" in repr(e):
55                sys.exit(3)
56        log.flush()
57