1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/testing/mozbase/mozdevice/sut_tests/genfiles.py Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,85 @@ 1.4 +# This Source Code Form is subject to the terms of the Mozilla Public 1.5 +# License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 +# file, You can obtain one at http://mozilla.org/MPL/2.0/. 1.7 + 1.8 + 1.9 +from random import randint 1.10 +from zipfile import ZipFile 1.11 +import os 1.12 +import shutil 1.13 + 1.14 + 1.15 +def gen_binary_file(path, size): 1.16 + with open(path, 'wb') as f: 1.17 + for i in xrange(size): 1.18 + byte = '%c' % randint(0, 255) 1.19 + f.write(byte) 1.20 + 1.21 + 1.22 +def gen_zip(path, files, stripped_prefix=''): 1.23 + with ZipFile(path, 'w') as z: 1.24 + for f in files: 1.25 + new_name = f.replace(stripped_prefix, '') 1.26 + z.write(f, new_name) 1.27 + 1.28 + 1.29 +def mkdir(path, *args): 1.30 + try: 1.31 + os.mkdir(path, *args) 1.32 + except OSError: 1.33 + pass 1.34 + 1.35 + 1.36 +def gen_folder_structure(): 1.37 + root = 'test-files' 1.38 + prefix = os.path.join(root, 'push2') 1.39 + mkdir(prefix) 1.40 + 1.41 + gen_binary_file(os.path.join(prefix, 'file4.bin'), 59036) 1.42 + mkdir(os.path.join(prefix, 'sub1')) 1.43 + shutil.copyfile(os.path.join(root, 'mytext.txt'), 1.44 + os.path.join(prefix, 'sub1', 'file1.txt')) 1.45 + mkdir(os.path.join(prefix, 'sub1', 'sub1.1')) 1.46 + shutil.copyfile(os.path.join(root, 'mytext.txt'), 1.47 + os.path.join(prefix, 'sub1', 'sub1.1', 'file2.txt')) 1.48 + mkdir(os.path.join(prefix, 'sub2')) 1.49 + shutil.copyfile(os.path.join(root, 'mytext.txt'), 1.50 + os.path.join(prefix, 'sub2', 'file3.txt')) 1.51 + 1.52 + 1.53 +def gen_test_files(): 1.54 + gen_folder_structure() 1.55 + flist = [ 1.56 + os.path.join('test-files', 'push2'), 1.57 + os.path.join('test-files', 'push2', 'file4.bin'), 1.58 + os.path.join('test-files', 'push2', 'sub1'), 1.59 + os.path.join('test-files', 'push2', 'sub1', 'file1.txt'), 1.60 + os.path.join('test-files', 'push2', 'sub1', 'sub1.1'), 1.61 + os.path.join('test-files', 'push2', 'sub1', 'sub1.1', 'file2.txt'), 1.62 + os.path.join('test-files', 'push2', 'sub2'), 1.63 + os.path.join('test-files', 'push2', 'sub2', 'file3.txt') 1.64 + ] 1.65 + gen_zip(os.path.join('test-files', 'mybinary.zip'), 1.66 + flist, stripped_prefix=('test-files' + os.path.sep)) 1.67 + gen_zip(os.path.join('test-files', 'mytext.zip'), 1.68 + [os.path.join('test-files', 'mytext.txt')]) 1.69 + 1.70 + 1.71 +def clean_test_files(): 1.72 + ds = [os.path.join('test-files', d) for d in ('push1', 'push2')] 1.73 + for d in ds: 1.74 + try: 1.75 + shutil.rmtree(d) 1.76 + except OSError: 1.77 + pass 1.78 + 1.79 + fs = [os.path.join('test-files', f) for f in ('mybinary.zip', 'mytext.zip')] 1.80 + for f in fs: 1.81 + try: 1.82 + os.remove(f) 1.83 + except OSError: 1.84 + pass 1.85 + 1.86 + 1.87 +if __name__ == '__main__': 1.88 + gen_test_files()