generate the zip test data files run to build the testszipdatannziptestdata zip files from files in testsdatann replaces the file with the working copy but does commit anything to the source repo from unittest import mock monkeypatch getfixture monkeypatch monkeypatch setattrzipfile zipfile mock magicmock print main print workaround for bpo32509 blankline data01 ziptestdata data02 ziptestdata from unittest import mock monkeypatch getfixture monkeypatch monkeypatch setattr zipfile zipfile mock magicmock print main print workaround for bpo 32509 blankline data01 ziptestdata data02 ziptestdata
import contextlib
import os
import pathlib
import zipfile


def main():
    
    suffixes = '01', '02'
    tuple(map(generate, suffixes))


def generate(suffix):
    root = pathlib.Path(__file__).parent.relative_to(os.getcwd())
    zfpath = root / f'zipdata{suffix}/ziptestdata.zip'
    with zipfile.ZipFile(zfpath, 'w') as zf:
        for src, rel in walk(root / f'data{suffix}'):
            dst = 'ziptestdata' / pathlib.PurePosixPath(rel.as_posix())
            print(src, '->', dst)
            zf.write(src, dst)


def walk(datapath):
    for dirpath, dirnames, filenames in os.walk(datapath):
        with contextlib.suppress(ValueError):
            dirnames.remove('__pycache__')
        for filename in filenames:
            res = pathlib.Path(dirpath) / filename
            rel = res.relative_to(datapath)
            yield res, rel


__name__ == '__main__' and main()