Mercurial > cpython-lru_cache-weakref
annotate Tools/scripts/checkpyc.py @ 74006:88198b93ff2f
Issue #13560: Add PyUnicode_EncodeLocale()
* Use PyUnicode_EncodeLocale() in time.strftime() if wcsftime() is not
available
* Document my last changes in Misc/NEWS
| author | Victor Stinner <victor.stinner@haypocalc.com> |
|---|---|
| date | Sat, 17 Dec 2011 04:13:41 +0100 |
| parents | 5c0d5de56fdf |
| children | 6cfe2982de42 |
| rev | line source |
|---|---|
|
59522
fcfe324663a6
convert shebang lines: python -> python3
Benjamin Peterson <benjamin@python.org>
parents:
47100
diff
changeset
|
1 #! /usr/bin/env python3 |
| 500 | 2 # Check that all ".pyc" files exist and are up-to-date |
| 758 | 3 # Uses module 'os' |
| 500 | 4 |
| 5 import sys | |
| 758 | 6 import os |
| 500 | 7 from stat import ST_MTIME |
|
9294
0de662cecacf
Use the imp module to get the magic word.
Guido van Rossum <guido@python.org>
parents:
4573
diff
changeset
|
8 import imp |
| 500 | 9 |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
10 # PEP 3147 compatibility (PYC Repository Directories) |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
11 cache_from_source = (imp.cache_from_source if hasattr(imp, 'get_tag') else |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
12 lambda path: path + 'c') |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
13 |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
14 |
| 500 | 15 def main(): |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
16 if len(sys.argv) > 1: |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
17 verbose = (sys.argv[1] == '-v') |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
18 silent = (sys.argv[1] == '-s') |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
19 else: |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
20 verbose = silent = False |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
21 MAGIC = imp.get_magic() |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
22 if not silent: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
23 print('Using MAGIC word', repr(MAGIC)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
24 for dirname in sys.path: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
25 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
26 names = os.listdir(dirname) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
27 except os.error: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
28 print('Cannot list directory', repr(dirname)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
29 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
30 if not silent: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
31 print('Checking ', repr(dirname), '...') |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
32 for name in sorted(names): |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
33 if name.endswith('.py'): |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
34 name = os.path.join(dirname, name) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
35 try: |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
36 st = os.stat(name) |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
37 except os.error: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
38 print('Cannot stat', repr(name)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
39 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
40 if verbose: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
41 print('Check', repr(name), '...') |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
42 name_c = cache_from_source(name) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
43 try: |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
44 with open(name_c, 'rb') as f: |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
45 magic_str = f.read(4) |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
46 mtime_str = f.read(4) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
47 except IOError: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
48 print('Cannot open', repr(name_c)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
49 continue |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
42427
diff
changeset
|
50 if magic_str != MAGIC: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
51 print('Bad MAGIC word in ".pyc" file', end=' ') |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
52 print(repr(name_c)) |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
53 continue |
|
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
54 mtime = get_long(mtime_str) |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
55 if mtime in {0, -1}: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
56 print('Bad ".pyc" file', repr(name_c)) |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
42427
diff
changeset
|
57 elif mtime != st[ST_MTIME]: |
|
42427
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
58 print('Out-of-date ".pyc" file', end=' ') |
|
46625e6122e9
Convert print statements to function calls in Tools/.
Collin Winter <collinw@gmail.com>
parents:
33074
diff
changeset
|
59 print(repr(name_c)) |
| 500 | 60 |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
61 |
| 500 | 62 def get_long(s): |
|
47100
893a4dc17a91
Ran 2to3 over scripts directory.
Georg Brandl <georg@python.org>
parents:
42427
diff
changeset
|
63 if len(s) != 4: |
|
15930
14a27acc755a
Whitespace normalization.
Tim Peters <tim.peters@gmail.com>
parents:
9294
diff
changeset
|
64 return -1 |
|
63873
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
65 return s[0] + (s[1] << 8) + (s[2] << 16) + (s[3] << 24) |
|
5c0d5de56fdf
Fix ``Tools/scripts/checkpyc.py`` after PEP 3147.
Florent Xicluna <florent.xicluna@gmail.com>
parents:
59522
diff
changeset
|
66 |
| 500 | 67 |
|
33074
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
68 if __name__ == '__main__': |
|
981fff7363b9
[Patch #1005491 ] use __name__ == '__main__' in scripts
Andrew M. Kuchling <amk@amk.ca>
parents:
31515
diff
changeset
|
69 main() |
