1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3# Copyright (c) 2021 Huawei Device Co., Ltd.
4# Licensed under the Apache License, Version 2.0 (the "License");
5# you may not use this file except in compliance with the License.
6# You may obtain a copy of the License at
7#
8#     http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS,
12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13# See the License for the specific language governing permissions and
14# limitations under the License.
15
16import optparse
17import os
18import sys
19
20sys.path.append(os.path.join(os.path.dirname(__file__), os.pardir, os.pardir))
21from scripts.util import build_utils  # noqa: E402
22
23PORTAL_CONTENTS = """
24<meta http-equiv="REFRESH" content="0;URL=%s">
25"""
26
27
28def parse_args(args):
29    args = build_utils.expand_file_args(args)
30
31    parser = optparse.OptionParser()
32    build_utils.add_depfile_option(parser)
33    parser.add_option('--portal-path', help='path to portal html')
34    parser.add_option('--doxygen-output', help='ndk doxygen outputs')
35    parser.add_option('--record-path', help='path to md5.stamp file')
36    parser.add_option('--docs-archive', help='path of docs archive zipfile')
37    parser.add_option(
38        '--archive-doc',
39        default=False,
40        action='store_true',
41        help='whether to archive doc or not')
42
43    options, _ = parser.parse_args(args)
44    return options
45
46
47def write_portal_and_archive(options):
48    # If user doesn't have doxygen installed, then no document is generated,
49    # no need to generate portal html
50    if not os.path.exists(options.doxygen_output):
51        return
52
53    contents = [
54        PORTAL_CONTENTS % os.path.relpath(options.doxygen_output,
55                                          os.path.dirname(options.portal_path))
56    ]
57    with open(options.portal_path, 'w') as f:
58        f.write('\n'.join(contents))
59
60    if options.archive_doc:
61        os.makedirs(os.path.dirname(options.docs_archive), exist_ok=True)
62        build_utils.zip_dir(
63            options.docs_archive,
64            os.path.dirname(options.portal_path),
65            compress_fn=lambda _: True)
66
67
68def main(args):
69    options = parse_args(args)
70
71    depfile_deps = set()
72    if os.path.exists(options.doxygen_output):
73        depfile_deps.add(options.doxygen_output)
74    outputs = [options.portal_path]
75    if options.docs_archive:
76        outputs.append(options.docs_archive)
77
78    build_utils.call_and_write_depfile_if_stale(
79        lambda: write_portal_and_archive(options),
80        options,
81        depfile_deps=depfile_deps,
82        input_paths=depfile_deps,
83        input_strings=[options.archive_doc],
84        output_paths=(outputs),
85        record_path=options.record_path,
86        force=False,
87        add_pydeps=False)
88
89
90if __name__ == '__main__':
91    sys.exit(main(sys.argv[1:]))
92