blob: bada5a538abbd5d095f362e2e986d49707ba3249 [file] [log] [blame]
Guido van Rossumf06ee5f1996-11-27 19:52:01 +00001#! /usr/bin/env python
Guido van Rossumec758ea1991-06-04 20:36:54 +00002
Guido van Rossumbcf50121991-07-01 18:20:35 +00003# Print the product of age and size of each file, in suitable units.
Guido van Rossumec758ea1991-06-04 20:36:54 +00004#
Guido van Rossumbcf50121991-07-01 18:20:35 +00005# Usage: byteyears [ -a | -m | -c ] file ...
6#
7# Options -[amc] select atime, mtime (default) or ctime as age.
Guido van Rossumec758ea1991-06-04 20:36:54 +00008
Guido van Rossum9a6757d1992-03-30 11:15:26 +00009import sys, os, time
Guido van Rossumbcf50121991-07-01 18:20:35 +000010import string
Guido van Rossumec758ea1991-06-04 20:36:54 +000011from stat import *
12
Guido van Rossumbcf50121991-07-01 18:20:35 +000013# Use lstat() to stat files if it exists, else stat()
14try:
Guido van Rossum9a6757d1992-03-30 11:15:26 +000015 statfunc = os.lstat
16except AttributeError:
17 statfunc = os.stat
Guido van Rossumec758ea1991-06-04 20:36:54 +000018
Guido van Rossumbcf50121991-07-01 18:20:35 +000019# Parse options
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000020if sys.argv[1] == '-m':
Guido van Rossumbcf50121991-07-01 18:20:35 +000021 itime = ST_MTIME
22 del sys.argv[1]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000023elif sys.argv[1] == '-c':
Guido van Rossumbcf50121991-07-01 18:20:35 +000024 itime = ST_CTIME
25 del sys.argv[1]
Guido van Rossumbdfcfcc1992-01-01 19:35:13 +000026elif sys.argv[1] == '-a':
Guido van Rossumbcf50121991-07-01 18:20:35 +000027 itime = ST_CTIME
28 del sys.argv[1]
29else:
30 itime = ST_MTIME
31
32secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
33now = time.time() # Current time, for age computations
34status = 0 # Exit status, set to 1 on errors
35
36# Compute max file name length
37maxlen = 1
38for file in sys.argv[1:]:
39 if len(file) > maxlen: maxlen = len(file)
40
41# Process each argument in turn
Guido van Rossumec758ea1991-06-04 20:36:54 +000042for file in sys.argv[1:]:
43 try:
Guido van Rossumbcf50121991-07-01 18:20:35 +000044 st = statfunc(file)
Guido van Rossum9a6757d1992-03-30 11:15:26 +000045 except os.error, msg:
Guido van Rossumec758ea1991-06-04 20:36:54 +000046 sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n')
47 status = 1
48 st = ()
49 if st:
Guido van Rossumbcf50121991-07-01 18:20:35 +000050 anytime = st[itime]
Guido van Rossumec758ea1991-06-04 20:36:54 +000051 size = st[ST_SIZE]
Guido van Rossumbcf50121991-07-01 18:20:35 +000052 age = now - anytime
Guido van Rossumec758ea1991-06-04 20:36:54 +000053 byteyears = float(size) * float(age) / secs_per_year
Guido van Rossumbcf50121991-07-01 18:20:35 +000054 print string.ljust(file, maxlen),
55 print string.rjust(`int(byteyears)`, 8)
Guido van Rossumec758ea1991-06-04 20:36:54 +000056
57sys.exit(status)