-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Reimplement, because CPython3.3 impl is rather bloated | ||
import os | ||
from collections import namedtuple | ||
|
||
_ntuple_diskusage = namedtuple("usage", ("total", "used", "free")) | ||
|
||
|
||
def rmtree(d): | ||
if not d: | ||
raise ValueError | ||
|
||
for name, type, *_ in os.ilistdir(d): | ||
path = d + "/" + name | ||
if type & 0x4000: # dir | ||
rmtree(path) | ||
else: # file | ||
os.unlink(path) | ||
os.rmdir(d) | ||
|
||
|
||
def copyfileobj(src, dest, length=512): | ||
if hasattr(src, "readinto"): | ||
buf = bytearray(length) | ||
while True: | ||
sz = src.readinto(buf) | ||
if not sz: | ||
break | ||
if sz == length: | ||
dest.write(buf) | ||
else: | ||
b = memoryview(buf)[:sz] | ||
dest.write(b) | ||
else: | ||
while True: | ||
buf = src.read(length) | ||
if not buf: | ||
break | ||
dest.write(buf) | ||
|
||
|
||
def disk_usage(path): | ||
bit_tuple = os.statvfs(path) | ||
blksize = bit_tuple[0] # system block size | ||
total = bit_tuple[2] * blksize | ||
free = bit_tuple[3] * blksize | ||
used = total - free | ||
|
||
return _ntuple_diskusage(total, used, free) |