1"""
2Generate the zip test data files.
3
4Run to build the tests/zipdataNN/ziptestdata.zip files from
5files in tests/dataNN.
6
7Replaces the file with the working copy, but does commit anything
8to the source repo.
9"""
10
11import contextlib
12import os
13import pathlib
14import zipfile
15
16
17def main():
18    """
19    >>> from unittest import mock
20    >>> monkeypatch = getfixture('monkeypatch')
21    >>> monkeypatch.setattr(zipfile, 'ZipFile', mock.MagicMock())
22    >>> print(); main()  # print workaround for bpo-32509
23    <BLANKLINE>
24    ...data01... -> ziptestdata/...
25    ...
26    ...data02... -> ziptestdata/...
27    ...
28    """
29    suffixes = '01', '02'
30    tuple(map(generate, suffixes))
31
32
33def generate(suffix):
34    root = pathlib.Path(__file__).parent.relative_to(os.getcwd())
35    zfpath = root / f'zipdata{suffix}/ziptestdata.zip'
36    with zipfile.ZipFile(zfpath, 'w') as zf:
37        for src, rel in walk(root / f'data{suffix}'):
38            dst = 'ziptestdata' / pathlib.PurePosixPath(rel.as_posix())
39            print(src, '->', dst)
40            zf.write(src, dst)
41
42
43def walk(datapath):
44    for dirpath, dirnames, filenames in os.walk(datapath):
45        with contextlib.suppress(ValueError):
46            dirnames.remove('__pycache__')
47        for filename in filenames:
48            res = pathlib.Path(dirpath) / filename
49            rel = res.relative_to(datapath)
50            yield res, rel
51
52
53__name__ == '__main__' and main()
54