|
5 | 5 | from typing import Iterable, Optional, Sequence |
6 | 6 | from pathlib import Path |
7 | 7 |
|
8 | | -from ._ignore import IgnorePattern, compile_ignore, read_ignorefile, iter_included_files |
| 8 | +from ._ignore import IgnoreMatcher, IgnorePattern, DockerIgnoreMatcher, iter_included_files |
9 | 9 |
|
10 | 10 |
|
11 | 11 | def build_docker_context_tar( |
12 | 12 | context_root: Path, |
13 | 13 | *, |
14 | | - ignore: Optional[Sequence[str] | Path | str] = None, |
| 14 | + ignore: Optional[IgnoreMatcher] = None, |
15 | 15 | ) -> bytes: |
16 | | - """Create a .tar.gz of the build context, honoring ignore patterns. |
| 16 | + """Create a .tar.gz of the build context, honoring Docker-style ignore patterns. |
17 | 17 |
|
18 | 18 | - Treats ``context_root`` as the build context root. |
19 | 19 | - Always loads ``.dockerignore`` under ``context_root`` if present. |
20 | | - - An optional ``ignore`` argument may be provided: |
21 | | -
|
22 | | - * If a :class:`pathlib.Path` or string is given, it is treated as an |
23 | | - additional ignorefile path whose patterns are appended after |
24 | | - ``.dockerignore``. |
25 | | - * If a sequence of strings is given, they are treated as inline patterns |
26 | | - appended after any file-derived patterns. |
27 | | -
|
28 | | - Patterns use Docker-style semantics with ``!`` negation and ``**`` support. |
| 20 | + - An optional :class:`IgnoreMatcher` may be provided to customise how ignore |
| 21 | + patterns are resolved; when omitted, :class:`DockerIgnoreMatcher` is used. |
29 | 22 | """ |
30 | 23 |
|
31 | 24 | context_root = context_root.resolve() |
32 | 25 |
|
33 | | - all_patterns: list[str] = [] |
34 | | - |
35 | | - # 1) Always consider .dockerignore under the context root, if present. |
36 | | - default_ignorefile = context_root / ".dockerignore" |
37 | | - all_patterns.extend(read_ignorefile(default_ignorefile)) |
38 | | - |
39 | | - # 2) Optional additional ignore source |
40 | | - if ignore is not None: |
41 | | - if isinstance(ignore, (str, Path)): |
42 | | - ignore_path = Path(ignore) |
43 | | - if not ignore_path.exists(): |
44 | | - raise FileNotFoundError(f"Ignore file does not exist: {ignore_path}") |
45 | | - all_patterns.extend(read_ignorefile(ignore_path)) |
46 | | - else: |
47 | | - # Treat as a sequence of raw patterns |
48 | | - all_patterns.extend(list(ignore)) |
49 | | - |
50 | | - compiled: list[IgnorePattern] = compile_ignore(all_patterns) |
| 26 | + matcher: IgnoreMatcher = ignore or DockerIgnoreMatcher() |
51 | 27 |
|
52 | 28 | buf = io.BytesIO() |
53 | 29 |
|
54 | 30 | with tarfile.open(mode="w:gz", fileobj=buf) as tf: |
55 | | - for path in _iter_build_context_files(context_root, patterns=compiled): |
| 31 | + for path in matcher.iter_paths(context_root): |
56 | 32 | rel = path.relative_to(context_root) |
57 | 33 | tf.add(path, arcname=rel.as_posix()) |
58 | 34 |
|
|
0 commit comments