scripts.py 18 KB

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