예제 #1
0
def testrr_helper (filee, jalangi, norm_fn, record_fn, instrument_fn=instrument):
    try:
        shutil.rmtree("jalangi_tmp")
    except: pass
    os.mkdir("jalangi_tmp")
    os.mkdir("jalangi_tmp/out")
    os.putenv("JALANGI_HOME", jalangi.get_home())
    os.chdir("jalangi_tmp")
    (instrumented_f, out) = instrument_fn(os.path.join(os.pardir,filee), jalangi=jalangi)
    try:                    # Ignore failures on first iteration
        os.remove("inputs.js")
        shutil.copy("jalangi_inputs{}.js".format(i), "inputs.js")
    except:
        pass
    if not os.path.isfile("inputs.js"):
        util.mkempty("inputs.js")
    print "---- Running without instrumentation ----"
    try:
        os.remove("jalangi_trace")
    except:
        pass
    norm = norm_fn(os.path.join(os.pardir,filee + ".js"), jalangi=jalangi)
    with open("jalangi_normal", "w") as normfile:
        normfile.write(norm)
    print norm
    print "---- Recording execution of {} ----".format(filee)
    rec = record_fn(os.path.join(os.pardir,filee), instrumented_f)
    with open("jalangi_record", "w") as recfile:
        recfile.write(rec)
    print rec
    print "---- Replaying {} ----".format(filee)
    os.putenv("JALANGI_MODE", "replay")
    os.putenv("JALANGI_ANALYSIS", "none")
    rep = replay()
    with open("jalangi_replay", "w") as repfile:
        repfile.write(rep)
    print rep
    try:
	    wcl = util.count_lines("jalangi_trace")
	
	    with open("../jalangi_test_results", 'a') as f:
		f.write("# of lines in jalangi_trace for {}: {}".format(filee,str(wcl)))
		f.write("\n")
    except: pass	
    if norm != rep: #TODO: Factor out this.
        print "{}.js failed".format(filee)
        import difflib
        with open("../jalangi_test_results", 'a') as f:
            f.write("\n")
            for line in difflib.unified_diff(norm.splitlines(1), rec.splitlines(1), fromfile='normal.{}'.format(filee), tofile='replay.{}'.format(filee)):
                f.write(line)
    if rec != rep:
        print "{}.js failed".format(filee)
        import difflib
        with open("../jalangi_test_results", 'a') as f:
            f.write("\n")
            for line in difflib.unified_diff(rec.splitlines(1), rep.splitlines(1), fromfile='record.{}'.format(filee), tofile='replay.{}'.format(filee)):
                f.write(line)
        
    util.move_coverage(jalangi)
예제 #2
0
def _create_local_mapping(location, outer_loop=True):
    f = open(location, 'r')

    if outer_loop:
        nlines = util.count_lines(location)
        progress = tqdm(total=nlines)
        progress.set_description('Reading ' + location.split('/')[-1])

    ids = set()

    for line in f:
        line = line.split(' ')
        pid1 = line[0]
        pid2 = line[1]

        ids.add(pid1)
        ids.add(pid2)

        if outer_loop:
            progress.update(1)

    if outer_loop:
        progress.close()

    nids = len(ids)
    ids_list = list(ids)

    f.close()

    return ids_list
예제 #3
0
def read_cog_links(location=settings.COG_LINKS):
    f = open(location, 'r')
    association_mapping = {}

    progress = tqdm(total=util.count_lines(location))

    first = True
    for line in f:
        if first:
            first = False
            progress.update(1)
            continue
        else:
            line = line.strip('\n').split(' ')
            cog_1 = line[0]
            cog_2 = line[1]
            ascore = int(line[2])

            if line[0] in association_mapping:
                association_mapping[cog_1].append([cog_2, ascore])
            else:
                association_mapping[cog_1] = [[cog_2, ascore]]

            if line[1] in association_mapping:
                association_mapping[cog_2].append([cog_1, ascore])
            else:
                association_mapping[cog_2] = [[cog_1, ascore]]

            progress.set_description('Reading COG ' + cog_1)
            progress.update(1)

    progress.close()

    return association_mapping
