Skip to content

Commit 3ace301

Browse files
authored
Use scandir in DirectoryStore.getsize (#431)
* Require `scandir` for Python pre-3.5 As `scandir` was added to the `os` module in Python 3.5, require the backport package for `scandir` is installed for versions prior to that (e.g. Python 2.7). * Import `scandir` on Python 2/3 * Use `scandir` to determine directory size quickly * Note use of scandir in DirectoryStore's getsize * Use double backticks in release note's inline code * Use escaped whitespace after double backticks
1 parent adb2e2c commit 3ace301

File tree

4 files changed

+15
-7
lines changed

4 files changed

+15
-7
lines changed

docs/release.rst

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,19 @@ Release notes
66
2.3.2
77
-----
88

9+
Enhancements
10+
~~~~~~~~~~~~
11+
12+
* Use ``scandir`` in ``DirectoryStore``'s ``getsize`` method.
13+
By :user:`John Kirkham <jakirkham>`; :issue:`431`
14+
915
Bug fixes
1016
~~~~~~~~~
1117

1218
* Add and use utility functions to simplify reading and writing JSON.
1319
By :user:`John Kirkham <jakirkham>`; :issue:`429`, :issue:`430`
1420

15-
* Fix `collections`'s `DeprecationWarning`s.
21+
* Fix ``collections``'s ``DeprecationWarning``\ s.
1622
By :user:`John Kirkham <jakirkham>`; :issue:`432`
1723

1824

setup.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
'numcodecs>=0.6.2',
1818
]
1919

20+
if sys.version_info < (3, 5):
21+
dependencies.append('scandir')
2022
if sys.version_info < (3, 3) and sys.platform == "win32":
2123
dependencies.append('pyosreplace')
2224

zarr/compat.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def OrderedDict_move_to_end(od, key):
2121
od[key] = od.pop(key)
2222

2323
from collections import Mapping, MutableMapping
24+
from scandir import scandir
2425

2526

2627
else: # pragma: py2 no cover
@@ -35,3 +36,4 @@ def OrderedDict_move_to_end(od, key):
3536
od.move_to_end(key)
3637

3738
from collections.abc import Mapping, MutableMapping
39+
from os import scandir

zarr/storage.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
normalize_storage_path, buffer_size,
3838
normalize_fill_value, nolock, normalize_dtype)
3939
from zarr.meta import encode_array_metadata, encode_group_metadata
40-
from zarr.compat import PY2, MutableMapping, OrderedDict_move_to_end
40+
from zarr.compat import PY2, MutableMapping, OrderedDict_move_to_end, scandir
4141
from numcodecs.registry import codec_registry
4242
from numcodecs.compat import ensure_bytes, ensure_contiguous_ndarray
4343
from zarr.errors import (err_contains_group, err_contains_array, err_bad_compressor,
@@ -846,12 +846,10 @@ def getsize(self, path=None):
846846
if os.path.isfile(fs_path):
847847
return os.path.getsize(fs_path)
848848
elif os.path.isdir(fs_path):
849-
children = os.listdir(fs_path)
850849
size = 0
851-
for child in children:
852-
child_fs_path = os.path.join(fs_path, child)
853-
if os.path.isfile(child_fs_path):
854-
size += os.path.getsize(child_fs_path)
850+
for child in scandir(fs_path):
851+
if child.is_file():
852+
size += child.stat().st_size
855853
return size
856854
else:
857855
return 0

0 commit comments

Comments
 (0)