|
2 | 2 | from pathlib import Path |
3 | 3 | from typing import BinaryIO, Optional |
4 | 4 |
|
| 5 | +import ignore |
| 6 | +import ignore.overrides |
5 | 7 | from typing_extensions import Literal |
6 | 8 |
|
7 | 9 | from dstack._internal.core.models.repos.base import BaseRepoInfo, Repo |
| 10 | +from dstack._internal.utils.common import sizeof_fmt |
8 | 11 | from dstack._internal.utils.hash import get_sha256, slugify |
9 | | -from dstack._internal.utils.ignore import GitIgnore |
| 12 | +from dstack._internal.utils.logging import get_logger |
10 | 13 | from dstack._internal.utils.path import PathLike |
11 | 14 |
|
| 15 | +logger = get_logger(__name__) |
| 16 | + |
12 | 17 |
|
13 | 18 | class LocalRepoInfo(BaseRepoInfo): |
14 | 19 | repo_type: Literal["local"] = "local" |
@@ -69,22 +74,23 @@ def __init__( |
69 | 74 | self.run_repo_data = repo_data |
70 | 75 |
|
71 | 76 | def write_code_file(self, fp: BinaryIO) -> str: |
| 77 | + repo_path = Path(self.run_repo_data.repo_dir) |
72 | 78 | with tarfile.TarFile(mode="w", fileobj=fp) as t: |
73 | | - t.add( |
74 | | - self.run_repo_data.repo_dir, |
75 | | - arcname="", |
76 | | - filter=TarIgnore(self.run_repo_data.repo_dir, globs=[".git"]), |
77 | | - ) |
| 79 | + for entry in ( |
| 80 | + ignore.WalkBuilder(repo_path) |
| 81 | + .overrides(ignore.overrides.OverrideBuilder(repo_path).add("!/.git/").build()) |
| 82 | + .hidden(False) # do not ignore files that start with a dot |
| 83 | + .require_git(False) # respect git ignore rules even if not a git repo |
| 84 | + .add_custom_ignore_filename(".dstackignore") |
| 85 | + .build() |
| 86 | + ): |
| 87 | + path = entry.path().relative_to(repo_path.absolute()) |
| 88 | + if path != Path("."): |
| 89 | + t.add(path, recursive=False) |
| 90 | + logger.debug("Code file size: %s", sizeof_fmt(fp.tell())) |
78 | 91 | return get_sha256(fp) |
79 | 92 |
|
80 | 93 | def get_repo_info(self) -> LocalRepoInfo: |
81 | 94 | return LocalRepoInfo( |
82 | 95 | repo_dir=self.run_repo_data.repo_dir, |
83 | 96 | ) |
84 | | - |
85 | | - |
86 | | -class TarIgnore(GitIgnore): |
87 | | - def __call__(self, tarinfo: tarfile.TarInfo) -> Optional[tarfile.TarInfo]: |
88 | | - if self.ignore(tarinfo.path): |
89 | | - return None |
90 | | - return tarinfo |
|
0 commit comments