Whitespace normalization.
[python/dscho.git] / Tools / scripts / byteyears.py
blobb2a114fc9355e782934f582cb447dd2b635d620a
1 #! /usr/bin/env python
3 # Print the product of age and size of each file, in suitable units.
5 # Usage: byteyears [ -a | -m | -c ] file ...
7 # Options -[amc] select atime, mtime (default) or ctime as age.
9 import sys, os, time
10 from stat import *
12 # Use lstat() to stat files if it exists, else stat()
13 try:
14 statfunc = os.lstat
15 except AttributeError:
16 statfunc = os.stat
18 # Parse options
19 if sys.argv[1] == '-m':
20 itime = ST_MTIME
21 del sys.argv[1]
22 elif sys.argv[1] == '-c':
23 itime = ST_CTIME
24 del sys.argv[1]
25 elif sys.argv[1] == '-a':
26 itime = ST_CTIME
27 del sys.argv[1]
28 else:
29 itime = ST_MTIME
31 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
32 now = time.time() # Current time, for age computations
33 status = 0 # Exit status, set to 1 on errors
35 # Compute max file name length
36 maxlen = 1
37 for filename in sys.argv[1:]:
38 maxlen = max(maxlen, len(filename))
40 # Process each argument in turn
41 for filename in sys.argv[1:]:
42 try:
43 st = statfunc(filename)
44 except os.error, msg:
45 sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
46 status = 1
47 st = ()
48 if st:
49 anytime = st[itime]
50 size = st[ST_SIZE]
51 age = now - anytime
52 byteyears = float(size) * float(age) / secs_per_year
53 print filename.ljust(maxlen),
54 print repr(int(byteyears)).rjust(8)
56 sys.exit(status)