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 | |
| 26 | import os |
| 27 | from setuptools import setup |
| 28 | |
| 29 | from setuptools.command.sdist import sdist |
| 30 | from setuptools.command.build_py import build_py |
| 31 | |
| 32 | import inspect |
| 33 | from autodiscover_version import autodiscover_version |
| 34 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 35 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 36 | class SdistCommand(sdist): |
| 37 | def copy_file(self, infile, outfile, *args, **kwargs): |
| 38 | if kwargs.get("dry_run"): |
| 39 | return (outfile, 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 40 | if os.path.split(outfile)[1] == "version.py": |
| 41 | open(outfile, "w").write( |
| 42 | "# do not edit. Autogenerated file.\n" |
| 43 | "__version__ = '%s'\n" % self.distribution.metadata.version |
| 44 | ) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 45 | return (outfile, 1) |
| 46 | else: |
| 47 | return sdist.copy_file(self, infile, outfile, *args, **kwargs) |
| 48 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 49 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 50 | class BuildPyCommand(build_py): |
| 51 | def copy_file(self, infile, outfile, *args, **kwargs): |
| 52 | if kwargs.get("dry_run"): |
| 53 | return (outfile, 1) |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 54 | if os.path.split(outfile)[1] == "version.py": |
| 55 | open(outfile, "w").write( |
| 56 | "# do not edit. Autogenerated file.\n" |
| 57 | "__version__ = '%s'\n" % self.distribution.metadata.version |
| 58 | ) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 59 | return (outfile, 1) |
| 60 | else: |
| 61 | return build_py.copy_file(self, infile, outfile, *args, **kwargs) |
| 62 | |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 63 | |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 64 | def setup_with_auto_version(*args, **kwargs): |
| 65 | # Learn the module that called this function, so we can search for any VERSION files in it. |
| 66 | frame = inspect.stack()[1] |
| 67 | caller_module = inspect.getmodule(frame[0]) |
| 68 | |
| 69 | # Search for a VERSION file and extract the version number from it. |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 70 | version = autodiscover_version(caller_filename=caller_module.__file__) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 71 | if version: |
| 72 | kwargs["version"] = version |
| 73 | |
| 74 | cmdclass = kwargs.get("cmdclass", {}).copy() |
Zack Williams | 045b63d | 2019-01-22 16:30:57 -0700 | [diff] [blame^] | 75 | cmdclass.update({"sdist": SdistCommand, "build_py": BuildPyCommand}) |
Scott Baker | bcbd4cc | 2018-03-07 13:50:21 -0800 | [diff] [blame] | 76 | kwargs["cmdclass"] = cmdclass |
| 77 | |
| 78 | return setup(*args, **kwargs) |