Hi there. I have been working with Serverless and ...
# python
f
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
__pycache__
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)
        super().__init__(assets=assets)

    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))
            else:
                filtered_assets[path] = asset
        return filtered_assets
then I use it this way:
Copy code
archive = gcp.storage.BucketObject(
    f'{function_name}-cloudfunction.zip',
    bucket=bucket.name,
    source=CodeAsset({
        '.': pulumi.asset.FileArchive(str(source_folder / 'pairbot')),
        'lib': pulumi.asset.FileArchive(str(source_folder / 'lib')),
    }, exclude_patterns={'**/__pycache__*', '**/tests*'}),
)
b
f
🙂 yep, I will
thanks @bland-lamp-16797
👍 1