File: byteyears.py

package info (click to toggle)
python2.3 2.3.5-3sarge2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 43,908 kB
  • ctags: 81,384
  • sloc: ansic: 266,250; python: 246,028; makefile: 4,194; perl: 3,702; lisp: 3,630; sh: 2,576; xml: 1,601; objc: 740; cpp: 106; sed: 2
file content (56 lines) | stat: -rwxr-xr-x 1,436 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! /usr/bin/env python

# Print the product of age and size of each file, in suitable units.
#
# Usage: byteyears [ -a | -m | -c ] file ...
#
# Options -[amc] select atime, mtime (default) or ctime as age.

import sys, os, time
from stat import *

# Use lstat() to stat files if it exists, else stat()
try:
    statfunc = os.lstat
except AttributeError:
    statfunc = os.stat

# Parse options
if sys.argv[1] == '-m':
    itime = ST_MTIME
    del sys.argv[1]
elif sys.argv[1] == '-c':
    itime = ST_CTIME
    del sys.argv[1]
elif sys.argv[1] == '-a':
    itime = ST_CTIME
    del sys.argv[1]
else:
    itime = ST_MTIME

secs_per_year = 365.0 * 24.0 * 3600.0   # Scale factor
now = time.time()                       # Current time, for age computations
status = 0                              # Exit status, set to 1 on errors

# Compute max file name length
maxlen = 1
for filename in sys.argv[1:]:
    maxlen = max(maxlen, len(filename))

# Process each argument in turn
for filename in sys.argv[1:]:
    try:
        st = statfunc(filename)
    except os.error, msg:
        sys.stderr.write('can\'t stat ' + `filename` + ': ' + `msg` + '\n')
        status = 1
        st = ()
    if st:
        anytime = st[itime]
        size = st[ST_SIZE]
        age = now - anytime
        byteyears = float(size) * float(age) / secs_per_year
        print filename.ljust(maxlen),
        print repr(int(byteyears)).rjust(8)

sys.exit(status)