1#!/usr/bin/env python 2# -*- coding: utf-8 -*- 3 4# 5# Copyright (c) 2020 Huawei Device Co., Ltd. 6# Licensed under the Apache License, Version 2.0 (the "License"); 7# you may not use this file except in compliance with the License. 8# You may obtain a copy of the License at 9# 10# http://www.apache.org/licenses/LICENSE-2.0 11# 12# Unless required by applicable law or agreed to in writing, software 13# distributed under the License is distributed on an "AS IS" BASIS, 14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 15# See the License for the specific language governing permissions and 16# limitations under the License. 17# 18 19import os 20import sys 21import shutil 22import re 23import tempfile 24import argparse 25import distutils.spawn 26sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 27from utils import check_output, makedirs # noqa: E402 28 29 30def gen_doc(args): 31 html_output_dir = args.output 32 contents = None 33 with tempfile.NamedTemporaryFile( 34 suffix=os.path.basename(args.doxygen_file)) as doxygen_file: 35 shutil.copyfile(args.doxygen_file, doxygen_file.name) 36 with open(doxygen_file.name, 'r') as file: 37 contents = file.read() 38 if contents is None: 39 raise Exception('Failed to read %s' % args.doxygen_file) 40 41 keys = { 42 '%VERSION%': 43 args.version, 44 '%EXCLUDE_DIR%': 45 args.exclude_dir, 46 '%OUTPUT_DIR%': 47 os.path.relpath(html_output_dir, args.working_dir) 48 } 49 for (key, value) in list(keys.items()): 50 value = value.replace('\\', '\\\\') 51 contents = re.sub(key, value, contents) 52 53 with open(doxygen_file.name, 'w') as file: 54 file.write(contents) 55 56 old_cwd = os.getcwd() 57 try: 58 # if no ndk headers exist, return. 59 if os.path.exists(args.working_dir) is not True: 60 print("no ndk headers exist, return") 61 return 62 os.chdir(args.working_dir) 63 64 doxygen_path = distutils.spawn.find_executable('doxygen') 65 if doxygen_path is None: 66 print( 67 "Warning: Failed to find doxygen, please install doxygen " 68 "with \"sudo apt-get install doxygen\" on Ubuntu" 69 ) 70 return 71 html_output_dir = os.path.relpath(html_output_dir, 72 args.working_dir) 73 if not os.path.exists(html_output_dir): 74 makedirs(html_output_dir) 75 cmd = [doxygen_path, doxygen_file.name] 76 check_output(cmd) 77 78 finally: 79 os.chdir(old_cwd) 80 81 82def main(): 83 parser = argparse.ArgumentParser(description='Generate ndk docs') 84 parser.add_argument('--version', help='OHOS version', required=True) 85 parser.add_argument('--exclude_dir', help='doxygen exclude dirs', 86 required=True) 87 parser.add_argument('--doxygen-file', help='doxygen config file') 88 parser.add_argument('--output', help='output index.html') 89 parser.add_argument( 90 '--working-dir', 91 help='the directory where doxygen command will be executed') 92 args = parser.parse_args() 93 94 return gen_doc(args) 95 96 97if __name__ == "__main__": 98 sys.exit(main()) 99