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