Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/byteyears.py @ 25838:8540887bcec6 2.2
Backport of 2.183:
Fix memory leak in add_subclass() found by NealN with valgrind.
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 18 Oct 2002 13:42:21 +0000 |
| parents | 14a27acc755a |
| children | 8a7a2f1f88f0 |
| rev | line source |
|---|---|
|
4573
73758db2f464
/usr/local/bin/python -> /usr/bin/env python
Guido van Rossum <guido@python.org>
parents:
1269
diff
changeset
|
1 #! /usr/bin/env python |
| 274 | 2 |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
3 # Print the product of age and size of each file, in suitable units. |
| 274 | 4 # |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
5 # Usage: byteyears [ -a | -m | -c ] file ... |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
6 # |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
7 # Options -[amc] select atime, mtime (default) or ctime as age. |
| 274 | 8 |
| 758 | 9 import sys, os, time |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
10 import string |
| 274 | 11 from stat import * |
| 12 | |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
13 # Use lstat() to stat files if it exists, else stat() |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
14 try: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
15 statfunc = os.lstat |
| 758 | 16 except AttributeError: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
17 statfunc = os.stat |
| 274 | 18 |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
19 # Parse options |
| 559 | 20 if sys.argv[1] == '-m': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
21 itime = ST_MTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
22 del sys.argv[1] |
| 559 | 23 elif sys.argv[1] == '-c': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
24 itime = ST_CTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
25 del sys.argv[1] |
| 559 | 26 elif sys.argv[1] == '-a': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
27 itime = ST_CTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
28 del sys.argv[1] |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
29 else: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
30 itime = ST_MTIME |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
31 |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
32 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
33 now = time.time() # Current time, for age computations |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
34 status = 0 # Exit status, set to 1 on errors |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
35 |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
36 # Compute max file name length |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
37 maxlen = 1 |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
38 for file in sys.argv[1:]: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
39 if len(file) > maxlen: maxlen = len(file) |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
40 |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
41 # Process each argument in turn |
| 274 | 42 for file in sys.argv[1:]: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
43 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
44 st = statfunc(file) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
45 except os.error, msg: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
46 sys.stderr.write('can\'t stat ' + `file` + ': ' + `msg` + '\n') |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
47 status = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
48 st = () |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
49 if st: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
50 anytime = st[itime] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
51 size = st[ST_SIZE] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
52 age = now - anytime |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
53 byteyears = float(size) * float(age) / secs_per_year |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
54 print string.ljust(file, maxlen), |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
55 print string.rjust(`int(byteyears)`, 8) |
| 274 | 56 |
| 57 sys.exit(status) |
