# Copyright 2017-present Open Networking Foundation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
   xosutil/autodiscover_version.py

   This module implements support for recursively searching for a VERSION file and extracting the version number from
   it. Search starts from the directory of this file, or if autodiscover_version_of_caller is called, the directory
   of the caller.
"""

from __future__ import absolute_import

import inspect
import os


def autodiscover_version(caller_filename=None, save_to=None, max_parent_depth=None):
    """ walk back along the path to the current module, searching for a VERSION file """
    if not caller_filename:
        caller_filename = os.path.realpath(__file__)
    file_path = os.path.abspath(os.path.dirname(caller_filename))
    cur_path = file_path
    while True:
        version_fn = os.path.join(cur_path, "VERSION")
        if os.path.exists(version_fn):
            version = open(version_fn, "rt").readline().strip()
            if save_to:
                f = open(os.path.join(file_path, save_to), "wt")
                f.write("# This file is autogenerated. Do not edit.\n")
                f.write("__version__ = '%s'\n" % version)
                f.close()
            return version

        # limit_parent_depth can be used to limit how far back we search the tree for a VERSION file.
        if max_parent_depth is not None:
            if max_parent_depth <= 0:
                return None
            max_parent_depth -= 1

        (cur_path, remainder) = os.path.split(cur_path)
        if not remainder:
            return None


def autodiscover_version_of_caller(*args, **kwargs):
    frame = inspect.stack()[1]
    module = inspect.getmodule(frame[0])
    return autodiscover_version(module.__file__, *args, **kwargs)


def autodiscover_version_of_main(*args, **kwargs):
    import __main__

    if hasattr(__main__, "__file__"):
        return autodiscover_version(__main__.__file__, *args, **kwargs)
    else:
        return None