예제 #4
0
def create_graph(name, weighted=False):
    i = open(settings.INDIVIDUAL_PPI_DIR + '/' + name + '.txt', 'r')
    if weighted:
        o = open(settings.INDIVIDUAL_W_GRAPH_DIR + '/' + name + '.txt', 'wb')
    else:
        o = open(settings.INDIVIDUAL_UW_GRAPH_DIR + '/' + name + '.txt', 'wb')

    nd = re.compile(r'[^\d.]+')
    for line in tqdm(i,
                     total=util.count_lines(settings.INDIVIDUAL_PPI_DIR + '/' +
                                            name + '.txt')):
        try:
            v = line.split(' ')
            w = int(v[2]) + int(v[3]) + int(v[4]) + int(v[5]) + int(
                v[6]) + int(v[7])
            n1 = int(nd.sub('', v[0].split('.')[1]).lstrip('0'))
            n2 = int(nd.sub('', v[1].split('.')[1]).lstrip('0'))

            if weighted and w > 0:
                o.write(str(n1) + ' ' + str(n2) + ' ' + str(w) + '\n')
            if not weighted and w > 0:
                o.write(str(n1) + ' ' + str(n2) + '\n')
        except:
            print "ERROR - ", line
            pass
    o.close()
예제 #5
0
def visualize_graph(graph_path, output=''):
    weighted = False
    if settings.INDIVIDUAL_W_GRAPH_DIR in graph_path:
        weighted = True

    G = pgv.AGraph()
    for line in tqdm(open(graph_path, 'r'),
                     total=util.count_lines(graph_path)):
        v = line.split(' ')
        n1 = v[0]
        n2 = v[1]
        G.add_edge(n1, n2)
        if weighted:
            edge = G.get_edge(n1, n2)
            edge.attr['weight'] = int(v[2])

    if output == '':
        output = graph_path.split('/')[-1].strip('.txt') + '.dot'

    G.write(output)
예제 #6
0
def save_graph(data, fname, tag):
    # `data` are the groups (by ID)
    # `fname` is the raw data file
    # `tag` helps create a uniquely-named output file
    new_name = util.gen_name(fname, tag, "png")
    plt.xlabel("Avg. Runtime (ms)", )
    plt.ylabel("Num. Configs")
    plt.title(new_name.rsplit(".", 1)[0].rsplit("/", 1)[-1])
    num_configs = util.count_lines(fname) - 1
    bin_width = data[0]["max"] - data[0]["min"]
    # Plot bins
    for bkt in data:
        # x-position is average runtime, height is count
        plt.bar(bkt["min"],
                bkt["count"],# / num_configs,
                alpha=0.6,
                bottom=0,
                # label="%s - %s" % (bkt["min"], bkt["max"]),
                width=bin_width)
    # Add median line (take median of alread-computed rows' medians)
    md = statistics.median([bkt["median"] for bkt in data if bkt["median"] is not None])
    plt.axvline(md, color='k', linestyle="solid", linewidth=5, label="median = %s" % int(md))
    # Add UNTYPED  & TYPED configuration line
    arbitrary_key = next((bkt["data"][0]["key"] for bkt in data if bkt["data"]))
    untyped = statistics.mean(get_row(fname, "".join(("0" for _ in arbitrary_key))))
    typed   = statistics.mean(get_row(fname, "".join(("1" for _ in arbitrary_key))))
    plt.axvline(untyped, color='g', linestyle='dashed', linewidth=5, label="untyped = %s" % int(untyped))
    plt.axvline(typed, color='r', linestyle='dotted', linewidth=5, label="typed = %s" % int(typed))
    plt.xticks(sorted([bkt["min"] for bkt in data] + [data[-1]["max"]]))#, rotation="vertical")
    plt.xlim(xmax=data[-1]["max"])
    #plt.ylim(ymax=0.5) #50%
    lgd = plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plt.savefig(new_name,bbox_extra_artists=(lgd,), bbox_inches='tight')
    print("Saved plot to '%s'" % new_name)
    plt.clf()
    return new_name
