pub struct PythonResourceCollector { /* private fields */ }
Expand description
Type used to collect Python resources so they can be serialized.
We often want to turn Python resource primitives (module source,
bytecode, etc) into a collection of Resource
so they can be
serialized to the Python packed resources format. This type
exists to facilitate doing this.
This type is not only responsible for tracking resources but also for enforcing policies on where those resources can be loaded from and what types of resources are allowed. This includes tracking the licensing metadata for indexed resources.
Implementations§
Source§impl PythonResourceCollector
impl PythonResourceCollector
Sourcepub fn new(
allowed_locations: Vec<AbstractResourceLocation>,
allowed_extension_module_locations: Vec<AbstractResourceLocation>,
allow_new_builtin_extension_modules: bool,
allow_files: bool,
) -> Self
pub fn new( allowed_locations: Vec<AbstractResourceLocation>, allowed_extension_module_locations: Vec<AbstractResourceLocation>, allow_new_builtin_extension_modules: bool, allow_files: bool, ) -> Self
Construct a new instance of the collector.
The instance is associated with a resources policy to validate that added resources conform with rules.
We also pass a Python bytecode cache tag, which is used to derive filenames.
Sourcepub fn allowed_locations(&self) -> &Vec<AbstractResourceLocation>
pub fn allowed_locations(&self) -> &Vec<AbstractResourceLocation>
Obtain locations that resources can be loaded from.
Sourcepub fn all_top_level_module_names(&self) -> BTreeSet<String>
pub fn all_top_level_module_names(&self) -> BTreeSet<String>
Obtain a set of all top-level Python module names registered with the collector.
The returned values correspond to packages or single file modules without children modules.
Sourcepub fn check_policy(&self, location: AbstractResourceLocation) -> Result<()>
pub fn check_policy(&self, location: AbstractResourceLocation) -> Result<()>
Validate that a resource add in the specified location is allowed.
Sourcepub fn filter_resources_mut<F>(&mut self, filter: F) -> Result<()>
pub fn filter_resources_mut<F>(&mut self, filter: F) -> Result<()>
Apply a filter function on resources in this collection and mutate in place.
If the filter function returns true, the item will be preserved.
Sourcepub fn iter_resources(
&self,
) -> impl Iterator<Item = (&String, &PrePackagedResource)>
pub fn iter_resources( &self, ) -> impl Iterator<Item = (&String, &PrePackagedResource)>
Obtain an iterator over the resources in this collector.
Sourcepub fn add_licensed_component(
&mut self,
component: LicensedComponent,
) -> Result<()>
pub fn add_licensed_component( &mut self, component: LicensedComponent, ) -> Result<()>
Register a licensed software component to this collection.
Sourcepub fn normalized_licensed_components(&self) -> LicensedComponents
pub fn normalized_licensed_components(&self) -> LicensedComponents
Obtain a finalized collection of licensed components.
The collection has entries for components that lack licenses and has additional normalization performed.
Sourcepub fn add_python_module_source(
&mut self,
module: &PythonModuleSource,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_source( &mut self, module: &PythonModuleSource, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add Python module source with a specific location.
Sourcepub fn add_python_module_source_with_context(
&mut self,
module: &PythonModuleSource,
add_context: &PythonResourceAddCollectionContext,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_source_with_context( &mut self, module: &PythonModuleSource, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Add Python module source using an add context to influence operation.
All of the context’s properties are respected. This includes doing
nothing if include
is false, not adding source if store_source
is
false, and automatically deriving a bytecode request if the
optimize_level_*
fields are set.
This method is a glorified proxy to other add_*
methods: it
simply contains the logic for expanding the context’s wishes into
function calls.
Sourcepub fn add_python_module_bytecode(
&mut self,
module: &PythonModuleBytecode,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_bytecode( &mut self, module: &PythonModuleBytecode, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add Python module bytecode to the specified location.
Sourcepub fn add_python_module_bytecode_with_context(
&mut self,
module: &PythonModuleBytecode,
add_context: &PythonResourceAddCollectionContext,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_bytecode_with_context( &mut self, module: &PythonModuleBytecode, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Add Python module bytecode using an add context.
This takes the context’s fields into consideration when adding
the resource. If include
is false, this is a no-op. The context
must also have an optimize_level_*
field set corresponding with
the optimization level of the passed bytecode, or this is a no-op.
Sourcepub fn add_python_module_bytecode_from_source(
&mut self,
module: &PythonModuleBytecodeFromSource,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_bytecode_from_source( &mut self, module: &PythonModuleBytecodeFromSource, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add Python module bytecode derived from source code to the collection.
Sourcepub fn add_python_module_bytecode_from_source_with_context(
&mut self,
module: &PythonModuleBytecodeFromSource,
add_context: &PythonResourceAddCollectionContext,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_module_bytecode_from_source_with_context( &mut self, module: &PythonModuleBytecodeFromSource, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Add Python module bytecode from source using an add context to influence operations.
This method respects the settings of the context, including include
and the optimize_level_*
fields.
PythonModuleBytecodeFromSource
defines an explicit bytecode optimization level,
so this method can result in at most 1 bytecode request being added to the
collection.
Sourcepub fn add_python_package_resource(
&mut self,
resource: &PythonPackageResource,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_package_resource( &mut self, resource: &PythonPackageResource, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add resource data to a given location.
Resource data belongs to a Python package and has a name and bytes data.
Sourcepub fn add_python_package_resource_with_context(
&mut self,
resource: &PythonPackageResource,
add_context: &PythonResourceAddCollectionContext,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_package_resource_with_context( &mut self, resource: &PythonPackageResource, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Add a Python package resource using an add context.
The fields from the context will be respected. This includes not doing
anything if include
is false.
Sourcepub fn add_python_package_distribution_resource(
&mut self,
resource: &PythonPackageDistributionResource,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_package_distribution_resource( &mut self, resource: &PythonPackageDistributionResource, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add a Python package distribution resource to a given location.
Sourcepub fn add_python_package_distribution_resource_with_context(
&mut self,
resource: &PythonPackageDistributionResource,
add_context: &PythonResourceAddCollectionContext,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_package_distribution_resource_with_context( &mut self, resource: &PythonPackageDistributionResource, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Add a Python package distribution resource using an add context.
The fields from the context will be respected. This includes not doing
anything if include
is false.
Sourcepub fn add_python_extension_module_with_context(
&mut self,
extension_module: &PythonExtensionModule,
add_context: &PythonResourceAddCollectionContext,
) -> Result<(Vec<AddResourceAction>, Option<LibPythonBuildContext>)>
pub fn add_python_extension_module_with_context( &mut self, extension_module: &PythonExtensionModule, add_context: &PythonResourceAddCollectionContext, ) -> Result<(Vec<AddResourceAction>, Option<LibPythonBuildContext>)>
Add a Python extension module using an add context.
Sourcepub fn add_builtin_python_extension_module(
&mut self,
module: &PythonExtensionModule,
) -> Result<Vec<AddResourceAction>>
pub fn add_builtin_python_extension_module( &mut self, module: &PythonExtensionModule, ) -> Result<Vec<AddResourceAction>>
Add a built-in extension module.
Built-in extension modules are statically linked into the binary and cannot have their location defined.
Sourcepub fn add_python_extension_module(
&mut self,
module: &PythonExtensionModule,
location: &ConcreteResourceLocation,
) -> Result<Vec<AddResourceAction>>
pub fn add_python_extension_module( &mut self, module: &PythonExtensionModule, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
Add a Python extension module shared library that should be imported from memory.
Add a shared library to be loaded from a location.
pub fn add_file_data( &mut self, file: &File, location: &ConcreteResourceLocation, ) -> Result<Vec<AddResourceAction>>
pub fn add_file_data_with_context( &mut self, file: &File, add_context: &PythonResourceAddCollectionContext, ) -> Result<Vec<AddResourceAction>>
Sourcepub fn find_dunder_file(&self) -> Result<BTreeSet<String>>
pub fn find_dunder_file(&self) -> Result<BTreeSet<String>>
Searches for Python sources for references to file.
file usage can be problematic for in-memory modules. This method searches for its occurrences and returns module names having it present.
Sourcepub fn compile_resources(
&self,
compiler: &mut dyn PythonBytecodeCompiler,
) -> Result<CompiledResourcesCollection<'_>>
pub fn compile_resources( &self, compiler: &mut dyn PythonBytecodeCompiler, ) -> Result<CompiledResourcesCollection<'_>>
Compiles resources into a finalized collection.
This will take all resources collected so far and convert them into
a collection of Resource
plus extra file install rules.
Missing parent packages will be added automatically.
Trait Implementations§
Source§impl Clone for PythonResourceCollector
impl Clone for PythonResourceCollector
Source§fn clone(&self) -> PythonResourceCollector
fn clone(&self) -> PythonResourceCollector
1.0.0 · Source§const fn clone_from(&mut self, source: &Self)
const fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for PythonResourceCollector
impl RefUnwindSafe for PythonResourceCollector
impl Send for PythonResourceCollector
impl Sync for PythonResourceCollector
impl Unpin for PythonResourceCollector
impl UnwindSafe for PythonResourceCollector
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more