1 """
2 Generate the zip test data files.
3
4 Run to build the tests/zipdataNN/ziptestdata.zip files from
5 files in tests/dataNN.
6
7 Replaces the file with the working copy, but does commit anything
8 to the source repo.
9 """
10
11 import contextlib
12 import os
13 import pathlib
14 import zipfile
15
16
17 def 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
33 def 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
43 def 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()