#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# -----------------------------------------------------------------------------------------------------------
# Copyright (c) 2026 Huawei Technologies Co., Ltd. All Rights Reserved.
# This program is free software, you can redistribute it and/or modify it under the terms and conditions of
# CANN Open Software License Agreement Version 2.0 (the "License").
# Please refer to the License for details. You may not use this file except in compliance with the License.
# THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
# INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
# See LICENSE in the root of the software repository for the full text of the License.
# -----------------------------------------------------------------------------------------------------------

"""main function to convert user scripts"""

import os
import ast
import pandas as pd
import util_global
from conver_by_ast import conver_ast
from file_op import mkdir
from file_op import mkdir_and_copyfile
from file_op import write_report_terminator
from file_op import abs_join
from file_op import get_api_statistic
from file_op import adjust_index
from util import check_path_length
from util import log_warning
from visit_by_ast import preprocess_visit


def conver():
    """The entry point to convert Tensorflow script"""
    print("Begin conver, input file: " + util_global.get_value('input') + '\n')
    out_path = util_global.get_value('output')
    dst_path = os.path.split(util_global.get_value('input').rstrip('\\/'))[-1]
    dst_path_new = dst_path + util_global.get_value('timestap')
    conver_path = os.walk(util_global.get_value('input'))
    report_dir = util_global.get_value('report')
    mkdir(report_dir)
    report_xlsx = os.path.join(report_dir, 'api_analysis_report.xlsx')
    util_global.set_value('generate_dir_report', pd.DataFrame())


    for path, _, file_list in os.walk(util_global.get_value('input')):
        for file_name in file_list:
            file_path = os.path.join(path, file_name).replace('\\', '/')
            if file_path.endswith('.py'):
                preprocess_visit(file_path)

    for path, _, file_list in conver_path:
        for file_name in file_list:
            out_path_dst = abs_join(dst_path_new, path.split(util_global.get_value('input'))[1])
            file_path = os.path.join(path, file_name).replace('\\', '/')
            if not check_path_length(file_path):
                content = "".join(["The file:", file_path, " length is invalid, skip convert."])
                log_warning(content)
                continue
            content = "".join(["Begin conver file: ", file_path])
            print(content)
            threshold_file_size = 10 * 1024 * 1024
            if file_name.endswith(".py"):
                if os.path.getsize(file_path) > threshold_file_size:
                    content = "".join(["The file:", file_path, " size is over 10M, skip convert."])
                    log_warning(content)
                    continue
                util_global.set_value('path', file_path)
                mkdir(os.path.join(out_path, out_path_dst))
                conver_ast(path, out_path_dst, file_name)
                if util_global.get_value('need_conver', False):
                    content = "".join(["Finish conver file: ", file_path, '\n'])
                    print(content)
                    write_report_terminator(content)
                else:
                    mkdir_and_copyfile(path, abs_join(out_path, out_path_dst), file_name)
            else:
                mkdir_and_copyfile(path, abs_join(out_path, out_path_dst), file_name)

    adjust_index()
    analysis_report = util_global.get_value('generate_dir_report')
    if analysis_report.empty:
        print('No api data in the report')
    else:
        analysis_report.to_excel(report_xlsx, index=True)
        get_api_statistic(analysis_report)
    print("Finish conver, output file: " + out_path + "; report file: " + util_global.get_value('report'))