def output_to_png_file(dot_filename): png_filename = os.path.splitext(dot_filename)[0] + '.png' with open(png_filename, 'w') as png_file: cmd = 'dot -Tpng %s' % dot_filename proc = subprocess.Popen(cmd, shell=True, stdout=png_file, stderr=subprocess.PIPE) _, _ = proc.communicate() if proc.returncode != 0: debug.exit_message('Running "%s" failed' % cmd)
def parse_the_command_line(): class CheckForPositiveValue(Action): def __call__(self, parser, namespace, value, option_string=None): if value <= 0: raise ArgumentError('Argument {} requires a positive integer'.\ format(option_string)) setattr(namespace, self.dest, value) parser = ArgumentParser(description='Generate a random program') parser.add_argument('--directory', help='write the program file to this directory', default=os.path.abspath(os.curdir)) parser.add_argument('--program_file', help='write the program to this file', default=None) parser.add_argument('--subprograms', action=CheckForPositiveValue, type=int, help='number of subprograms', metavar='<INT>', default=1) parser.add_argument('--loops', type=int, help='maximum number of loops in a control flow graph', metavar='<INT>', default=0) parser.add_argument('--nesting-depth', type=int, help='maximum nesting depth of loops', metavar='<INT>', default=1) parser.add_argument('--fan-out', action=CheckForPositiveValue, type=int, help='select maximum fan out of a basic block', metavar='<INT>', default=2) parser.add_argument('--vertices', type=int, action=CheckForPositiveValue, help='maximum number of basic blocks in a control flow graph', metavar='<INT>', default=10) globals.add_common_command_line_arguments(parser) globals.args = vars(parser.parse_args()) base_directory = os.path.abspath(globals.args['directory']) if globals.args['program_file'] is None: globals.args['program_file'] = create_program_filename(base_directory) globals.args['program_file'] = base_directory + os.sep + globals.args['program_file'] globals.set_filename_prefix(globals.args['program_file']) if globals.args['vertices'] < globals.args['loops'] * 2: debug.exit_message('The number of vertices in a control flow graph ' 'must be at least twice the number of loops')