Example #1
0
def get_builder( deltas ):
    if options.sources is None:
        if not options.compound_edits:
            try:
                deltas = lower_genome( deltas )
            except ValueError as e:
                infomsg( "ERROR:", e.message, file = sys.stderr )
                exit( 1 )
        deltas = list( enumerate( deltas ) )
        builder = GenomeBuilder( genprog )
    else:
        print >>sys.stderr, "file-based differences not implemented yet"
        exit( 2 )
    return deltas, builder
Example #2
0
def process_genome( best ):

########
# Write the genome to disk so that minimize and maximize can use it
########

    if options.stop_after is None:
        genome = "genome.%d" % best[ 0 ]
    else:
        genome = "genome.%d-%d" % ( options.stop_after, best[ 0 ] )
    this_genome = os.path.join( results, genome ) + ".orig"
    if not os.path.exists( this_genome ) or options.force:
        infomsg( "INFO: saving genome to", this_genome )
        with open( this_genome, 'w' ) as fh:
            infomsg( " ".join( lower_genome( best[ 1 ].split() ) ), file = fh )

########
# Maximize the genome for all inputs
########

    if input_size not in options.inputs:
        options.inputs = [ input_size ] + options.inputs
    infomsg( "INFO: maximizing genome for inputs:", *options.inputs )
    with saving( "multi.cache" ):
        for test_input in options.inputs:
            next_genome = this_genome + "." + test_input
            if not os.path.exists( next_genome ) or options.force:
                with mkconfig( test_cmd( test_input ) ) as config:
                    check_call( [
                        maximize, genprog, config,
                            "--genome-file", this_genome,
                            "--save-genome", next_genome
                    ] )
            with open( '/dev/null', 'w' ) as null:
                status = call(
                    [ "diff", this_genome, next_genome ],
                    stdout = null, stderr = null
                )
                if status != 0:
                    this_genome = next_genome
        infomsg( "INFO: final genome stored at", this_genome )

########
# Minimize the genome for the original input
########

        if not options.skip_minimize:
            min_genome = this_genome + ".min"
            min_binary = min_genome + ".bin"
            min_source = min_genome + ".src"
            fnames = [ min_genome, min_binary, min_source ]
            if options.force or any(
                    [ not os.path.exists( f ) for f in fnames ]
                ):
                with mkconfig( test_cmd ) as config:
                    cmd = [
                        minimize, genprog, config,
                            "--genome-file", this_genome,
                            "--save-binary", min_binary,
                            "--save-genome", min_genome,
                            "--save-sources", min_source
                    ]
                    if options.low_error is not None:
                        cmd += [ "--low-error", str( options.low_error ) ]
                    check_call( cmd )
            this_genome = min_genome

########
# Measure the improvement over the original
########

        if options.stop_after is None:
            cache = lambda i: os.path.join(
                results, "genome.%s-%s.cache" % ( options.filter, i )
            )
        else:
            cache = lambda i: os.path.join(
                results, "genome.%d-%s-%s.cache" % (
                    options.stop_after, options.filter, i
                )
            )
        genome_key = os.path.basename( this_genome )
        with ImprovementTable( os.path.join( results, "improvement" ) ) as imprv:
            for test_input in options.inputs:
                if not options.force and ( genome_key, test_input ) in imprv:
                    continue
                with mkconfig( test_cmd( test_input ) ) as config:
                    cmd = [
                        minimize, genprog, config,
                            "--search", "none",
                            "--genome-file", this_genome,
                    ]
                    if not options.force:
                        cmd += [ "--cache", cache( test_input ) ]
                    if options.low_error is not None:
                        cmd += [ "--low-error", str( options.low_error ) ]
                    with mktemp() as log:
                        try:
                            pipeline( [ cmd, [ "tee", log ] ] )
                        except CalledProcessError:
                            if ( genome_key, test_input ) in imprv:
                                del imprv[ genome_key, test_input ]
                            continue
                        imprv[ genome_key, test_input ] = " ".join(
                            map( str, get_improvement( log ) )
                        )
Example #3
0
    exit()

if options.genome is not None:
    deltas = options.genome.split()
elif options.genome_file is not None:
    with open( options.genome_file ) as fh:
        deltas = " ".join( fh.readlines() ).split()
else:
    infomsg(
        "ERROR: either --genome or --genome-file is required", file = sys.stderr
    )
    parser.print_help()
    exit( 1 )
if not options.compound_edits:
    try:
        deltas = lower_genome( deltas )
    except ValueError as e:
        infomsg( "ERROR:", e.message, file = sys.stderr )
        exit( 1 )
deltas = list( enumerate( deltas ) )

genprog    = args[ 0 ]
configfile = args[ 1 ]

@contextmanager
def swallow( exn, f, *args, **kw ):
    mgr = f( *args, **kw )
    try:
        obj = mgr.__enter__()
    except exn:
        yield None