Skip to content

Commit 4b2c06b

Browse files
authored
Add functionality for .flyteignore file (#2479)
* Add functionality for .flyteignore file Signed-off-by: Daniel Sola <daniel.sola@union.ai> * copy docker ignore for flyteignore --------- Signed-off-by: Daniel Sola <daniel.sola@union.ai>
1 parent 8562fd9 commit 4b2c06b

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
lines changed

flytekit/tools/fast_registration.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
from flytekit.core.context_manager import FlyteContextManager
1717
from flytekit.core.utils import timeit
18-
from flytekit.tools.ignore import DockerIgnore, GitIgnore, Ignore, IgnoreGroup, StandardIgnore
18+
from flytekit.tools.ignore import DockerIgnore, FlyteIgnore, GitIgnore, Ignore, IgnoreGroup, StandardIgnore
1919
from flytekit.tools.script_mode import tar_strip_file_attributes
2020

2121
FAST_PREFIX = "fast"
@@ -46,7 +46,7 @@ def fast_package(
4646
:param bool deref_symlinks: Enables dereferencing symlinks when packaging directory
4747
:return os.PathLike:
4848
"""
49-
default_ignores = [GitIgnore, DockerIgnore, StandardIgnore]
49+
default_ignores = [GitIgnore, DockerIgnore, StandardIgnore, FlyteIgnore]
5050
if options is not None:
5151
if options.keep_default_ignores:
5252
ignores = options.ignores + default_ignores

flytekit/tools/ignore.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,27 @@ def _is_ignored(self, path: str) -> bool:
8787
return self.pm.matches(path)
8888

8989

90+
class FlyteIgnore(Ignore):
91+
"""Uses a .flyteignore file to determine ignored files."""
92+
93+
def __init__(self, root: Path):
94+
super().__init__(root)
95+
self.pm = self._parse()
96+
97+
def _parse(self) -> PatternMatcher:
98+
patterns = []
99+
flyteignore = os.path.join(self.root, ".flyteignore")
100+
if os.path.isfile(flyteignore):
101+
with open(flyteignore, "r") as f:
102+
patterns = [l.strip() for l in f.readlines() if l and not l.startswith("#")]
103+
else:
104+
logger.info(f"No .flyteignore found in {self.root}, not applying any filters")
105+
return PatternMatcher(patterns)
106+
107+
def _is_ignored(self, path: str) -> bool:
108+
return self.pm.matches(path)
109+
110+
90111
class StandardIgnore(Ignore):
91112
"""Retains the standard ignore functionality that previously existed. Could in theory
92113
by fed with custom ignore patterns from cli."""

tests/flytekit/unit/tools/test_ignore.py

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import pytest
99
from docker.utils.build import PatternMatcher
1010

11-
from flytekit.tools.ignore import DockerIgnore, GitIgnore, IgnoreGroup, StandardIgnore
11+
from flytekit.tools.ignore import DockerIgnore, GitIgnore, IgnoreGroup, StandardIgnore, FlyteIgnore
1212

1313

1414
def make_tree(root: Path, tree: Dict):
@@ -102,6 +102,19 @@ def all_ignore(tmp_path):
102102
return tmp_path
103103

104104

105+
@pytest.fixture
106+
def simple_flyteignore(tmp_path):
107+
tree = {
108+
"sub": {"some.bar": ""},
109+
"test.foo": "",
110+
"keep.foo": "",
111+
".flyteignore": "\n".join(["*.foo", "!keep.foo", "# A comment", "sub"]),
112+
}
113+
114+
make_tree(tmp_path, tree)
115+
return tmp_path
116+
117+
105118
def test_simple_gitignore(simple_gitignore):
106119
gitignore = GitIgnore(simple_gitignore)
107120
assert gitignore.is_ignored(str(simple_gitignore / "test.foo"))
@@ -219,7 +232,7 @@ def test_all_ignore(all_ignore):
219232

220233
def test_all_ignore_tar_filter(all_ignore):
221234
"""Test tar_filter method of all ignores grouped together"""
222-
ignore = IgnoreGroup(all_ignore, [GitIgnore, DockerIgnore, StandardIgnore])
235+
ignore = IgnoreGroup(all_ignore, [GitIgnore, DockerIgnore, StandardIgnore, FlyteIgnore])
223236
assert ignore.tar_filter(TarInfo(name="sub")).name == "sub"
224237
assert ignore.tar_filter(TarInfo(name="sub/some.bar")).name == "sub/some.bar"
225238
assert not ignore.tar_filter(TarInfo(name="sub/__pycache__/"))
@@ -232,4 +245,23 @@ def test_all_ignore_tar_filter(all_ignore):
232245
assert ignore.tar_filter(TarInfo(name="keep.foo")).name == "keep.foo"
233246
assert ignore.tar_filter(TarInfo(name=".gitignore")).name == ".gitignore"
234247
assert ignore.tar_filter(TarInfo(name=".dockerignore")).name == ".dockerignore"
248+
assert ignore.tar_filter(TarInfo(name=".flyteignore")).name == ".flyteignore"
235249
assert not ignore.tar_filter(TarInfo(name=".git"))
250+
251+
252+
def test_flyteignore_parse(simple_flyteignore):
253+
"""Test .flyteignore file parsing"""
254+
flyteignore = FlyteIgnore(simple_flyteignore)
255+
assert flyteignore.pm.matches("whatever.foo")
256+
assert not flyteignore.pm.matches("keep.foo")
257+
assert flyteignore.pm.matches("sub")
258+
assert flyteignore.pm.matches("sub/stuff.txt")
259+
260+
261+
def test_simple_flyteignore(simple_flyteignore):
262+
flyteignore = FlyteIgnore(simple_flyteignore)
263+
assert flyteignore.is_ignored(str(simple_flyteignore / "test.foo"))
264+
assert flyteignore.is_ignored(str(simple_flyteignore / "sub"))
265+
assert flyteignore.is_ignored(str(simple_flyteignore / "sub" / "some.bar"))
266+
assert not flyteignore.is_ignored(str(simple_flyteignore / "keep.foo"))
267+
assert not flyteignore.is_ignored(str(simple_flyteignore / ".flyteignore"))

0 commit comments

Comments
 (0)