图片解析应用
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

452 lines
18 KiB

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