|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import os |
| 4 | +import random |
| 5 | +import string |
| 6 | +import argparse |
| 7 | + |
| 8 | +QUANTITY = 1000 |
| 9 | +FILE_NAME_LENGTH = 10 |
| 10 | +FILE_SIZE = 1 # 1 Byte |
| 11 | +ROOT_NAME = "1000-1B" |
| 12 | +SPECIAL_FILES_ROOT = "test-tree/special-files/" |
| 13 | + |
| 14 | + |
| 15 | +def random_string(length=FILE_NAME_LENGTH): |
| 16 | + "Generate random string of specified length" |
| 17 | + return "".join(random.choices(string.ascii_lowercase, k=length)) |
| 18 | + |
| 19 | + |
| 20 | +def parse_cli(): |
| 21 | + """Prepare parser""" |
| 22 | + parser = argparse.ArgumentParser(prog="create-files", description="Generate files") |
| 23 | + parser.add_argument("--quantity", help="Number of files to be generated", type=int) |
| 24 | + parser.add_argument("--size", help="Size of each file to be generated", type=int) |
| 25 | + parser.add_argument("--root-name", help="Download zip files for testing") |
| 26 | + return parser.parse_args() |
| 27 | + |
| 28 | + |
| 29 | +def write_file(path, size): |
| 30 | + """Write file of {size} to {path}""" |
| 31 | + with open(SPECIAL_FILES_ROOT + "/" + path, "wb") as out: |
| 32 | + out.seek((size) - 1) |
| 33 | + out.write(b"\0") |
| 34 | + |
| 35 | + |
| 36 | +def check_path(path): |
| 37 | + """Make sure given path exists within special files directory""" |
| 38 | + if not os.path.exists(SPECIAL_FILES_ROOT): |
| 39 | + os.makedirs(SPECIAL_FILES_ROOT) |
| 40 | + if not os.path.exists(SPECIAL_FILES_ROOT + "/" + path): |
| 41 | + os.makedirs(SPECIAL_FILES_ROOT + "/" + path) |
| 42 | + |
| 43 | + |
| 44 | +def main(): |
| 45 | + global QUANTITY |
| 46 | + global FILE_SIZE |
| 47 | + global ROOT_NAME |
| 48 | + args = parse_cli() |
| 49 | + if args.quantity: |
| 50 | + QUANTITY = args.quantity |
| 51 | + if args.size: |
| 52 | + FILE_SIZE = args.size |
| 53 | + if args.root_name: |
| 54 | + ROOT_NAME = args.root_name |
| 55 | + check_path(ROOT_NAME) |
| 56 | + for _ in range(0, QUANTITY): |
| 57 | + write_file(ROOT_NAME + "/" + random_string(), FILE_SIZE) |
| 58 | + |
| 59 | + |
| 60 | +if __name__ == "__main__": |
| 61 | + main() |
0 commit comments