Skip to content

Commit 25d9bb6

Browse files
committed
improve pdu
1 parent 2c3317d commit 25d9bb6

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

tests-ng/pdu.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,19 +68,37 @@ def main(path: str, human: bool,
6868
if must_ignore(root, ign):
6969
debug(f'ignore root {root}')
7070
continue
71+
files.sort(key=lambda f: f[0] == '.')
7172
for file in files:
7273
fpath = os.path.join(root, file)
7374
if must_ignore(fpath, ign):
7475
debug(f'ignore sub {fpath}')
7576
continue
76-
size = os.path.getsize(fpath)
77+
# size = os.path.getsize(fpath)
78+
stat_info = os.stat(fpath)
79+
size = stat_info.st_size
7780
dirsz += size
7881
total += size
79-
if root != path:
80-
print(f'{size_to_str(dirsz, human=human, digits=1)} {root}')
81-
print(f'{size_to_str(total, human=human)} {path}')
82+
if root != path and dirsz > 0:
83+
print_line(dirsz, get_norm_path(root, path), human, digits=1)
84+
print_line(total, get_norm_path(path, path), human)
8285
return True
8386

87+
def get_norm_path(path: str, base: str) -> str:
88+
npath = os.path.normpath(path)
89+
bnpath = os.path.normpath(base)
90+
bbasename = os.path.basename(bnpath)
91+
rel = os.path.relpath(npath, bnpath)
92+
if rel == '.':
93+
return bbasename
94+
return os.path.join(bbasename, rel)
95+
96+
def print_line(size, path, human, digits=None):
97+
hsize = size_to_str(size, human=human, digits=digits)
98+
if not human:
99+
print(f'{hsize:<10}{path}')
100+
return
101+
print(f'{hsize:<10}{path}')
84102

85103
if __name__ == "__main__":
86104
parser = argparse.ArgumentParser(prog=NAME,

0 commit comments

Comments
 (0)