Mercurial > cpython-lru_cache-weakref
annotate Lib/packaging/command/build_scripts.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 | cc5e3a65e452 |
| children |
| rev | line source |
|---|---|
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
1 """Build scripts (copy to build dir and fix up shebang line).""" |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
2 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
3 import os |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
4 import re |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
5 import sysconfig |
|
72408
cc5e3a65e452
A few style changes originally done in the distutils2 repo
Éric Araujo <merwok@netwok.org>
parents:
70199
diff
changeset
|
6 from tokenize import detect_encoding |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
7 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
8 from packaging.command.cmd import Command |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
9 from packaging.util import convert_path, newer |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
10 from packaging import logger |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
11 from packaging.compat import Mixin2to3 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
12 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
13 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
14 # check if Python is called on the first line with this expression |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
15 first_line_re = re.compile(b'^#!.*python[0-9.]*([ \t].*)?$') |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
16 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
17 class build_scripts(Command, Mixin2to3): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
18 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
19 description = "build scripts (copy and fix up shebang line)" |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
20 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
21 user_options = [ |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
22 ('build-dir=', 'd', "directory to build (copy) to"), |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
23 ('force', 'f', "forcibly build everything (ignore file timestamps"), |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
24 ('executable=', 'e', "specify final destination interpreter path"), |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
25 ] |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
26 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
27 boolean_options = ['force'] |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
28 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
29 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
30 def initialize_options(self): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
31 self.build_dir = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
32 self.scripts = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
33 self.force = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
34 self.executable = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
35 self.outfiles = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
36 self.use_2to3 = False |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
37 self.convert_2to3_doctests = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
38 self.use_2to3_fixers = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
39 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
40 def finalize_options(self): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
41 self.set_undefined_options('build', |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
42 ('build_scripts', 'build_dir'), |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
43 'use_2to3', 'use_2to3_fixers', |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
44 'convert_2to3_doctests', 'force', |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
45 'executable') |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
46 self.scripts = self.distribution.scripts |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
47 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
48 def get_source_files(self): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
49 return self.scripts |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
50 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
51 def run(self): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
52 if not self.scripts: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
53 return |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
54 copied_files = self.copy_scripts() |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
55 if self.use_2to3 and copied_files: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
56 self._run_2to3(copied_files, fixers=self.use_2to3_fixers) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
57 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
58 def copy_scripts(self): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
59 """Copy each script listed in 'self.scripts'; if it's marked as a |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
60 Python script in the Unix way (first line matches 'first_line_re', |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
61 ie. starts with "\#!" and contains "python"), then adjust the first |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
62 line to refer to the current Python interpreter as we copy. |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
63 """ |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
64 self.mkpath(self.build_dir) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
65 outfiles = [] |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
66 for script in self.scripts: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
67 adjust = False |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
68 script = convert_path(script) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
69 outfile = os.path.join(self.build_dir, os.path.basename(script)) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
70 outfiles.append(outfile) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
71 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
72 if not self.force and not newer(script, outfile): |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
73 logger.debug("not copying %s (up-to-date)", script) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
74 continue |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
75 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
76 # Always open the file, but ignore failures in dry-run mode -- |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
77 # that way, we'll get accurate feedback if we can read the |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
78 # script. |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
79 try: |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
80 f = open(script, "rb") |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
81 except IOError: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
82 if not self.dry_run: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
83 raise |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
84 f = None |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
85 else: |
|
72408
cc5e3a65e452
A few style changes originally done in the distutils2 repo
Éric Araujo <merwok@netwok.org>
parents:
70199
diff
changeset
|
86 encoding, lines = detect_encoding(f.readline) |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
87 f.seek(0) |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
88 first_line = f.readline() |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
89 if not first_line: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
90 logger.warning('%s: %s is an empty file (skipping)', |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
91 self.get_command_name(), script) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
92 continue |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
93 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
94 match = first_line_re.match(first_line) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
95 if match: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
96 adjust = True |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
97 post_interp = match.group(1) or b'' |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
98 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
99 if adjust: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
100 logger.info("copying and adjusting %s -> %s", script, |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
101 self.build_dir) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
102 if not self.dry_run: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
103 if not sysconfig.is_python_build(): |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
104 executable = self.executable |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
105 else: |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
106 executable = os.path.join( |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
107 sysconfig.get_config_var("BINDIR"), |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
108 "python%s%s" % (sysconfig.get_config_var("VERSION"), |
|
70199
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
109 sysconfig.get_config_var("EXE"))) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
110 executable = os.fsencode(executable) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
111 shebang = b"#!" + executable + post_interp + b"\n" |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
112 # Python parser starts to read a script using UTF-8 until |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
113 # it gets a #coding:xxx cookie. The shebang has to be the |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
114 # first line of a file, the #coding:xxx cookie cannot be |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
115 # written before. So the shebang has to be decodable from |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
116 # UTF-8. |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
117 try: |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
118 shebang.decode('utf-8') |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
119 except UnicodeDecodeError: |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
120 raise ValueError( |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
121 "The shebang ({!r}) is not decodable " |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
122 "from utf-8".format(shebang)) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
123 # If the script is encoded to a custom encoding (use a |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
124 # #coding:xxx cookie), the shebang has to be decodable from |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
125 # the script encoding too. |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
126 try: |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
127 shebang.decode(encoding) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
128 except UnicodeDecodeError: |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
129 raise ValueError( |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
130 "The shebang ({!r}) is not decodable " |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
131 "from the script encoding ({})" |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
132 .format(shebang, encoding)) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
133 with open(outfile, "wb") as outf: |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
134 outf.write(shebang) |
|
cc5cfeaa4a8d
Issue #10419, issue #6011: port 6ad356525381 fix from distutils to packaging
Victor Stinner <victor.stinner@haypocalc.com>
parents:
70194
diff
changeset
|
135 outf.writelines(f.readlines()) |
|
70194
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
136 if f: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
137 f.close() |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
138 else: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
139 if f: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
140 f.close() |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
141 self.copy_file(script, outfile) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
142 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
143 if os.name == 'posix': |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
144 for file in outfiles: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
145 if self.dry_run: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
146 logger.info("changing mode of %s", file) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
147 else: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
148 oldmode = os.stat(file).st_mode & 0o7777 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
149 newmode = (oldmode | 0o555) & 0o7777 |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
150 if newmode != oldmode: |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
151 logger.info("changing mode of %s from %o to %o", |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
152 file, oldmode, newmode) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
153 os.chmod(file, newmode) |
|
1a31c3be88b1
initial import of the packaging package in the standard library
Tarek Ziade <tarek@ziade.org>
parents:
diff
changeset
|
154 return outfiles |
