|
6 | 6 | # This is not the script you are looking for. This is just a wrapper. |
7 | 7 | # The actual scripts of this application were installed |
8 | 8 | # in subdirectories of @PYTHON_SCRIPTROOT@. |
9 | | -# You are most likely looking for one of those. |
| 9 | +# You are most likely looking for one of these. |
10 | 10 |
|
11 | 11 | from __future__ import with_statement |
12 | 12 |
|
| 13 | + |
13 | 14 | # prepare a clean globals for exec() |
14 | 15 | new_globals = dict(globals()) |
15 | 16 | # we have to keep the import top, so delete it from globals |
16 | 17 | del new_globals['with_statement'] |
17 | 18 |
|
18 | | -import errno, os, os.path, sys |
| 19 | + |
| 20 | +import errno |
| 21 | +import os |
| 22 | +import os.path |
| 23 | +import sys |
| 24 | + |
19 | 25 |
|
20 | 26 | try: |
21 | | - from epython import EPYTHON |
| 27 | + from epython import EPYTHON |
22 | 28 | except ImportError: |
23 | | - EPYTHON = os.path.basename(sys.executable) |
24 | | - if '@exeext@' and EPYTHON.endswith('@exeext@'): |
25 | | - EPYTHON = EPYTHON[:-len('@exeext@')] |
| 29 | + EPYTHON = os.path.basename(sys.executable) |
| 30 | + if '@exeext@' and EPYTHON.endswith('@exeext@'): |
| 31 | + EPYTHON = EPYTHON[:-len('@exeext@')] |
| 32 | + |
26 | 33 |
|
27 | 34 | # Alike python-exec2c, perform symlink resolution on target until |
28 | 35 | # EINVAL is hit. If the final target is python-exec2, use the last |
29 | 36 | # symlink name preceding it. Otherwise, use the final target. |
30 | 37 | prev_target = None |
31 | 38 | target = sys.argv[0] |
32 | 39 | while True: |
33 | | - try: |
34 | | - next_target = os.path.join(os.path.dirname(target), |
35 | | - os.readlink(target)) |
36 | | - except OSError as e: |
37 | | - if e.errno == errno.EINTR: |
38 | | - # retry |
39 | | - continue |
40 | | - elif e.errno == errno.EINVAL: |
41 | | - # if the final target is python-exec2, use last symlink |
42 | | - if os.path.basename(target) in ( 'python-exec2', 'python-exec2@exeext@' ): |
43 | | - if prev_target is None: |
44 | | - sys.stderr.write('%s: python-exec2 is a wrapper, it must not be run directly.\n' |
45 | | - % target) |
46 | | - sys.exit(127) |
47 | | - |
48 | | - target = prev_target |
49 | | - break |
50 | | - else: |
51 | | - raise |
52 | | - else: |
53 | | - prev_target = target |
54 | | - target = next_target |
| 40 | + try: |
| 41 | + next_target = os.path.join(os.path.dirname(target), |
| 42 | + os.readlink(target)) |
| 43 | + except OSError as e: |
| 44 | + if e.errno == errno.EINTR: |
| 45 | + # retry |
| 46 | + continue |
| 47 | + if e.errno == errno.EINVAL: |
| 48 | + # if the final target is python-exec2, use last symlink |
| 49 | + if os.path.basename(target) in ('python-exec2', |
| 50 | + 'python-exec2@exeext@'): |
| 51 | + if prev_target is None: |
| 52 | + sys.stderr.write( |
| 53 | + '{}: python-exec2 is a wrapper, it must not ' |
| 54 | + 'be run directly.\n'.format(target)) |
| 55 | + sys.exit(127) |
| 56 | + |
| 57 | + target = prev_target |
| 58 | + break |
| 59 | + raise |
| 60 | + else: |
| 61 | + prev_target = target |
| 62 | + target = next_target |
55 | 63 |
|
56 | 64 | target = os.path.join('@PYTHON_SCRIPTROOT@', EPYTHON, |
57 | | - os.path.basename(target)) |
| 65 | + os.path.basename(target)) |
58 | 66 |
|
59 | 67 | data = None |
60 | 68 | while data is None: |
61 | | - try: |
62 | | - kwargs = {} |
63 | | - if sys.version_info[0] >= 3: |
64 | | - import tokenize |
65 | | - |
66 | | - # need to provide encoding |
67 | | - with open(target, 'rb') as f: |
68 | | - kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0] |
69 | | - |
70 | | - with open(target, 'r', **kwargs) as f: |
71 | | - data = f.read() |
72 | | - except IOError as e: |
73 | | - if e.errno == errno.EINTR: |
74 | | - # retry |
75 | | - continue |
76 | | - elif e.errno == errno.ENOENT: |
77 | | - sys.stderr.write('%s: this Python implementation (%s) is not supported by the script.\n' |
78 | | - % (target, EPYTHON)) |
79 | | - sys.exit(127) |
80 | | - else: |
81 | | - raise |
| 69 | + try: |
| 70 | + kwargs = {} |
| 71 | + if sys.version_info >= (3,): |
| 72 | + import tokenize |
| 73 | + |
| 74 | + # need to provide encoding |
| 75 | + with open(target, 'rb') as f: |
| 76 | + kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0] |
| 77 | + |
| 78 | + with open(target, 'r', **kwargs) as f: |
| 79 | + data = f.read() |
| 80 | + except IOError as e: |
| 81 | + if e.errno == errno.EINTR: |
| 82 | + # retry |
| 83 | + continue |
| 84 | + elif e.errno == errno.ENOENT: |
| 85 | + sys.stderr.write( |
| 86 | + '{}: this Python implementation ({}) is not supported ' |
| 87 | + 'by the script.\n'.format(target, EPYTHON)) |
| 88 | + sys.exit(127) |
| 89 | + else: |
| 90 | + raise |
82 | 91 |
|
83 | 92 | sys.argv[0] = target |
84 | 93 | # in python3.9+, __file__ paths are absolute |
85 | 94 | if sys.version_info >= (3, 9): |
86 | | - target = os.path.abspath(target) |
| 95 | + target = os.path.abspath(target) |
87 | 96 | new_globals['__file__'] = target |
88 | 97 |
|
89 | 98 | exec(data, new_globals) |
0 commit comments