blob: e027d5cdca4e83c4d8e2d86c2e350b86f2f24147 [file] [log] [blame]
Scott Bakerbcbd4cc2018-03-07 13:50:21 -08001# 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
26import os
27from setuptools import setup
28
29from setuptools.command.sdist import sdist
30from setuptools.command.build_py import build_py
31
32import inspect
33from autodiscover_version import autodiscover_version
34
Zack Williams045b63d2019-01-22 16:30:57 -070035
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080036class SdistCommand(sdist):
37 def copy_file(self, infile, outfile, *args, **kwargs):
38 if kwargs.get("dry_run"):
39 return (outfile, 1)
Zack Williams045b63d2019-01-22 16:30:57 -070040 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 Bakerbcbd4cc2018-03-07 13:50:21 -080045 return (outfile, 1)
46 else:
47 return sdist.copy_file(self, infile, outfile, *args, **kwargs)
48
Zack Williams045b63d2019-01-22 16:30:57 -070049
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080050class BuildPyCommand(build_py):
51 def copy_file(self, infile, outfile, *args, **kwargs):
52 if kwargs.get("dry_run"):
53 return (outfile, 1)
Zack Williams045b63d2019-01-22 16:30:57 -070054 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 Bakerbcbd4cc2018-03-07 13:50:21 -080059 return (outfile, 1)
60 else:
61 return build_py.copy_file(self, infile, outfile, *args, **kwargs)
62
Zack Williams045b63d2019-01-22 16:30:57 -070063
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080064def 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 Williams045b63d2019-01-22 16:30:57 -070070 version = autodiscover_version(caller_filename=caller_module.__file__)
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080071 if version:
72 kwargs["version"] = version
73
74 cmdclass = kwargs.get("cmdclass", {}).copy()
Zack Williams045b63d2019-01-22 16:30:57 -070075 cmdclass.update({"sdist": SdistCommand, "build_py": BuildPyCommand})
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080076 kwargs["cmdclass"] = cmdclass
77
78 return setup(*args, **kwargs)