annotate Tools/scripts/byteyears.py @ 20737:00a581bdedeb legacy-trunk

When overriding __str__ or __repr__, set the tp_print slot to NULL.
author Guido van Rossum <guido@python.org>
date Mon, 29 Oct 2001 14:33:44 +0000
parents 14a27acc755a
children 8a7a2f1f88f0
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
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
11 from stat import *
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
bfc655074523 posix -> os
Guido van Rossum <guido@python.org>
parents: 559
diff changeset
16 except AttributeError:
15930
14a27acc755a Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents: 4573
diff changeset
17 statfunc = os.stat
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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
683edb0c4463 New == syntax
Guido van Rossum <guido@python.org>
parents: 296
diff changeset
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
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
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 sys.exit(status)