123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756 |
- """
- Various classes and functions to provide some backwards-compatibility
- with previous versions of Python onward.
- """
- import os
- import platform
- import site
- import subprocess
- import sys
- import errno
- import importlib.machinery
- from PyInstaller.exceptions import ExecCommandFailed
- from PyInstaller._shared_with_waf import _pyi_machine
- is_64bits = sys.maxsize > 2**32
- is_py35 = sys.version_info >= (3, 5)
- is_py36 = sys.version_info >= (3, 6)
- is_py37 = sys.version_info >= (3, 7)
- is_py38 = sys.version_info >= (3, 8)
- is_py39 = sys.version_info >= (3, 9)
- is_win = sys.platform.startswith('win')
- is_win_10 = is_win and (platform.win32_ver()[0] == '10')
- is_cygwin = sys.platform == 'cygwin'
- is_darwin = sys.platform == 'darwin'
- is_linux = sys.platform.startswith('linux')
- is_solar = sys.platform.startswith('sun')
- is_aix = sys.platform.startswith('aix')
- is_freebsd = sys.platform.startswith('freebsd')
- is_openbsd = sys.platform.startswith('openbsd')
- is_hpux = sys.platform.startswith('hp-ux')
- is_unix = is_linux or is_solar or is_aix or is_freebsd or is_hpux or is_openbsd
- _pyver = sys.version_info[:2]
- if is_win or is_cygwin:
- PYDYLIB_NAMES = {'python%d%d.dll' % _pyver,
- 'libpython%d%d.dll' % _pyver,
- 'libpython%d%dm.dll' % _pyver,
- 'libpython%d.%d.dll' % _pyver,
- 'libpython%d.%dm.dll' % _pyver}
- elif is_darwin:
-
- PYDYLIB_NAMES = {'Python', '.Python',
- 'Python%d' % _pyver[0],
- 'libpython%d.%d.dylib' % _pyver,
- 'libpython%d.%dm.dylib' % _pyver}
- elif is_aix:
-
-
-
- PYDYLIB_NAMES = {'libpython%d.%d.a' % _pyver,
- 'libpython%d.%dm.a' % _pyver,
- 'libpython%d.%d.so' % _pyver,
- 'libpython%d.%dm.so' % _pyver}
- elif is_freebsd:
- PYDYLIB_NAMES = {'libpython%d.%d.so.1' % _pyver,
- 'libpython%d.%dm.so.1' % _pyver,
- 'libpython%d.%d.so.1.0' % _pyver,
- 'libpython%d.%dm.so.1.0' % _pyver}
- elif is_openbsd:
- PYDYLIB_NAMES = {'libpython%d.%d.so.0.0' % _pyver,
- 'libpython%d.%dm.so.0.0' % _pyver}
- elif is_hpux:
- PYDYLIB_NAMES = {'libpython%d.%d.so' % _pyver}
- elif is_unix:
-
-
-
- PYDYLIB_NAMES = {'libpython%d.%d.so.1.0' % _pyver,
- 'libpython%d.%dm.so.1.0' % _pyver,
- 'libpython%d.%dmu.so.1.0' % _pyver,
- 'libpython%d.%dm.so' % _pyver,
- 'libpython%d.%d.so' % _pyver}
- else:
- raise SystemExit('Your platform is not yet supported. '
- 'Please define constant PYDYLIB_NAMES for your platform.')
- open_file = open
- text_read_mode = 'r'
- stdin_input = input
- safe_repr = ascii
- string_types = str
- if __debug__:
- PYCO = 'c'
- else:
- PYCO = 'o'
- if __debug__:
-
- _PYOPTS = ''
- else:
- _PYOPTS = '-O'
- base_prefix = os.path.abspath(
- getattr(sys, 'real_prefix', getattr(sys, 'base_prefix', sys.prefix))
- )
- is_venv = is_virtualenv = base_prefix != os.path.abspath(sys.prefix)
- is_conda = os.path.isdir(os.path.join(base_prefix, 'conda-meta'))
- is_pure_conda = os.path.isdir(os.path.join(sys.prefix, 'conda-meta'))
- python_executable = getattr(sys, '_base_executable', sys.executable)
- is_ms_app_store = is_win and os.path.getsize(python_executable) == 0
- if is_ms_app_store:
-
- python_executable = os.path.join(
- base_prefix, os.path.basename(python_executable))
- if not os.path.exists(python_executable):
- raise SystemExit('PyInstaller cannot locate real python executable '
- 'belonging to Python from Microsoft App Store!')
- import importlib.util
- BYTECODE_MAGIC = importlib.util.MAGIC_NUMBER
- from importlib.machinery import EXTENSION_SUFFIXES, all_suffixes
- ALL_SUFFIXES = all_suffixes()
- modname_tkinter = 'tkinter'
- if is_win:
- try:
- from win32ctypes.pywin32 import pywintypes
- from win32ctypes.pywin32 import win32api
- except ImportError:
-
-
- if not os.environ.get('PYINSTALLER_NO_PYWIN32_FAILURE'):
- raise SystemExit('PyInstaller cannot check for assembly dependencies.\n'
- 'Please install pywin32-ctypes.\n\n'
- 'pip install pywin32-ctypes\n')
- architecture = '64bit' if sys.maxsize > 2**32 and is_darwin else \
- '32bit' if is_darwin else platform.architecture()[0]
- if is_cygwin:
- system = 'Cygwin'
- else:
- system = platform.system()
- machine = _pyi_machine(platform.machine(), platform.system())
- def getenv(name, default=None):
- """
- Returns unicode string containing value of environment variable 'name'.
- """
- return os.environ.get(name, default)
- def setenv(name, value):
- """
- Accepts unicode string and set it as environment variable 'name' containing
- value 'value'.
- """
- os.environ[name] = value
- def unsetenv(name):
- """
- Delete the environment variable 'name'.
- """
-
-
-
- os.environ[name] = ""
- del os.environ[name]
- def exec_command(*cmdargs, **kwargs):
- """
- Run the command specified by the passed positional arguments, optionally
- configured by the passed keyword arguments.
- .. DANGER::
- **Ignore this function's return value** -- unless this command's standard
- output contains _only_ pathnames, in which case this function returns the
- correct filesystem-encoded string expected by PyInstaller. In all other
- cases, this function's return value is _not_ safely usable. Consider
- calling the general-purpose `exec_command_stdout()` function instead.
- For backward compatibility, this function's return value non-portably
- depends on the current Python version and passed keyword arguments:
- * Under Python 2.7, this value is an **encoded `str` string** rather than
- a decoded `unicode` string. This value _cannot_ be safely used for any
- purpose (e.g., string manipulation or parsing), except to be passed
- directly to another non-Python command.
- * Under Python 3.x, this value is a **decoded `str` string**. However,
- even this value is _not_ necessarily safely usable:
- * If the `encoding` parameter is passed, this value is guaranteed to be
- safely usable.
- * Else, this value _cannot_ be safely used for any purpose (e.g.,
- string manipulation or parsing), except to be passed directly to
- another non-Python command. Why? Because this value has been decoded
- with the encoding specified by `sys.getfilesystemencoding()`, the
- encoding used by `os.fsencode()` and `os.fsdecode()` to convert from
- platform-agnostic to platform-specific pathnames. This is _not_
- necessarily the encoding with which this command's standard output
- was encoded. Cue edge-case decoding exceptions.
- Parameters
- ----------
- cmdargs :
- Variadic list whose:
- 1. Mandatory first element is the absolute path, relative path,
- or basename in the current `${PATH}` of the command to run.
- 1. Optional remaining elements are arguments to pass to this command.
- encoding : str, optional
- Optional keyword argument specifying the encoding with which to decode
- this command's standard output under Python 3. As this function's return
- value should be ignored, this argument should _never_ be passed.
- __raise_ENOENT__ : boolean, optional
- Optional keyword argument to simply raise the exception if the
- executing the command fails since to the command is not found. This is
- useful to checking id a command exists.
- All remaining keyword arguments are passed as is to the `subprocess.Popen()`
- constructor.
- Returns
- ----------
- str
- Ignore this value. See discussion above.
- """
- encoding = kwargs.pop('encoding', None)
- raise_ENOENT = kwargs.pop('__raise_ENOENT__', None)
- try:
- proc = subprocess.Popen(cmdargs, stdout=subprocess.PIPE, **kwargs)
- out = proc.communicate(timeout=60)[0]
- except OSError as e:
- if raise_ENOENT and e.errno == errno.ENOENT:
- raise
- print('--' * 20, file=sys.stderr)
- print("Error running '%s':" % " ".join(cmdargs), file=sys.stderr)
- print(e, file=sys.stderr)
- print('--' * 20, file=sys.stderr)
- raise ExecCommandFailed("Error: Executing command failed!") from e
- except subprocess.TimeoutExpired:
- proc.kill()
- raise
-
-
- try:
- if encoding:
- out = out.decode(encoding)
- else:
-
-
- out = os.fsdecode(out)
- except UnicodeDecodeError as e:
-
-
- print('--' * 20, file=sys.stderr)
- print(str(e), file=sys.stderr)
- print('These are the bytes around the offending byte:',
- file=sys.stderr)
- print('--' * 20, file=sys.stderr)
- raise
- return out
- def exec_command_rc(*cmdargs, **kwargs):
- """
- Return the exit code of the command specified by the passed positional
- arguments, optionally configured by the passed keyword arguments.
- Parameters
- ----------
- cmdargs : list
- Variadic list whose:
- 1. Mandatory first element is the absolute path, relative path,
- or basename in the current `${PATH}` of the command to run.
- 1. Optional remaining elements are arguments to pass to this command.
- All keyword arguments are passed as is to the `subprocess.call()` function.
- Returns
- ----------
- int
- This command's exit code as an unsigned byte in the range `[0, 255]`,
- where 0 signifies success and all other values failure.
- """
-
-
- if 'encoding' in kwargs:
- kwargs.pop('encoding')
- return subprocess.call(cmdargs, **kwargs)
- def exec_command_stdout(*command_args, **kwargs):
- """
- Capture and return the standard output of the command specified by the
- passed positional arguments, optionally configured by the passed keyword
- arguments.
- Unlike the legacy `exec_command()` and `exec_command_all()` functions, this
- modern function is explicitly designed for cross-platform portability. The
- return value may be safely used for any purpose, including string
- manipulation and parsing.
- .. NOTE::
- If this command's standard output contains _only_ pathnames, this
- function does _not_ return the correct filesystem-encoded string expected
- by PyInstaller. If this is the case, consider calling the
- filesystem-specific `exec_command()` function instead.
- Parameters
- ----------
- cmdargs : list
- Variadic list whose:
- 1. Mandatory first element is the absolute path, relative path,
- or basename in the current `${PATH}` of the command to run.
- 1. Optional remaining elements are arguments to pass to this command.
- encoding : str, optional
- Optional name of the encoding with which to decode this command's
- standard output (e.g., `utf8`), passed as a keyword argument. If
- unpassed , this output will be decoded in a portable manner specific to
- to the current platform, shell environment, and system settings with
- Python's built-in `universal_newlines` functionality.
- All remaining keyword arguments are passed as is to the
- `subprocess.check_output()` function.
- Returns
- ----------
- str
- Unicode string of this command's standard output decoded according to
- the "encoding" keyword argument.
- """
-
- encoding = kwargs.pop('encoding', None)
-
-
-
- kwargs['universal_newlines'] = encoding is None
-
-
- stdout = subprocess.check_output(command_args, **kwargs)
-
- return stdout if encoding is None else stdout.decode(encoding)
- def exec_command_all(*cmdargs, **kwargs):
- """
- Run the command specified by the passed positional arguments, optionally
- configured by the passed keyword arguments.
- .. DANGER::
- **Ignore this function's return value.** If this command's standard
- output consists solely of pathnames, consider calling `exec_command()`;
- else, consider calling `exec_command_stdout()`.
- Parameters
- ----------
- cmdargs : list
- Variadic list whose:
- 1. Mandatory first element is the absolute path, relative path,
- or basename in the current `${PATH}` of the command to run.
- 1. Optional remaining elements are arguments to pass to this command.
- encoding : str, optional
- Optional keyword argument specifying the encoding with which to decode
- this command's standard output. As this function's return
- value should be ignored, this argument should _never_ be passed.
- All remaining keyword arguments are passed as is to the `subprocess.Popen()`
- constructor.
- Returns
- ----------
- (int, str, str)
- Ignore this 3-element tuple `(exit_code, stdout, stderr)`. See the
- `exec_command()` function for discussion.
- """
- encoding = kwargs.pop('encoding', None)
- proc = subprocess.Popen(cmdargs, bufsize=-1,
- stdout=subprocess.PIPE, stderr=subprocess.PIPE, **kwargs)
-
- try:
- out, err = proc.communicate(timeout=60)
- except subprocess.TimeoutExpired:
- proc.kill()
- raise
-
-
- try:
- if encoding:
- out = out.decode(encoding)
- err = err.decode(encoding)
- else:
-
-
- out = os.fsdecode(out)
- err = os.fsdecode(err)
- except UnicodeDecodeError as e:
-
-
- print('--' * 20, file=sys.stderr)
- print(str(e), file=sys.stderr)
- print('These are the bytes around the offending byte:',
- file=sys.stderr)
- print('--' * 20, file=sys.stderr)
- raise
- return proc.returncode, out, err
- def __wrap_python(args, kwargs):
- cmdargs = [sys.executable]
-
-
-
-
- if is_darwin:
- if architecture == '64bit':
- if platform.machine() == 'arm64':
- py_prefix = ['arch', '-arm64']
- else:
- py_prefix = ['arch', '-x86_64']
- elif architecture == '32bit':
- py_prefix = ['arch', '-i386']
- else:
- py_prefix = []
-
-
-
- if 'DYLD_LIBRARY_PATH' in os.environ:
- path = os.environ['DYLD_LIBRARY_PATH']
- py_prefix += ['-e', 'DYLD_LIBRARY_PATH=%s' % path]
- cmdargs = py_prefix + cmdargs
- if _PYOPTS:
- cmdargs.append(_PYOPTS)
- cmdargs.extend(args)
- env = kwargs.get('env')
- if env is None:
- env = dict(**os.environ)
-
- env['PYTHONIOENCODING'] = 'UTF-8'
-
- kwargs['encoding'] = 'UTF-8'
- return cmdargs, kwargs
- def exec_python(*args, **kwargs):
- """
- Wrap running python script in a subprocess.
- Return stdout of the invoked command.
- """
- cmdargs, kwargs = __wrap_python(args, kwargs)
- return exec_command(*cmdargs, **kwargs)
- def exec_python_rc(*args, **kwargs):
- """
- Wrap running python script in a subprocess.
- Return exit code of the invoked command.
- """
- cmdargs, kwargs = __wrap_python(args, kwargs)
- return exec_command_rc(*cmdargs, **kwargs)
- def expand_path(path):
- """
- Replace initial tilde '~' in path with user's home directory and also
- expand environment variables (${VARNAME} - Unix, %VARNAME% - Windows).
- """
- return os.path.expandvars(os.path.expanduser(path))
- def getsitepackages(prefixes=None):
- """Returns a list containing all global site-packages directories.
- For each directory present in ``prefixes`` (or the global ``PREFIXES``),
- this function will find its `site-packages` subdirectory depending on the
- system environment, and will return a list of full paths.
- """
-
- sitepackages = []
- seen = set()
- if prefixes is None:
- prefixes = [sys.prefix, sys.exec_prefix]
- for prefix in prefixes:
- if not prefix or prefix in seen:
- continue
- seen.add(prefix)
- if os.sep == '/':
- sitepackages.append(
- os.path.join(
- prefix, "lib", "python%d.%d" % sys.version_info[:2],
- "site-packages"
- )
- )
- else:
- sitepackages.append(prefix)
- sitepackages.append(os.path.join(prefix, "lib", "site-packages"))
- return sitepackages
- getsitepackages = getattr(site, 'getsitepackages', getsitepackages)
- def importlib_load_source(name, pathname):
-
- mod_loader = importlib.machinery.SourceFileLoader(name, pathname)
- return mod_loader.load_module()
- PY3_BASE_MODULES = {
-
-
-
-
- '_bootlocale',
- '_collections_abc',
- '_weakrefset',
- 'abc',
- 'codecs',
- 'collections',
- 'copyreg',
- 'encodings',
- 'enum',
- 'functools',
- 'io',
- 'heapq',
- 'keyword',
- 'linecache',
- 'locale',
- 'operator',
- 're',
- 'reprlib',
- 'sre_compile',
- 'sre_constants',
- 'sre_parse',
- 'traceback',
- 'types',
- 'weakref',
- 'warnings',
- }
- PURE_PYTHON_MODULE_TYPES = {
- 'SourceModule',
- 'CompiledModule',
- 'Package',
- 'NamespacePackage',
-
-
- 'FlatPackage',
- 'ArchiveModule',
- }
- SPECIAL_MODULE_TYPES = {
- 'AliasNode',
- 'BuiltinModule',
- 'RuntimeModule',
- 'RuntimePackage',
-
- 'Script',
- }
- BINARY_MODULE_TYPES = {
- 'Extension',
- 'ExtensionPackage',
- }
- VALID_MODULE_TYPES = PURE_PYTHON_MODULE_TYPES | SPECIAL_MODULE_TYPES | BINARY_MODULE_TYPES
- BAD_MODULE_TYPES = {
- 'BadModule',
- 'ExcludedModule',
- 'InvalidSourceModule',
- 'InvalidCompiledModule',
- 'MissingModule',
-
-
-
-
- 'RuntimeModule',
- 'RuntimePackage',
- }
- ALL_MODULE_TYPES = VALID_MODULE_TYPES | BAD_MODULE_TYPES
- MODULE_TYPES_TO_TOC_DICT = {
-
- 'AliasNode': 'PYMODULE',
- 'Script': 'PYSOURCE',
- 'SourceModule': 'PYMODULE',
- 'CompiledModule': 'PYMODULE',
- 'Package': 'PYMODULE',
- 'FlatPackage': 'PYMODULE',
- 'ArchiveModule': 'PYMODULE',
-
- 'Extension': 'EXTENSION',
- 'ExtensionPackage': 'EXTENSION',
-
- 'BuiltinModule': 'BUILTIN',
- 'NamespacePackage': 'PYMODULE',
-
- 'BadModule': 'bad',
- 'ExcludedModule': 'excluded',
- 'InvalidSourceModule': 'invalid',
- 'InvalidCompiledModule': 'invalid',
- 'MissingModule': 'missing',
- 'RuntimeModule': 'runtime',
- 'RuntimePackage': 'runtime',
-
- 'does not occur': 'BINARY',
- }
- def check_requirements():
- """
- Verify that all requirements to run PyInstaller are met.
- Fail hard if any requirement is not met.
- """
-
- if sys.version_info < (3, 6):
- raise EnvironmentError('PyInstaller requires at Python 3.6 or newer.')
|