filehasher / testdata /

#!/usr/bin/env python3
Generate files filled with random data.

Copyright (C) 2012-2013 Robert Nitsch
Licensed according to GPL v3.
import argparse
import os
import sys

def write_random_data(file, size, progress_callback=None):
    remaining = size
    while remaining > 0:
        n = min(remaining, 2**18)
        random_data = os.urandom(n)
        remaining = remaining - n

        if progress_callback is not None:
            progress_callback(size - remaining)

def main(argv):
    parser = argparse.ArgumentParser(description="Generate files filled with random data")
    parser.add_argument("target", help="The target file")
    parser.add_argument("size", type=int, help="The target size")
    parser.add_argument("--force", "--overwrite", action="store_true", default=False, help="Overwrite existing files")
    args = parser.parse_args()

    if os.path.isdir(
        print("Target is a directory: '%s'" %, file=sys.stderr)
        return 1

    if not args.force and os.path.isfile(
        print("Target file does already exist: '%s'. Add --force to overwrite." %, file=sys.stderr)
        return 1

    if args.size < 0:
        print("Size must be non-negative.", file=sys.stderr)
        return 1

    with open(, "w+b") as fh:
        write_random_data(fh, args.size, lambda i: print("\r%d / %d done (%.2f%%)" % (i, args.size, float(i)/args.size*100), end=""))

    print("\rTarget file generation has completed.")

if __name__ == "__main__":