Benjamin Peterson | 90f5ba5 | 2010-03-11 22:53:45 +0000 | [diff] [blame] | 1 | #! /usr/bin/env python3 |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 2 | |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 3 | # Print the product of age and size of each file, in suitable units. |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 4 | # |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 5 | # Usage: byteyears [ -a | -m | -c ] file ... |
| 6 | # |
| 7 | # Options -[amc] select atime, mtime (default) or ctime as age. |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 9 | import sys, os, time |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 10 | from stat import * |
| 11 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 12 | def main(): |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 13 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 14 | # Use lstat() to stat files if it exists, else stat() |
Tim Peters | 70c4378 | 2001-01-17 08:48:39 +0000 | [diff] [blame] | 15 | try: |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 16 | statfunc = os.lstat |
| 17 | except AttributeError: |
| 18 | statfunc = os.stat |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 19 | |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 20 | # 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 Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 46 | except os.error as msg: |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 47 | 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 Winter | 6afaeb7 | 2007-08-03 17:06:41 +0000 | [diff] [blame] | 55 | print(filename.ljust(maxlen), end=' ') |
| 56 | print(repr(int(byteyears)).rjust(8)) |
Andrew M. Kuchling | e236b38 | 2004-08-09 17:27:55 +0000 | [diff] [blame] | 57 | |
| 58 | sys.exit(status) |
| 59 | |
| 60 | if __name__ == '__main__': |
| 61 | main() |