summaryrefslogtreecommitdiffstats
path: root/testing/mozbase/mozdevice/sut_tests/genfiles.py
blob: 5ab8d6349e94bb4f1145f807d762f624721ae34e (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


from random import randint
from zipfile import ZipFile
import os
import shutil


def gen_binary_file(path, size):
    with open(path, 'wb') as f:
        for i in xrange(size):
            byte = '%c' % randint(0, 255)
            f.write(byte)


def gen_zip(path, files, stripped_prefix=''):
    with ZipFile(path, 'w') as z:
        for f in files:
            new_name = f.replace(stripped_prefix, '')
            z.write(f, new_name)


def mkdir(path, *args):
    try:
        os.mkdir(path, *args)
    except OSError:
        pass


def gen_folder_structure():
    root = 'test-files'
    prefix = os.path.join(root, 'push2')
    mkdir(prefix)

    gen_binary_file(os.path.join(prefix, 'file4.bin'), 59036)
    mkdir(os.path.join(prefix, 'sub1'))
    shutil.copyfile(os.path.join(root, 'mytext.txt'),
                    os.path.join(prefix, 'sub1', 'file1.txt'))
    mkdir(os.path.join(prefix, 'sub1', 'sub1.1'))
    shutil.copyfile(os.path.join(root, 'mytext.txt'),
                    os.path.join(prefix, 'sub1', 'sub1.1', 'file2.txt'))
    mkdir(os.path.join(prefix, 'sub2'))
    shutil.copyfile(os.path.join(root, 'mytext.txt'),
                    os.path.join(prefix, 'sub2', 'file3.txt'))


def gen_test_files():
    gen_folder_structure()
    flist = [
        os.path.join('test-files', 'push2'),
        os.path.join('test-files', 'push2', 'file4.bin'),
        os.path.join('test-files', 'push2', 'sub1'),
        os.path.join('test-files', 'push2', 'sub1', 'file1.txt'),
        os.path.join('test-files', 'push2', 'sub1', 'sub1.1'),
        os.path.join('test-files', 'push2', 'sub1', 'sub1.1', 'file2.txt'),
        os.path.join('test-files', 'push2', 'sub2'),
        os.path.join('test-files', 'push2', 'sub2', 'file3.txt')
    ]
    gen_zip(os.path.join('test-files', 'mybinary.zip'),
            flist, stripped_prefix=('test-files' + os.path.sep))
    gen_zip(os.path.join('test-files', 'mytext.zip'),
            [os.path.join('test-files', 'mytext.txt')])


def clean_test_files():
    ds = [os.path.join('test-files', d) for d in ('push1', 'push2')]
    for d in ds:
        try:
            shutil.rmtree(d)
        except OSError:
            pass

    fs = [os.path.join('test-files', f) for f in ('mybinary.zip', 'mytext.zip')]
    for f in fs:
        try:
            os.remove(f)
        except OSError:
            pass


if __name__ == '__main__':
    gen_test_files()