-
Notifications
You must be signed in to change notification settings - Fork 356
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: Trial APIs --local
should respect .detignore
. [MLG-1352]
#8683
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import os | ||
import pathlib | ||
from typing import TYPE_CHECKING, Callable, Iterable, List, Set | ||
|
||
if TYPE_CHECKING: | ||
import pathspec | ||
|
||
from determined.common import constants, v1file_utils | ||
from determined.common.api import bindings | ||
|
||
|
||
def _build_detignore_pathspec(root_path: pathlib.Path) -> "pathspec.PathSpec": | ||
ignore = list(constants.DEFAULT_DETIGNORE) | ||
ignore_path = root_path / ".detignore" | ||
if ignore_path.is_file(): | ||
with ignore_path.open("r") as detignore_file: | ||
ignore.extend(detignore_file) | ||
|
||
# Lazy import to speed up load time. | ||
# See https://github.com/determined-ai/determined/pull/6590 for details. | ||
import pathspec | ||
|
||
return pathspec.PathSpec.from_lines(pathspec.patterns.GitWildMatchPattern, ignore) | ||
|
||
|
||
def make_shutil_ignore(root_path: pathlib.Path) -> Callable: | ||
ignore_spec = _build_detignore_pathspec(root_path) | ||
|
||
def _ignore(path: str, names: List[str]) -> Set[str]: | ||
ignored_names = set() # type: Set[str] | ||
for name in names: | ||
if name == ".detignore": | ||
ignored_names.add(name) | ||
continue | ||
|
||
file_path = pathlib.Path(path) / name | ||
file_rel_path = file_path.relative_to(root_path) | ||
|
||
if ( | ||
file_path.is_dir() and ignore_spec.match_file(str(file_rel_path) + "/") | ||
) or ignore_spec.match_file(str(file_rel_path)): | ||
ignored_names.add(name) | ||
|
||
return ignored_names | ||
|
||
return _ignore | ||
|
||
|
||
def os_walk_to_v1Files( | ||
root_path: pathlib.Path, entry_prefix: pathlib.Path | ||
) -> Iterable[bindings.v1File]: | ||
ignore_spec = _build_detignore_pathspec(root_path) | ||
|
||
for parent, dirs, files in os.walk(str(root_path)): | ||
keep_dirs = [] | ||
for directory in dirs: | ||
dir_path = pathlib.Path(parent).joinpath(directory) | ||
dir_rel_path = dir_path.relative_to(root_path) | ||
|
||
# If the directory matches any path specified in .detignore, then ignore it. | ||
if ignore_spec.match_file(str(dir_rel_path)): | ||
continue | ||
if ignore_spec.match_file(str(dir_rel_path) + "/"): | ||
continue | ||
keep_dirs.append(directory) | ||
# Determined only supports POSIX-style file paths. Use as_posix() in case this code | ||
# is executed in a non-POSIX environment. | ||
entry_path = (entry_prefix / dir_rel_path).as_posix() | ||
|
||
yield v1file_utils.v1File_from_local_dir(entry_path, dir_path) | ||
# We can modify dirs in-place so that we do not recurse into ignored directories | ||
# See https://docs.python.org/3/library/os.html#os.walk | ||
dirs[:] = keep_dirs | ||
|
||
for file in files: | ||
file_path = pathlib.Path(parent).joinpath(file) | ||
file_rel_path = file_path.relative_to(root_path) | ||
|
||
# If the file is the .detignore file or matches one of the | ||
# paths specified in .detignore, then ignore it. | ||
if file_rel_path.name == ".detignore": | ||
continue | ||
if ignore_spec.match_file(str(file_rel_path)): | ||
continue | ||
|
||
# Determined only supports POSIX-style file paths. Use as_posix() in case this code | ||
# is executed in a non-POSIX environment. | ||
entry_path = (entry_prefix / file_rel_path).as_posix() | ||
|
||
try: | ||
entry = v1file_utils.v1File_from_local_file(entry_path, file_path) | ||
except OSError: | ||
print(f"Error reading '{entry_path}', skipping this file.") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you set |
||
continue | ||
|
||
yield entry |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import base64 | ||
import pathlib | ||
import tarfile | ||
from typing import Any, Dict | ||
|
||
from determined.common.api import bindings | ||
|
||
|
||
def v1File_size(f: bindings.v1File) -> int: | ||
if f.content: | ||
# The content is already base64-encoded, and we want the real length. | ||
return len(f.content) // 4 * 3 | ||
return 0 | ||
|
||
|
||
def v1File_to_dict(f: bindings.v1File) -> Dict[str, Any]: | ||
d = { | ||
"path": f.path, | ||
"type": f.type, | ||
"uid": f.uid, | ||
"gid": f.gid, | ||
# Echo API expects int-type int64 value | ||
"mtime": int(f.mtime), | ||
"mode": f.mode, | ||
} | ||
if f.type in (ord(tarfile.REGTYPE), ord(tarfile.DIRTYPE)): | ||
d["content"] = f.content | ||
return d | ||
|
||
|
||
def v1File_from_local_file(archive_path: str, path: pathlib.Path) -> bindings.v1File: | ||
with path.open("rb") as f: | ||
content = base64.b64encode(f.read()).decode("utf8") | ||
st = path.stat() | ||
return bindings.v1File( | ||
path=archive_path, | ||
type=ord(tarfile.REGTYPE), | ||
content=content, | ||
# Protobuf expects string-encoded int64 | ||
mtime=str(int(st.st_mtime)), | ||
mode=st.st_mode, | ||
uid=0, | ||
gid=0, | ||
) | ||
|
||
|
||
def v1File_from_local_dir(archive_path: str, path: pathlib.Path) -> bindings.v1File: | ||
st = path.stat() | ||
return bindings.v1File( | ||
path=archive_path, | ||
type=ord(tarfile.DIRTYPE), | ||
content="", | ||
# Protobuf expects string-encoded int64 | ||
mtime=str(int(st.st_mtime)), | ||
mode=st.st_mode, | ||
uid=0, | ||
gid=0, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: why bounce back and forth between pathlib.Path and str? Why not just use os.path.join() and os.path.relpath()?
non-blocking; feel free to ignore my inner C programmer which cringes at unnecessary
malloc()
calls.