:py:mod:`portability` ===================== .. py:module:: conda.core.portability .. autoapi-nested-parse:: Tools for cross-OS portability. Functions --------- .. autoapisummary:: conda.core.portability._subdir_is_win conda.core.portability.update_prefix conda.core.portability.replace_prefix conda.core.portability.binary_replace conda.core.portability.has_pyzzer_entry_point conda.core.portability.replace_pyzzer_entry_point_shebang conda.core.portability.replace_long_shebang conda.core.portability.generate_shebang_for_entry_point Attributes ---------- .. autoapisummary:: conda.core.portability.SHEBANG_REGEX conda.core.portability.MAX_SHEBANG_LENGTH conda.core.portability.POPULAR_ENCODINGS .. py:data:: SHEBANG_REGEX :value: b'^(#!(?:[ ]*)(/(?:\\\\ |[^ \\n\\r\\t])*)(.*))$' .. py:data:: MAX_SHEBANG_LENGTH .. py:data:: POPULAR_ENCODINGS :value: ('utf-8', 'utf-16-le', 'utf-16-be', 'utf-32-le', 'utf-32-be') .. py:exception:: _PaddingError Bases: :py:obj:`Exception` Common base class for all non-exit exceptions. Initialize self. See help(type(self)) for accurate signature. .. py:function:: _subdir_is_win(subdir: str) -> bool Determine if the given `subdir` corresponds to a Windows operating system. :param subdir: The subdirectory name which may contain an OS identifier. :return: Returns True if `subdir` indicates a Windows OS; otherwise, False. .. py:function:: update_prefix(path: str, new_prefix: str, placeholder: str = PREFIX_PLACEHOLDER, mode: conda.models.enums.FileMode = FileMode.text, subdir: str = context.subdir) -> None Update the prefix in a file or directory. :param path: The path to the file or directory. :param new_prefix: The new prefix to replace the old prefix with. :param placeholder: The placeholder to use for the old prefix. Defaults to PREFIX_PLACEHOLDER. :param mode: The file mode. Defaults to FileMode.text. :param subdir: The subdirectory. Defaults to context.subdir. .. py:function:: replace_prefix(mode: conda.models.enums.FileMode, data: bytes, placeholder: str, new_prefix: str, subdir: str = 'noarch') -> bytes Replaces `placeholder` text with the `new_prefix` provided. The `mode` provided can either be text or binary. We use the `POPULAR_ENCODINGS` module level constant defined above to make several passes at replacing the placeholder. We do this to account for as many encodings as possible. If this causes any performance problems in the future, it could potentially be removed (i.e. just using the most popular "utf-8" encoding"). More information/discussion available here: https://github.com/conda/conda/pull/9946 :param mode: The mode of operation. :param original_data: The original data to be updated. :param placeholder: The placeholder to be replaced. :param new_prefix: The new prefix to be used. :param subdir: The subdirectory to be used. :return: The updated data after prefix replacement. .. py:function:: binary_replace(data: bytes, search: bytes, replacement: bytes, encoding: str = 'utf-8', subdir: str = 'noarch') -> bytes Replaces occurrences of a search string with a replacement string in a given byte string. :param data: The byte string in which to perform the replacements. :param search: The string to search for in the byte string. :param replacement: The string to replace occurrences of the search string with. :param encoding: The encoding to use when encoding and decoding strings. Defaults to "utf-8". :param subdir: The subdirectory to search for. Defaults to "noarch". :return: The byte string with the replacements made. :raises _PaddingError: If the padding calculation results in a negative value. This function performs replacements only for pyzzer-type entry points on Windows. For all other cases, it returns the original byte string unchanged. .. py:function:: has_pyzzer_entry_point(data: bytes) -> bool Check if the given byte string contains a pyzzer entry point. :param data: The byte string to check. :return: True if the byte string contains a pyzzer entry point, False otherwise. .. py:function:: replace_pyzzer_entry_point_shebang(all_data: bytes, placeholder: bytes, new_prefix: str) -> bytes Code adapted from pyzzer. This is meant to deal with entry point exe's created by distlib, which consist of a launcher, then a shebang, then a zip archive of the entry point code to run. We need to change the shebang. https://bitbucket.org/vinay.sajip/pyzzer/src/5d5740cb04308f067d5844a56fbe91e7a27efccc/pyzzer/__init__.py?at=default&fileviewer=file-view-default#__init__.py-112 # NOQA :param all_data: The byte string to search for the entry point. :param placeholder: The placeholder string to search for in the entry point. :param new_prefix: The new prefix to replace the placeholder string with. :return: The updated byte string with the replaced shebang. .. py:function:: replace_long_shebang(mode: conda.models.enums.FileMode, data: bytes) -> bytes Replace long shebang lines in text mode with a shorter one. :param mode: The file mode. :param data: The byte string to search for the entry point. :return: The updated byte string with the replaced shebang. .. py:function:: generate_shebang_for_entry_point(executable: str, with_usr_bin_env: bool = False) -> str This function can be used to generate a shebang line for Python entry points. Use cases: - At install/link time, to generate the `noarch: python` entry points. - conda init uses it to create its own entry point during conda-build :param executable: The path to the Python executable. :param with_usr_bin_env: Whether to use the `/usr/bin/env` approach. :return: The generated shebang line.