Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/byteyears.py @ 31175:5cc1a400a02c legacy-trunk
Use dictionary specific looping idiom where possible.
Simplifies and speeds-up the code.
| author | Raymond Hettinger <python@rcn.com> |
|---|---|
| date | Sat, 13 Dec 2003 14:46:46 +0000 |
| parents | 93c92f20b6bf |
| children | 71a294bd7950 |
| 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 |
| 274 | 10 from stat import * |
| 11 | |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
12 # 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
|
13 try: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
14 statfunc = os.lstat |
| 758 | 15 except AttributeError: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
16 statfunc = os.stat |
| 274 | 17 |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
18 # Parse options |
| 559 | 19 if sys.argv[1] == '-m': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
20 itime = ST_MTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
21 del sys.argv[1] |
| 559 | 22 elif sys.argv[1] == '-c': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
23 itime = ST_CTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
24 del sys.argv[1] |
| 559 | 25 elif sys.argv[1] == '-a': |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
26 itime = ST_CTIME |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
27 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
|
28 else: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
29 itime = ST_MTIME |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
30 |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
31 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
32 now = time.time() # Current time, for age computations |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
33 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
|
34 |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
35 # 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
|
36 maxlen = 1 |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
37 for filename in sys.argv[1:]: |
|
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
38 maxlen = max(maxlen, len(filename)) |
|
296
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
39 |
|
7a35d985763b
Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents:
274
diff
changeset
|
40 # Process each argument in turn |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
41 for filename in sys.argv[1:]: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
42 try: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
43 st = statfunc(filename) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
44 except os.error, msg: |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
45 sys.stderr.write('can\'t stat ' + `filename` + ': ' + `msg` + '\n') |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
46 status = 1 |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
47 st = () |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
48 if st: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
49 anytime = st[itime] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
50 size = st[ST_SIZE] |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
51 age = now - anytime |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
4573
diff
changeset
|
52 byteyears = float(size) * float(age) / secs_per_year |
|
29107
93c92f20b6bf
Fix use of 'file' as a variable name.
Andrew M. Kuchling <amk@amk.ca>
parents:
25219
diff
changeset
|
53 print filename.ljust(maxlen), |
|
25219
8a7a2f1f88f0
Apply diff2.txt from SF patch http://www.python.org/sf/572113
Walter Dörwald <walter@livinglogic.de>
parents:
15930
diff
changeset
|
54 print repr(int(byteyears)).rjust(8) |
| 274 | 55 |
| 56 sys.exit(status) |
