# Copyright © 2019 Arm Ltd. All rights reserved.
# Copyright 2020 NXP
# SPDX-License-Identifier: MIT

import os
import tarfile

import pyarmnn as ann
import shutil

from typing import List, Union

from pdoc.cli import main

package_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)), '..')

def __copy_file_to_dir(file_paths: Union[List[str], str], target_dir_path: str):
    """Copies multiple files to a directory.

    Args:
        file_paths (Union[List(str)]): List of files to copy
        target_dir_path (str): Target directory.

    Returns:
        None
    """
    
    file_paths = [] + file_paths

    if not (os.path.exists(target_dir_path) and os.path.isdir(target_dir_path)):
        os.makedirs(target_dir_path)

    for file_path in file_paths:
        if not (os.path.exists(file_path) and os.path.isfile(file_path)):
            raise RuntimeError('Not a file: {}'.format(file_path))

        file_name = os.path.basename(file_path)
        shutil.copyfile(file_path, os.path.join(str(target_dir_path), file_name))


def archive_docs(path: str, version: str):
    """Creates an archive.

    Args:
        path (str): Path which will be archived.
        version (str): Version of Arm NN.

    Returns:
        None
    """
    
    output_filename = f'pyarmnn_docs-{version}.tar'

    with tarfile.open(os.path.join(package_dir, output_filename), "w") as tar:
        tar.add(path)


if __name__ == "__main__":
    readme_filename = os.path.join(package_dir, '..', '..', 'README.md')
    with open(readme_filename, 'r') as readme_file:
        top_level_pyarmnn_doc = ''.join(readme_file.readlines())
        ann.__doc__ = top_level_pyarmnn_doc

    main()
    target_path = os.path.join(package_dir, 'docs')
    archive_docs(target_path, ann.__version__)

[((59, 22, 59, 72), 'os.path.join', 'os.path.join', ({(59, 35, 59, 46): 'package_dir', (59, 48, 59, 52): '""".."""', (59, 54, 59, 58): '""".."""', (59, 60, 59, 71): '"""README.md"""'}, {}), "(package_dir, '..', '..', 'README.md')", False, 'import os\n'), ((64, 4, 64, 10), 'pdoc.cli.main', 'main', ({}, {}), '()', False, 'from pdoc.cli import main\n'), ((65, 18, 65, 51), 'os.path.join', 'os.path.join', ({(65, 31, 65, 42): 'package_dir', (65, 44, 65, 50): '"""docs"""'}, {}), "(package_dir, 'docs')", False, 'import os\n'), ((15, 43, 15, 69), 'os.path.realpath', 'os.path.realpath', ({(15, 60, 15, 68): '__file__'}, {}), '(__file__)', False, 'import os\n'), ((31, 8, 31, 36), 'os.makedirs', 'os.makedirs', ({(31, 20, 31, 35): 'target_dir_path'}, {}), '(target_dir_path)', False, 'import os\n'), ((37, 20, 37, 47), 'os.path.basename', 'os.path.basename', ({(37, 37, 37, 46): 'file_path'}, {}), '(file_path)', False, 'import os\n'), ((30, 12, 30, 43), 'os.path.exists', 'os.path.exists', ({(30, 27, 30, 42): 'target_dir_path'}, {}), '(target_dir_path)', False, 'import os\n'), ((30, 48, 30, 78), 'os.path.isdir', 'os.path.isdir', ({(30, 62, 30, 77): 'target_dir_path'}, {}), '(target_dir_path)', False, 'import os\n'), ((54, 22, 54, 64), 'os.path.join', 'os.path.join', ({(54, 35, 54, 46): 'package_dir', (54, 48, 54, 63): 'output_filename'}, {}), '(package_dir, output_filename)', False, 'import os\n'), ((34, 16, 34, 41), 'os.path.exists', 'os.path.exists', ({(34, 31, 34, 40): 'file_path'}, {}), '(file_path)', False, 'import os\n'), ((34, 46, 34, 71), 'os.path.isfile', 'os.path.isfile', ({(34, 61, 34, 70): 'file_path'}, {}), '(file_path)', False, 'import os\n')]