diff options
author | Guido van Rossum <guido@python.org> | 1991-07-01 18:20:35 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1991-07-01 18:20:35 (GMT) |
commit | bcf50128874cf934e3f48a77b1d1da2e60b8b6aa (patch) | |
tree | 746c2a2e2e60f0256f84a559b3de0555000f8e52 | |
parent | 30a685f0fe60c11c18a5d7d2bd9abc6981c16276 (diff) | |
download | cpython-bcf50128874cf934e3f48a77b1d1da2e60b8b6aa.zip cpython-bcf50128874cf934e3f48a77b1d1da2e60b8b6aa.tar.gz cpython-bcf50128874cf934e3f48a77b1d1da2e60b8b6aa.tar.bz2 |
Add options -amc; do lstat if possible; columnize properly.
-rwxr-xr-x | Tools/scripts/byteyears.py | 48 |
1 files changed, 38 insertions, 10 deletions
diff --git a/Tools/scripts/byteyears.py b/Tools/scripts/byteyears.py index 8c5ec69..06f7559 100755 --- a/Tools/scripts/byteyears.py +++ b/Tools/scripts/byteyears.py @@ -1,29 +1,57 @@ #! /usr/local/python -# byteyears file ... +# Print the product of age and size of each file, in suitable units. # -# Print a number representing the product of age and size of each file, -# in suitable units. +# Usage: byteyears [ -a | -m | -c ] file ... +# +# Options -[amc] select atime, mtime (default) or ctime as age. import sys, posix, time +import string from stat import * -secs_per_year = 365.0 * 24.0 * 3600.0 -now = time.time() -status = 0 +# Use lstat() to stat files if it exists, else stat() +try: + statfunc = posix.lstat +except NameError: + statfunc = posix.stat + +# Parse options +if sys.argv[1] = '-m': + itime = ST_MTIME + del sys.argv[1] +elif sys.argv[1] = '-c': + itime = ST_CTIME + del sys.argv[1] +elif sys.argv[1] = '-a': + itime = ST_CTIME + del sys.argv[1] +else: + itime = ST_MTIME + +secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor +now = time.time() # Current time, for age computations +status = 0 # Exit status, set to 1 on errors + +# Compute max file name length +maxlen = 1 +for file in sys.argv[1:]: + if len(file) > maxlen: maxlen = len(file) +# Process each argument in turn for file in sys.argv[1:]: try: - st = posix.stat(file) + st = statfunc(file) except posix.error, msg: sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n') status = 1 st = () if st: - mtime = st[ST_MTIME] + anytime = st[itime] size = st[ST_SIZE] - age = now - mtime + age = now - anytime byteyears = float(size) * float(age) / secs_per_year - print file + '\t\t' + `int(byteyears)` + print string.ljust(file, maxlen), + print string.rjust(`int(byteyears)`, 8) sys.exit(status) |