#-------------------------------------------------------------------------------
# Name: random_binary_file.py
# Author: Donald Whyte
# Date last modified: 29/06/12
# Description:
# This script generates a random binary file, taking the name of the file to
# generate and its size in bytes.
#-------------------------------------------------------------------------------

import sys
import random

def random_bytes(size):
    """Returns a byte array with the given size that contains randomly
    generated bytes."""

    return bytes(random.randrange(0 ,256) for i in range(size))

def write_random_binary_file(filename, size):
    """Writes a binar file with random bytes with the given size (in bytes)
    at the given path (filename argument)."""
    with open(filename, 'wb') as output_file:
        output_file.write(random_bytes(size))
        output_file.flush()

def parse_commandline_arguments():
    """Parses the command line arguments and returns a dictionary containing
    the arguments' values."""
    args = {}
    # Parsing mandatory arguments
    try:
        args['filename'] = sys.argv[1]
        args['size'] = int(sys.argv[2])
    except ValueError:
        sys.exit("Invalid size '{}' given.".format(sys.argv[2]))
    return args


if __name__ == "__main__":
    if len(sys.argv) < 3:
        sys.exit("Usage: python {} <output_filename> <size>".format(sys.argv[0]))

    args = parse_commandline_arguments()
    write_random_binary_file(args['filename'], args['size'])