Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 1 | #! /usr/local/python |
| 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 | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 10 | import string |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 11 | from stat import * |
| 12 | |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 13 | # Use lstat() to stat files if it exists, else stat() |
| 14 | try: |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 15 | statfunc = os.lstat |
| 16 | except AttributeError: |
| 17 | statfunc = os.stat |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 18 | |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 19 | # Parse options |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 20 | if sys.argv[1] == '-m': |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 21 | itime = ST_MTIME |
| 22 | del sys.argv[1] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 23 | elif sys.argv[1] == '-c': |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 24 | itime = ST_CTIME |
| 25 | del sys.argv[1] |
Guido van Rossum | bdfcfcc | 1992-01-01 19:35:13 +0000 | [diff] [blame] | 26 | elif sys.argv[1] == '-a': |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 27 | itime = ST_CTIME |
| 28 | del sys.argv[1] |
| 29 | else: |
| 30 | itime = ST_MTIME |
| 31 | |
| 32 | secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor |
| 33 | now = time.time() # Current time, for age computations |
| 34 | status = 0 # Exit status, set to 1 on errors |
| 35 | |
| 36 | # Compute max file name length |
| 37 | maxlen = 1 |
| 38 | for file in sys.argv[1:]: |
| 39 | if len(file) > maxlen: maxlen = len(file) |
| 40 | |
| 41 | # Process each argument in turn |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 42 | for file in sys.argv[1:]: |
| 43 | try: |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 44 | st = statfunc(file) |
Guido van Rossum | 9a6757d | 1992-03-30 11:15:26 +0000 | [diff] [blame] | 45 | except os.error, msg: |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 46 | sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n') |
| 47 | status = 1 |
| 48 | st = () |
| 49 | if st: |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 50 | anytime = st[itime] |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 51 | size = st[ST_SIZE] |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 52 | age = now - anytime |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 53 | byteyears = float(size) * float(age) / secs_per_year |
Guido van Rossum | bcf5012 | 1991-07-01 18:20:35 +0000 | [diff] [blame] | 54 | print string.ljust(file, maxlen), |
| 55 | print string.rjust(`int(byteyears)`, 8) |
Guido van Rossum | ec758ea | 1991-06-04 20:36:54 +0000 | [diff] [blame] | 56 | |
| 57 | sys.exit(status) |