Skip to content

Commit

Permalink
du alternative
Browse files Browse the repository at this point in the history
  • Loading branch information
deadc0de6 committed Feb 16, 2024
1 parent 70e30a2 commit 2f6ba0b
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 7 deletions.
88 changes: 88 additions & 0 deletions tests-ng/pdu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#!/usr/bin/env python3
# author: deadc0de6 (https://github.com/deadc0de6)
# Copyright (c) 2024, deadc0de6
#
# a naive python du alternative
# ./tests-ng/pdu.py . --ignore='*/.git*' -H
#


import os
import argparse
import fnmatch
from typing import List


NAME = 'pdu'
DEBUG = True


def debug(txt: str):
"""debug output"""
if not DEBUG:
return
print(f'[DEBUG] {txt}')


def size_to_str(size: float, human: bool = False) -> str:
"""size to string"""
div = 1024.
suf = ['B', 'K', 'M', 'G', 'T', 'P']
if not human or size < div:
return f'{size}'
for i in suf:
if size < div:
return f'{size:.1f}{i}'
size = size / div
sufix = suf[-1]
return f'{size:.1f}{sufix}'


def must_ignore(path: str, patterns: List[str]) -> bool:
"""must ignore path"""
if not patterns:
return False
lst = [fnmatch.fnmatch(path, patt) for patt in patterns]
debug(f'{path} -> {lst}')
return any(lst)


def main(path: str, human: bool,
ign: List[str] = None):
"""entry point"""
if not os.path.exists(path):
print(f'[ERROR] {path} does not exist')
return False
total = 0
for root, _, files in os.walk(path, topdown=True):
dirsz = 0
if must_ignore(root, ign):
debug(f'ignore root {root}')
continue
for file in files:
fpath = os.path.join(root, file)
if must_ignore(fpath, ign):
debug(f'ignore sub {fpath}')
continue
size = os.path.getsize(fpath)
dirsz += size
total += size
if root != path:
print(f'{size_to_str(dirsz, human=human)} {root}')
print(f'{size_to_str(total, human=human)} {path}')
return True


if __name__ == "__main__":
parser = argparse.ArgumentParser(prog=NAME,
description='python du')
parser.add_argument('path')
parser.add_argument('-H', '--human',
action='store_true')
parser.add_argument('-i', '--ignore',
nargs='+')
parser.add_argument('-d', '--debug',
action='store_true')
args = parser.parse_args()
DEBUG = args.debug
main(args.path, args.human, ign=args.ignore)
6 changes: 4 additions & 2 deletions tests-ng/test-du.sh
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@ cnt=$(wc -l "${out}" | awk '{print $1}')

# bin size
echo ">>> test du bin size raw <<<"
expected=$(du -c --block=1 --apparent-size "${cur}/../cmd/gocatcli" | tail -1 | awk '{print $1}')
#expected=$(du -c --block=1 --apparent-size "${cur}/../cmd/gocatcli" | tail -1 | awk '{print $1}')
expected=$("${cur}/pdu.py" "${cur}/../cmd/gocatcli" | tail -1 | awk '{print $1}')
size=$(grep '^.* *gocatcli/cmd/gocatcli$' "${out}" | awk '{print $1}')
echo "size:${size} VS exp:${expected}"
[ "${expected}" != "${size}" ] && (echo "bad bin size" && exit 1)

# total size
echo ">>> test du total size raw <<<"
expected=$(du -c --block=1 --apparent-size "${cur}/../" | tail -1 | awk '{print $1}')
#expected=$(du -c --block=1 --apparent-size "${cur}/../" | tail -1 | awk '{print $1}')
expected=$("${cur}/pdu.py" "${cur}/../" | tail -1 | awk '{print $1}')
#cat_file "${out}"
size=$(tail -1 "${out}" | awk '{print $1}')
echo "size:${size} VS exp:${expected}"
Expand Down
14 changes: 9 additions & 5 deletions tests-ng/test-size.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ out="${tmpd}/output.txt"
echo ">>> test file size <<<"
"${bin}" --debug ls -r -S -c "${catalog}" "${cur}/../internal/walker/walker.go" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
# shellcheck disable=SC2126
du --block=1 "${cur}/../internal/walker/walker.go"
expected=$(du --block=1 --apparent-size "${cur}/../internal/walker/walker.go" | awk '{print $1}')
#expected=$(du --block=1 --apparent-size "${cur}/../internal/walker/walker.go" | awk '{print $1}')
# shellcheck disable=SC2012
expected=$(ls -la "${cur}/../internal/walker/walker.go" | awk '{print $5}')
cat_file "${out}"
size=$(grep 'walker.go' "${out}" | awk '{print $4}')
echo "size:${size} VS exp:${expected}"
Expand All @@ -40,16 +41,19 @@ echo "size:${size} VS exp:${expected}"
echo ">>> test directory size <<<"
"${bin}" --debug ls -r -S -c "${catalog}" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
# shellcheck disable=SC2126
expected=$(du -c --block=1 --apparent-size "${cur}/../internal/catcli" | tail -1 | awk '{print $1}')
cat_file "${out}"
#expected=$(du -c --block=1 --apparent-size "${cur}/../internal/catcli" | tail -1 | awk '{print $1}')
"${cur}/pdu.py" "${cur}/../internal/catcli" | tail -1
expected=$("${cur}/pdu.py" "${cur}/../internal/catcli" | tail -1 | awk '{print $1}')
#cat_file "${out}"
size=$(grep '^catcli' "${out}" | awk '{print $4}')
echo "size:${size} VS exp:${expected}"
[ "${size}" != "${expected}" ] && echo "expecting ${expected} (got ${size})" && exit 1

echo ">>> test storage size <<<"
"${bin}" --debug ls -S -c "${catalog}" | sed -e 's/\x1b\[[0-9;]*m//g' > "${out}"
cat_file "${out}"
expected=$(du -c --block=1 --apparent-size "${cur}/../internal" | tail -1 | awk '{print $1}')
#expected=$(du -c --block=1 --apparent-size "${cur}/../internal" | tail -1 | awk '{print $1}')
expected=$("${cur}/pdu.py" "${cur}/../internal" | tail -1 | awk '{print $1}')
size=$(grep '^storage' "${out}" | awk '{print $3}')
echo "size:${size} VS exp:${expected}"
[ "${size}" != "${expected}" ] && echo "expecting ${expected} (got ${size})" && exit 1
Expand Down

0 comments on commit 2f6ba0b

Please sign in to comment.