Skip to content
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

Make path_size_megabytes() more robust. #2049

Merged
merged 2 commits into from
Jun 8, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions sky/backends/backend_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,21 +276,36 @@ def _optimize_file_mounts(yaml_path: str) -> None:


def path_size_megabytes(path: str) -> int:
"""Returns the size of 'path' (directory or file) in megabytes."""
"""Returns the size of 'path' (directory or file) in megabytes.

Returns:
If successful: the size of 'path' in megabytes, rounded down. Otherwise,
-1.
"""
resolved_path = pathlib.Path(path).expanduser().resolve()
git_exclude_filter = ''
if (resolved_path / command_runner.GIT_EXCLUDE).exists():
# Ensure file exists; otherwise, rsync will error out.
git_exclude_filter = command_runner.RSYNC_EXCLUDE_OPTION.format(
str(resolved_path / command_runner.GIT_EXCLUDE))
rsync_output = str(
subprocess.check_output(
f'rsync {command_runner.RSYNC_DISPLAY_OPTION} '
f'{command_runner.RSYNC_FILTER_OPTION} '
f'{git_exclude_filter} --dry-run {path!r}',
shell=True).splitlines()[-1])
total_bytes = rsync_output.split(' ')[3].replace(',', '')
return int(float(total_bytes)) // 10**6
rsync_output = ''
try:
rsync_output = str(
subprocess.check_output(
f'rsync {command_runner.RSYNC_DISPLAY_OPTION} '
f'{command_runner.RSYNC_FILTER_OPTION} '
f'{git_exclude_filter} --dry-run {path!r}',
shell=True))
except subprocess.CalledProcessError:
return -1
Michaelvll marked this conversation as resolved.
Show resolved Hide resolved
match = re.search(r'total size is (\d+)', rsync_output)
Michaelvll marked this conversation as resolved.
Show resolved Hide resolved
if match is not None:
try:
total_bytes = int(float(match.group(1)))
return total_bytes // (1024**2)
except ValueError:
pass # Maybe different rsync versions have different output.
concretevitamin marked this conversation as resolved.
Show resolved Hide resolved
return -1


class FileMountHelper(object):
Expand Down