1996-11-27 20:52:01 +01:00
|
|
|
#! /usr/bin/env python
|
1991-06-04 22:36:54 +02:00
|
|
|
|
1991-07-01 20:20:35 +02:00
|
|
|
# Print the product of age and size of each file, in suitable units.
|
1991-06-04 22:36:54 +02:00
|
|
|
#
|
1991-07-01 20:20:35 +02:00
|
|
|
# Usage: byteyears [ -a | -m | -c ] file ...
|
|
|
|
#
|
|
|
|
# Options -[amc] select atime, mtime (default) or ctime as age.
|
1991-06-04 22:36:54 +02:00
|
|
|
|
1992-03-30 13:15:26 +02:00
|
|
|
import sys, os, time
|
1991-06-04 22:36:54 +02:00
|
|
|
from stat import *
|
|
|
|
|
1991-07-01 20:20:35 +02:00
|
|
|
# Use lstat() to stat files if it exists, else stat()
|
|
|
|
try:
|
2001-01-17 09:48:39 +01:00
|
|
|
statfunc = os.lstat
|
1992-03-30 13:15:26 +02:00
|
|
|
except AttributeError:
|
2001-01-17 09:48:39 +01:00
|
|
|
statfunc = os.stat
|
1991-07-01 20:20:35 +02:00
|
|
|
|
|
|
|
# Parse options
|
1992-01-01 20:35:13 +01:00
|
|
|
if sys.argv[1] == '-m':
|
2001-01-17 09:48:39 +01:00
|
|
|
itime = ST_MTIME
|
|
|
|
del sys.argv[1]
|
1992-01-01 20:35:13 +01:00
|
|
|
elif sys.argv[1] == '-c':
|
2001-01-17 09:48:39 +01:00
|
|
|
itime = ST_CTIME
|
|
|
|
del sys.argv[1]
|
1992-01-01 20:35:13 +01:00
|
|
|
elif sys.argv[1] == '-a':
|
2001-01-17 09:48:39 +01:00
|
|
|
itime = ST_CTIME
|
|
|
|
del sys.argv[1]
|
1991-07-01 20:20:35 +02:00
|
|
|
else:
|
2001-01-17 09:48:39 +01:00
|
|
|
itime = ST_MTIME
|
1991-07-01 20:20:35 +02:00
|
|
|
|
2001-01-17 09:48:39 +01:00
|
|
|
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
|
1991-07-01 20:20:35 +02:00
|
|
|
|
|
|
|
# Compute max file name length
|
|
|
|
maxlen = 1
|
2003-05-13 20:14:25 +02:00
|
|
|
for filename in sys.argv[1:]:
|
|
|
|
maxlen = max(maxlen, len(filename))
|
1991-06-04 22:36:54 +02:00
|
|
|
|
1991-07-01 20:20:35 +02:00
|
|
|
# Process each argument in turn
|
2003-05-13 20:14:25 +02:00
|
|
|
for filename in sys.argv[1:]:
|
2001-01-17 09:48:39 +01:00
|
|
|
try:
|
2003-05-13 20:14:25 +02:00
|
|
|
st = statfunc(filename)
|
2001-01-17 09:48:39 +01:00
|
|
|
except os.error, msg:
|
2003-05-13 20:14:25 +02:00
|
|
|
sys.stderr.write('can\'t stat ' + `filename` + ': ' + `msg` + '\n')
|
2001-01-17 09:48:39 +01:00
|
|
|
status = 1
|
|
|
|
st = ()
|
|
|
|
if st:
|
|
|
|
anytime = st[itime]
|
|
|
|
size = st[ST_SIZE]
|
|
|
|
age = now - anytime
|
|
|
|
byteyears = float(size) * float(age) / secs_per_year
|
2003-05-13 20:14:25 +02:00
|
|
|
print filename.ljust(maxlen),
|
2002-09-11 22:36:02 +02:00
|
|
|
print repr(int(byteyears)).rjust(8)
|
1991-06-04 22:36:54 +02:00
|
|
|
|
|
|
|
sys.exit(status)
|