From faf55d4f89c37f72b3aa7de3bd82526a4e018aa4 Mon Sep 17 00:00:00 2001 From: Lubomír Sedlář Date: May 17 2023 13:47:51 +0000 Subject: Add support for layered products latest links --- diff --git a/compose_utils/symlink.py b/compose_utils/symlink.py index 4f92c1b..424f6ed 100644 --- a/compose_utils/symlink.py +++ b/compose_utils/symlink.py @@ -99,6 +99,10 @@ def _deprecated_arg(arg, replace): def update_symlinks(compose_path, dry_run=False): """Update the symlinks of the minor and major version of the compose""" + latest_symlink_re = re.compile( + r'(?Platest-.+?-)(?P\d+(\.\d+)*)(?P-.+)?' + ) + def natsort(s): return [int(t) if t.isdigit() else t.lower() for t in re.split(r'(\d+)', s)] @@ -118,20 +122,25 @@ def update_symlinks(compose_path, dry_run=False): to_create = {} for latest_link in list(next(groupby(links, key=ver_len))[1]): - for i in range(latest_link.count(".")): - version = latest_link[:latest_link.rfind(".")] - if version in to_create: - if natsort(latest_link) > natsort(to_create[version]): - to_create[version] = latest_link + m = latest_symlink_re.match(latest_link) + link_pattern = "%s%%s%s" % (m.group("prefix"), m.group("suffix") or "") + version = m.group("version") + for i in range(version.count(".")): + shorter_version = version[:version.rfind(".")] + link = link_pattern % shorter_version + dest = link_pattern % version + if link in to_create: + if natsort(version) > natsort(to_create[link][0]): + to_create[link] = (version, dest) else: - to_create[version] = latest_link - latest_link = latest_link[:latest_link.rfind(".")] + to_create[link] = (version, dest) + version = shorter_version for link, dest in sorted(to_create.items(), reverse=True): # Use the temp file in order to overwrite the existing symlinks temp = os.path.join(compose_path, ".link.tmp") # Create the symlink for the version - print("%s -> %s" % (os.path.join(compose_path, link), dest)) + print("%s -> %s" % (os.path.join(compose_path, link), dest[1])) if not dry_run: - os.symlink(dest, temp) + os.symlink(dest[1], temp) os.rename(temp, os.path.join(compose_path, link)) diff --git a/tests/test_symlink.py b/tests/test_symlink.py index 9ae40f5..0690748 100644 --- a/tests/test_symlink.py +++ b/tests/test_symlink.py @@ -158,9 +158,20 @@ class TestUpdateSymlinks(unittest.TestCase): def test_scenario(self): for y in range(1, 4): self.create_full_symlink('latest-DP-1.%d.0' % y) - symlink.update_symlinks(self.compose_dir) + + symlink.update_symlinks(self.compose_dir) self.assert_symlink('latest-DP-1', 'latest-DP-1.3') self.assert_symlink('latest-DP-1.1', 'latest-DP-1.1.0') self.assert_symlink('latest-DP-1.2', 'latest-DP-1.2.0') self.assert_symlink('latest-DP-1.3', 'latest-DP-1.3.0') + + def test_layered_product(self): + for y in range(1, 4): + self.create_full_symlink('latest-DP-1.%d.0-B-2' % y) + symlink.update_symlinks(self.compose_dir) + + self.assert_symlink('latest-DP-1-B-2', 'latest-DP-1.3-B-2') + self.assert_symlink('latest-DP-1.1-B-2', 'latest-DP-1.1.0-B-2') + self.assert_symlink('latest-DP-1.2-B-2', 'latest-DP-1.2.0-B-2') + self.assert_symlink('latest-DP-1.3-B-2', 'latest-DP-1.3.0-B-2')