예제 #7
0
파일: commands.py 프로젝트: imclab/jalangi
def concolic (filee, inputs, jalangi=util.DEFAULT_INSTALL):
    try:
        shutil.rmtree("jalangi_tmp")
    except: pass
    os.mkdir("jalangi_tmp")
    os.mkdir("jalangi_tmp/out")
    os.putenv("JALANGI_HOME", jalangi.get_home())
    os.chdir("jalangi_tmp")
    (instrumented_f, out) = instrument(os.path.join(os.pardir,filee), jalangi=jalangi)
    i = 0
    iters = 0
    mismatch = False
    while i <= iters and i <= inputs:
        try:                    # Ignore failures on first iteration
            os.remove("inputs.js")
            shutil.copy("jalangi_inputs{}.js".format(i), "inputs.js")
        except:
            pass
        if not os.path.isfile("inputs.js"):
            util.mkempty("inputs.js")
        print "==== Input {} ====".format(i)
        print "---- Runing without instrumentation ----"
        try:
            os.remove("jalangi_trace")
        except:
            pass
        norm = util.run_node_script(os.path.join(os.pardir,filee + ".js"), jalangi=jalangi, savestderr=True)
        #(open("jalangi_normal", "w")).write(norm)
        print "---- Recording execution of {} ----".format(filee)
        rec = record(os.path.join(os.pardir,filee), instrumented_f)
        print "---- Replaying {} ----".format(filee)
        os.putenv("JALANGI_MODE", "replay")
        os.putenv("JALANGI_ANALYSIS", "analyses/concolic/SymbolicEngine")
        rep = replay()
        (open("jalangi_replay", "w")).write(rep)
        print rep
	try:
		wcl = util.count_lines("jalangi_trace")
	
		with open("../jalangi_test_results", 'a') as f:
		    f.write("# of lines in jalangi_trace for {}: {}".format(filee,str(wcl)))
		    f.write("\n")
	except: pass	
        if norm != rep: #TODO: Factor out this.
            import difflib
            with open("../jalangi_test_results", 'a') as f:
                f.write("\n")
                for line in difflib.unified_diff(norm.splitlines(1), rec.splitlines(1), fromfile='normal.{}'.format(filee), tofile='replay.{}'.format(filee)):
                    f.write(line)
        if rec != rep:
            import difflib
            with open("../jalangi_test_results", 'a') as f:
                f.write("\n")
                for line in difflib.unified_diff(rec.splitlines(1), rep.splitlines(1), fromfile='replay.{}'.format(filee), tofile='record.{}'.format(filee)):
                    f.write(line)
	
        try:
            iters = int(util.head("jalangi_tail",1)[0])
        except: pass
        i = i + 1
        
    for i in glob.glob("jalangi_inputs*"):
        print "*** Generated ({}:1:1) for ({}.js:1:1)".format(os.path.abspath(i),filee)
    iters = iters + 1
    if iters == inputs:
        print "{}.js passed".format(filee)
    else:
        print "{}.js failed".format(filee)
    util.move_coverage(jalangi)
예제 #8
0
파일: commands.py 프로젝트: xmorgan/jalangi
def testrr_helper(filee,
                  jalangi,
                  norm_fn,
                  record_fn,
                  instrument_fn=instrument):
    try:
        shutil.rmtree("jalangi_tmp")
    except:
        pass
    os.mkdir("jalangi_tmp")
    os.mkdir("jalangi_tmp/out")
    os.putenv("JALANGI_HOME", jalangi.get_home())
    os.chdir("jalangi_tmp")
    (instrumented_f, out) = instrument_fn(os.path.join(os.pardir, filee),
                                          jalangi=jalangi)
    try:  # Ignore failures on first iteration
        os.remove("inputs.js")
        shutil.copy("jalangi_inputs{}.js".format(i), "inputs.js")
    except:
        pass
    if not os.path.isfile("inputs.js"):
        util.mkempty("inputs.js")
    print "---- Running without instrumentation ----"
    try:
        os.remove("jalangi_trace")
    except:
        pass
    norm = norm_fn(os.path.join(os.pardir, filee + ".js"), jalangi=jalangi)
    with open("jalangi_normal", "w") as normfile:
        normfile.write(norm)
    print norm
    print "---- Recording execution of {} ----".format(filee)
    rec = record_fn(os.path.join(os.pardir, filee), instrumented_f)
    with open("jalangi_record", "w") as recfile:
        recfile.write(rec)
    print rec
    print "---- Replaying {} ----".format(filee)
    os.putenv("JALANGI_MODE", "replay")
    os.putenv("JALANGI_ANALYSIS", "none")
    rep = replay()
    with open("jalangi_replay", "w") as repfile:
        repfile.write(rep)
    print rep
    try:
        wcl = util.count_lines("jalangi_trace")

        with open("../jalangi_test_results", 'a') as f:
            f.write("# of lines in jalangi_trace for {}: {}".format(
                filee, str(wcl)))
            f.write("\n")
    except:
        pass
    if norm != rep:  #TODO: Factor out this.
        print "{}.js failed".format(filee)
        import difflib
        with open("../jalangi_test_results", 'a') as f:
            f.write("\n")
            for line in difflib.unified_diff(
                    norm.splitlines(1),
                    rec.splitlines(1),
                    fromfile='normal.{}'.format(filee),
                    tofile='replay.{}'.format(filee)):
                f.write(line)
    if rec != rep:
        print "{}.js failed".format(filee)
        import difflib
        with open("../jalangi_test_results", 'a') as f:
            f.write("\n")
            for line in difflib.unified_diff(
                    rec.splitlines(1),
                    rep.splitlines(1),
                    fromfile='record.{}'.format(filee),
                    tofile='replay.{}'.format(filee)):
                f.write(line)

    util.move_coverage(jalangi)