annotate Tools/scripts/byteyears.py @ 73323:dad02a080bbc

Improve byte-compilation in packaging to be independent of -O or -B. The code I fixed to comply with PEP 3147 still had one bug: When run under python -O, some paths for pyc files would be pyo, because I called imp.cache_from_source without explicit debug_override argument in some places, and under -O that would return .pyo (this is well explained in the imp docs). Now all code (util.byte_compile, build_py, install_lib) can create .pyo files according to options given by users, without interference from the calling Python’s own optimize mode. On a related topic, I also removed the code that prevented byte compilation under python -B. The rationale is that packaging gives control over the creation of pyc files to the user with its own explicit option, and the behavior should not be changed if the calling Python happens to run with -B for whatever reason. I will argue that this is a bug fix and ask to be allowed to backport this change to distutils. Finally, I moved one nugget of information about the --compile and --optimize options from the source into the doc. It clears up a misunderstanding that I (and maybe other people) had.
author Éric Araujo <merwok@netwok.org>
date Thu, 03 Nov 2011 05:08:28 +0100
parents fcfe324663a6
children 6cfe2982de42
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
59522
fcfe324663a6 convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents: 42427
diff changeset
1 #! /usr/bin/env python3
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
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
12 def main():
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
13
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
14 # Use lstat() to stat files if it exists, else stat()
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
15 try:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
16 statfunc = os.lstat
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
17 except AttributeError:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
18 statfunc = os.stat
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
20 # Parse options
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
21 if sys.argv[1] == '-m':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
22 itime = ST_MTIME
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
23 del sys.argv[1]
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
24 elif sys.argv[1] == '-c':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
25 itime = ST_CTIME
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
26 del sys.argv[1]
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
27 elif sys.argv[1] == '-a':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
28 itime = ST_CTIME
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
29 del sys.argv[1]
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
30 else:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
31 itime = ST_MTIME
296
7a35d985763b Add options -amc; do lstat if possible; columnize properly.
Guido van Rossum <guido@python.org>
parents: 274
diff changeset
32
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
33 secs_per_year = 365.0 * 24.0 * 3600.0 # Scale factor
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
34 now = time.time() # Current time, for age computations
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
35 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
36
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
37 # Compute max file name length
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
38 maxlen = 1
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
39 for filename in sys.argv[1:]:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
40 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
41
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
42 # Process each argument in turn
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
43 for filename in sys.argv[1:]:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
44 try:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
45 st = statfunc(filename)
40587
6b18a095c2e7 SF patch 1631942 by Collin Winter:
Guido van Rossum <guido@python.org>
parents: 33074
diff changeset
46 except os.error as msg:
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
47 sys.stderr.write("can't stat %r: %r\n" % (filename, msg))
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
48 status = 1
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
49 st = ()
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
50 if st:
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
51 anytime = st[itime]
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
52 size = st[ST_SIZE]
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
53 age = now - anytime
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
54 byteyears = float(size) * float(age) / secs_per_year
42427
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
55 print(filename.ljust(maxlen), end=' ')
46625e6122e9 Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents: 40587
diff changeset
56 print(repr(int(byteyears)).rjust(8))
274
12a64115a4ee Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
33074
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
58 sys.exit(status)
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
59
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
60 if __name__ == '__main__':
981fff7363b9 [Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents: 31515
diff changeset
61 main()