blob: a0f30f0c19c7beaff1fa12aad988faa965bcd8ee [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
Zack Williams9a42f872019-02-15 17:56:04 -070026from __future__ import absolute_import
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080027
28import inspect
Zack Williams9a42f872019-02-15 17:56:04 -070029import os
30
31from setuptools import setup
32from setuptools.command.build_py import build_py
33from setuptools.command.sdist import sdist
34
35from .autodiscover_version import autodiscover_version
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080036
Zack Williams045b63d2019-01-22 16:30:57 -070037
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080038class SdistCommand(sdist):
39 def copy_file(self, infile, outfile, *args, **kwargs):
40 if kwargs.get("dry_run"):
41 return (outfile, 1)
Zack Williams045b63d2019-01-22 16:30:57 -070042 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 Bakerbcbd4cc2018-03-07 13:50:21 -080047 return (outfile, 1)
48 else:
49 return sdist.copy_file(self, infile, outfile, *args, **kwargs)
50
Zack Williams045b63d2019-01-22 16:30:57 -070051
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080052class BuildPyCommand(build_py):
53 def copy_file(self, infile, outfile, *args, **kwargs):
54 if kwargs.get("dry_run"):
55 return (outfile, 1)
Zack Williams045b63d2019-01-22 16:30:57 -070056 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 Bakerbcbd4cc2018-03-07 13:50:21 -080061 return (outfile, 1)
62 else:
63 return build_py.copy_file(self, infile, outfile, *args, **kwargs)
64
Zack Williams045b63d2019-01-22 16:30:57 -070065
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080066def 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 Williams045b63d2019-01-22 16:30:57 -070072 version = autodiscover_version(caller_filename=caller_module.__file__)
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080073 if version:
74 kwargs["version"] = version
75
76 cmdclass = kwargs.get("cmdclass", {}).copy()
Zack Williams045b63d2019-01-22 16:30:57 -070077 cmdclass.update({"sdist": SdistCommand, "build_py": BuildPyCommand})
Scott Bakerbcbd4cc2018-03-07 13:50:21 -080078 kwargs["cmdclass"] = cmdclass
79
80 return setup(*args, **kwargs)