Skip to content

Commit 21b70c1

Browse files
authored
fix(cli): make file size display consistent (#3111)
1 parent 3f062c0 commit 21b70c1

File tree

9 files changed

+200
-194
lines changed

9 files changed

+200
-194
lines changed

poetry.lock

Lines changed: 180 additions & 175 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

renku/core/dataset/dataset.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -483,17 +483,17 @@ def confirm_download(files):
483483
if yes:
484484
return
485485

486-
headers = {"checksum": "checksum", "filename": "name", "size_in_mb": "size (b)", "filetype": "type"}
486+
headers = {"checksum": "checksum", "filename": "name", "filesize_str": "size", "filetype": "type"}
487487
communication.echo(tabulate(files, headers=headers, floatfmt=".2f"))
488488
communication.confirm("Do you wish to download this version?", abort=True, warning=True)
489489

490490
def calculate_total_size(files):
491491
total_size = 0.0
492492
for file in files:
493-
if file.size_in_mb is not None:
494-
total_size += file.size_in_mb
493+
if file.filesize is not None:
494+
total_size += file.filesize
495495

496-
return total_size * 2**20
496+
return total_size
497497

498498
def remove_files(dataset):
499499
"""Remove files that exist in ``previous_dataset`` but not in ``dataset``.

renku/core/dataset/providers/dataverse.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939
from renku.core.interface.client_dispatcher import IClientDispatcher
4040
from renku.core.util import communication
4141
from renku.core.util.doi import extract_doi, get_doi_url, is_doi
42-
from renku.core.util.file_size import bytes_to_unit
4342
from renku.core.util.urls import remove_credentials
4443

4544
if TYPE_CHECKING:
@@ -283,7 +282,7 @@ def fix_data(self, data, **kwargs):
283282
source=file.remote_url.geturl(),
284283
filename=Path(file.name).name,
285284
checksum="",
286-
size_in_mb=bytes_to_unit(file.content_size, "mi"),
285+
filesize=file.content_size,
287286
filetype=file.file_format,
288287
path="",
289288
)

renku/core/dataset/providers/models.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from pathlib import Path
2121
from typing import Any, List, NamedTuple, Optional, Type
2222

23+
from humanize import naturalsize
2324
from marshmallow import EXCLUDE
2425

2526
from renku.command.schema.dataset import DatasetSchema
@@ -132,13 +133,16 @@ class ProviderDatasetFile:
132133
"""Store metadata for dataset files that will be downloaded from a provider."""
133134

134135
def __init__(
135-
self, source: Optional[str], filename: str, checksum: str, size_in_mb: Optional[float], filetype: str, path: str
136+
self, source: Optional[str], filename: str, checksum: str, filesize: Optional[int], filetype: str, path: str
136137
):
137138
self.checksum: str = checksum
138139
self.filename: str = filename
139140
self.filetype: str = filetype
140141
self.path: str = path
141-
self.size_in_mb: Optional[float] = size_in_mb
142+
self.filesize: Optional[int] = filesize
143+
self.filesize_str: Optional[str] = (
144+
naturalsize(filesize).upper().replace("BYTES", " B") if filesize is not None else None
145+
)
142146
self.source: Optional[str] = source
143147

144148

renku/core/dataset/providers/renku.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
from renku.core.interface.database_dispatcher import IDatabaseDispatcher
3434
from renku.core.project.project_properties import project_properties
3535
from renku.core.util import communication
36-
from renku.core.util.file_size import bytes_to_unit
3736
from renku.core.util.git import clone_renku_repository, get_cache_directory_for_repository, get_file_size
3837
from renku.core.util.metadata import is_external_file, make_project_temp_dir
3938
from renku.core.util.urls import remove_credentials
@@ -523,7 +522,7 @@ def _fetch_dataset(self, client_dispatcher: IClientDispatcher, database_dispatch
523522
checksum=file.entity.checksum,
524523
filename=Path(file.entity.path).name,
525524
filetype=Path(file.entity.path).suffix.replace(".", ""),
526-
size_in_mb=bytes_to_unit(get_file_size(self._remote_path, file.entity.path), "mi"),
525+
filesize=get_file_size(self._remote_path, file.entity.path),
527526
source=file.source,
528527
)
529528
for file in dataset.files

renku/core/dataset/providers/zenodo.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
from renku.core.dataset.providers.repository import RepositoryImporter, make_request
3131
from renku.core.util import communication
3232
from renku.core.util.doi import is_doi
33-
from renku.core.util.file_size import bytes_to_unit
3433
from renku.core.util.urls import remove_credentials
3534

3635
if TYPE_CHECKING:
@@ -219,7 +218,7 @@ def fix_data(self, data, **kwargs):
219218
source=file.remote_url.geturl(),
220219
filename=Path(file.filename).name,
221220
checksum=file.checksum,
222-
size_in_mb=bytes_to_unit(file.filesize, "mi"),
221+
filesize=file.filesize,
223222
filetype=file.type,
224223
path="",
225224
)

renku/core/migration/models/v9.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1405,9 +1405,9 @@ def full_path(self):
14051405
return Path(os.path.abspath(path))
14061406

14071407
@property
1408-
def size_in_mb(self):
1409-
"""Return file size in megabytes."""
1410-
return None if self.filesize is None else self.filesize * 1e-6
1408+
def filesize(self):
1409+
"""Return file size."""
1410+
return None if self.filesize is None else self.filesize
14111411

14121412
def __attrs_post_init__(self):
14131413
"""Set the property "name" after initialization."""

renku/core/util/git.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -831,7 +831,7 @@ def _clear_line(self):
831831
return GitProgress()
832832

833833

834-
def get_file_size(repository_path: Path, path: str) -> Optional[float]:
834+
def get_file_size(repository_path: Path, path: str) -> Optional[int]:
835835
"""Return file size for a file inside a git repository."""
836836
# NOTE: First try to get file size from Git LFS
837837
try:
@@ -865,11 +865,11 @@ def get_file_size(repository_path: Path, path: str) -> Optional[float]:
865865
multiplier = conversions.get(unit, None)
866866
if multiplier is None:
867867
continue
868-
return size * multiplier
868+
return int(size * multiplier)
869869

870870
# Return size of the file on disk
871871
full_path = repository_path / path
872-
return float(os.path.getsize(full_path)) if full_path.exists() else None
872+
return os.path.getsize(full_path) if full_path.exists() else None
873873

874874

875875
def shorten_message(message: str, line_length: int = 100, body_length: int = 65000) -> str:

tests/cli/test_integration_datasets.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ def test_dataset_import_renkulab_dataset_with_image(runner, project, client, cli
378378
assert 0 == result.exit_code, format_result_exception(result)
379379
assert "e69de29bb2d1d6434b8b29ae775ad8c2e48c5391" in result.output
380380

381-
assert "0.00" in result.output
381+
assert "0" in result.output
382382
assert "OK" in result.output
383383

384384
result = runner.invoke(cli, ["dataset", "ls-files"])

0 commit comments

Comments
 (0)