From 584fe73b5470c028801c6f659105373b671d3367 Mon Sep 17 00:00:00 2001 From: Simon Toens Date: Wed, 17 Apr 2024 10:20:50 +0900 Subject: [PATCH] Support excluding pinned files --- common/maveninstallinfo.py | 10 ++++++++++ tests/maveninstallinfotest.py | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/common/maveninstallinfo.py b/common/maveninstallinfo.py index d311d26..e05cdc4 100644 --- a/common/maveninstallinfo.py +++ b/common/maveninstallinfo.py @@ -17,14 +17,21 @@ def get_maven_install_names_and_paths(self, repository_root): """ Returns a list of tuples (mvn install name, mvn install path) """ + # paths that start with '-' are excluded if found in glob expansions + excluded_paths = [p[1:].strip() for p in self.maven_install_paths if self._is_excluded_path(p)] names_and_paths = [] for rel_path in self.maven_install_paths: + if self._is_excluded_path(rel_path): + # excluded paths are handled below + continue path = os.path.join(repository_root, rel_path) name_and_path = self._process_path(path) if name_and_path is None: globbed_names_and_paths = [] if "*" in path: for path in glob.glob(path): + if path[len(repository_root)+1:] in excluded_paths: + continue name_and_path = self._process_path(path) if name_and_path is not None: globbed_names_and_paths.append(name_and_path) @@ -36,6 +43,9 @@ def get_maven_install_names_and_paths(self, repository_root): names_and_paths.append(name_and_path) return names_and_paths + def _is_excluded_path(self, path): + return path.startswith("-") + def _process_path(self, path): """ Returns a tuple (mvn install name, mvn install path) or None diff --git a/tests/maveninstallinfotest.py b/tests/maveninstallinfotest.py index 32b9a7c..a2af3b6 100644 --- a/tests/maveninstallinfotest.py +++ b/tests/maveninstallinfotest.py @@ -43,6 +43,21 @@ def test_path_with_glob(self): self.assertEquals("my_rules", files[1][0]) self.assertEquals(os.path.join(repo_root, "tools", "my_rules_install.json"), files[1][1]) + def test_path_with_glob_and_exclusions(self): + repo_root = tempfile.mkdtemp("monorepo") + self._touch_file_at_path(repo_root, "tools/my_rules_install.json") + self._touch_file_at_path(repo_root, "tools/maven_install.json") + self._touch_file_at_path(repo_root, "tools/maven_blah_install.json") + m = maveninstallinfo.MavenInstallInfo(("tools/*", "-tools/maven_install.json",)) + + files = m.get_maven_install_names_and_paths(repo_root) + + self.assertEquals(2, len(files)) + self.assertEquals("maven_blah", files[0][0]) + self.assertEquals(os.path.join(repo_root, "tools", "maven_blah_install.json"), files[0][1]) + self.assertEquals("my_rules", files[1][0]) + self.assertEquals(os.path.join(repo_root, "tools", "my_rules_install.json"), files[1][1]) + def _touch_file_at_path(self, repo_root_path, file_path): path = os.path.join(repo_root_path, file_path) parent_dir = os.path.dirname(path)