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 <palgarvio@vmware.com>
This commit is contained in:
Pedro Algarvio 2023-01-07 12:18:08 +00:00 committed by Megan Wilhite
parent 0ed8dfffc7
commit 42b7d1be89
6 changed files with 52 additions and 49 deletions

4
.gitignore vendored
View file

@ -70,8 +70,8 @@ tags
*.mo *.mo
.doctrees .doctrees
# Allow a user to set their own _version.py for testing # Allow a user to set their own _version.txt for testing
_version.py _version.txt
# Ignore auto generated _syspaths.py file # Ignore auto generated _syspaths.py file
_syspaths.py _syspaths.py

View file

@ -19,6 +19,7 @@ recursive-include conf *
recursive-include pkg * recursive-include pkg *
recursive-include salt *.jinja recursive-include salt *.jinja
recursive-include templates * recursive-include templates *
include salt/_version.txt
include salt/templates/git/* include salt/templates/git/*
include salt/templates/lxc/* include salt/templates/lxc/*
include salt/utils/pyinstaller/rthooks.dat include salt/utils/pyinstaller/rthooks.dat

1
changelog/63383.changed Normal file
View file

@ -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.

View file

@ -1,8 +1,8 @@
""" """
Set up the version of Salt Set up the version of Salt
""" """
import operator import operator
import os
import platform import platform
import re import re
import sys import sys
@ -581,7 +581,6 @@ __saltstack_version__ = SaltStackVersion.current_release()
def __discover_version(saltstack_version): def __discover_version(saltstack_version):
# This might be a 'python setup.py develop' installation type. Let's # This might be a 'python setup.py develop' installation type. Let's
# discover the version information at runtime. # discover the version information at runtime.
import os
import subprocess import subprocess
if "SETUP_DIRNAME" in globals(): 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 If we can get a version provided at installation time or from Git, use
that instead, otherwise we carry on. that instead, otherwise we carry on.
""" """
try: _hardcoded_version_file = os.path.join(
# Try to import the version information provided at install time os.path.dirname(os.path.abspath(__file__)), "_version.txt"
from salt._version import __saltstack_version__ # pylint: disable=E0611,F0401 )
if not os.path.exists(_hardcoded_version_file):
return __saltstack_version__
except ImportError:
## except ImportError as exc:
return __discover_version(saltstack_version) 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 # Get additional version information if available

View file

@ -25,7 +25,6 @@ from distutils.command.install_lib import install_lib
from distutils.errors import DistutilsArgError from distutils.errors import DistutilsArgError
from distutils.version import LooseVersion # pylint: disable=blacklisted-module from distutils.version import LooseVersion # pylint: disable=blacklisted-module
import setuptools
from setuptools import setup from setuptools import setup
from setuptools.command.bdist_egg import bdist_egg from setuptools.command.bdist_egg import bdist_egg
from setuptools.command.develop import develop from setuptools.command.develop import develop
@ -108,9 +107,9 @@ try:
except ImportError: except ImportError:
HAS_ESKY = False 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( 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( SALT_SYSPATHS_HARDCODED = os.path.join(
os.path.abspath(SETUP_DIRNAME), "salt", "_syspaths.py" 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 # 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 # pylint: enable=W0122
@ -222,9 +226,7 @@ class WriteSaltVersion(Command):
sys.stderr.flush() sys.stderr.flush()
if not self.distribution.with_salt_version: if not self.distribution.with_salt_version:
salt_version = ( salt_version = SALT_VERSION
__saltstack_version__ # pylint: disable=undefined-variable
)
else: else:
from salt.version import SaltStackVersion from salt.version import SaltStackVersion
@ -232,13 +234,10 @@ class WriteSaltVersion(Command):
self.distribution.with_salt_version self.distribution.with_salt_version
) )
# pylint: disable=E0602 with open(
open(self.distribution.salt_version_hardcoded_path, "w").write( self.distribution.salt_version_hardcoded_path, "w", encoding="utf-8"
INSTALL_VERSION_TEMPLATE.format( ) as wfh:
date=DATE, full_version_info=salt_version.full_info_all_versions wfh.write(str(salt_version))
)
)
# pylint: enable=E0602
class GenerateSaltSyspaths(Command): class GenerateSaltSyspaths(Command):
@ -317,7 +316,7 @@ class Develop(develop):
( (
"write-salt-version", "write-salt-version",
None, 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. " "reporting. This defaults to False on develop/editable setups. "
"If WRITE_SALT_VERSION is found in the environment this flag is " "If WRITE_SALT_VERSION is found in the environment this flag is "
"switched to True.", "switched to True.",
@ -334,7 +333,7 @@ class Develop(develop):
"mimic-salt-install", "mimic-salt-install",
None, None,
"Mimmic the install command when running the develop command. " "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 " "Generate Salt's _syspaths.py file which allows tweaking some "
"This defaults to False on develop/editable setups. " "This defaults to False on develop/editable setups. "
"If MIMIC_INSTALL is found in the environment this flag is " "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) 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.running_salt_sdist = True
self.distribution.salt_version_hardcoded_path = os.path.join( 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") self.run_command("write_salt_version")
@ -683,20 +682,20 @@ class Build(build):
def run(self): def run(self):
# Run build.run function # Run build.run function
build.run(self) 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): 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.distribution.salt_version_hardcoded_path = salt_build_ver_file
self.run_command("write_salt_version") self.run_command("write_salt_version")
if getattr(self.distribution, "build_egg", False): 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.distribution.salt_version_hardcoded_path = salt_build_ver_file
self.run_command("write_salt_version") self.run_command("write_salt_version")
if getattr(self.distribution, "build_wheel", False): 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.distribution.salt_version_hardcoded_path = salt_build_ver_file
self.run_command("write_salt_version") 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 # If our install attribute is present and set to True, we'll go
# ahead and write our install time python modules. # 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") self.run_command("write_salt_version")
# Write the system paths file # Write the system paths file
@ -731,17 +730,17 @@ class Install(install):
sys.exit(1) sys.exit(1)
# Let's set the running_salt_install attribute so we can add # 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.running_salt_install = True
self.distribution.salt_version_hardcoded_path = os.path.join( 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: if IS_WINDOWS_PLATFORM:
# Download the required DLLs # Download the required DLLs
self.distribution.salt_download_windows_dlls = True self.distribution.salt_download_windows_dlls = True
self.run_command("download-windows-dlls") self.run_command("download-windows-dlls")
self.distribution.salt_download_windows_dlls = None 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 os.path.exists(os.path.join(self.build_lib)):
if not self.skip_build: if not self.skip_build:
self.run_command("build") self.run_command("build")
@ -935,7 +934,7 @@ class SaltDistribution(distutils.dist.Distribution):
self.with_salt_version = None self.with_salt_version = None
self.name = "salt-ssh" if PACKAGED_FOR_SALT_SSH else "salt" 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 = ( self.description = (
"Portable, distributed, remote execution and configuration management" "Portable, distributed, remote execution and configuration management"
" system" " system"
@ -1039,6 +1038,9 @@ class SaltDistribution(distutils.dist.Distribution):
@property @property
def _property_package_data(self): def _property_package_data(self):
package_data = { package_data = {
"salt": [
"_version.txt",
],
"salt.templates": [ "salt.templates": [
"rh_ip/*.jinja", "rh_ip/*.jinja",
"suse_ip/*.jinja", "suse_ip/*.jinja",
@ -1046,7 +1048,7 @@ class SaltDistribution(distutils.dist.Distribution):
"virt/*.jinja", "virt/*.jinja",
"git/*", "git/*",
"lxc/*", "lxc/*",
] ],
} }
if not IS_WINDOWS_PLATFORM: if not IS_WINDOWS_PLATFORM:
package_data["salt.cloud"] = ["deploy/*.sh"] package_data["salt.cloud"] = ["deploy/*.sh"]

View file

@ -104,7 +104,7 @@ def test_wheel(virtualenv, cache_dir, use_static_requirements, src_dir):
installed_version, salt.version.__version__ 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 = [ subdir = [
"lib", "lib",
"python{}.{}".format(*sys.version_info), "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 = pathlib.Path(venv.venv_dir)
installed_salt_path = installed_salt_path.joinpath(*subdir) installed_salt_path = installed_salt_path.joinpath(*subdir)
assert installed_salt_path.is_dir() 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() 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__ 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 = [ subdir = [
"lib", "lib",
"python{}.{}".format(*sys.version_info), "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) log.debug("Installed salt path glob matches: %s", installed_salt_path)
installed_salt_path = installed_salt_path[0] / "salt" installed_salt_path = installed_salt_path[0] / "salt"
assert installed_salt_path.is_dir() 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( assert salt_generated_version_file_path.is_file(), "{} is not a file".format(
salt_generated_version_file_path 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)) 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 = [ subdir = [
"lib", "lib",
"python{}.{}".format(*sys.version_info), "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 = pathlib.Path(venv.venv_dir)
installed_salt_path = installed_salt_path.joinpath(*subdir) installed_salt_path = installed_salt_path.joinpath(*subdir)
assert installed_salt_path.is_dir() 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() assert salt_generated_version_file_path.is_file()
with salt_generated_version_file_path.open() as rfh: 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 # Let's ensure the version is correct
cmd = venv.run(venv.venv_python, "-m", "pip", "list", "--format", "json") 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__ 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 = [ subdir = [
"lib", "lib",
"python{}.{}".format(*sys.version_info), "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: if not installed_salt_path:
pytest.fail("Failed to find the installed salt path") pytest.fail("Failed to find the installed salt path")
installed_salt_path = installed_salt_path[0] / "salt" 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() assert salt_generated_version_file_path.is_file()