Hi there. I have been working with Serverless and ...
# python
Hi there. I have been working with Serverless and lambda functions in Python for a while, one thing I usually do is filter out the tests folders and
folders from the artifacts that I push to the cloud. Today I made this work in Pulumi with:
Copy code
from fnmatch import fnmatch
from typing import Dict, Iterable, Union
from pathlib import Path

from pulumi.asset import Asset, Archive, AssetArchive, FileArchive, FileAsset

def filter_file_archive(
    path: str, archive: FileArchive, exclude_patterns: Iterable[str] = None
) -> Dict[str, FileAsset]:
    if not exclude_patterns:
        exclude_patterns = []

    file_iterator = Path(archive.path).rglob('*')
    asset_root = Path(path)
    assets = {
        str(asset_root / file.relative_to(archive.path)): FileAsset(str(file))
        for file in file_iterator if not any({fnmatch(file, pattern) for pattern in exclude_patterns})
    return assets

class CodeAsset(AssetArchive):

    def __init__(self, assets: Dict[str, Union[Asset, Archive]], exclude_patterns: Iterable[str] = None):
        assets = self._filter_assets(assets, exclude_patterns)

    def _filter_assets(
        self, assets: Dict[str, Union[Asset, Archive]], exclude_patterns: Iterable[str] = None
    ) -> Dict[str, Union[Asset, Archive]]:
        filtered_assets: Dict[str, Union[Asset, Archive]] = {}
        for path, asset in assets.items():
            if isinstance(asset, FileArchive):
                filtered_assets.update(filter_file_archive(path, asset, exclude_patterns))
                filtered_assets[path] = asset
        return filtered_assets
then I use it this way:
Copy code
archive = gcp.storage.BucketObject(
        '.': pulumi.asset.FileArchive(str(source_folder / 'pairbot')),
        'lib': pulumi.asset.FileArchive(str(source_folder / 'lib')),
    }, exclude_patterns={'**/__pycache__*', '**/tests*'}),
🙂 yep, I will
thanks @bland-lamp-16797
👍 1