From ebfe2cfa2a116d581eade553d259dfb13ef06918 Mon Sep 17 00:00:00 2001 From: Francois Chollet Date: Fri, 10 Mar 2023 14:42:17 -0800 Subject: [PATCH 1/4] Make version number SSoT. --- keras_nlp/__init__.py | 1 + pip_build.py | 7 +++++++ setup.py | 13 ++++++++++++- 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/keras_nlp/__init__.py b/keras_nlp/__init__.py index bf56a56f61..771bd78836 100644 --- a/keras_nlp/__init__.py +++ b/keras_nlp/__init__.py @@ -27,4 +27,5 @@ from keras_nlp import tokenizers from keras_nlp import utils +# This is the global source of truth for the version number. __version__ = "0.5.0" diff --git a/pip_build.py b/pip_build.py index b139a094c4..eda71a6a98 100644 --- a/pip_build.py +++ b/pip_build.py @@ -74,6 +74,13 @@ def build(): with open(os.path.join(package, "__init__.py"), "w") as f: f.write(init_contents + "\n\n" + f'__version__ = "{__version__}"\n') + # Insert the version string in setup.py + with open("setup.py") as f: + setup_contents = f.read() + setup_contents = setup_contents.replace("{{VERSION}}", __version__) + with open("setup.py", "w") as f: + f.write(setup_contents) + # Build the package os.system("python3 -m build") diff --git a/setup.py b/setup.py index 646ef621cd..ca4a182f0f 100644 --- a/setup.py +++ b/setup.py @@ -14,11 +14,22 @@ """Setup script.""" +import os import pathlib from setuptools import find_packages from setuptools import setup +with open(os.path.abspath(__file__)) as f: + contents = f.read() + if contents.count("{VERSION}") > 1: + raise ValueError( + "If you are trying to build a fresh package, " + "you should be using `pip_build.py` instead of `setup.py`. " + "You can do `python pip_build.py --install` " + "to build and immediately install the new package." + ) + HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text() @@ -29,7 +40,7 @@ ), long_description=README, long_description_content_type="text/markdown", - version="0.5.0", + version="{{VERSION}}", url="https://github.com/keras-team/keras-nlp", author="Keras team", author_email="keras-nlp@google.com", From 9b2cc0b2eaa0946d19db1aa03630626522aa5154 Mon Sep 17 00:00:00 2001 From: Matt Watson Date: Tue, 2 May 2023 15:35:15 +0000 Subject: [PATCH 2/4] Don't restrict "local installs" for now --- setup.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/setup.py b/setup.py index ca4a182f0f..ad84dde545 100644 --- a/setup.py +++ b/setup.py @@ -20,15 +20,18 @@ from setuptools import find_packages from setuptools import setup -with open(os.path.abspath(__file__)) as f: - contents = f.read() - if contents.count("{VERSION}") > 1: - raise ValueError( - "If you are trying to build a fresh package, " - "you should be using `pip_build.py` instead of `setup.py`. " - "You can do `python pip_build.py --install` " - "to build and immediately install the new package." - ) +def read(rel_path): + here = os.path.abspath(os.path.dirname(__file__)) + with open(os.path.join(here, rel_path)) as fp: + return fp.read() + + +def get_version(rel_path): + for line in read(rel_path).splitlines(): + if line.startswith("__version__"): + delim = '"' if '"' in line else "'" + return line.split(delim)[1] + raise RuntimeError("Unable to find version string.") HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text() @@ -40,7 +43,7 @@ ), long_description=README, long_description_content_type="text/markdown", - version="{{VERSION}}", + version=get_version("keras_nlp/__init__.py"), url="https://github.com/keras-team/keras-nlp", author="Keras team", author_email="keras-nlp@google.com", From 257299a9858ed90d56d3cdc2a8ea52466783614b Mon Sep 17 00:00:00 2001 From: Matt Watson Date: Tue, 2 May 2023 15:43:09 +0000 Subject: [PATCH 3/4] reformat --- setup.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/setup.py b/setup.py index ad84dde545..090e4b0136 100644 --- a/setup.py +++ b/setup.py @@ -20,6 +20,7 @@ from setuptools import find_packages from setuptools import setup + def read(rel_path): here = os.path.abspath(os.path.dirname(__file__)) with open(os.path.join(here, rel_path)) as fp: @@ -33,6 +34,7 @@ def get_version(rel_path): return line.split(delim)[1] raise RuntimeError("Unable to find version string.") + HERE = pathlib.Path(__file__).parent README = (HERE / "README.md").read_text() From bb063be5aa92a354ea44edf6ca0c90c7bb66ab0a Mon Sep 17 00:00:00 2001 From: Matt Watson Date: Tue, 2 May 2023 15:45:43 +0000 Subject: [PATCH 4/4] Remove no longer needed version replacement --- pip_build.py | 7 ------- 1 file changed, 7 deletions(-) diff --git a/pip_build.py b/pip_build.py index eda71a6a98..b139a094c4 100644 --- a/pip_build.py +++ b/pip_build.py @@ -74,13 +74,6 @@ def build(): with open(os.path.join(package, "__init__.py"), "w") as f: f.write(init_contents + "\n\n" + f'__version__ = "{__version__}"\n') - # Insert the version string in setup.py - with open("setup.py") as f: - setup_contents = f.read() - setup_contents = setup_contents.replace("{{VERSION}}", __version__) - with open("setup.py", "w") as f: - f.write(setup_contents) - # Build the package os.system("python3 -m build")