annotate Tools/scripts/byteyears.py @ 32273:95287cddbc1e legacy-trunk

add -L flag to cause leaks(1) command to run just before exit
author Skip Montanaro <skip@pobox.com>
date Sun, 06 Jun 2004 15:53:18 +0000
parents 71a294bd7950
children 981fff7363b9
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
8
758
bfc655074523 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
9 import sys, os, time
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
10 from stat import *
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
bfc655074523 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
15 except AttributeError:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
16 statfunc = os.stat
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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:
31515
71a294bd7950 Replace backticks with repr() or "%r"
Walter Dörwald <walter@livinglogic.de>
parents: 29107
diff changeset
45 sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 sys.exit(status)