portability#

Tools for cross-OS portability.

Functions#

update_prefix(path, new_prefix[, placeholder, mode, ...])

replace_prefix(→ bytes)

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

binary_replace(→ bytes)

Perform a binary replacement of data, where the placeholder search is

has_pyzzer_entry_point(data)

replace_pyzzer_entry_point_shebang(all_data, ...)

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

replace_long_shebang(mode, data)

generate_shebang_for_entry_point(executable[, ...])

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

Attributes#

SHEBANG_REGEX

MAX_SHEBANG_LENGTH

POPULAR_ENCODINGS

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.

update_prefix(path, new_prefix, placeholder=PREFIX_PLACEHOLDER, mode=FileMode.text, subdir=context.subdir)#
replace_prefix(mode: conda.models.enums.FileMode, data: bytes, placeholder: str, new_prefix: str) 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

binary_replace(data: bytes, search: bytes, replacement: bytes, encoding: str = 'utf-8') bytes#

Perform a binary replacement of data, where the placeholder search is replaced with replacement and the remaining string is padded with null characters. All input arguments are expected to be bytes objects.

Parameters:
  • data -- The bytes object that will be searched and replaced

  • search -- The bytes object to find

  • replacement -- The bytes object that will replace search

  • encoding (str) -- The encoding of the expected string in the binary.

has_pyzzer_entry_point(data)#
replace_pyzzer_entry_point_shebang(all_data, placeholder, new_prefix)#

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

replace_long_shebang(mode, data)#
generate_shebang_for_entry_point(executable, with_usr_bin_env=False)#

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