Skip to content

Commit

Permalink
SG-27385 Add a Check to Stop Autoupdating tk-config-basic in tk frame…
Browse files Browse the repository at this point in the history
…work desktopstartup remove envvar dependency (#867)

* Fix logic for Python 2 users not depending on having 'SGTK_CONFIG_LOCK_VERSION' set to block auto update.
* Remove Python 2 conditional in the status() method.
* Remove 'SGTK_CONFIG_LOCK_VERSION' envvar from constants.py.
* Fix tests.
* Run Black linter.
* Fix resolver comment.
  • Loading branch information
NorberMV authored Jan 12, 2023
1 parent 3a2e245 commit 66dc283
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 199 deletions.
47 changes: 0 additions & 47 deletions python/tank/bootstrap/cached_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,30 +165,6 @@ def verify_required_shotgun_fields(self):
"tank_name field in ShotGrid."
)

def _verify_descriptor_compatible_with_python_version(self, descriptor_dict):
"""
If we are running Python 2 and the "SGTK_CONFIG_LOCK_VERSION" environment
variable has been set, this method validates if the actual config installed
on disk is Python 2 compatible.
:param descriptor_dict: This is the config descriptor from the
config metadata file on disk.
:returns: True if config version is older or equal than the
max config version supporting python 2.
"""
# get the versions from the the associated configuration described
# by the descriptor, and from the config descriptor from the config
# metadata file on disk.
installed_config_version = descriptor_dict.get("version")
associated_descriptor_config_version = self._descriptor.get_version()
#max_version_python2 = associated_descriptor_config_version if not constants.MAX_CONFIG_BASIC_PYTHON2_SUPPORTED
# If config version is older or equal than the max config version
# supporting python 2, return True.
return version.is_version_older_or_equal(
installed_config_version,
associated_descriptor_config_version
)

def status(self):
"""
Compares the actual configuration installed on disk against the
Expand Down Expand Up @@ -241,29 +217,6 @@ def status(self):
)
return self.LOCAL_CFG_DIFFERENT

# validate if we are currently running in Python 2,
# and "SGTK_CONFIG_LOCK_VERSION" environment variable has been set.
if (
constants.SGTK_CONFIG_LOCK_VERSION in os.environ
and sys.version_info[0] != 3
):
if not self._verify_descriptor_compatible_with_python_version(descriptor_dict):
# the config already installed does not supports Python 2
log.debug(
"Local config %r does not support Python 2. "
"Triggering full config rebuild to the latest "
"config supporting Python 2."
% (descriptor_dict)
)
return self.LOCAL_CFG_DIFFERENT
else:
log.debug(
"Local config %r does support Python 2, "
"so is up to date."
% (descriptor_dict)
)
return self.LOCAL_CFG_UP_TO_DATE

if descriptor_dict != self._descriptor.get_dict():
log.debug(
"Local Config %r does not match "
Expand Down
6 changes: 0 additions & 6 deletions python/tank/bootstrap/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,12 +66,6 @@
# configuration loaded when a bootstrap/plugin is starting up.
CONFIG_OVERRIDE_ENV_VAR = "TK_BOOTSTRAP_CONFIG_OVERRIDE"

# Environment variable that if set, enables auto update to the latest
# version of tk-config-basic supporting Python 2 when Python 2 is being
# used to launch SG Desktop to startup the tk-desktop engine on a site
# or Project context.
SGTK_CONFIG_LOCK_VERSION = "SGTK_CONFIG_LOCK_VERSION"

# environment variable that is used to indicate a specific
# pipeline configuration to be used.
PIPELINE_CONFIG_ID_ENV_VAR = "SHOTGUN_PIPELINE_CONFIGURATION_ID"
Expand Down
1 change: 0 additions & 1 deletion python/tank/bootstrap/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,6 @@ def _get_updated_configuration(self, entity, progress_callback):
:returns: A :class:`sgtk.bootstrap.configuration.Configuration` instance.
"""

config = self._get_configuration(entity, progress_callback)

