PythonPackagingPolicy
¶
When building a Python binary, there are various settings that control which
Python resources are added, where they are imported from, and other various
settings. This collection of settings is referred to as a Python Packaging
Policy. These settings are represented by the PythonPackagingPolicy
type.
Attributes¶
The following sections describe the attributes available on each instance.
allow_files
¶
(bool
)
Whether to allow the collection of generic file resources.
If false, all collected/packaged resources must be instances of
concrete resource types (PythonModuleSource
, PythonPackageResource
,
etc).
If true, File instances can be added to resource collectors.
bytecode_optimize_level_zero
¶
(bool
)
Whether to add Python bytecode at optimization level 0 (the default optimization level the Python interpreter compiles bytecode for).
bytecode_optimize_level_one
¶
(bool
)
Whether to add Python bytecode at optimization level 1.
bytecode_optimize_level_two
¶
(bool
)
Whether to add Python bytecode at optimization level 2.
extension_module_filter
¶
(string
)
The filter to apply to determine which extension modules to add. The following values are recognized:
all
Every named extension module will be included.
minimal
Return only extension modules that are required to initialize a Python interpreter. This is a very small set and various functionality from the Python standard library will not work with this value.
no-libraries
Return only extension modules that don’t require any additional libraries.
Most common Python extension modules are included. Extension modules like
_ssl
(links against OpenSSL) andzlib
are not included.no-gpl
Return only extension modules that do not link against GPL licensed libraries.
Not all Python distributions may annotate license info for all extensions or the libraries they link against. If license info is missing, the extension is not included because it could be GPL licensed. Similarly, the mechanism for determining whether a license is GPL is based on an explicit list of non-GPL licenses. This ensures new GPL licenses don’t slip through.
Default is all
.
file_scanner_classify_files
¶
(bool
)
Whether file scanning should attempt to classify files and emit typed resources corresponding to the detected file type.
If True
, operations that emit resource objects (such as
PythonExecutable.pip_install()) will emit specific
types for each resource flavor. e.g. PythonModuleSource,
PythonExtensionModule, etc.
If False
, the file scanner does not attempt to classify the type of
a file and this rich resource types are not emitted.
Can be used in conjunction with
file_scanner_emit_files. If both
are True
, there will be a File and an optional non-file
resource for each source file.
Default is True
.
file_scanner_emit_files
¶
(bool
)
Whether file scanning should emit file resources for each seen file.
If True
, operations that emit resource objects (such as
PythonExecutable.pip_install()) will emit
File instances for each encountered file.
If False
, File instances will not be emitted.
Can be used in conjunction with file_scanner_classify_files.
Default is False
.
include_classified_resources
¶
(bool
)
Whether strongly typed, classified non-File
resources have their
add_include
attribute set to True
by default.
Default is True
.
include_distribution_sources
¶
(bool
)
Whether to add source code for Python modules in the Python distribution.
Default is True
.
include_distribution_resources
¶
(bool
)
Whether to add Python package resources for Python packages in the Python distribution.
Default is False
.
include_file_resources
¶
(bool
)
Whether File resources have their add_include
attribute
set to True
by default.
Default is False
.
include_non_distribution_sources
¶
(bool
)
Whether to add source code for Python modules not in the Python distribution.
include_test
¶
(bool
)
Whether to add Python resources related to tests.
Not all files associated with tests may be properly flagged as such. This is a best effort setting.
Default is False
.
resources_location
¶
(string
)
The location that resources should be added to by default.
Default is in-memory
.
resources_location_fallback
¶
(string
or None
)
The fallback location that resources should be added to if
resources_location
fails.
Default is None
.
preferred_extension_module_variants
¶
(dict<string, string>
) (readonly)
Mapping of extension module name to variant name.
This mapping defines which preferred named variant of an extension module to use. Some Python distributions offer multiple variants of the same extension module. This mapping allows defining which variant of which extension to use when choosing among them.
Keys set on this dict are not reflected in the underlying policy. To set
a key, call the set_preferred_extension_module_variant()
method.
Methods¶
The following sections describe methods on PythonPackagingPolicy
instances.
PythonPackagingPolicy.register_resource_callback()
¶
This method registers a Starlark function to be called when resource objects
are created. The passed function receives 2 arguments: this
PythonPackagingPolicy
instance and the resource (e.g.
PythonModuleSource
) that was created.
The purpose of the callback is to enable Starlark configuration files to mutate resources upon creation so they can globally influence how those resources are packaged.
PythonPackagingPolicy.set_preferred_extension_module_variant()
¶
This method will set a preferred Python extension module variant to
use. See the documentation for preferred_extension_module_variants
above for more.
It accepts 2 string
arguments defining the extension module name
and its preferred variant.
PythonPackagingPolicy.set_resource_handling_mode()
¶
This method takes a string argument denoting the resource handling mode to apply to the policy. This string can have the following values:
classify
Files are classified as typed resources and handled as such.
Only classified resources can be added by default.
files
Files are handled as raw files (as opposed to typed resources).
Only files can be added by default.
This method is effectively a convenience method for bulk-setting multiple attributes on the instance given a behavior mode.
classify
will configure the file scanner to emit classified resources,
configure the add_include
attribute to only be True
on classified
resources, and will disable the addition of File
resources on resource
collectors.
files
will configure the file scanner to only emit File
resources,
configure the add_include
attribute to True
on File
and classified
resources, and will allow resource collectors to add File
instances.