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/autoversion_setup.py |
| 17 | |
| 18 | This module exports a function, setup_with_auto_version(), that will automatically generate a version.py file |
| 19 | dynamically from the version option passed to the setup function. It does this without having to modify the |
| 20 | source copy of version.py. |
| 21 | |
| 22 | It also automatically searches for VERSION files in the directory of the caller and its parent hierarchy, and will |
| 23 | automatically load the version number from the VERSION file, if one is detected. |
| 24 | """ |
| 25 | |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 26 | from __future__ import absolute_import |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 27 | |
| 28 | import inspect |
Zack Williams | 9a42f87 | 2019-02-15 17:56:04 -0700 | [diff] [blame] | 29 | import os |
| 30 | |
| 31 | from setuptools import setup |
| 32 | from setuptools.command.build_py import build_py |
| 33 | from setuptools.command.sdist import sdist |
| 34 | |
| 35 | from .autodiscover_version import autodiscover_version |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 36 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 37 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 38 | class SdistCommand(sdist): |
| 39 | def copy_file(self, infile, outfile, *args, **kwargs): |
| 40 | if kwargs.get("dry_run"): |
| 41 | return (outfile, 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 42 | if os.path.split(outfile)[1] == "version.py": |
| 43 | open(outfile, "w").write( |
| 44 | "# do not edit. Autogenerated file.\n" |
| 45 | "__version__ = '%s'\n" % self.distribution.metadata.version |
| 46 | ) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 47 | return (outfile, 1) |
| 48 | else: |
| 49 | return sdist.copy_file(self, infile, outfile, *args, **kwargs) |
| 50 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 51 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 52 | class BuildPyCommand(build_py): |
| 53 | def copy_file(self, infile, outfile, *args, **kwargs): |
| 54 | if kwargs.get("dry_run"): |
| 55 | return (outfile, 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 56 | if os.path.split(outfile)[1] == "version.py": |
| 57 | open(outfile, "w").write( |
| 58 | "# do not edit. Autogenerated file.\n" |
| 59 | "__version__ = '%s'\n" % self.distribution.metadata.version |
| 60 | ) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 61 | return (outfile, 1) |
| 62 | else: |
| 63 | return build_py.copy_file(self, infile, outfile, *args, **kwargs) |
| 64 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 65 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 66 | def setup_with_auto_version(*args, **kwargs): |
| 67 | # Learn the module that called this function, so we can search for any VERSION files in it. |
| 68 | frame = inspect.stack()[1] |
| 69 | caller_module = inspect.getmodule(frame[0]) |
| 70 | |
| 71 | # Search for a VERSION file and extract the version number from it. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 72 | version = autodiscover_version(caller_filename=caller_module.__file__) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 73 | if version: |
| 74 | kwargs["version"] = version |
| 75 | |
| 76 | cmdclass = kwargs.get("cmdclass", {}).copy() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame] | 77 | cmdclass.update({"sdist": SdistCommand, "build_py": BuildPyCommand}) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 78 | kwargs["cmdclass"] = cmdclass |
| 79 | |
| 80 | return setup(*args, **kwargs) |