Skip to content

Commit adf944c

Browse files
authored
Add version() API to unify with Keras and KerasNLP (#2199)
* Unify `version` API with keras and keras_nlp * Formatting * Update to keep `version` parity with KerasNLP, support nightly version string * Update version_utils.py * Update version_utils.py
1 parent dc7a7c0 commit adf944c

File tree

4 files changed

+57
-3
lines changed

4 files changed

+57
-3
lines changed

keras_cv/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@
4141
from keras_cv.core import FactorSampler
4242
from keras_cv.core import NormalFactorSampler
4343
from keras_cv.core import UniformFactorSampler
44-
45-
__version__ = "0.8.2"
44+
from keras_cv.version_utils import __version__
45+
from keras_cv.version_utils import version

keras_cv/version_utils.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright 2023 The KerasCV Authors
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+
# https://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+
from keras_cv.api_export import keras_cv_export
16+
17+
# Unique source of truth for the version number.
18+
__version__ = "0.8.2"
19+
20+
21+
@keras_cv_export("keras_cv.version")
22+
def version():
23+
return __version__

pip_build.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,22 @@ def export_version_string(version, is_nightly=False):
6464
)
6565
f.write(setup_contents)
6666

67+
# Overwrite the version string with our package version.
68+
with open(os.path.join(package, "src", "version_utils.py")) as f:
69+
version_contents = f.readlines()
70+
with open(os.path.join(package, "src", "version_utils.py"), "w") as f:
71+
for line in version_contents:
72+
if line.startswith("__version__"):
73+
f.write(f'__version__ = "{version}"\n')
74+
else:
75+
f.write(line)
76+
6777
# Make sure to export the __version__ string
6878
with open(os.path.join(package, "__init__.py")) as f:
6979
init_contents = f.read()
7080
with open(os.path.join(package, "__init__.py"), "w") as f:
71-
f.write(init_contents + "\n\n" + f'__version__ = "{version}"\n')
81+
f.write(init_contents)
82+
f.write("from keras_cv.src.version_utils import __version__\n")
7283

7384

7485
def copy_source_to_build_directory(root_path):

setup.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,32 @@
2121
from setuptools import setup
2222
from setuptools.dist import Distribution
2323

24+
25+
def read(rel_path):
26+
here = os.path.abspath(os.path.dirname(__file__))
27+
with open(os.path.join(here, rel_path)) as fp:
28+
return fp.read()
29+
30+
31+
def get_version(rel_path):
32+
for line in read(rel_path).splitlines():
33+
if line.startswith("__version__"):
34+
delim = '"' if '"' in line else "'"
35+
return line.split(delim)[1]
36+
raise RuntimeError("Unable to find version string.")
37+
38+
2439
BUILD_WITH_CUSTOM_OPS = (
2540
"BUILD_WITH_CUSTOM_OPS" in os.environ
2641
and os.environ["BUILD_WITH_CUSTOM_OPS"] == "true"
2742
)
2843

2944
HERE = pathlib.Path(__file__).parent
3045
README = (HERE / "README.md").read_text()
46+
if os.path.exists("keras_cv/version_utils.py"):
47+
VERSION = get_version("keras_cv/version_utils.py")
48+
else:
49+
VERSION = get_version("keras_cv/src/version_utils.py")
3150

3251

3352
class BinaryDistribution(Distribution):
@@ -45,6 +64,7 @@ def is_pure(self):
4564
description="Industry-strength computer Vision extensions for Keras.",
4665
long_description=README,
4766
long_description_content_type="text/markdown",
67+
version=VERSION,
4868
url="https://github.com/keras-team/keras-cv",
4969
author="Keras team",
5070
author_email="[email protected]",

0 commit comments

Comments
 (0)