# 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__)

[((1525, 1575), 'os.path.join', 'os.path.join', (['package_dir', '""".."""', '""".."""', '"""README.md"""'], {}), "(package_dir, '..', '..', 'README.md')\n", (1537, 1575), False, 'import os\n'), ((1747, 1753), 'pdoc.cli.main', 'main', ([], {}), '()\n', (1751, 1753), False, 'from pdoc.cli import main\n'), ((1773, 1806), 'os.path.join', 'os.path.join', (['package_dir', '"""docs"""'], {}), "(package_dir, 'docs')\n", (1785, 1806), False, 'import os\n'), ((281, 307), 'os.path.realpath', 'os.path.realpath', (['__file__'], {}), '(__file__)\n', (297, 307), False, 'import os\n'), ((743, 771), 'os.makedirs', 'os.makedirs', (['target_dir_path'], {}), '(target_dir_path)\n', (754, 771), False, 'import os\n'), ((974, 1001), 'os.path.basename', 'os.path.basename', (['file_path'], {}), '(file_path)\n', (990, 1001), False, 'import os\n'), ((665, 696), 'os.path.exists', 'os.path.exists', (['target_dir_path'], {}), '(target_dir_path)\n', (679, 696), False, 'import os\n'), ((701, 731), 'os.path.isdir', 'os.path.isdir', (['target_dir_path'], {}), '(target_dir_path)\n', (714, 731), False, 'import os\n'), ((1390, 1432), 'os.path.join', 'os.path.join', (['package_dir', 'output_filename'], {}), '(package_dir, output_filename)\n', (1402, 1432), False, 'import os\n'), ((825, 850), 'os.path.exists', 'os.path.exists', (['file_path'], {}), '(file_path)\n', (839, 850), False, 'import os\n'), ((855, 880), 'os.path.isfile', 'os.path.isfile', (['file_path'], {}), '(file_path)\n', (869, 880), False, 'import os\n')]