Skip to content

Commit

Permalink
No type variable or generic argument
Browse files Browse the repository at this point in the history
  • Loading branch information
Avasam committed Sep 6, 2022
1 parent d5f46a2 commit 903454b
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 120 deletions.
7 changes: 3 additions & 4 deletions PyInstaller/__init__.py
Expand Up @@ -55,12 +55,11 @@
# Where to put all the temporary files; .log, .pyz, etc.
DEFAULT_WORKPATH = os.path.join(os.getcwd(), 'build')

_PLATFORM = compat.system + '-' + compat.architecture
PLATFORM = _PLATFORM
PLATFORM = compat.system + '-' + compat.architecture
# Include machine name in path to bootloader for some machines (e.g., 'arm'). Explicitly avoid doing this on macOS,
# where we keep universal2 bootloaders in Darwin-64bit folder regardless of whether we are on x86_64 or arm64.
if compat.machine and not compat.is_darwin:
PLATFORM = _PLATFORM + '-' + compat.machine
PLATFORM += '-' + compat.machine
# Similarly, disambiguate musl Linux from glibc Linux.
if compat.is_musl:
PLATFORM = _PLATFORM + '-musl'
PLATFORM += '-musl'
6 changes: 1 addition & 5 deletions PyInstaller/__main__.py
Expand Up @@ -17,16 +17,12 @@
import os
import platform
from collections import defaultdict
from typing import Dict, Iterable, List, Tuple, Union

from PyInstaller import __version__
from PyInstaller import log as logging
# Note: do not import anything else until compat.check_requirements function is run!
from PyInstaller import compat

_PyIConfig = Union[Dict[str, Union[bool, str, List[str], None]], Iterable[Tuple[str, Union[bool, str, List[str],
None]]]]

logger = logging.getLogger(__name__)

# Taken from https://stackoverflow.com/a/22157136 to format args more flexibly: any help text which beings with ``R|``
Expand Down Expand Up @@ -151,7 +147,7 @@ def generate_parser() -> _PyiArgumentParser:
return parser


