From 5f8de423f190bbb79a62f804151bc24824fa32d8 Mon Sep 17 00:00:00 2001 From: "Matt A. Tobin" Date: Fri, 2 Feb 2018 04:16:08 -0500 Subject: Add m-esr52 at 52.6.0 --- testing/mozbase/mozdevice/sut_tests/genfiles.py | 85 +++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 testing/mozbase/mozdevice/sut_tests/genfiles.py (limited to 'testing/mozbase/mozdevice/sut_tests/genfiles.py') diff --git a/testing/mozbase/mozdevice/sut_tests/genfiles.py b/testing/mozbase/mozdevice/sut_tests/genfiles.py new file mode 100644 index 000000000..5ab8d6349 --- /dev/null +++ b/testing/mozbase/mozdevice/sut_tests/genfiles.py @@ -0,0 +1,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() -- cgit v1.2.3