testing/mozbase/mozdevice/sut_tests/genfiles.py

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:f3cbf1eafceb
1 # This Source Code Form is subject to the terms of the Mozilla Public
2 # License, v. 2.0. If a copy of the MPL was not distributed with this
3 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
4
5
6 from random import randint
7 from zipfile import ZipFile
8 import os
9 import shutil
10
11
12 def gen_binary_file(path, size):
13 with open(path, 'wb') as f:
14 for i in xrange(size):
15 byte = '%c' % randint(0, 255)
16 f.write(byte)
17
18
19 def gen_zip(path, files, stripped_prefix=''):
20 with ZipFile(path, 'w') as z:
21 for f in files:
22 new_name = f.replace(stripped_prefix, '')
23 z.write(f, new_name)
24
25
26 def mkdir(path, *args):
27 try:
28 os.mkdir(path, *args)
29 except OSError:
30 pass
31
32
33 def gen_folder_structure():
34 root = 'test-files'
35 prefix = os.path.join(root, 'push2')
36 mkdir(prefix)
37
38 gen_binary_file(os.path.join(prefix, 'file4.bin'), 59036)
39 mkdir(os.path.join(prefix, 'sub1'))
40 shutil.copyfile(os.path.join(root, 'mytext.txt'),
41 os.path.join(prefix, 'sub1', 'file1.txt'))
42 mkdir(os.path.join(prefix, 'sub1', 'sub1.1'))
43 shutil.copyfile(os.path.join(root, 'mytext.txt'),
44 os.path.join(prefix, 'sub1', 'sub1.1', 'file2.txt'))
45 mkdir(os.path.join(prefix, 'sub2'))
46 shutil.copyfile(os.path.join(root, 'mytext.txt'),
47 os.path.join(prefix, 'sub2', 'file3.txt'))
48
49
50 def gen_test_files():
51 gen_folder_structure()
52 flist = [
53 os.path.join('test-files', 'push2'),
54 os.path.join('test-files', 'push2', 'file4.bin'),
55 os.path.join('test-files', 'push2', 'sub1'),
56 os.path.join('test-files', 'push2', 'sub1', 'file1.txt'),
57 os.path.join('test-files', 'push2', 'sub1', 'sub1.1'),
58 os.path.join('test-files', 'push2', 'sub1', 'sub1.1', 'file2.txt'),
59 os.path.join('test-files', 'push2', 'sub2'),
60 os.path.join('test-files', 'push2', 'sub2', 'file3.txt')
61 ]
62 gen_zip(os.path.join('test-files', 'mybinary.zip'),
63 flist, stripped_prefix=('test-files' + os.path.sep))
64 gen_zip(os.path.join('test-files', 'mytext.zip'),
65 [os.path.join('test-files', 'mytext.txt')])
66
67
68 def clean_test_files():
69 ds = [os.path.join('test-files', d) for d in ('push1', 'push2')]
70 for d in ds:
71 try:
72 shutil.rmtree(d)
73 except OSError:
74 pass
75
76 fs = [os.path.join('test-files', f) for f in ('mybinary.zip', 'mytext.zip')]
77 for f in fs:
78 try:
79 os.remove(f)
80 except OSError:
81 pass
82
83
84 if __name__ == '__main__':
85 gen_test_files()

mercurial