From a5b080645c1b22cf4d0ba900235816130ab921c4 Mon Sep 17 00:00:00 2001 From: Ruslan Kuprieiev Date: Thu, 13 May 2021 13:23:58 +0300 Subject: [PATCH] dvcignore: refactor from_file(s) (#6002) --- dvc/ignore.py | 23 +++++++++-------------- tests/unit/test_ignore.py | 4 ++-- 2 files changed, 11 insertions(+), 16 deletions(-) diff --git a/dvc/ignore.py b/dvc/ignore.py index a58230f33..2f1026322 100644 --- a/dvc/ignore.py +++ b/dvc/ignore.py @@ -52,16 +52,12 @@ class DvcIgnorePatterns(DvcIgnore): ] @classmethod - def from_files(cls, ignore_file_path, fs, root): - assert os.path.isabs(ignore_file_path) - dirname = os.path.normpath(os.path.dirname(ignore_file_path)) - ignore_file_rel_path = os.path.relpath(ignore_file_path, root) - with fs.open(ignore_file_path, encoding="utf-8") as fobj: + def from_file(cls, path, fs, name): + assert os.path.isabs(path) + dirname = os.path.normpath(os.path.dirname(path)) + with fs.open(path, encoding="utf-8") as fobj: path_spec_lines = [ - PatternInfo( - line, - "{}:{}:{}".format(ignore_file_rel_path, line_no + 1, line), - ) + PatternInfo(line, "{}:{}:{}".format(name, line_no + 1, line),) for line_no, line in enumerate( map(str.strip, fobj.readlines()) ) @@ -198,11 +194,10 @@ class DvcIgnoreFilter: old_pattern = trie.longest_prefix(dirname).value matches = old_pattern.matches(dirname, DvcIgnore.DVCIGNORE_FILE, False) - ignore_file_path = os.path.join(dirname, DvcIgnore.DVCIGNORE_FILE) - if not matches and self.fs.exists(ignore_file_path): - new_pattern = DvcIgnorePatterns.from_files( - ignore_file_path, self.fs, self.root_dir, - ) + path = os.path.join(dirname, DvcIgnore.DVCIGNORE_FILE) + if not matches and self.fs.exists(path): + name = os.path.relpath(path, self.root_dir) + new_pattern = DvcIgnorePatterns.from_file(path, self.fs, name) if old_pattern: trie[dirname] = DvcIgnorePatterns( *merge_patterns( diff --git a/tests/unit/test_ignore.py b/tests/unit/test_ignore.py index 7832496ea..e897d83b4 100644 --- a/tests/unit/test_ignore.py +++ b/tests/unit/test_ignore.py @@ -9,8 +9,8 @@ from dvc.ignore import DvcIgnorePatterns def mock_dvcignore(dvcignore_path, patterns): fs = MagicMock() with patch.object(fs, "open", mock_open(read_data="\n".join(patterns))): - ignore_patterns = DvcIgnorePatterns.from_files( - dvcignore_path, fs, None + ignore_patterns = DvcIgnorePatterns.from_file( + dvcignore_path, fs, "mocked" ) return ignore_patterns