blob: 490b37f0bf22c72321259afbccd13e4474a17cb4 [file] [log] [blame]
Benjamin Peterson90f5ba52010-03-11 22:53:45 +00001#! /usr/bin/env python3
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 Rossumec758ea1991-06-04 20:36:54 +000010from stat import *
11
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000012def main():
Guido van Rossumec758ea1991-06-04 20:36:54 +000013
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000014 # Use lstat() to stat files if it exists, else stat()
Tim Peters70c43782001-01-17 08:48:39 +000015 try:
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000016 statfunc = os.lstat
17 except AttributeError:
18 statfunc = os.stat
Guido van Rossumec758ea1991-06-04 20:36:54 +000019
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000020 # Parse options
21 if sys.argv[1] == '-m':
22 itime = ST_MTIME
23 del sys.argv[1]
24 elif sys.argv[1] == '-c':
25 itime = ST_CTIME
26 del sys.argv[1]
27 elif sys.argv[1] == '-a':
28 itime = ST_CTIME
29 del sys.argv[1]
30 else:
31 itime = ST_MTIME
32
33 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
34 now = time.time() # Current time, for age computations
35 status = 0 # Exit status, set to 1 on errors
36
37 # Compute max file name length
38 maxlen = 1
39 for filename in sys.argv[1:]:
40 maxlen = max(maxlen, len(filename))
41
42 # Process each argument in turn
43 for filename in sys.argv[1:]:
44 try:
45 st = statfunc(filename)
Guido van Rossumb940e112007-01-10 16:19:56 +000046 except os.error as msg:
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000047 sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
48 status = 1
49 st = ()
50 if st:
51 anytime = st[itime]
52 size = st[ST_SIZE]
53 age = now - anytime
54 byteyears = float(size) * float(age) / secs_per_year
Collin Winter6afaeb72007-08-03 17:06:41 +000055 print(filename.ljust(maxlen), end=' ')
56 print(repr(int(byteyears)).rjust(8))
Andrew M. Kuchlinge236b382004-08-09 17:27:55 +000057
58 sys.exit(status)
59
60if __name__ == '__main__':
61 main()