1991-06-04 20:36:54 +00:00
|
|
|
#! /usr/local/python
|
|
|
|
|
1991-07-01 18:20:35 +00:00
|
|
|
# Print the product of age and size of each file, in suitable units.
|
1991-06-04 20:36:54 +00:00
|
|
|
#
|
1991-07-01 18:20:35 +00:00
|
|
|
# Usage: byteyears [ -a | -m | -c ] file ...
|
|
|
|
#
|
|
|
|
# Options -[amc] select atime, mtime (default) or ctime as age.
|
1991-06-04 20:36:54 +00:00
|
|
|
|
|
|
|
import sys, posix, time
|
1991-07-01 18:20:35 +00:00
|
|
|
import string
|
1991-06-04 20:36:54 +00:00
|
|
|
from stat import *
|
|
|
|
|
1991-07-01 18:20:35 +00:00
|
|
|
# Use lstat() to stat files if it exists, else stat()
|
|
|
|
try:
|
|
|
|
statfunc = posix.lstat
|
|
|
|
except NameError:
|
|
|
|
statfunc = posix.stat
|
|
|
|
|
|
|
|
# Parse options
|
1992-01-01 19:35:13 +00:00
|
|
|
if sys.argv[1] == '-m':
|
1991-07-01 18:20:35 +00:00
|
|
|
itime = ST_MTIME
|
|
|
|
del sys.argv[1]
|
1992-01-01 19:35:13 +00:00
|
|
|
elif sys.argv[1] == '-c':
|
1991-07-01 18:20:35 +00:00
|
|
|
itime = ST_CTIME
|
|
|
|
del sys.argv[1]
|
1992-01-01 19:35:13 +00:00
|
|
|
elif sys.argv[1] == '-a':
|
1991-07-01 18:20:35 +00:00
|
|
|
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)
|
1991-06-04 20:36:54 +00:00
|
|
|
|
1991-07-01 18:20:35 +00:00
|
|
|
# Process each argument in turn
|
1991-06-04 20:36:54 +00:00
|
|
|
for file in sys.argv[1:]:
|
|
|
|
try:
|
1991-07-01 18:20:35 +00:00
|
|
|
st = statfunc(file)
|
1991-06-04 20:36:54 +00:00
|
|
|
except posix.error, msg:
|
|
|
|
sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n')
|
|
|
|
status = 1
|
|
|
|
st = ()
|
|
|
|
if st:
|
1991-07-01 18:20:35 +00:00
|
|
|
anytime = st[itime]
|
1991-06-04 20:36:54 +00:00
|
|
|
size = st[ST_SIZE]
|
1991-07-01 18:20:35 +00:00
|
|
|
age = now - anytime
|
1991-06-04 20:36:54 +00:00
|
|
|
byteyears = float(size) * float(age) / secs_per_year
|
1991-07-01 18:20:35 +00:00
|
|
|
print string.ljust(file, maxlen),
|
|
|
|
print string.rjust(`int(byteyears)`, 8)
|
1991-06-04 20:36:54 +00:00
|
|
|
|
|
|
|
sys.exit(status)
|