https://pulumi.com logo
Title
f

flat-australia-79845

07/06/2020, 1:39 PM
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:
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:
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

bland-lamp-16797

07/06/2020, 2:39 PM
f

flat-australia-79845

07/06/2020, 2:44 PM
🙂 yep, I will
thanks @bland-lamp-16797
👍 1