Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 1 | |
| 2 | # Copyright 2017-present Open Networking Foundation |
| 3 | # |
| 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 | |
| 16 | """ |
| 17 | xosutil/autodiscover_version.py |
| 18 | |
| 19 | This module implements support for recursively searching for a VERSION file and extracting the version number from |
| 20 | it. Search starts from the directory of this file, or if autodiscover_version_of_caller is called, the directory |
| 21 | of the caller. |
| 22 | """ |
| 23 | |
| 24 | import inspect |
| 25 | import os |
| 26 | |
Scott Baker | e02aa69 | 2018-03-23 09:54:14 -0700 | [diff] [blame] | 27 | def autodiscover_version(caller_filename=None, save_to=None, max_parent_depth=None): |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 28 | """ walk back along the path to the current module, searching for a VERSION file """ |
| 29 | if not caller_filename: |
| 30 | caller_filename = os.path.realpath(__file__) |
| 31 | file_path = os.path.abspath(os.path.dirname(caller_filename)) |
| 32 | cur_path = file_path |
| 33 | while True: |
| 34 | version_fn = os.path.join(cur_path, "VERSION") |
| 35 | if os.path.exists(version_fn): |
| 36 | version = open(version_fn, "rt").readline().strip() |
| 37 | if save_to: |
| 38 | f = open(os.path.join(file_path, save_to), "wt") |
| 39 | f.write("# This file is autogenerated. Do not edit.\n") |
| 40 | f.write("__version__ = '%s'\n" % version) |
| 41 | f.close() |
| 42 | return version |
| 43 | |
Scott Baker | e02aa69 | 2018-03-23 09:54:14 -0700 | [diff] [blame] | 44 | # limit_parent_depth can be used to limit how far back we search the tree for a VERSION file. |
| 45 | if (max_parent_depth is not None): |
| 46 | if (max_parent_depth <= 0): |
| 47 | return None |
| 48 | max_parent_depth -= 1 |
| 49 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 50 | (cur_path, remainder) = os.path.split(cur_path) |
| 51 | if not remainder: |
| 52 | return None |
| 53 | |
| 54 | def autodiscover_version_of_caller(*args, **kwargs): |
| 55 | frame = inspect.stack()[1] |
| 56 | module = inspect.getmodule(frame[0]) |
| 57 | return autodiscover_version(module.__file__, *args, **kwargs) |
Scott Baker | fdb7e60 | 2018-03-21 09:09:12 -0700 | [diff] [blame] | 58 | |
| 59 | def autodiscover_version_of_main(*args, **kwargs): |
| 60 | import __main__ |
| 61 | if hasattr(__main__, "__file__"): |
| 62 | return autodiscover_version(__main__.__file__, *args, **kwargs) |
| 63 | else: |
| 64 | return None |