api.py 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006
  1. #-----------------------------------------------------------------------------
  2. # Copyright (c) 2005-2021, PyInstaller Development Team.
  3. #
  4. # Distributed under the terms of the GNU General Public License (version 2
  5. # or later) with exception for distributing the bootloader.
  6. #
  7. # The full license is in the file COPYING.txt, distributed with this software.
  8. #
  9. # SPDX-License-Identifier: (GPL-2.0-or-later WITH Bootloader-exception)
  10. #-----------------------------------------------------------------------------
  11. """
  12. This module contains classes that are available for the .spec files.
  13. Spec file is generated by PyInstaller. The generated code from .spec file
  14. is a way how PyInstaller does the dependency analysis and creates executable.
  15. """
  16. import os
  17. import time
  18. import pprint
  19. import shutil
  20. from operator import itemgetter
  21. from PyInstaller import HOMEPATH, PLATFORM
  22. from PyInstaller import log as logging
  23. from PyInstaller.archive.writers import CArchiveWriter, ZlibArchiveWriter
  24. from PyInstaller.building.datastruct import TOC, Target, _check_guts_eq
  25. from PyInstaller.building.utils import (
  26. _check_guts_toc, _make_clean_directory, _rmtree, add_suffix_to_extension, checkCache, get_code_object,
  27. strip_paths_in_code
  28. )
  29. from PyInstaller.compat import (exec_command_all, is_cygwin, is_darwin, is_linux, is_win)
  30. from PyInstaller.depend import bindepend
  31. from PyInstaller.depend.analysis import get_bootstrap_modules
  32. from PyInstaller.depend.utils import is_path_to_egg
  33. from PyInstaller.utils import misc
  34. logger = logging.getLogger(__name__)
  35. if is_win:
  36. from PyInstaller.utils.win32 import (icon, versioninfo, winmanifest, winresource, winutils)
  37. if is_darwin:
  38. import PyInstaller.utils.osx as osxutils
  39. class PYZ(Target):
  40. """
  41. Creates a ZlibArchive that contains all pure Python modules.
  42. """
  43. typ = 'PYZ'
  44. def __init__(self, *tocs, **kwargs):
  45. """
  46. tocs
  47. One or more TOCs (Tables of Contents), normally an Analysis.pure.
  48. If this TOC has an attribute `_code_cache`, this is expected to be a dict of module code objects
  49. from ModuleGraph.
  50. kwargs
  51. Possible keyword arguments:
  52. name
  53. A filename for the .pyz. Normally not needed, as the generated name will do fine.
  54. cipher
  55. The block cipher that will be used to encrypt Python bytecode.
  56. """
  57. from PyInstaller.config import CONF
  58. Target.__init__(self)
  59. name = kwargs.get('name', None)
  60. cipher = kwargs.get('cipher', None)
  61. self.toc = TOC()
  62. # If available, use code objects directly from ModuleGraph to speed up PyInstaller.
  63. self.code_dict = {}
  64. for t in tocs:
  65. self.toc.extend(t)
  66. self.code_dict.update(getattr(t, '_code_cache', {}))
  67. self.name = name
  68. if name is None:
  69. self.name = os.path.splitext(self.tocfilename)[0] + '.pyz'
  70. # PyInstaller bootstrapping modules.
  71. self.dependencies = get_bootstrap_modules()
  72. # Bundle the crypto key.
  73. self.cipher = cipher
  74. if cipher:
  75. key_file = ('pyimod00_crypto_key', os.path.join(CONF['workpath'], 'pyimod00_crypto_key.pyc'), 'PYMODULE')
  76. # Insert the key as the first module in the list. The key module contains just variables and does not depend
  77. # on other modules.
  78. self.dependencies.insert(0, key_file)
  79. # Compile the top-level modules so that they end up in the CArchive and can be imported by the bootstrap script.
  80. self.dependencies = misc.compile_py_files(self.dependencies, CONF['workpath'])
  81. self.__postinit__()
  82. _GUTS = ( # input parameters
  83. ('name', _check_guts_eq),
  84. ('toc', _check_guts_toc), # todo: pyc=1
  85. # no calculated/analysed values
  86. )
  87. def _check_guts(self, data, last_build):
  88. if Target._check_guts(self, data, last_build):
  89. return True
  90. return False
  91. def assemble(self):
  92. logger.info("Building PYZ (ZlibArchive) %s", self.name)
  93. # Do not bundle PyInstaller bootstrap modules into PYZ archive.
  94. toc = self.toc - self.dependencies
  95. for entry in toc[:]:
  96. if not entry[0] in self.code_dict and entry[2] == 'PYMODULE':
  97. # For some reason the code-object that modulegraph created is unavailable. Re-create it.
  98. try:
  99. self.code_dict[entry[0]] = get_code_object(entry[0], entry[1])
  100. except SyntaxError:
  101. # Exclude the module in case this is code meant for a newer Python version.
  102. toc.remove(entry)
  103. # Sort content alphabetically to support reproducible builds.
  104. toc.sort()
  105. # Remove leading parts of paths in code objects.
  106. self.code_dict = {key: strip_paths_in_code(code) for key, code in self.code_dict.items()}
  107. ZlibArchiveWriter(self.name, toc, code_dict=self.code_dict, cipher=self.cipher)
  108. logger.info("Building PYZ (ZlibArchive) %s completed successfully.", self.name)
  109. class PKG(Target):
  110. """
  111. Creates a CArchive. CArchive is the data structure that is embedded into the executable. This data structure allows
  112. to include various read-only data in a single-file deployment.
  113. """
  114. typ = 'PKG'
  115. xformdict = {
  116. 'PYMODULE': 'm',
  117. 'PYSOURCE': 's',
  118. 'EXTENSION': 'b',
  119. 'PYZ': 'z',
  120. 'PKG': 'a',
  121. 'DATA': 'x',
  122. 'BINARY': 'b',
  123. 'ZIPFILE': 'Z',
  124. 'EXECUTABLE': 'b',
  125. 'DEPENDENCY': 'd',
  126. 'SPLASH': 'l'
  127. }
  128. def __init__(
  129. self,
  130. toc,
  131. name=None,
  132. cdict=None,
  133. exclude_binaries=0,
  134. strip_binaries=False,
  135. upx_binaries=False,
  136. upx_exclude=None,
  137. target_arch=None,
  138. codesign_identity=None,
  139. entitlements_file=None
  140. ):
  141. """
  142. toc
  143. A TOC (Table of Contents)
  144. name
  145. An optional filename for the PKG.
  146. cdict
  147. Dictionary that specifies compression by typecode. For Example, PYZ is left uncompressed so that it
  148. can be accessed inside the PKG. The default uses sensible values. If zlib is not available, no
  149. compression is used.
  150. exclude_binaries
  151. If True, EXTENSIONs and BINARYs will be left out of the PKG, and forwarded to its container (usually
  152. a COLLECT).
  153. strip_binaries
  154. If True, use 'strip' command to reduce the size of binary files.
  155. upx_binaries
  156. """
  157. Target.__init__(self)
  158. self.toc = toc
  159. self.cdict = cdict
  160. self.name = name
  161. if name is None:
  162. self.name = os.path.splitext(self.tocfilename)[0] + '.pkg'
  163. self.exclude_binaries = exclude_binaries
  164. self.strip_binaries = strip_binaries
  165. self.upx_binaries = upx_binaries
  166. self.upx_exclude = upx_exclude or []
  167. self.target_arch = target_arch
  168. self.codesign_identity = codesign_identity
  169. self.entitlements_file = entitlements_file
  170. # This dict tells PyInstaller what items embedded in the executable should be compressed.
  171. if self.cdict is None:
  172. self.cdict = {
  173. 'EXTENSION': COMPRESSED,
  174. 'DATA': COMPRESSED,
  175. 'BINARY': COMPRESSED,
  176. 'EXECUTABLE': COMPRESSED,
  177. 'PYSOURCE': COMPRESSED,
  178. 'PYMODULE': COMPRESSED,
  179. 'SPLASH': COMPRESSED,
  180. # Do not compress PYZ as a whole. Single modules are compressed when creating PYZ archive.
  181. 'PYZ': UNCOMPRESSED
  182. }
  183. self.__postinit__()
  184. _GUTS = ( # input parameters
  185. ('name', _check_guts_eq),
  186. ('cdict', _check_guts_eq),
  187. ('toc', _check_guts_toc), # list unchanged and no newer files
  188. ('exclude_binaries', _check_guts_eq),
  189. ('strip_binaries', _check_guts_eq),
  190. ('upx_binaries', _check_guts_eq),
  191. ('upx_exclude', _check_guts_eq),
  192. ('target_arch', _check_guts_eq),
  193. ('codesign_identity', _check_guts_eq),
  194. ('entitlements_file', _check_guts_eq),
  195. # no calculated/analysed values
  196. )
  197. def _check_guts(self, data, last_build):
  198. if Target._check_guts(self, data, last_build):
  199. return True
  200. return False
  201. def assemble(self):
  202. logger.info("Building PKG (CArchive) %s", os.path.basename(self.name))
  203. trash = []
  204. mytoc = []
  205. srctoc = []
  206. seen_inms = {}
  207. seen_fnms = {}
  208. seen_fnms_typ = {}
  209. # 'inm' - relative filename inside a CArchive
  210. # 'fnm' - absolute filename as it is on the file system.
  211. for inm, fnm, typ in self.toc:
  212. # Adjust name for extensions, if applicable
  213. inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
  214. # Ensure filename 'fnm' is not None or empty string. Otherwise, it will fail when 'typ' is OPTION.
  215. if fnm and not os.path.isfile(fnm) and is_path_to_egg(fnm):
  216. # File is contained within python egg; it is added with the egg.
  217. continue
  218. if typ in ('BINARY', 'EXTENSION', 'DEPENDENCY'):
  219. if self.exclude_binaries and typ == 'EXTENSION':
  220. self.dependencies.append((inm, fnm, typ))
  221. elif not self.exclude_binaries or typ == 'DEPENDENCY':
  222. if typ == 'BINARY':
  223. # Avoid importing the same binary extension twice. This might happen if they come from different
  224. # sources (eg. once from binary dependence, and once from direct import).
  225. if inm in seen_inms:
  226. logger.warning('Two binaries added with the same internal name.')
  227. logger.warning(pprint.pformat((inm, fnm, typ)))
  228. logger.warning('was placed previously at')
  229. logger.warning(pprint.pformat((inm, seen_inms[inm], seen_fnms_typ[seen_inms[inm]])))
  230. logger.warning('Skipping %s.' % fnm)
  231. continue
  232. # Warn if the same binary extension was included with multiple internal names
  233. if fnm in seen_fnms:
  234. logger.warning('One binary added with two internal names.')
  235. logger.warning(pprint.pformat((inm, fnm, typ)))
  236. logger.warning('was placed previously at')
  237. logger.warning(pprint.pformat((seen_fnms[fnm], fnm, seen_fnms_typ[fnm])))
  238. seen_inms[inm] = fnm
  239. seen_fnms[fnm] = inm
  240. seen_fnms_typ[fnm] = typ
  241. fnm = checkCache(
  242. fnm,
  243. strip=self.strip_binaries,
  244. upx=self.upx_binaries,
  245. upx_exclude=self.upx_exclude,
  246. dist_nm=inm,
  247. target_arch=self.target_arch,
  248. codesign_identity=self.codesign_identity,
  249. entitlements_file=self.entitlements_file
  250. )
  251. mytoc.append((inm, fnm, self.cdict.get(typ, 0), self.xformdict.get(typ, 'b')))
  252. elif typ == 'OPTION':
  253. mytoc.append((inm, '', 0, 'o'))
  254. elif typ in ('PYSOURCE', 'PYMODULE'):
  255. # collect sourcefiles and module in a toc of it's own which will not be sorted.
  256. srctoc.append((inm, fnm, self.cdict[typ], self.xformdict[typ]))
  257. else:
  258. mytoc.append((inm, fnm, self.cdict.get(typ, 0), self.xformdict.get(typ, 'b')))
  259. # Bootloader has to know the name of Python library. Pass python libname to CArchive.
  260. pylib_name = os.path.basename(bindepend.get_python_library_path())
  261. # Sort content alphabetically by type and name to support reproducible builds.
  262. mytoc.sort(key=itemgetter(3, 0))
  263. # Do *not* sort modules and scripts, as their order is important.
  264. # TODO: Think about having all modules first and then all scripts.
  265. CArchiveWriter(self.name, srctoc + mytoc, pylib_name=pylib_name)
  266. for item in trash:
  267. os.remove(item)
  268. logger.info("Building PKG (CArchive) %s completed successfully.", os.path.basename(self.name))
  269. class EXE(Target):
  270. """
  271. Creates the final executable of the frozen app. This bundles all necessary files together.
  272. """
  273. typ = 'EXECUTABLE'
  274. def __init__(self, *args, **kwargs):
  275. """
  276. args
  277. One or more arguments that are either TOCs Targets.
  278. kwargs
  279. Possible keyword arguments:
  280. bootloader_ignore_signals
  281. Non-Windows only. If True, the bootloader process will ignore all ignorable signals. If False (default),
  282. it will forward all signals to the child process. Useful in situations where for example a supervisor
  283. process signals both the bootloader and the child (e.g., via a process group) to avoid signalling the
  284. child twice.
  285. console
  286. On Windows or Mac OS governs whether to use the console executable or the windowed executable. Always
  287. True on Linux/Unix (always console executable - it does not matter there).
  288. disable_windowed_traceback
  289. Disable traceback dump of unhandled exception in windowed (noconsole) mode (Windows and macOS only),
  290. and instead display a message that this feature is disabled.
  291. debug
  292. Setting to True gives you progress messages from the executable (for console=False there will be
  293. annoying MessageBoxes on Windows).
  294. name
  295. The filename for the executable. On Windows suffix '.exe' is appended.
  296. exclude_binaries
  297. Forwarded to the PKG the EXE builds.
  298. icon
  299. Windows and Mac OS only. icon='myicon.ico' to use an icon file or icon='notepad.exe,0' to grab an icon
  300. resource. Defaults to use PyInstaller's console or windowed icon. Use icon=`NONE` to not add any icon.
  301. version
  302. Windows only. version='myversion.txt'. Use grab_version.py to get a version resource from an executable
  303. and then edit the output to create your own. (The syntax of version resources is so arcane that I would
  304. not attempt to write one from scratch).
  305. uac_admin
  306. Windows only. Setting to True creates a Manifest with will request elevation upon application start.
  307. uac_uiaccess
  308. Windows only. Setting to True allows an elevated application to work with Remote Desktop.
  309. embed_manifest
  310. Windows only. Setting to True (the default) embeds the manifest into the executable. Setting to False
  311. generates an external .exe.manifest file. Applicable only in onedir mode (exclude_binaries=True); in
  312. onefile mode (exclude_binaries=False), the manifest is always embedded in the executable, regardless
  313. of this option.
  314. target_arch
  315. macOS only. Used to explicitly specify the target architecture; either single-arch ('x86_64' or 'arm64')
  316. or 'universal2'. Used in checks that the collected binaries contain the requires arch slice(s) and/or
  317. to convert fat binaries into thin ones as necessary. If not specified (default), a single-arch build
  318. corresponding to running architecture is assumed.
  319. codesign_identity
  320. macOS only. Use the provided identity to sign collected binaries and the generated executable. If
  321. signing identity is not provided, ad-hoc signing is performed.
  322. entitlements_file
  323. macOS only. Optional path to entitlements file to use with code signing of collected binaries
  324. (--entitlements option to codesign utility).
  325. """
  326. from PyInstaller.config import CONF
  327. Target.__init__(self)
  328. # Available options for EXE in .spec files.
  329. self.exclude_binaries = kwargs.get('exclude_binaries', False)
  330. self.bootloader_ignore_signals = kwargs.get('bootloader_ignore_signals', False)
  331. self.console = kwargs.get('console', True)
  332. self.disable_windowed_traceback = kwargs.get('disable_windowed_traceback', False)
  333. self.debug = kwargs.get('debug', False)
  334. self.name = kwargs.get('name', None)
  335. self.icon = kwargs.get('icon', None)
  336. self.versrsrc = kwargs.get('version', None)
  337. self.manifest = kwargs.get('manifest', None)
  338. self.embed_manifest = kwargs.get('embed_manifest', True)
  339. self.resources = kwargs.get('resources', [])
  340. self.strip = kwargs.get('strip', False)
  341. self.upx_exclude = kwargs.get("upx_exclude", [])
  342. self.runtime_tmpdir = kwargs.get('runtime_tmpdir', None)
  343. # If ``append_pkg`` is false, the archive will not be appended to the exe, but copied beside it.
  344. self.append_pkg = kwargs.get('append_pkg', True)
  345. # On Windows allows the exe to request admin privileges.
  346. self.uac_admin = kwargs.get('uac_admin', False)
  347. self.uac_uiaccess = kwargs.get('uac_uiaccess', False)
  348. # Target architecture (macOS only)
  349. self.target_arch = kwargs.get('target_arch', None)
  350. if is_darwin:
  351. if self.target_arch is None:
  352. import platform
  353. self.target_arch = platform.machine()
  354. else:
  355. assert self.target_arch in {'x86_64', 'arm64', 'universal2'}, \
  356. f"Unsupported target arch: {self.target_arch}"
  357. logger.info("EXE target arch: %s", self.target_arch)
  358. else:
  359. self.target_arch = None # explicitly disable
  360. # Code signing identity (macOS only)
  361. self.codesign_identity = kwargs.get('codesign_identity', None)
  362. if is_darwin:
  363. logger.info("Code signing identity: %s", self.codesign_identity)
  364. else:
  365. self.codesign_identity = None # explicitly disable
  366. # Code signing entitlements
  367. self.entitlements_file = kwargs.get('entitlements_file', None)
  368. if CONF['hasUPX']:
  369. self.upx = kwargs.get('upx', False)
  370. else:
  371. self.upx = False
  372. # Old .spec format included in 'name' the path where to put created app. New format includes only exename.
  373. #
  374. # Ignore fullpath in the 'name' and prepend DISTPATH or WORKPATH.
  375. # DISTPATH - onefile
  376. # WORKPATH - onedir
  377. if self.exclude_binaries:
  378. # onedir mode - create executable in WORKPATH.
  379. self.name = os.path.join(CONF['workpath'], os.path.basename(self.name))
  380. else:
  381. # onefile mode - create executable in DISTPATH.
  382. self.name = os.path.join(CONF['distpath'], os.path.basename(self.name))
  383. # Old .spec format included on Windows in 'name' .exe suffix.
  384. if is_win or is_cygwin:
  385. # Append .exe suffix if it is not already there.
  386. if not self.name.endswith('.exe'):
  387. self.name += '.exe'
  388. base_name = os.path.splitext(os.path.basename(self.name))[0]
  389. else:
  390. base_name = os.path.basename(self.name)
  391. # Create the CArchive PKG in WORKPATH. When instancing PKG(), set name so that guts check can test whether the
  392. # file already exists.
  393. self.pkgname = os.path.join(CONF['workpath'], base_name + '.pkg')
  394. self.toc = TOC()
  395. for arg in args:
  396. if isinstance(arg, TOC):
  397. self.toc.extend(arg)
  398. elif isinstance(arg, Target):
  399. self.toc.append((os.path.basename(arg.name), arg.name, arg.typ))
  400. self.toc.extend(arg.dependencies)
  401. else:
  402. self.toc.extend(arg)
  403. if self.runtime_tmpdir is not None:
  404. self.toc.append(("pyi-runtime-tmpdir " + self.runtime_tmpdir, "", "OPTION"))
  405. if self.bootloader_ignore_signals:
  406. # no value; presence means "true"
  407. self.toc.append(("pyi-bootloader-ignore-signals", "", "OPTION"))
  408. if self.disable_windowed_traceback:
  409. # no value; presence means "true"
  410. self.toc.append(("pyi-disable-windowed-traceback", "", "OPTION"))
  411. if is_win:
  412. if not self.exclude_binaries:
  413. # onefile mode forces embed_manifest=True
  414. if not self.embed_manifest:
  415. logger.warning("Ignoring embed_manifest=False setting in onefile mode!")
  416. self.embed_manifest = True
  417. if not self.icon:
  418. # --icon not specified; use default from bootloader folder
  419. if self.console:
  420. ico = 'icon-console.ico'
  421. else:
  422. ico = 'icon-windowed.ico'
  423. self.icon = os.path.join(os.path.dirname(os.path.dirname(__file__)), 'bootloader', 'images', ico)
  424. filename = os.path.join(CONF['workpath'], CONF['specnm'] + ".exe.manifest")
  425. self.manifest = winmanifest.create_manifest(
  426. filename, self.manifest, self.console, self.uac_admin, self.uac_uiaccess
  427. )
  428. manifest_filename = os.path.basename(self.name) + ".manifest"
  429. # If external manifest file is requested (supported only in onedir mode), add the file to the TOC in order
  430. # for it to be collected as an external manifest file. Otherwise, the assembly pipeline will embed the
  431. # manifest into the executable later on.
  432. if not self.embed_manifest:
  433. self.toc.append((manifest_filename, filename, 'BINARY'))
  434. if self.versrsrc:
  435. if not isinstance(self.versrsrc, versioninfo.VSVersionInfo) and not os.path.isabs(self.versrsrc):
  436. # relative version-info path is relative to spec file
  437. self.versrsrc = os.path.join(CONF['specpath'], self.versrsrc)
  438. self.pkg = PKG(
  439. self.toc,
  440. name=self.pkgname,
  441. cdict=kwargs.get('cdict', None),
  442. exclude_binaries=self.exclude_binaries,
  443. strip_binaries=self.strip,
  444. upx_binaries=self.upx,
  445. upx_exclude=self.upx_exclude,
  446. target_arch=self.target_arch,
  447. codesign_identity=self.codesign_identity,
  448. entitlements_file=self.entitlements_file
  449. )
  450. self.dependencies = self.pkg.dependencies
  451. # Get the path of the bootloader and store it in a TOC, so it can be checked for being changed.
  452. exe = self._bootloader_file('run', '.exe' if is_win or is_cygwin else '')
  453. self.exefiles = TOC([(os.path.basename(exe), exe, 'EXECUTABLE')])
  454. self.__postinit__()
  455. _GUTS = ( # input parameters
  456. ('name', _check_guts_eq),
  457. ('console', _check_guts_eq),
  458. ('debug', _check_guts_eq),
  459. ('exclude_binaries', _check_guts_eq),
  460. ('icon', _check_guts_eq),
  461. ('versrsrc', _check_guts_eq),
  462. ('uac_admin', _check_guts_eq),
  463. ('uac_uiaccess', _check_guts_eq),
  464. ('manifest', _check_guts_eq),
  465. ('embed_manifest', _check_guts_eq),
  466. ('append_pkg', _check_guts_eq),
  467. ('target_arch', _check_guts_eq),
  468. ('codesign_identity', _check_guts_eq),
  469. ('entitlements_file', _check_guts_eq),
  470. # for the case the directory ius shared between platforms:
  471. ('pkgname', _check_guts_eq),
  472. ('toc', _check_guts_eq),
  473. ('resources', _check_guts_eq),
  474. ('strip', _check_guts_eq),
  475. ('upx', _check_guts_eq),
  476. ('mtm', None), # checked below
  477. # no calculated/analysed values
  478. ('exefiles', _check_guts_toc),
  479. )
  480. def _check_guts(self, data, last_build):
  481. if not os.path.exists(self.name):
  482. logger.info("Rebuilding %s because %s missing", self.tocbasename, os.path.basename(self.name))
  483. return 1
  484. if not self.append_pkg and not os.path.exists(self.pkgname):
  485. logger.info("Rebuilding because %s missing", os.path.basename(self.pkgname))
  486. return 1
  487. if Target._check_guts(self, data, last_build):
  488. return True
  489. if (data['versrsrc'] or data['resources']) and not is_win:
  490. # todo: really ignore :-)
  491. logger.warning('ignoring version, manifest and resources, platform not capable')
  492. if data['icon'] and not (is_win or is_darwin):
  493. logger.warning('ignoring icon, platform not capable')
  494. mtm = data['mtm']
  495. if mtm != misc.mtime(self.name):
  496. logger.info("Rebuilding %s because mtimes don't match", self.tocbasename)
  497. return True
  498. if mtm < misc.mtime(self.pkg.tocfilename):
  499. logger.info("Rebuilding %s because pkg is more recent", self.tocbasename)
  500. return True
  501. return False
  502. def _bootloader_file(self, exe, extension=None):
  503. """
  504. Pick up the right bootloader file - debug, console, windowed.
  505. """
  506. # Having console/windowed bootloader makes sense only on Windows and Mac OS.
  507. if is_win or is_darwin:
  508. if not self.console:
  509. exe = exe + 'w'
  510. # There are two types of bootloaders:
  511. # run - release, no verbose messages in console.
  512. # run_d - contains verbose messages in console.
  513. if self.debug:
  514. exe = exe + '_d'
  515. if extension:
  516. exe = exe + extension
  517. bootloader_file = os.path.join(HOMEPATH, 'PyInstaller', 'bootloader', PLATFORM, exe)
  518. logger.info('Bootloader %s' % bootloader_file)
  519. return bootloader_file
  520. def assemble(self):
  521. from PyInstaller.config import CONF
  522. logger.info("Building EXE from %s", self.tocbasename)
  523. if os.path.exists(self.name):
  524. if os.path.isdir(self.name):
  525. _rmtree(self.name) # will prompt for confirmation if --noconfirm is not given
  526. else:
  527. os.remove(self.name)
  528. if not os.path.exists(os.path.dirname(self.name)):
  529. os.makedirs(os.path.dirname(self.name))
  530. exe = self.exefiles[0][1] # pathname of bootloader
  531. if not os.path.exists(exe):
  532. raise SystemExit(_MISSING_BOOTLOADER_ERRORMSG)
  533. # Step 1: copy the bootloader file, and perform any operations that need to be done prior to appending the PKG.
  534. logger.info("Copying bootloader EXE to %s", self.name)
  535. self._copyfile(exe, self.name)
  536. os.chmod(self.name, 0o755)
  537. if is_win:
  538. # First, remove all resources from the file. This ensures that no manifest is embedded, even if bootloader
  539. # was compiled with a toolchain that forcibly embeds a default manifest (e.g., mingw toolchain from msys2).
  540. winresource.RemoveAllResources(self.name)
  541. # Embed icon.
  542. if self.icon != "NONE":
  543. logger.info("Copying icon to EXE")
  544. icon.CopyIcons(self.name, self.icon)
  545. # Embed version info.
  546. if self.versrsrc:
  547. logger.info("Copying version information to EXE")
  548. versioninfo.SetVersion(self.name, self.versrsrc)
  549. # Embed other resources.
  550. logger.info("Copying %d resources to EXE", len(self.resources))
  551. for res in self.resources:
  552. res = res.split(",")
  553. for i in range(1, len(res)):
  554. try:
  555. res[i] = int(res[i])
  556. except ValueError:
  557. pass
  558. resfile = res[0]
  559. if not os.path.isabs(resfile):
  560. resfile = os.path.join(CONF['specpath'], resfile)
  561. restype = resname = reslang = None
  562. if len(res) > 1:
  563. restype = res[1]
  564. if len(res) > 2:
  565. resname = res[2]
  566. if len(res) > 3:
  567. reslang = res[3]
  568. try:
  569. winresource.UpdateResourcesFromResFile(
  570. self.name, resfile, [restype or "*"], [resname or "*"], [reslang or "*"]
  571. )
  572. except winresource.pywintypes.error as exc:
  573. if exc.args[0] != winresource.ERROR_BAD_EXE_FORMAT:
  574. logger.error(
  575. "Error while updating resources in %s from resource file %s!",
  576. self.name,
  577. resfile,
  578. exc_info=1
  579. )
  580. continue
  581. # Handle the case where the file contains no resources, and is intended as a single resource to be
  582. # added to the exe.
  583. if not restype or not resname:
  584. logger.error("Resource type and/or name not specified!")
  585. continue
  586. if "*" in (restype, resname):
  587. logger.error(
  588. "No wildcards allowed for resource type and name when the source file does not contain "
  589. "any resources!"
  590. )
  591. continue
  592. try:
  593. winresource.UpdateResourcesFromDataFile(self.name, resfile, restype, [resname], [reslang or 0])
  594. except winresource.pywintypes.error:
  595. logger.error(
  596. "Error while updating resource %s %s in %s from data file %s!",
  597. restype,
  598. resname,
  599. self.name,
  600. resfile,
  601. exc_info=1
  602. )
  603. # Embed the manifest into the executable.
  604. if self.embed_manifest:
  605. logger.info("Emedding manifest in EXE")
  606. self.manifest.update_resources(self.name, [1])
  607. elif is_darwin:
  608. # Convert bootloader to the target arch
  609. logger.info("Converting EXE to target arch (%s)", self.target_arch)
  610. osxutils.binary_to_target_arch(self.name, self.target_arch, display_name='Bootloader EXE')
  611. # Step 2: append the PKG, if necessary
  612. if self.append_pkg:
  613. append_file = self.pkg.name # Append PKG
  614. append_type = 'PKG archive' # For debug messages
  615. else:
  616. # In onefile mode, copy the stand-alone PKG next to the executable. In onedir, this will be done by the
  617. # COLLECT() target.
  618. if not self.exclude_binaries:
  619. pkg_dst = os.path.join(os.path.dirname(self.name), os.path.basename(self.pkgname))
  620. logger.info("Copying stand-alone PKG archive from %s to %s", self.pkg.name, pkg_dst)
  621. self._copyfile(self.pkg.name, pkg_dst)
  622. else:
  623. logger.info("Stand-alone PKG archive will be handled by COLLECT")
  624. # The bootloader requires package side-loading to be explicitly enabled, which is done by embedding custom
  625. # signature to the executable. This extra signature ensures that the sideload-enabled executable is at least
  626. # slightly different from the stock bootloader executables, which should prevent antivirus programs from
  627. # flagging our stock bootloaders due to sideload-enabled applications in the wild.
  628. # Write to temporary file
  629. pkgsig_file = self.pkg.name + '.sig'
  630. with open(pkgsig_file, "wb") as f:
  631. # 8-byte MAGIC; slightly changed PKG MAGIC pattern
  632. f.write(b'MEI\015\013\012\013\016')
  633. append_file = pkgsig_file # Append PKG-SIG
  634. append_type = 'PKG sideload signature' # For debug messages
  635. if is_linux:
  636. # Linux: append data into custom ELF section using objcopy.
  637. logger.info("Appending %s to custom ELF section in EXE", append_type)
  638. retcode, stdout, stderr = exec_command_all('objcopy', '--add-section', 'pydata=%s' % append_file, self.name)
  639. logger.debug("objcopy returned %i", retcode)
  640. if stdout:
  641. logger.debug(stdout)
  642. if stderr:
  643. logger.debug(stderr)
  644. if retcode != 0:
  645. raise SystemError("objcopy Failure: %s" % stderr)
  646. elif is_darwin:
  647. # macOS: remove signature, append data, and fix-up headers so that the appended data appears to be part of
  648. # the executable (which is required by strict validation during code-signing).
  649. # Strip signatures from all arch slices. Strictly speaking, we need to remove signature (if present) from
  650. # the last slice, because we will be appending data to it. When building universal2 bootloaders natively on
  651. # macOS, only arm64 slices have a (dummy) signature. However, when cross-compiling with osxcross, we seem to
  652. # get dummy signatures on both x86_64 and arm64 slices. While the former should not have any impact, it does
  653. # seem to cause issues with further binary signing using real identity. Therefore, we remove all signatures
  654. # and re-sign the binary using dummy signature once the data is appended.
  655. logger.info("Removing signature(s) from EXE")
  656. osxutils.remove_signature_from_binary(self.name)
  657. # Append the data
  658. logger.info("Appending %s to EXE", append_type)
  659. with open(self.name, 'ab') as outf:
  660. with open(append_file, 'rb') as inf:
  661. shutil.copyfileobj(inf, outf, length=64 * 1024)
  662. # Fix Mach-O headers
  663. logger.info("Fixing EXE headers for code signing")
  664. osxutils.fix_exe_for_code_signing(self.name)
  665. else:
  666. # Fall back to just appending data at the end of the file
  667. logger.info("Appending %s to EXE", append_type)
  668. with open(self.name, 'ab') as outf:
  669. with open(append_file, 'rb') as inf:
  670. shutil.copyfileobj(inf, outf, length=64 * 1024)
  671. # Step 3: post-processing
  672. if is_win:
  673. # Set checksum to appease antiviral software. Also set build timestamp to current time to increase entropy
  674. # (but honor SOURCE_DATE_EPOCH environment variable for reproducible builds).
  675. build_timestamp = int(os.environ.get('SOURCE_DATE_EPOCH', time.time()))
  676. winutils.fixup_exe_headers(self.name, build_timestamp)
  677. elif is_darwin:
  678. # If the version of macOS SDK used to build bootloader exceeds that of macOS SDK used to built Python
  679. # library (and, by extension, bundled Tcl/Tk libraries), force the version declared by the frozen executable
  680. # to match that of the Python library.
  681. # Having macOS attempt to enable new features (based on SDK version) for frozen application has no benefit
  682. # if the Python library does not support them as well.
  683. # On the other hand, there seem to be UI issues in tkinter due to failed or partial enablement of dark mode
  684. # (i.e., the bootloader executable being built against SDK 10.14 or later, which causes macOS to enable dark
  685. # mode, and Tk libraries being built against an earlier SDK version that does not support the dark mode).
  686. # With python.org Intel macOS installers, this manifests as black Tk windows and UI elements (see issue
  687. # #5827), while in Anaconda python, it may result in white text on bright background.
  688. pylib_version = osxutils.get_macos_sdk_version(bindepend.get_python_library_path())
  689. exe_version = osxutils.get_macos_sdk_version(self.name)
  690. if pylib_version < exe_version:
  691. logger.info(
  692. "Rewriting the executable's macOS SDK version (%d.%d.%d) to match the SDK version of the Python "
  693. "library (%d.%d.%d) in order to avoid inconsistent behavior and potential UI issues in the "
  694. "frozen application.", *exe_version, *pylib_version
  695. )
  696. osxutils.set_macos_sdk_version(self.name, *pylib_version)
  697. # Re-sign the binary (either ad-hoc or using real identity, if provided).
  698. logger.info("Re-signing the EXE")
  699. osxutils.sign_binary(self.name, self.codesign_identity, self.entitlements_file)
  700. # Ensure executable flag is set
  701. os.chmod(self.name, 0o755)
  702. # Get mtime for storing into the guts
  703. self.mtm = misc.mtime(self.name)
  704. logger.info("Building EXE from %s completed successfully.", self.tocbasename)
  705. def _copyfile(self, infile, outfile):
  706. with open(infile, 'rb') as infh:
  707. with open(outfile, 'wb') as outfh:
  708. shutil.copyfileobj(infh, outfh, length=64 * 1024)
  709. class COLLECT(Target):
  710. """
  711. In one-dir mode creates the output folder with all necessary files.
  712. """
  713. def __init__(self, *args, **kws):
  714. """
  715. args
  716. One or more arguments that are either TOCs Targets.
  717. kws
  718. Possible keyword arguments:
  719. name
  720. The name of the directory to be built.
  721. """
  722. from PyInstaller.config import CONF
  723. Target.__init__(self)
  724. self.strip_binaries = kws.get('strip', False)
  725. self.upx_exclude = kws.get("upx_exclude", [])
  726. self.console = True
  727. self.target_arch = None
  728. self.codesign_identity = None
  729. self.entitlements_file = None
  730. if CONF['hasUPX']:
  731. self.upx_binaries = kws.get('upx', False)
  732. else:
  733. self.upx_binaries = False
  734. self.name = kws.get('name')
  735. # Old .spec format included in 'name' the path where to collect files for the created app. app. New format
  736. # includes only directory name.
  737. #
  738. # The 'name' directory is created in DISTPATH and necessary files are then collected to this directory.
  739. self.name = os.path.join(CONF['distpath'], os.path.basename(self.name))
  740. self.toc = TOC()
  741. for arg in args:
  742. if isinstance(arg, TOC):
  743. self.toc.extend(arg)
  744. elif isinstance(arg, Target):
  745. self.toc.append((os.path.basename(arg.name), arg.name, arg.typ))
  746. if isinstance(arg, EXE):
  747. self.console = arg.console
  748. self.target_arch = arg.target_arch
  749. self.codesign_identity = arg.codesign_identity
  750. self.entitlements_file = arg.entitlements_file
  751. for tocnm, fnm, typ in arg.toc:
  752. if tocnm == os.path.basename(arg.name) + ".manifest":
  753. self.toc.append((tocnm, fnm, typ))
  754. if not arg.append_pkg:
  755. self.toc.append((os.path.basename(arg.pkgname), arg.pkgname, 'PKG'))
  756. self.toc.extend(arg.dependencies)
  757. else:
  758. self.toc.extend(arg)
  759. self.__postinit__()
  760. _GUTS = (
  761. # COLLECT always builds, just want the toc to be written out
  762. ('toc', None),
  763. )
  764. def _check_guts(self, data, last_build):
  765. # COLLECT always needs to be executed, since it will clean the output directory anyway to make sure there is no
  766. # existing cruft accumulating
  767. return 1
  768. def assemble(self):
  769. _make_clean_directory(self.name)
  770. logger.info("Building COLLECT %s", self.tocbasename)
  771. for inm, fnm, typ in self.toc:
  772. # Adjust name for extensions, if applicable
  773. inm, fnm, typ = add_suffix_to_extension(inm, fnm, typ)
  774. if not os.path.exists(fnm) or not os.path.isfile(fnm) and is_path_to_egg(fnm):
  775. # File is contained within python egg; it is added with the egg.
  776. continue
  777. if os.pardir in os.path.normpath(inm).split(os.sep) or os.path.isabs(inm):
  778. raise SystemExit('Security-Alert: try to store file outside of dist-directory. Aborting. %r' % inm)
  779. tofnm = os.path.join(self.name, inm)
  780. todir = os.path.dirname(tofnm)
  781. if not os.path.exists(todir):
  782. os.makedirs(todir)
  783. elif not os.path.isdir(todir):
  784. raise SystemExit(
  785. "Pyinstaller needs to make a directory at %r, but there already exists a file at that path!" % todir
  786. )
  787. if typ in ('EXTENSION', 'BINARY'):
  788. fnm = checkCache(
  789. fnm,
  790. strip=self.strip_binaries,
  791. upx=self.upx_binaries,
  792. upx_exclude=self.upx_exclude,
  793. dist_nm=inm,
  794. target_arch=self.target_arch,
  795. codesign_identity=self.codesign_identity,
  796. entitlements_file=self.entitlements_file
  797. )
  798. if typ != 'DEPENDENCY':
  799. if os.path.isdir(fnm):
  800. # Because shutil.copy2() is the default copy function for shutil.copytree, this will also copy file
  801. # metadata.
  802. shutil.copytree(fnm, tofnm)
  803. else:
  804. shutil.copy(fnm, tofnm)
  805. try:
  806. shutil.copystat(fnm, tofnm)
  807. except OSError:
  808. logger.warning("failed to copy flags of %s", fnm)
  809. if typ in ('EXTENSION', 'BINARY'):
  810. os.chmod(tofnm, 0o755)
  811. logger.info("Building COLLECT %s completed successfully.", self.tocbasename)
  812. class MERGE(object):
  813. """
  814. Merge repeated dependencies from other executables into the first executable. Data and binary files are then
  815. present only once and some disk space is thus reduced.
  816. """
  817. def __init__(self, *args):
  818. """
  819. Repeated dependencies are then present only once in the first executable in the 'args' list. Other
  820. executables depend on the first one. Other executables have to extract necessary files from the first
  821. executable.
  822. args dependencies in a list of (Analysis, id, filename) tuples.
  823. Replace id with the correct filename.
  824. """
  825. # The first Analysis object with all dependencies.
  826. # Any item from the first executable cannot be removed.
  827. self._main = None
  828. self._dependencies = {}
  829. self._id_to_path = {}
  830. for _, i, p in args:
  831. self._id_to_path[os.path.normcase(i)] = p
  832. # Get the longest common path
  833. common_prefix = os.path.commonprefix([os.path.normcase(os.path.abspath(a.scripts[-1][1])) for a, _, _ in args])
  834. self._common_prefix = os.path.dirname(common_prefix)
  835. if self._common_prefix[-1] != os.sep:
  836. self._common_prefix += os.sep
  837. logger.info("Common prefix: %s", self._common_prefix)
  838. self._merge_dependencies(args)
  839. def _merge_dependencies(self, args):
  840. """
  841. Filter shared dependencies to be only in first executable.
  842. """
  843. for analysis, _, _ in args:
  844. path = os.path.normcase(os.path.abspath(analysis.scripts[-1][1]))
  845. path = path.replace(self._common_prefix, "", 1)
  846. path = os.path.splitext(path)[0]
  847. if os.path.normcase(path) in self._id_to_path:
  848. path = self._id_to_path[os.path.normcase(path)]
  849. self._set_dependencies(analysis, path)
  850. def _set_dependencies(self, analysis, path):
  851. """
  852. Synchronize the Analysis result with the needed dependencies.
  853. """
  854. for toc in (analysis.binaries, analysis.datas):
  855. for i, tpl in enumerate(toc):
  856. if not tpl[1] in self._dependencies:
  857. logger.debug("Adding dependency %s located in %s", tpl[1], path)
  858. self._dependencies[tpl[1]] = path
  859. else:
  860. dep_path = self._get_relative_path(path, self._dependencies[tpl[1]])
  861. # Ignore references that point to the origin package. This can happen if the same resource is listed
  862. # multiple times in TOCs (e.g., once as binary and once as data).
  863. if dep_path.endswith(path):
  864. logger.debug(
  865. "Ignoring self-reference of %s for %s, located in %s - duplicated TOC entry?", tpl[1], path,
  866. dep_path
  867. )
  868. # Clear the entry as it is a duplicate.
  869. toc[i] = (None, None, None)
  870. continue
  871. logger.debug("Referencing %s to be a dependency for %s, located in %s", tpl[1], path, dep_path)
  872. # Determine the path relative to dep_path (i.e, within the target directory) from the 'name'
  873. # component of the TOC tuple. If entry is EXTENSION, then the relative path needs to be
  874. # reconstructed from the name components.
  875. if tpl[2] == 'EXTENSION':
  876. # Split on os.path.sep first, to handle additional path prefix (e.g., lib-dynload)
  877. ext_components = tpl[0].split(os.path.sep)
  878. ext_components = ext_components[:-1] + ext_components[-1].split('.')[:-1]
  879. if ext_components:
  880. rel_path = os.path.join(*ext_components)
  881. else:
  882. rel_path = ''
  883. else:
  884. rel_path = os.path.dirname(tpl[0])
  885. # Take filename from 'path' (second component of TOC tuple); this way, we don't need to worry about
  886. # suffix of extensions.
  887. filename = os.path.basename(tpl[1])
  888. # Construct the full file path relative to dep_path...
  889. filename = os.path.join(rel_path, filename)
  890. # ...and use it in new DEPENDENCY entry
  891. analysis.dependencies.append((":".join((dep_path, filename)), tpl[1], "DEPENDENCY"))
  892. toc[i] = (None, None, None)
  893. # Clean the list
  894. toc[:] = [tpl for tpl in toc if tpl != (None, None, None)]
  895. # TODO: use pathlib.Path.relative_to() instead.
  896. def _get_relative_path(self, startpath, topath):
  897. start = startpath.split(os.sep)[:-1]
  898. start = ['..'] * len(start)
  899. if start:
  900. start.append(topath)
  901. return os.sep.join(start)
  902. else:
  903. return topath
  904. UNCOMPRESSED = 0
  905. COMPRESSED = 1
  906. _MISSING_BOOTLOADER_ERRORMSG = """Fatal error: PyInstaller does not include a pre-compiled bootloader for your
  907. platform. For more details and instructions how to build the bootloader see
  908. <https://pyinstaller.readthedocs.io/en/stable/bootloader-building.html>"""