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"""Locates the external tools the checkers need: an environment variable first, then the verified fetch cache (aootest.fetch).""" 23import os 24import shutil 25 26from aootest import fetch 27 28 29def _file(var): 30 p = os.environ.get(var) 31 return p if p and os.path.isfile(p) else None 32 33 34def _cached(name): 35 p = fetch.cached_path(name) 36 return str(p) if p else None 37 38 39def java(): 40 return shutil.which(os.environ.get("AOO_JAVA", "java")) 41 42 43def jing(): 44 """Path of a jing jar (AOO_JING, else the fetch cache), or None.""" 45 return _file("AOO_JING") or _cached("jing") 46 47 48def odfvalidator(): 49 """Path of the odfvalidator jar with dependencies (AOO_ODFVALIDATOR, else the fetch cache), or None.""" 50 return _file("AOO_ODFVALIDATOR") or _cached("odfvalidator") 51 52 53def schema_dir(): 54 """Directory with the OASIS ODF 1.2, 1.3 and 1.4 RELAX NG schemas (AOO_SCHEMA_DIR, else the fetch cache), or None when files are missing.""" 55 d = os.environ.get("AOO_SCHEMA_DIR") 56 need = ("OpenDocument-v1.2-os-schema.rng", "OpenDocument-v1.2-os-manifest-schema.rng", "OpenDocument-v1.2-os-dsig-schema.rng", 57 "OpenDocument-v1.3-schema.rng", "OpenDocument-v1.3-manifest-schema.rng", "OpenDocument-v1.4-schema.rng", "OpenDocument-v1.4-manifest-schema.rng") 58 if d and all(os.path.isfile(os.path.join(d, f)) for f in need): 59 return d 60 cached = fetch.group_dir("schemas") 61 return str(cached) if cached else None 62 63 64def jing_command(): 65 """[java, -jar, jar, -i] or None when java or jing is missing.""" 66 j, jar = java(), jing() 67 return [j, "-jar", jar, "-i"] if j and jar else None 68