|
7 | 7 | import sys |
8 | 8 | import tarfile |
9 | 9 | import textwrap |
| 10 | +from enum import StrEnum, auto |
10 | 11 | from functools import lru_cache |
11 | 12 |
|
12 | 13 | import escapism |
|
205 | 206 | DEFAULT_NB_UID = 1000 |
206 | 207 |
|
207 | 208 |
|
| 209 | +class ExcludesStrategy(StrEnum): |
| 210 | + theirs = auto() |
| 211 | + ours = auto() |
| 212 | + merge = auto() |
| 213 | + |
| 214 | + @classmethod |
| 215 | + def values(cls): |
| 216 | + return [item.value for item in cls] |
| 217 | + |
| 218 | + |
208 | 219 | class BuildPack: |
209 | 220 | """ |
210 | 221 | A composable BuildPack. |
@@ -582,6 +593,8 @@ def build( |
582 | 593 | cache_from, |
583 | 594 | extra_build_kwargs, |
584 | 595 | platform=None, |
| 596 | + extra_ignore_file=None, |
| 597 | + ignore_file_strategy=ExcludesStrategy.theirs, |
585 | 598 | ): |
586 | 599 | tarf = io.BytesIO() |
587 | 600 | tar = tarfile.open(fileobj=tarf, mode="w") |
@@ -609,24 +622,35 @@ def _filter_tar(tarinfo): |
609 | 622 | for fname in ("repo2docker-entrypoint", "python3-login"): |
610 | 623 | tar.add(os.path.join(HERE, fname), fname, filter=_filter_tar) |
611 | 624 |
|
612 | | - exclude = [] |
| 625 | + def _read_excludes(filepath): |
| 626 | + with open(filepath) as ignore_file: |
| 627 | + cleaned_lines = [ |
| 628 | + line.strip() for line in ignore_file.read().splitlines() |
| 629 | + ] |
| 630 | + return [line for line in cleaned_lines if line != "" and line[0] != "#"] |
| 631 | + |
| 632 | + extra_excludes = [] |
| 633 | + if extra_ignore_file: |
| 634 | + extra_excludes = _read_excludes(extra_ignore_file) |
613 | 635 |
|
| 636 | + excludes = [] |
614 | 637 | for ignore_file_name in [".dockerignore", ".containerignore"]: |
615 | 638 | ignore_file_name = self.binder_path(ignore_file_name) |
616 | 639 | if os.path.exists(ignore_file_name): |
617 | | - with open(ignore_file_name) as ignore_file: |
618 | | - cleaned_lines = [ |
619 | | - line.strip() for line in ignore_file.read().splitlines() |
620 | | - ] |
621 | | - exclude.extend( |
622 | | - [ |
623 | | - line |
624 | | - for line in cleaned_lines |
625 | | - if line != "" and line[0] != "#" |
626 | | - ] |
627 | | - ) |
628 | | - |
629 | | - files_to_add = exclude_paths(".", exclude) |
| 640 | + excludes.extend(_read_excludes(ignore_file_name)) |
| 641 | + |
| 642 | + if extra_ignore_file is not None: |
| 643 | + if ignore_file_strategy == ExcludesStrategy.ours: |
| 644 | + excludes = extra_excludes |
| 645 | + elif ignore_file_strategy == ExcludesStrategy.merge: |
| 646 | + excludes.extend(extra_excludes) |
| 647 | + else: |
| 648 | + # ignore means that if an ignore file exist, its content is used |
| 649 | + # otherwise, the extra exclude |
| 650 | + if not excludes: |
| 651 | + excludes = extra_excludes |
| 652 | + |
| 653 | + files_to_add = exclude_paths(".", excludes) |
630 | 654 |
|
631 | 655 | if files_to_add: |
632 | 656 | for item in files_to_add: |
|
0 commit comments