# verify that this configuration works with Shotgun
Expand Down
31 changes: 14 additions & 17 deletions python/tank/bootstrap/resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,31 +206,28 @@ def resolve_not_found_sg_configuration(self, config_descriptor, sg_connection):
# Validate if descriptor version token is omitted
resolve_latest = is_descriptor_version_missing(config_descriptor)
if (
constants.SGTK_CONFIG_LOCK_VERSION in os.environ
and sys.version_info[0] != 3
sys.version_info[0] < 3
and self._plugin_id == "basic.desktop"
and resolve_latest
):
# Check if "SGTK_CONFIG_LOCK_VERSION" has been set, this will avoid auto update your
# tk-config-basic configuration to the latest available version when running Python 2
# and instead it will resolve the maximum config version supporting Python 2.
# This cover the cases below:
# This will avoid auto update your tk-config-basic configuration to the
# latest available version when running Python 2.
# This cover the following case:
#
# 1. Python 2 users launch SG Desktop and it startup the tk-desktop engine for their site
# configuration using the fallback descriptor(If no pipeline configuration found in Shotgrid).
# 2. When click on a Project in SG Desktop that has been configured to use a Python2 interpreter,
# this will initialize the tk-desktop for that project using the fallback
# descriptor(If no pipeline configuration found in Shotgrid).
# * SG Desktop is launched using Python 2 by setting 'SHOTGUN_PYTHON_VERSION'
# environment variable to '2'and it will startup the tk-desktop engine for
# the Site configuration.
#
# In those cases we request that the latest supported python 2 version should be resolved.
# In this case this will initialize the tk-desktop for the 'Site' environment and
# resolve a configuration object using a descriptor pointing to the maximum config
# version supporting Python 2 which is maintained in the variable
# 'MAX_CONFIG_BASIC_PYTHON2_SUPPORTED' in the bootstrap constants.
log.info(
"Detected a 'SGTK_CONFIG_LOCK_VERSION' environment variable."
"Using Python version '%s'" % ".".join(str(i) for i in sys.version_info[0:3])
)
log.debug(
"Base configuration descriptor does not have a "
"version token defined. If the actual "
"configuration installed on disk does not support Python 2 "
"will resolve the latest version available supporting Python 2."
"version token defined."
)

