summaryrefslogtreecommitdiff
path: root/tests/testutils/repo/zip.py
blob: c57e09e7d5b5f8af14c8b19ed8aeb9e6da9ed027 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
import zipfile

from buildstream.utils import sha256sum

from buildstream.plugintestutils import Repo


class Zip(Repo):

    def create(self, directory):
        archive = os.path.join(self.repo, 'file.zip')

        old_dir = os.getcwd()
        os.chdir(directory)
        with zipfile.ZipFile(archive, "w") as zipfp:
            for root, dirs, files in os.walk('.'):
                names = dirs + files
                names = [os.path.join(root, name) for name in names]

                for name in names:
                    zipfp.write(name)

        os.chdir(old_dir)

        return sha256sum(archive)

    def source_config(self, ref=None):
        archive = os.path.join(self.repo, 'file.zip')
        config = {
            'kind': 'zip',
            'url': 'file://' + archive,
            'directory': '',
            'base-dir': ''
        }
        if ref is not None:
            config['ref'] = ref

        return config