def run(pyi_args: Iterable[str] | None = None, pyi_config: _PyIConfig | None = None):
def run(pyi_args: list | None = None, pyi_config: dict | list | None = None):
"""
pyi_args allows running PyInstaller programmatically without a subprocess
pyi_config allows checking configuration once when running multiple tests
Expand Down
43 changes: 12 additions & 31 deletions PyInstaller/compat.py
Expand Up @@ -23,27 +23,10 @@
import subprocess
import sys
import shutil
from typing import Iterable, List, Tuple, Union

from PyInstaller._shared_with_waf import _pyi_machine
from PyInstaller.exceptions import ExecCommandFailed

if sys.version_info >= (3, 9):
_StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
else:
_StrOrBytesPath = Union[str, bytes, os.PathLike]
_OpenFile = Union[_StrOrBytesPath, int]

if sys.version_info >= (3, 8):
from typing import Literal
_Architecture = Literal['64bit', 'n32bit', '32bit']
_System = Literal['Cygwin', 'Linux', 'Darwin', 'Java', 'Windows']
_Machine = Literal['sw_64', 'loongarch64', 'arm', 'intel', 'ppc', 'mips', 'riscv', 's390x', 'unknown']
else:
_Architecture = str
_System = str
_Machine = str

# Copied from https://docs.python.org/3/library/platform.html#cross-platform.
is_64bits: bool = sys.maxsize > 2**32

Expand Down Expand Up @@ -220,20 +203,20 @@
# macOS's platform.architecture() can be buggy, so we do this manually here. Based off the python documentation:
# https://docs.python.org/3/library/platform.html#platform.architecture
if is_darwin:
architecture: _Architecture = '64bit' if sys.maxsize > 2**32 else '32bit'
architecture = '64bit' if sys.maxsize > 2**32 else '32bit'
else:
architecture: _Architecture = platform.architecture()[0]
architecture = platform.architecture()[0]

# Cygwin needs special handling, because platform.system() contains identifiers such as MSYS_NT-10.0-19042 and
# CYGWIN_NT-10.0-19042 that do not fit PyInstaller's OS naming scheme. Explicitly set `system` to 'Cygwin'.
system: _System = 'Cygwin' if is_cygwin else platform.system()
system = 'Cygwin' if is_cygwin else platform.system()

# Machine suffix for bootloader.
machine: _Machine | None = _pyi_machine(platform.machine(), platform.system())
machine = _pyi_machine(platform.machine(), platform.system())


# Wine detection and support
def is_wine_dll(filename: _OpenFile):
def is_wine_dll(filename: str | bytes | os.PathLike | int):
"""
Check if the given PE file is a Wine DLL (PE-converted built-in, or fake/placeholder one).
Expand Down Expand Up @@ -303,7 +286,7 @@ def exec_command(
*cmdargs: str,
encoding: str | None = None,
raise_enoent: bool | None = None,
**kwargs: int | bool | Iterable[int] | None
**kwargs: int | bool | Iterable | None
):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -383,7 +366,7 @@ def exec_command(
return out


def exec_command_rc(*cmdargs: str, **kwargs: float | bool | Iterable[int] | None):
def exec_command_rc(*cmdargs: str, **kwargs: float | bool | list | None):
"""
Return the exit code of the command specified by the passed positional arguments, optionally configured by the
passed keyword arguments.
Expand Down Expand Up @@ -412,7 +395,7 @@ def exec_command_rc(*cmdargs: str, **kwargs: float | bool | Iterable[int] | None


def exec_command_stdout(
*command_args: str, encoding: str | None = None, **kwargs: float | str | bytes | bool | Iterable[int] | None
*command_args: str, encoding: str | None = None, **kwargs: float | str | bytes | bool | list | None
):
"""
Capture and return the standard output of the command specified by the passed positional arguments, optionally
Expand Down Expand Up @@ -459,9 +442,7 @@ def exec_command_stdout(
return stdout if encoding is None else stdout.decode(encoding)


def exec_command_all(*cmdargs: str,
encoding: str | None = None,
**kwargs: int | bool | Iterable[int] | None) -> Tuple[int, str, str]:
def exec_command_all(*cmdargs: str, encoding: str | None = None, **kwargs: int | bool | list | None):
"""
Run the command specified by the passed positional arguments, optionally configured by the passed keyword arguments.
Expand Down Expand Up @@ -583,7 +564,7 @@ def exec_python_rc(*args: str, **kwargs: str | None):
# Path handling.


def expand_path(path: _StrOrBytesPath):
def expand_path(path: str | bytes | os.PathLike):
"""
Replace initial tilde '~' in path with user's home directory, and also expand environment variables
(i.e., ${VARNAME} on Unix, %VARNAME% on Windows).
Expand All @@ -592,15 +573,15 @@ def expand_path(path: _StrOrBytesPath):


# Site-packages functions - use native function if available.
def getsitepackages(prefixes: Iterable[str] | None = None):
def getsitepackages(prefixes: list | None = None):
"""
Returns a list containing all global site-packages directories.
For each directory present in ``prefixes`` (or the global ``PREFIXES``), this function finds its `site-packages`
subdirectory depending on the system environment, and returns a list of full paths.
"""
# This implementation was copied from the ``site`` module, python 3.7.3.
sitepackages: List[str] = []
sitepackages = []
seen = set()

if prefixes is None:
Expand Down
71 changes: 30 additions & 41 deletions PyInstaller/utils/hooks/__init__.py
Expand Up @@ -18,7 +18,7 @@
import fnmatch
from pathlib import Path
from collections import deque
from typing import Callable, Dict, Iterable, List, Tuple, Union
from typing import Callable

import pkg_resources

Expand All @@ -30,19 +30,6 @@
get_pywin32_module_file_attribute # noqa: F401
from PyInstaller import isolated

if sys.version_info >= (3, 9):
_StrOrBytesPath = Union[str, bytes, os.PathLike[str], os.PathLike[bytes]]
else:
_StrOrBytesPath = Union[str, bytes, os.PathLike]

if sys.version_info >= (3, 8):
from typing import Literal
_OnError = Literal["ignore", "warn once", "warn", "raise"]
else:
_OnError = str

_Environ = Union[Dict[str, str], Iterable[Tuple[str, str]]]

logger = logging.getLogger(__name__)

# These extensions represent Python executables and should therefore be ignored when collecting data files.
Expand All @@ -55,7 +42,7 @@
#
# For example the 'wx' module needs variable 'wxpubsub'. This tells PyInstaller which protocol of the wx module
# should be bundled.
hook_variables: Dict[str, str] = {}
hook_variables = {}


def __exec_python_cmd(cmd, env=None, capture_stdout=True):
Expand Down Expand Up @@ -140,7 +127,7 @@ def __exec_script(script_filename, *args, env=None, capture_stdout=True):
return __exec_python_cmd(cmd, env=env, capture_stdout=capture_stdout)


def exec_script(script_filename: _StrOrBytesPath, *args: str, env: _Environ | None = None):
def exec_script(script_filename: str | bytes | os.PathLike, *args: str, env: dict | list | None = None):
"""
Executes a Python script in an externally spawned interpreter, and returns anything that was emitted to the standard
output as a single string.
Expand All @@ -151,7 +138,7 @@ def exec_script(script_filename: _StrOrBytesPath, *args: str, env: _Environ | No
return __exec_script(script_filename, *args, env=env, capture_stdout=True)


def exec_script_rc(script_filename: _StrOrBytesPath, *args: str, env: _Environ | None = None):
def exec_script_rc(script_filename: str | bytes | os.PathLike, *args: str, env: dict | list | None = None):
"""
Executes a Python script in an externally spawned interpreter, and returns the exit code.
Expand Down Expand Up @@ -186,7 +173,7 @@ def eval_statement(statement: str):
return eval(txt)


def eval_script(script_filename: _StrOrBytesPath, *args: str, env: _Environ | None = None):
def eval_script(script_filename: str | bytes | os.PathLike, *args: str, env: dict | list | None = None):
txt = exec_script(script_filename, *args, env=env).strip()
if not txt:
# Return an empty string, which is "not true" but is iterable.
Expand Down Expand Up @@ -412,7 +399,7 @@ def _get_module_file_attribute(package):


def is_module_satisfies(
requirements: Iterable[str] | pkg_resources.Requirement,
requirements: list | pkg_resources.Requirement,
version: str | pkg_resources.Distribution | None = None,
version_attr: str = "__version__",
):
Expand Down Expand Up @@ -617,7 +604,7 @@ def get_package_paths(package: str):
def collect_submodules(
package: str,
filter: Callable[[str], bool] = lambda name: True,
on_error: _OnError = "warn once",
on_error: str = "warn once",
):
"""
List all submodules of a given package.
Expand Down Expand Up @@ -672,7 +659,7 @@ def collect_submodules(
return []

# Determine the filesystem path(s) to the specified package.
package_submodules: List[str] = []
package_submodules = []

todo = deque()
todo.append(package)
Expand All @@ -699,8 +686,8 @@ def collect_submodules(

# This function is called in an isolated sub-process via `isolated.Python.call`.
def _collect_submodules(name, on_error):
import pkgutil
import sys
import pkgutil
from traceback import format_exception_only

from PyInstaller.utils.hooks import logger
Expand Down Expand Up @@ -791,7 +778,7 @@ def collect_dynamic_libs(package: str, destdir: object | None = None):
return []

pkg_dirs = get_all_package_paths(package)
dylibs: List[Tuple[str, str]] = []
dylibs = []
for pkg_dir in pkg_dirs:
pkg_base = package_base_path(pkg_dir, package)
# Recursively glob for all file patterns in the package directory
Expand All @@ -814,9 +801,9 @@ def collect_dynamic_libs(package: str, destdir: object | None = None):
def collect_data_files(
package: str,
include_py_files: bool = False,
subdir: _StrOrBytesPath | None = None,
excludes: Iterable[str] | None = None,
includes: Iterable[str] | None = None,
subdir: str | bytes | os.PathLike | None = None,
excludes: list | None = None,
includes: list | None = None,
):
r"""
This function produces a list of ``(source, dest)`` non-Python (i.e., data) files that reside in ``package``.
Expand Down Expand Up @@ -899,7 +886,7 @@ def clude_walker(
sources.add(g) if is_include else sources.discard(g)

# Obtain all paths for the specified package, and process each path independently.
datas: List[Tuple[str, str]] = []
datas = []

pkg_dirs = get_all_package_paths(package)
for pkg_dir in pkg_dirs:
Expand All @@ -920,7 +907,9 @@ def clude_walker(
return datas


def collect_system_data_files(path: str, destdir: _StrOrBytesPath | None = None, include_py_files: bool = False):
def collect_system_data_files(
path: str, destdir: str | bytes | os.PathLike | None = None, include_py_files: bool = False
):
"""
This function produces a list of (source, dest) non-Python (i.e., data) files that reside somewhere on the system.
Its output can be directly assigned to ``datas`` in a hook script.
Expand All @@ -932,7 +921,7 @@ def collect_system_data_files(path: str, destdir: _StrOrBytesPath | None = None,
raise TypeError('path must be a str')

# Walk through all file in the given package, looking for data files.
datas: List[Tuple[str, str]] = []
datas = []
for dirpath, dirnames, files in os.walk(path):
for f in files:
extension = os.path.splitext(f)[1]
Expand Down Expand Up @@ -994,7 +983,7 @@ def copy_metadata(package_name: str, recursive: bool = False):

todo = deque([package_name])
done = set()
out: List[Tuple[str, str]] = []
out = []

while todo:
package_name = todo.pop()
Expand Down Expand Up @@ -1177,7 +1166,7 @@ def helper():
@_memoize
def _map_distribution_to_packages():
logger.info('Determining a mapping of distributions to packages...')
dist_to_packages: Dict[str, List[str]] = {}
dist_to_packages = {}
for p in sys.path:
# The path entry ``''`` refers to the current directory.
if not p:
Expand All @@ -1203,7 +1192,7 @@ def _map_distribution_to_packages():
# Given a ``package_name`` as a string, this function returns a list of packages needed to satisfy the requirements.
# This output can be assigned directly to ``hiddenimports``.
def requirements_for_package(package_name: str):
hiddenimports: List[str] = []
hiddenimports = []

dist_to_packages = _map_distribution_to_packages()
for requirement in pkg_resources.get_distribution(package_name).requires():
Expand All @@ -1222,10 +1211,10 @@ def requirements_for_package(package_name: str):
def collect_all(
package_name: str,
include_py_files: bool = True,
filter_submodules: Callable[[str], bool] | None = None,
exclude_datas: Iterable[str] | None = None,
include_datas: Iterable[str] | None = None,
on_error: _OnError = "warn once",
filter_submodules: Callable | None = None,
exclude_datas: list | None = None,
include_datas: list | None = None,
on_error: str = "warn once",
):
"""
Collect everything for a given package name.
Expand Down Expand Up @@ -1255,7 +1244,7 @@ def collect_all(
datas, binaries, hiddenimports = collect_all('my_module_name')
"""
datas: List[Tuple[str, str]] = []
datas = []
try:
datas += copy_metadata(package_name)
except Exception as e:
Expand Down Expand Up @@ -1299,8 +1288,8 @@ def collect_entry_point(name: str):
.. versionadded:: 4.3
"""
import pkg_resources
datas: List[Tuple[str, str]] = []
imports: List[str] = []
datas = []
imports = []
for dist in pkg_resources.iter_entry_points(name):
project_name = '' if dist.dist is None else dist.dist.project_name
datas += copy_metadata(project_name)
Expand Down Expand Up @@ -1345,8 +1334,8 @@ def get_hook_config(hook_api: PostGraphAPI, module_name: str, key: str):

def include_or_exclude_file(
filename: str,
include_list: Iterable[str] | None = None,
exclude_list: Iterable[str] | None = None,
include_list: list | None = None,
exclude_list: list | None = None,
):
"""
Generic inclusion/exclusion decision function based on filename and list of include and exclude patterns.
Expand Down

0 comments on commit 903454b

Please sign in to comment.