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

Does not work on Windows #9

Open
hapasa opened this issue May 26, 2020 · 1 comment
Open

Does not work on Windows #9

hapasa opened this issue May 26, 2020 · 1 comment

Comments

@hapasa
Copy link

hapasa commented May 26, 2020

Throws

AttributeError: 'os.stat_result' object has no attribute 'st_blocks'

instead

@wdoekes
Copy link
Member

wdoekes commented May 26, 2020

Try this:

diff --git a/dutree/dutree.py b/dutree/dutree.py
index 5f8a732..dbd3294 100755
--- a/dutree/dutree.py
+++ b/dutree/dutree.py
@@ -303,6 +303,20 @@ class DuNode:
 class DuScan:
     "Disk Usage Tree scanner"
 
+    class EmulateUnixStatResult:
+        """On some Unix systems (such as Linux), the following
+        attributes may also be available:
+        - st_blocks
+        ...
+
+        Windows does not have it. Wrap the stat_result in a
+        EmulateUnixStatResult and pretend we have st_blocks.
+        """
+        def __init__(self, st):
+            self.st_blocks = (st.st_size + 511) >> 9
+            self.st_mode = st.st_mode
+            self.st_size = st.st_size
+
     def __init__(self, pathname):
         self._path = self._normpath(pathname)
         self._tree = None
@@ -390,6 +404,15 @@ class DuScan:
                 warnings.warn(str(e), OsWarning)
                 continue
 
+            try:
+                st.st_blocks
+            except AttributeError:
+                # In Python, using exceptions for the _uncommon_ case,
+                # keeps the common (unix/linux) case (which has st_blocks)
+                # fast.
+                warnings.warn('No st_blocks on your OS, emulating', OsWarning)
+                st = self.EmulateUnixStatResult(st)
+
             if S_ISREG(st.st_mode):
                 if st.st_blocks == 0:
                     # Pseudo-files, like the one in /proc have 0-block

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants