comparison lib/python3.8/site-packages/pip/_vendor/distlib/scripts.py @ 0:9e54283cc701 draft

"planemo upload commit d12c32a45bcd441307e632fca6d9af7d60289d44"
author guerler
date Mon, 27 Jul 2020 03:47:31 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:9e54283cc701
1 # -*- coding: utf-8 -*-
2 #
3 # Copyright (C) 2013-2015 Vinay Sajip.
4 # Licensed to the Python Software Foundation under a contributor agreement.
5 # See LICENSE.txt and CONTRIBUTORS.txt.
6 #
7 from io import BytesIO
8 import logging
9 import os
10 import re
11 import struct
12 import sys
13
14 from .compat import sysconfig, detect_encoding, ZipFile
15 from .resources import finder
16 from .util import (FileOperator, get_export_entry, convert_path,
17 get_executable, in_venv)
18
19 logger = logging.getLogger(__name__)
20
21 _DEFAULT_MANIFEST = '''
22 <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
23 <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
24 <assemblyIdentity version="1.0.0.0"
25 processorArchitecture="X86"
26 name="%s"
27 type="win32"/>
28
29 <!-- Identify the application security requirements. -->
30 <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
31 <security>
32 <requestedPrivileges>
33 <requestedExecutionLevel level="asInvoker" uiAccess="false"/>
34 </requestedPrivileges>
35 </security>
36 </trustInfo>
37 </assembly>'''.strip()
38
39 # check if Python is called on the first line with this expression
40 FIRST_LINE_RE = re.compile(b'^#!.*pythonw?[0-9.]*([ \t].*)?$')
41 SCRIPT_TEMPLATE = r'''# -*- coding: utf-8 -*-
42 import re
43 import sys
44 from %(module)s import %(import_name)s
45 if __name__ == '__main__':
46 sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
47 sys.exit(%(func)s())
48 '''
49
50
51 def _enquote_executable(executable):
52 if ' ' in executable:
53 # make sure we quote only the executable in case of env
54 # for example /usr/bin/env "/dir with spaces/bin/jython"
55 # instead of "/usr/bin/env /dir with spaces/bin/jython"
56 # otherwise whole
57 if executable.startswith('/usr/bin/env '):
58 env, _executable = executable.split(' ', 1)
59 if ' ' in _executable and not _executable.startswith('"'):
60 executable = '%s "%s"' % (env, _executable)
61 else:
62 if not executable.startswith('"'):
63 executable = '"%s"' % executable
64 return executable
65
66
67 class ScriptMaker(object):
68 """
69 A class to copy or create scripts from source scripts or callable
70 specifications.
71 """
72 script_template = SCRIPT_TEMPLATE
73
74 executable = None # for shebangs
75
76 def __init__(self, source_dir, target_dir, add_launchers=True,
77 dry_run=False, fileop=None):
78 self.source_dir = source_dir
79 self.target_dir = target_dir
80 self.add_launchers = add_launchers
81 self.force = False
82 self.clobber = False
83 # It only makes sense to set mode bits on POSIX.
84 self.set_mode = (os.name == 'posix') or (os.name == 'java' and
85 os._name == 'posix')
86 self.variants = set(('', 'X.Y'))
87 self._fileop = fileop or FileOperator(dry_run)
88
89 self._is_nt = os.name == 'nt' or (
90 os.name == 'java' and os._name == 'nt')
91
92 def _get_alternate_executable(self, executable, options):
93 if options.get('gui', False) and self._is_nt: # pragma: no cover
94 dn, fn = os.path.split(executable)
95 fn = fn.replace('python', 'pythonw')
96 executable = os.path.join(dn, fn)
97 return executable
98
99 if sys.platform.startswith('java'): # pragma: no cover
100 def _is_shell(self, executable):
101 """
102 Determine if the specified executable is a script
103 (contains a #! line)
104 """
105 try:
106 with open(executable) as fp:
107 return fp.read(2) == '#!'
108 except (OSError, IOError):
109 logger.warning('Failed to open %s', executable)
110 return False
111
112 def _fix_jython_executable(self, executable):
113 if self._is_shell(executable):
114 # Workaround for Jython is not needed on Linux systems.
115 import java
116
117 if java.lang.System.getProperty('os.name') == 'Linux':
118 return executable
119 elif executable.lower().endswith('jython.exe'):
120 # Use wrapper exe for Jython on Windows
121 return executable
122 return '/usr/bin/env %s' % executable
123
124 def _build_shebang(self, executable, post_interp):
125 """
126 Build a shebang line. In the simple case (on Windows, or a shebang line
127 which is not too long or contains spaces) use a simple formulation for
128 the shebang. Otherwise, use /bin/sh as the executable, with a contrived
129 shebang which allows the script to run either under Python or sh, using
130 suitable quoting. Thanks to Harald Nordgren for his input.
131
132 See also: http://www.in-ulm.de/~mascheck/various/shebang/#length
133 https://hg.mozilla.org/mozilla-central/file/tip/mach
134 """
135 if os.name != 'posix':
136 simple_shebang = True
137 else:
138 # Add 3 for '#!' prefix and newline suffix.
139 shebang_length = len(executable) + len(post_interp) + 3
140 if sys.platform == 'darwin':
141 max_shebang_length = 512
142 else:
143 max_shebang_length = 127
144 simple_shebang = ((b' ' not in executable) and
145 (shebang_length <= max_shebang_length))
146
147 if simple_shebang:
148 result = b'#!' + executable + post_interp + b'\n'
149 else:
150 result = b'#!/bin/sh\n'
151 result += b"'''exec' " + executable + post_interp + b' "$0" "$@"\n'
152 result += b"' '''"
153 return result
154
155 def _get_shebang(self, encoding, post_interp=b'', options=None):
156 enquote = True
157 if self.executable:
158 executable = self.executable
159 enquote = False # assume this will be taken care of
160 elif not sysconfig.is_python_build():
161 executable = get_executable()
162 elif in_venv(): # pragma: no cover
163 executable = os.path.join(sysconfig.get_path('scripts'),
164 'python%s' % sysconfig.get_config_var('EXE'))
165 else: # pragma: no cover
166 executable = os.path.join(
167 sysconfig.get_config_var('BINDIR'),
168 'python%s%s' % (sysconfig.get_config_var('VERSION'),
169 sysconfig.get_config_var('EXE')))
170 if options:
171 executable = self._get_alternate_executable(executable, options)
172
173 if sys.platform.startswith('java'): # pragma: no cover
174 executable = self._fix_jython_executable(executable)
175
176 # Normalise case for Windows - COMMENTED OUT
177 # executable = os.path.normcase(executable)
178 # N.B. The normalising operation above has been commented out: See
179 # issue #124. Although paths in Windows are generally case-insensitive,
180 # they aren't always. For example, a path containing a ẞ (which is a
181 # LATIN CAPITAL LETTER SHARP S - U+1E9E) is normcased to ß (which is a
182 # LATIN SMALL LETTER SHARP S' - U+00DF). The two are not considered by
183 # Windows as equivalent in path names.
184
185 # If the user didn't specify an executable, it may be necessary to
186 # cater for executable paths with spaces (not uncommon on Windows)
187 if enquote:
188 executable = _enquote_executable(executable)
189 # Issue #51: don't use fsencode, since we later try to
190 # check that the shebang is decodable using utf-8.
191 executable = executable.encode('utf-8')
192 # in case of IronPython, play safe and enable frames support
193 if (sys.platform == 'cli' and '-X:Frames' not in post_interp
194 and '-X:FullFrames' not in post_interp): # pragma: no cover
195 post_interp += b' -X:Frames'
196 shebang = self._build_shebang(executable, post_interp)
197 # Python parser starts to read a script using UTF-8 until
198 # it gets a #coding:xxx cookie. The shebang has to be the
199 # first line of a file, the #coding:xxx cookie cannot be
200 # written before. So the shebang has to be decodable from
201 # UTF-8.
202 try:
203 shebang.decode('utf-8')
204 except UnicodeDecodeError: # pragma: no cover
205 raise ValueError(
206 'The shebang (%r) is not decodable from utf-8' % shebang)
207 # If the script is encoded to a custom encoding (use a
208 # #coding:xxx cookie), the shebang has to be decodable from
209 # the script encoding too.
210 if encoding != 'utf-8':
211 try:
212 shebang.decode(encoding)
213 except UnicodeDecodeError: # pragma: no cover
214 raise ValueError(
215 'The shebang (%r) is not decodable '
216 'from the script encoding (%r)' % (shebang, encoding))
217 return shebang
218
219 def _get_script_text(self, entry):
220 return self.script_template % dict(module=entry.prefix,
221 import_name=entry.suffix.split('.')[0],
222 func=entry.suffix)
223
224 manifest = _DEFAULT_MANIFEST
225
226 def get_manifest(self, exename):
227 base = os.path.basename(exename)
228 return self.manifest % base
229
230 def _write_script(self, names, shebang, script_bytes, filenames, ext):
231 use_launcher = self.add_launchers and self._is_nt
232 linesep = os.linesep.encode('utf-8')
233 if not shebang.endswith(linesep):
234 shebang += linesep
235 if not use_launcher:
236 script_bytes = shebang + script_bytes
237 else: # pragma: no cover
238 if ext == 'py':
239 launcher = self._get_launcher('t')
240 else:
241 launcher = self._get_launcher('w')
242 stream = BytesIO()
243 with ZipFile(stream, 'w') as zf:
244 zf.writestr('__main__.py', script_bytes)
245 zip_data = stream.getvalue()
246 script_bytes = launcher + shebang + zip_data
247 for name in names:
248 outname = os.path.join(self.target_dir, name)
249 if use_launcher: # pragma: no cover
250 n, e = os.path.splitext(outname)
251 if e.startswith('.py'):
252 outname = n
253 outname = '%s.exe' % outname
254 try:
255 self._fileop.write_binary_file(outname, script_bytes)
256 except Exception:
257 # Failed writing an executable - it might be in use.
258 logger.warning('Failed to write executable - trying to '
259 'use .deleteme logic')
260 dfname = '%s.deleteme' % outname
261 if os.path.exists(dfname):
262 os.remove(dfname) # Not allowed to fail here
263 os.rename(outname, dfname) # nor here
264 self._fileop.write_binary_file(outname, script_bytes)
265 logger.debug('Able to replace executable using '
266 '.deleteme logic')
267 try:
268 os.remove(dfname)
269 except Exception:
270 pass # still in use - ignore error
271 else:
272 if self._is_nt and not outname.endswith('.' + ext): # pragma: no cover
273 outname = '%s.%s' % (outname, ext)
274 if os.path.exists(outname) and not self.clobber:
275 logger.warning('Skipping existing file %s', outname)
276 continue
277 self._fileop.write_binary_file(outname, script_bytes)
278 if self.set_mode:
279 self._fileop.set_executable_mode([outname])
280 filenames.append(outname)
281
282 def _make_script(self, entry, filenames, options=None):
283 post_interp = b''
284 if options:
285 args = options.get('interpreter_args', [])
286 if args:
287 args = ' %s' % ' '.join(args)
288 post_interp = args.encode('utf-8')
289 shebang = self._get_shebang('utf-8', post_interp, options=options)
290 script = self._get_script_text(entry).encode('utf-8')
291 name = entry.name
292 scriptnames = set()
293 if '' in self.variants:
294 scriptnames.add(name)
295 if 'X' in self.variants:
296 scriptnames.add('%s%s' % (name, sys.version_info[0]))
297 if 'X.Y' in self.variants:
298 scriptnames.add('%s-%s.%s' % (name, sys.version_info[0],
299 sys.version_info[1]))
300 if options and options.get('gui', False):
301 ext = 'pyw'
302 else:
303 ext = 'py'
304 self._write_script(scriptnames, shebang, script, filenames, ext)
305
306 def _copy_script(self, script, filenames):
307 adjust = False
308 script = os.path.join(self.source_dir, convert_path(script))
309 outname = os.path.join(self.target_dir, os.path.basename(script))
310 if not self.force and not self._fileop.newer(script, outname):
311 logger.debug('not copying %s (up-to-date)', script)
312 return
313
314 # Always open the file, but ignore failures in dry-run mode --
315 # that way, we'll get accurate feedback if we can read the
316 # script.
317 try:
318 f = open(script, 'rb')
319 except IOError: # pragma: no cover
320 if not self.dry_run:
321 raise
322 f = None
323 else:
324 first_line = f.readline()
325 if not first_line: # pragma: no cover
326 logger.warning('%s: %s is an empty file (skipping)',
327 self.get_command_name(), script)
328 return
329
330 match = FIRST_LINE_RE.match(first_line.replace(b'\r\n', b'\n'))
331 if match:
332 adjust = True
333 post_interp = match.group(1) or b''
334
335 if not adjust:
336 if f:
337 f.close()
338 self._fileop.copy_file(script, outname)
339 if self.set_mode:
340 self._fileop.set_executable_mode([outname])
341 filenames.append(outname)
342 else:
343 logger.info('copying and adjusting %s -> %s', script,
344 self.target_dir)
345 if not self._fileop.dry_run:
346 encoding, lines = detect_encoding(f.readline)
347 f.seek(0)
348 shebang = self._get_shebang(encoding, post_interp)
349 if b'pythonw' in first_line: # pragma: no cover
350 ext = 'pyw'
351 else:
352 ext = 'py'
353 n = os.path.basename(outname)
354 self._write_script([n], shebang, f.read(), filenames, ext)
355 if f:
356 f.close()
357
358 @property
359 def dry_run(self):
360 return self._fileop.dry_run
361
362 @dry_run.setter
363 def dry_run(self, value):
364 self._fileop.dry_run = value
365
366 if os.name == 'nt' or (os.name == 'java' and os._name == 'nt'): # pragma: no cover
367 # Executable launcher support.
368 # Launchers are from https://bitbucket.org/vinay.sajip/simple_launcher/
369
370 def _get_launcher(self, kind):
371 if struct.calcsize('P') == 8: # 64-bit
372 bits = '64'
373 else:
374 bits = '32'
375 name = '%s%s.exe' % (kind, bits)
376 # Issue 31: don't hardcode an absolute package name, but
377 # determine it relative to the current package
378 distlib_package = __name__.rsplit('.', 1)[0]
379 resource = finder(distlib_package).find(name)
380 if not resource:
381 msg = ('Unable to find resource %s in package %s' % (name,
382 distlib_package))
383 raise ValueError(msg)
384 return resource.bytes
385
386 # Public API follows
387
388 def make(self, specification, options=None):
389 """
390 Make a script.
391
392 :param specification: The specification, which is either a valid export
393 entry specification (to make a script from a
394 callable) or a filename (to make a script by
395 copying from a source location).
396 :param options: A dictionary of options controlling script generation.
397 :return: A list of all absolute pathnames written to.
398 """
399 filenames = []
400 entry = get_export_entry(specification)
401 if entry is None:
402 self._copy_script(specification, filenames)
403 else:
404 self._make_script(entry, filenames, options=options)
405 return filenames
406
407 def make_multiple(self, specifications, options=None):
408 """
409 Take a list of specifications and make scripts from them,
410 :param specifications: A list of specifications.
411 :return: A list of all absolute pathnames written to,
412 """
413 filenames = []
414 for specification in specifications:
415 filenames.extend(self.make(specification, options))
416 return filenames