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 stat 23import re 24import tempfile 25import argparse 26import distutils.spawn 27sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir)) 28from utils import check_output, makedirs # noqa: E402 29 30 31def gen_doc(args): 32 html_output_dir = args.output 33 contents = None 34 with tempfile.NamedTemporaryFile( 35 suffix=os.path.basename(args.doxygen_file)) as doxygen_file: 36 shutil.copyfile(args.doxygen_file, doxygen_file.name) 37 with open(doxygen_file.name, 'r') as file: 38 contents = file.read() 39 if contents is None: 40 raise Exception('Failed to read %s' % args.doxygen_file) 41 42 keys = { 43 '%VERSION%': 44 args.version, 45 '%EXCLUDE_DIR%': 46 args.exclude_dir, 47 '%OUTPUT_DIR%': 48 os.path.relpath(html_output_dir, args.working_dir) 49 } 50 for (key, value) in list(keys.items()): 51 value = value.replace('\\', '\\\\') 52 contents = re.sub(key, value, contents) 53 54 with os.fdopen(os.open(doxygen_file.name, 55 os.O_RDWR | os.O_CREAT, stat.S_IWUSR | stat.S_IRUSR), 56 'w', encoding='utf-8') as file: 57 file.write(contents) 58 59 old_cwd = os.getcwd() 60 try: 61 # if no ndk headers exist, return. 62 if os.path.exists(args.working_dir) is not True: 63 print("no ndk headers exist, return") 64 return 65 os.chdir(args.working_dir) 66 67 doxygen_path = distutils.spawn.find_executable('doxygen') 68 if doxygen_path is None: 69 print( 70 "Warning: Failed to find doxygen, please install doxygen " 71 "with \"sudo apt-get install doxygen\" on Ubuntu" 72 ) 73 return 74 html_output_dir = os.path.relpath(html_output_dir, 75 args.working_dir) 76 if not os.path.exists(html_output_dir): 77 makedirs(html_output_dir) 78 cmd = [doxygen_path, doxygen_file.name] 79 check_output(cmd) 80 81 finally: 82 os.chdir(old_cwd) 83 84 85def main(): 86 parser = argparse.ArgumentParser(description='Generate ndk docs') 87 parser.add_argument('--version', help='OHOS version', required=True) 88 parser.add_argument('--exclude_dir', help='doxygen exclude dirs', 89 required=True) 90 parser.add_argument('--doxygen-file', help='doxygen config file') 91 parser.add_argument('--output', help='output index.html') 92 parser.add_argument( 93 '--working-dir', 94 help='the directory where doxygen command will be executed') 95 args = parser.parse_args() 96 97 return gen_doc(args) 98 99 100if __name__ == "__main__": 101 sys.exit(main()) 102