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