# Disable resolve latest
Expand All @@ -239,7 +236,7 @@ def resolve_not_found_sg_configuration(self, config_descriptor, sg_connection):
# supporting Python2
config_descriptor["version"] = constants.MAX_CONFIG_BASIC_PYTHON2_SUPPORTED
log.debug(
"%s resolving configuration for descriptor %s" % (self, config_descriptor)
"%s Resolving configuration for latest version supporting Python 2 descriptor %s" % (self, config_descriptor)
)
# create config descriptor
cfg_descriptor = create_descriptor(
Expand Down
118 changes: 0 additions & 118 deletions tests/bootstrap_tests/test_configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -635,121 +635,3 @@ def _update_deploy_file(self, generation=None, descriptor=None, corrupt=False):

with open(path, "wt") as fh:
yaml.dump(data, fh)

class TestCachedAutoUpdateConfiguration(ShotgunTestBase):
def setUp(self):
super(TestCachedAutoUpdateConfiguration, self).setUp()

# Reset the tank_name and create a storage named after the one in the config.
self.mockgun.update("Project", self.project["id"], {"tank_name": None})
self.mockgun.create("LocalStorage", {"code": "primary"})

# Initialize a cached configuration pointing to the config.
config_root = os.path.join(self.fixtures_root, "bootstrap_tests", "config")

self._temp_config_root = os.path.join(self.tank_temp, self.short_test_name)
self._cached_config = CachedConfiguration(
sgtk.util.ShotgunPath.from_current_os_path(self._temp_config_root),
self.mockgun,
sgtk.descriptor.create_descriptor(
self.mockgun,
sgtk.descriptor.Descriptor.CONFIG,
"sgtk:descriptor:path?path={0}".format(config_root),
),
self.project["id"],
"basic.*",
None,
[],
)

# Due to this being a test that runs offline, we can't use anything other than a
# path descriptor, which means that it is mutable. Because LOCAL_CFG_DIFFERENT
# is actually returned by three different code paths, the only way to ensure that
# we are indeed in the up to date state, which means everything is ready to do, is
# to cheat and make the descriptor immutable by monkey-patching it.
self._cached_config._descriptor.is_immutable = lambda: True
# Seems up the test tremendously since installing core becomes a noop.
self._cached_config._config_writer.install_core = lambda _: None
self._cached_config._config_writer.create_tank_command = lambda: None
# Maximum tk-config-basic version supporting Python 2.
version = constants.MAX_CONFIG_BASIC_PYTHON2_SUPPORTED
# Mock the cached descriptor version since we can't use anything other than a
# path descriptor.
self._cached_config._descriptor.get_version = Mock(spec=self._cached_config._descriptor.get_version,
return_value=version
)
# Mock the cached descriptor dictionary since we can't use anything other than a
# path descriptor.
self._cached_config._descriptor.get_dict = Mock(spec=self._cached_config._descriptor.get_dict,
return_value={
"type": "app_store",
"name": "tk-config-basic",
"version": version,
}
)

@patch("sys.version_info", return_value=Mock())
def test_python_compatible_descriptors(self, _):
"""
Ensures that the descriptor the actual config installed
on disk is Python 2 compatible otherwise will yield a
different config status.
"""
# Mock Python 2 Version
sys.version_info = [2, 7, 16, 'final', 0]
self.assertEqual(sys.version_info[0], 2)

self._cached_config.update_configuration()
# Test a config cached on disk with a descriptor pointing
# to a version that doesn't support Python 2.
self._update_deploy_file(
descriptor={
"type": "app_store",
"name": "tk-config-basic",
"version": "v1.4.7",
}
)
# Test that the configuration resolved is the maximum tk-config-basic
# version supporting Python 2 when auto-update is triggered for a Site
# or Project environment, and the 'SGTK_CONFIG_LOCK_VERSION' envvar
# has been set.
with temp_env_var(SGTK_CONFIG_LOCK_VERSION='1'):
self.assertEqual(
self._cached_config.status(), self._cached_config.LOCAL_CFG_DIFFERENT
)

# Test a config cached on disk with a descriptor pointing
# to a version that does support Python 2.
self._update_deploy_file(
descriptor={
"type": "app_store",
"name": "tk-config-basic",
"version": "v1.4.2",
}
)
with temp_env_var(SGTK_CONFIG_LOCK_VERSION='1'):
self.assertEqual(
self._cached_config.status(), self._cached_config.LOCAL_CFG_UP_TO_DATE
)

def _update_deploy_file(self, generation=None, descriptor=None, corrupt=False):
"""
Updates the deploy file.
:param generation: If set, will update the generation number of the config.
:param descriptor: If set, will update the descriptor of the config.
:param corrupt: If set, will corrupt the configuration file.
"""
path = self._cached_config._config_writer.get_descriptor_metadata_file()
if corrupt:
data = "corrupted"
else:
with open(path, "rt") as fh:
data = yaml.load(fh, Loader=yaml.FullLoader)
if generation is not None:
data["deploy_generation"] = generation
if descriptor is not None:
data["config_descriptor"] = descriptor

with open(path, "wt") as fh:
yaml.dump(data, fh)
17 changes: 7 additions & 10 deletions tests/bootstrap_tests/test_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ def test_resolve_latest_base_config(self, find_mock):
class TestAutoUpdate(TestResolverBase):
"""
A test class for the config resolved when
launch SG Desktop to startup the tk-desktop
SG Desktop is launched to startup the tk-desktop
engine on a site or Project context.
"""
def setUp(self):
Expand All @@ -355,9 +355,9 @@ def setUp(self):
@patch("tank_vendor.shotgun_api3.lib.mockgun.Shotgun.find")
def test_autoupdate_config(self, find_mock, _):
"""
Tests the direct config resolve for a descriptor with no version number set
when Python 2 is being used to launch SG Desktop to startup the tk-desktop
engine on a site or Project context.
Tests that the configuration resolved is the maximum tk-config-basic
version supporting Python 2 when SG Desktop is used to startup the
tk-desktop engine on a Site or Project context.
"""

# Mock Python2 Version
Expand All @@ -376,12 +376,9 @@ def test_autoupdate_config(self, find_mock, _):
}

# Test the configuration resolved is the Maximum tk-config-basic
# version supporting Python 2 when auto-update is triggered
# and the 'SGTK_CONFIG_LOCK_VERSION' environment variable
# has been set.
with temp_env_var(SGTK_CONFIG_LOCK_VERSION='1'):
config = self.resolver.resolve_not_found_sg_configuration(config_latest, self.mockgun)
self.assertEqual(config._descriptor.get_dict(), expected_config)
# version supporting Python 2 when auto-update is triggered.
config = self.resolver.resolve_not_found_sg_configuration(config_latest, self.mockgun)
self.assertEqual(config._descriptor.get_dict(), expected_config)

# make sure we didn't talk to shotgun
self.assertEqual(find_mock.called, False)
Expand Down

0 comments on commit 66dc283

Please sign in to comment.