From 42b7d1be891df3f75fa77062ad8e343aa8a0b003 Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Sat, 7 Jan 2023 12:18:08 +0000 Subject: [PATCH] Stop relying on `salt/_version.py` to write Salt's version. Instead use `salt/_version.txt` which only contains the version string. Signed-off-by: Pedro Algarvio --- .gitignore | 4 +- MANIFEST.in | 1 + changelog/63383.changed | 1 + salt/version.py | 19 +++--- setup.py | 58 ++++++++++--------- tests/pytests/scenarios/setup/test_install.py | 18 +++--- 6 files changed, 52 insertions(+), 49 deletions(-) create mode 100644 changelog/63383.changed diff --git a/.gitignore b/.gitignore index e9bdea9a139..b5ec7490644 100644 --- a/.gitignore +++ b/.gitignore @@ -70,8 +70,8 @@ tags *.mo .doctrees -# Allow a user to set their own _version.py for testing -_version.py +# Allow a user to set their own _version.txt for testing +_version.txt # Ignore auto generated _syspaths.py file _syspaths.py diff --git a/MANIFEST.in b/MANIFEST.in index 9886100b685..fd5d36cc3d1 100644 --- a/MANIFEST.in +++ b/MANIFEST.in @@ -19,6 +19,7 @@ recursive-include conf * recursive-include pkg * recursive-include salt *.jinja recursive-include templates * +include salt/_version.txt include salt/templates/git/* include salt/templates/lxc/* include salt/utils/pyinstaller/rthooks.dat diff --git a/changelog/63383.changed b/changelog/63383.changed new file mode 100644 index 00000000000..d0553dc0624 --- /dev/null +++ b/changelog/63383.changed @@ -0,0 +1 @@ +Stop relying on `salt/_version.py` to write Salt's version. Instead use `salt/_version.txt` which only contains the version string. diff --git a/salt/version.py b/salt/version.py index 4eb20ee8d72..ca4c490029b 100644 --- a/salt/version.py +++ b/salt/version.py @@ -1,8 +1,8 @@ """ Set up the version of Salt """ - import operator +import os import platform import re import sys @@ -581,7 +581,6 @@ __saltstack_version__ = SaltStackVersion.current_release() def __discover_version(saltstack_version): # This might be a 'python setup.py develop' installation type. Let's # discover the version information at runtime. - import os import subprocess if "SETUP_DIRNAME" in globals(): @@ -646,15 +645,15 @@ def __get_version(saltstack_version): If we can get a version provided at installation time or from Git, use that instead, otherwise we carry on. """ - try: - # Try to import the version information provided at install time - from salt._version import __saltstack_version__ # pylint: disable=E0611,F0401 - - return __saltstack_version__ - - except ImportError: - ## except ImportError as exc: + _hardcoded_version_file = os.path.join( + os.path.dirname(os.path.abspath(__file__)), "_version.txt" + ) + if not os.path.exists(_hardcoded_version_file): return __discover_version(saltstack_version) + with open( # pylint: disable=resource-leakage + _hardcoded_version_file, encoding="utf-8" + ) as rfh: + return SaltStackVersion.parse(rfh.read().strip()) # Get additional version information if available diff --git a/setup.py b/setup.py index 09b7bf97e72..4dd8f64251c 100755 --- a/setup.py +++ b/setup.py @@ -25,7 +25,6 @@ from distutils.command.install_lib import install_lib from distutils.errors import DistutilsArgError from distutils.version import LooseVersion # pylint: disable=blacklisted-module -import setuptools from setuptools import setup from setuptools.command.bdist_egg import bdist_egg from setuptools.command.develop import develop @@ -108,9 +107,9 @@ try: except ImportError: HAS_ESKY = False -SALT_VERSION = os.path.join(os.path.abspath(SETUP_DIRNAME), "salt", "version.py") +SALT_VERSION_MODULE = os.path.join(os.path.abspath(SETUP_DIRNAME), "salt", "version.py") SALT_VERSION_HARDCODED = os.path.join( - os.path.abspath(SETUP_DIRNAME), "salt", "_version.py" + os.path.abspath(SETUP_DIRNAME), "salt", "_version.txt" ) SALT_SYSPATHS_HARDCODED = os.path.join( os.path.abspath(SETUP_DIRNAME), "salt", "_syspaths.py" @@ -170,7 +169,12 @@ PACKAGED_FOR_SALT_SSH = os.path.isfile(PACKAGED_FOR_SALT_SSH_FILE) # pylint: disable=W0122 -exec(compile(open(SALT_VERSION).read(), SALT_VERSION, "exec")) +if os.path.exists(SALT_VERSION_HARDCODED): + with open(SALT_VERSION_HARDCODED, encoding="utf-8") as rfh: + SALT_VERSION = rfh.read().strip() +else: + exec(compile(open(SALT_VERSION_MODULE).read(), SALT_VERSION_MODULE, "exec")) + SALT_VERSION = str(__saltstack_version__) # pylint: disable=undefined-variable # pylint: enable=W0122 @@ -222,9 +226,7 @@ class WriteSaltVersion(Command): sys.stderr.flush() if not self.distribution.with_salt_version: - salt_version = ( - __saltstack_version__ # pylint: disable=undefined-variable - ) + salt_version = SALT_VERSION else: from salt.version import SaltStackVersion @@ -232,13 +234,10 @@ class WriteSaltVersion(Command): self.distribution.with_salt_version ) - # pylint: disable=E0602 - open(self.distribution.salt_version_hardcoded_path, "w").write( - INSTALL_VERSION_TEMPLATE.format( - date=DATE, full_version_info=salt_version.full_info_all_versions - ) - ) - # pylint: enable=E0602 + with open( + self.distribution.salt_version_hardcoded_path, "w", encoding="utf-8" + ) as wfh: + wfh.write(str(salt_version)) class GenerateSaltSyspaths(Command): @@ -317,7 +316,7 @@ class Develop(develop): ( "write-salt-version", None, - "Generate Salt's _version.py file which allows proper version " + "Generate Salt's _version.txt file which allows proper version " "reporting. This defaults to False on develop/editable setups. " "If WRITE_SALT_VERSION is found in the environment this flag is " "switched to True.", @@ -334,7 +333,7 @@ class Develop(develop): "mimic-salt-install", None, "Mimmic the install command when running the develop command. " - "This will generate salt's _version.py and _syspaths.py files. " + "This will generate salt's _version.txt and _syspaths.py files. " "Generate Salt's _syspaths.py file which allows tweaking some " "This defaults to False on develop/editable setups. " "If MIMIC_INSTALL is found in the environment this flag is " @@ -480,10 +479,10 @@ class Sdist(sdist): sdist.make_release_tree(self, base_dir, files) - # Let's generate salt/_version.py to include in the sdist tarball + # Let's generate salt/_version.txt to include in the sdist tarball self.distribution.running_salt_sdist = True self.distribution.salt_version_hardcoded_path = os.path.join( - base_dir, "salt", "_version.py" + base_dir, "salt", "_version.txt" ) self.run_command("write_salt_version") @@ -683,20 +682,20 @@ class Build(build): def run(self): # Run build.run function build.run(self) - salt_build_ver_file = os.path.join(self.build_lib, "salt", "_version.py") + salt_build_ver_file = os.path.join(self.build_lib, "salt", "_version.txt") if getattr(self.distribution, "with_salt_version", False): - # Write the hardcoded salt version module salt/_version.py + # Write the hardcoded salt version module salt/_version.txt self.distribution.salt_version_hardcoded_path = salt_build_ver_file self.run_command("write_salt_version") if getattr(self.distribution, "build_egg", False): - # we are building an egg package. need to include _version.py + # we are building an egg package. need to include _version.txt self.distribution.salt_version_hardcoded_path = salt_build_ver_file self.run_command("write_salt_version") if getattr(self.distribution, "build_wheel", False): - # we are building a wheel package. need to include _version.py + # we are building a wheel package. need to include _version.txt self.distribution.salt_version_hardcoded_path = salt_build_ver_file self.run_command("write_salt_version") @@ -704,7 +703,7 @@ class Build(build): # If our install attribute is present and set to True, we'll go # ahead and write our install time python modules. - # Write the hardcoded salt version module salt/_version.py + # Write the hardcoded salt version module salt/_version.txt self.run_command("write_salt_version") # Write the system paths file @@ -731,17 +730,17 @@ class Install(install): sys.exit(1) # Let's set the running_salt_install attribute so we can add - # _version.py in the build command + # _version.txt in the build command self.distribution.running_salt_install = True self.distribution.salt_version_hardcoded_path = os.path.join( - self.build_lib, "salt", "_version.py" + self.build_lib, "salt", "_version.txt" ) if IS_WINDOWS_PLATFORM: # Download the required DLLs self.distribution.salt_download_windows_dlls = True self.run_command("download-windows-dlls") self.distribution.salt_download_windows_dlls = None - # need to ensure _version.py is created in build dir before install + # need to ensure _version.txt is created in build dir before install if not os.path.exists(os.path.join(self.build_lib)): if not self.skip_build: self.run_command("build") @@ -935,7 +934,7 @@ class SaltDistribution(distutils.dist.Distribution): self.with_salt_version = None self.name = "salt-ssh" if PACKAGED_FOR_SALT_SSH else "salt" - self.salt_version = __version__ # pylint: disable=undefined-variable + self.salt_version = SALT_VERSION self.description = ( "Portable, distributed, remote execution and configuration management" " system" @@ -1039,6 +1038,9 @@ class SaltDistribution(distutils.dist.Distribution): @property def _property_package_data(self): package_data = { + "salt": [ + "_version.txt", + ], "salt.templates": [ "rh_ip/*.jinja", "suse_ip/*.jinja", @@ -1046,7 +1048,7 @@ class SaltDistribution(distutils.dist.Distribution): "virt/*.jinja", "git/*", "lxc/*", - ] + ], } if not IS_WINDOWS_PLATFORM: package_data["salt.cloud"] = ["deploy/*.sh"] diff --git a/tests/pytests/scenarios/setup/test_install.py b/tests/pytests/scenarios/setup/test_install.py index 7adfa18ba8d..97a37c75d1a 100644 --- a/tests/pytests/scenarios/setup/test_install.py +++ b/tests/pytests/scenarios/setup/test_install.py @@ -104,7 +104,7 @@ def test_wheel(virtualenv, cache_dir, use_static_requirements, src_dir): installed_version, salt.version.__version__ ) - # Let's also ensure we have a salt/_version.py from the installed salt wheel + # Let's also ensure we have a salt/_version.txt from the installed salt wheel subdir = [ "lib", "python{}.{}".format(*sys.version_info), @@ -117,7 +117,7 @@ def test_wheel(virtualenv, cache_dir, use_static_requirements, src_dir): installed_salt_path = pathlib.Path(venv.venv_dir) installed_salt_path = installed_salt_path.joinpath(*subdir) assert installed_salt_path.is_dir() - salt_generated_version_file_path = installed_salt_path / "_version.py" + salt_generated_version_file_path = installed_salt_path / "_version.txt" assert salt_generated_version_file_path.is_file() @@ -239,7 +239,7 @@ def test_egg(virtualenv, cache_dir, use_static_requirements, src_dir): installed_version, salt.version.__version__ ) - # Let's also ensure we have a salt/_version.py from the installed salt egg + # Let's also ensure we have a salt/_version.txt from the installed salt egg subdir = [ "lib", "python{}.{}".format(*sys.version_info), @@ -256,7 +256,7 @@ def test_egg(virtualenv, cache_dir, use_static_requirements, src_dir): log.debug("Installed salt path glob matches: %s", installed_salt_path) installed_salt_path = installed_salt_path[0] / "salt" assert installed_salt_path.is_dir() - salt_generated_version_file_path = installed_salt_path / "_version.py" + salt_generated_version_file_path = installed_salt_path / "_version.txt" assert salt_generated_version_file_path.is_file(), "{} is not a file".format( salt_generated_version_file_path ) @@ -334,7 +334,7 @@ def test_sdist(virtualenv, cache_dir, use_static_requirements, src_dir): venv.install(str(salt_generated_package)) - # Let's also ensure we have a salt/_version.py from the installed salt wheel + # Let's also ensure we have a salt/_version.txt from the installed salt wheel subdir = [ "lib", "python{}.{}".format(*sys.version_info), @@ -347,10 +347,10 @@ def test_sdist(virtualenv, cache_dir, use_static_requirements, src_dir): installed_salt_path = pathlib.Path(venv.venv_dir) installed_salt_path = installed_salt_path.joinpath(*subdir) assert installed_salt_path.is_dir() - salt_generated_version_file_path = installed_salt_path / "_version.py" + salt_generated_version_file_path = installed_salt_path / "_version.txt" assert salt_generated_version_file_path.is_file() with salt_generated_version_file_path.open() as rfh: - log.debug("_version.py contents:\n >>>>>>\n%s\n <<<<<<", rfh.read()) + log.debug("_version.txt contents:\n >>>>>>\n%s\n <<<<<<", rfh.read()) # Let's ensure the version is correct cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json") @@ -439,7 +439,7 @@ def test_setup_install(virtualenv, cache_dir, use_static_requirements, src_dir): installed_version, salt.version.__version__ ) - # Let's also ensure we have a salt/_version.py from the installed salt + # Let's also ensure we have a salt/_version.txt from the installed salt subdir = [ "lib", "python{}.{}".format(*sys.version_info), @@ -454,5 +454,5 @@ def test_setup_install(virtualenv, cache_dir, use_static_requirements, src_dir): if not installed_salt_path: pytest.fail("Failed to find the installed salt path") installed_salt_path = installed_salt_path[0] / "salt" - salt_generated_version_file_path = installed_salt_path / "_version.py" + salt_generated_version_file_path = installed_salt_path / "_version.txt" assert salt_generated_version_file_path.is_file()