portability#

Tools for cross-OS portability.

Functions#

_subdir_is_win(→ bool)

Determine if the given subdir corresponds to a Windows operating system.

update_prefix(→ None)

Update the prefix in a file or directory.

replace_prefix(→ bytes)

Replaces placeholder text with the new_prefix provided. The mode provided can

binary_replace(→ bytes)

Replaces occurrences of a search string with a replacement string in a given byte string.

has_pyzzer_entry_point(→ bool)

Check if the given byte string contains a pyzzer entry point.

replace_pyzzer_entry_point_shebang(→ bytes)

Code adapted from pyzzer. This is meant to deal with entry point exe's created by distlib,

replace_long_shebang(→ bytes)

Replace long shebang lines in text mode with a shorter one.

generate_shebang_for_entry_point(→ str)

This function can be used to generate a shebang line for Python entry points.

Attributes#

SHEBANG_REGEX = b'^(#!(?:[ ]*)(/(?:\\\\ |[^ \\n\\r\\t])*)(.*))$'#
MAX_SHEBANG_LENGTH#
POPULAR_ENCODINGS = ('utf-8', 'utf-16-le', 'utf-16-be', 'utf-32-le', 'utf-32-be')#
exception _PaddingError#

Bases: Exception

Common base class for all non-exit exceptions.

Initialize self. See help(type(self)) for accurate signature.

_subdir_is_win(subdir: str) bool#

Determine if the given subdir corresponds to a Windows operating system.

Parameters:

subdir -- The subdirectory name which may contain an OS identifier.

Returns:

Returns True if subdir indicates a Windows OS; otherwise, False.

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.

Parameters:
  • path -- The path to the file or directory.

  • new_prefix -- The new prefix to replace the old prefix with.

  • placeholder -- The placeholder to use for the old prefix. Defaults to PREFIX_PLACEHOLDER.

  • mode -- The file mode. Defaults to FileMode.text.

  • subdir -- The subdirectory. Defaults to context.subdir.

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: conda/conda#9946

Parameters:
  • mode -- The mode of operation.

  • original_data -- The original data to be updated.

  • placeholder -- The placeholder to be replaced.

  • new_prefix -- The new prefix to be used.

  • subdir -- The subdirectory to be used.

Returns:

The updated data after prefix replacement.

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.

Parameters:
  • data -- The byte string in which to perform the replacements.

  • search -- The string to search for in the byte string.

  • replacement -- The string to replace occurrences of the search string with.

  • encoding -- The encoding to use when encoding and decoding strings. Defaults to "utf-8".

  • subdir -- The subdirectory to search for. Defaults to "noarch".

Returns:

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.

has_pyzzer_entry_point(data: bytes) bool#

Check if the given byte string contains a pyzzer entry point.

Parameters:

data -- The byte string to check.

Returns:

True if the byte string contains a pyzzer entry point, False otherwise.

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

Parameters:
  • all_data -- The byte string to search for the entry point.

  • placeholder -- The placeholder string to search for in the entry point.

  • new_prefix -- The new prefix to replace the placeholder string with.

Returns:

The updated byte string with the replaced shebang.

replace_long_shebang(mode: conda.models.enums.FileMode, data: bytes) bytes#

Replace long shebang lines in text mode with a shorter one.

Parameters:
  • mode -- The file mode.

  • data -- The byte string to search for the entry point.

Returns:

The updated byte string with the replaced shebang.

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

Parameters:
  • executable -- The path to the Python executable.

  • with_usr_bin_env -- Whether to use the /usr/bin/env approach.

Returns:

The generated shebang line.