Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 1 | # Copyright 2017-present Open Networking Foundation |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | # you may not use this file except in compliance with the License. |
| 5 | # You may obtain a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | # See the License for the specific language governing permissions and |
| 13 | # limitations under the License. |
| 14 | |
| 15 | """ |
| 16 | xosutil/autodiscover_version.py |
| 17 | |
| 18 | This module implements support for recursively searching for a VERSION file and extracting the version number from |
| 19 | it. Search starts from the directory of this file, or if autodiscover_version_of_caller is called, the directory |
| 20 | of the caller. |
| 21 | """ |
| 22 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 23 | from __future__ import absolute_import |
| 24 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 25 | import inspect |
| 26 | import os |
| 27 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 28 | |
Scott Baker | e02aa69 | 2018-03-23 09:54:14 -0700 | [diff] [blame] | 29 | 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] | 30 | """ walk back along the path to the current module, searching for a VERSION file """ |
| 31 | if not caller_filename: |
| 32 | caller_filename = os.path.realpath(__file__) |
| 33 | file_path = os.path.abspath(os.path.dirname(caller_filename)) |
| 34 | cur_path = file_path |
| 35 | while True: |
| 36 | version_fn = os.path.join(cur_path, "VERSION") |
| 37 | if os.path.exists(version_fn): |
| 38 | version = open(version_fn, "rt").readline().strip() |
| 39 | if save_to: |
| 40 | f = open(os.path.join(file_path, save_to), "wt") |
| 41 | f.write("# This file is autogenerated. Do not edit.\n") |
| 42 | f.write("__version__ = '%s'\n" % version) |
| 43 | f.close() |
| 44 | return version |
| 45 | |
Scott Baker | e02aa69 | 2018-03-23 09:54:14 -0700 | [diff] [blame] | 46 | # limit_parent_depth can be used to limit how far back we search the tree for a VERSION file. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 47 | if max_parent_depth is not None: |
| 48 | if max_parent_depth <= 0: |
Scott Baker | e02aa69 | 2018-03-23 09:54:14 -0700 | [diff] [blame] | 49 | return None |
| 50 | max_parent_depth -= 1 |
| 51 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 52 | (cur_path, remainder) = os.path.split(cur_path) |
| 53 | if not remainder: |
| 54 | return None |
| 55 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 57 | def autodiscover_version_of_caller(*args, **kwargs): |
| 58 | frame = inspect.stack()[1] |
| 59 | module = inspect.getmodule(frame[0]) |
| 60 | return autodiscover_version(module.__file__, *args, **kwargs) |
Scott Baker | fdb7e60 | 2018-03-21 09:09:12 -0700 | [diff] [blame] | 61 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 62 | |
Scott Baker | fdb7e60 | 2018-03-21 09:09:12 -0700 | [diff] [blame] | 63 | def autodiscover_version_of_main(*args, **kwargs): |
| 64 | import __main__ |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 65 | |
Scott Baker | fdb7e60 | 2018-03-21 09:09:12 -0700 | [diff] [blame] | 66 | if hasattr(__main__, "__file__"): |
| 67 | return autodiscover_version(__main__.__file__, *args, **kwargs) |
| 68 | else: |
| 69 | return None |