Esempio n. 1
0
def extract_patch_info_from(
    diff_filepath: str,
    project_path: str,
    allowed_files_extensions: List[str],
) -> PatchInfo:
    patch_info = [
        # в файле changed_file_path.py изменены строки 33-36 и 40-46 (после применения дифа)
        # ('changed_file_path.py', [(33, 36), (40, 46)]),
    ]

    patch = PatchSet.from_filename(diff_filepath, encoding='utf-8')
    for patched_file in patch:
        if os.path.splitext(
                patched_file.path)[1] not in allowed_files_extensions:
            continue

        file_changes_info = (
            os.path.join(project_path, patched_file.path),
            [],
        )
        for changed_file_part_info in patched_file:
            file_changes_info[1].append((
                changed_file_part_info.target_start,
                changed_file_part_info.target_start +
                changed_file_part_info.target_length,
            ), )
        patch_info.append(file_changes_info)
    return patch_info
Esempio n. 2
0
def diff_parser_from_file(diff_path):
    patches = []
    try:
        patches = PatchSet.from_filename(diff_path, encoding='utf-8')
    except Exception as e:
        logging.error("diff_parser_from_file failed! diff path {}\nerror: {}".format(diff_path, e))
    return patches
Esempio n. 3
0
    def __init__(self, filename, strict=False):
        self.filename = filename
        diff = PatchSet.from_filename(filename)
        date = None
        author = None

        with open(self.filename, 'r') as f:
            lines = f.read().splitlines()
        lines = list(takewhile(lambda line: line != '---', lines))
        for line in lines:
            if line.startswith(DATE_PREFIX):
                date = parse(line[len(DATE_PREFIX):])
            elif line.startswith(FROM_PREFIX):
                author = GitCommit.format_git_author(line[len(FROM_PREFIX):])
        header = list(takewhile(lambda line: line != '', lines))
        body = lines[len(header) + 1:]

        modified_files = []
        for f in diff:
            if f.is_added_file:
                t = 'A'
            elif f.is_removed_file:
                t = 'D'
            else:
                t = 'M'
            modified_files.append((f.path, t))
        super().__init__(None,
                         date,
                         author,
                         body,
                         modified_files,
                         strict=strict)
def suggest_all_changes(diff_path, context, changed_files=[]):
    patch_set = PatchSet.from_filename(diff_path)

    suggestions = []
    for patched_file in patch_set.modified_files:
        path = os.path.relpath(patched_file.path, context["repo_path"])

        if path not in changed_files:
            continue

        for hunk in patched_file:
            suggestions.extend(
                (path, *partial_suggestion)
                for partial_suggestion in parse_suggestions_from_hunk(hunk))

    print(f"Making {len(suggestions)} on the pull request")

    last_path = None
    for path, begin, end, suggestion in suggestions:
        if last_path != path:
            print(f"Making suggestions on {path}")
            last_path = path

        response = suggest_changes(COMMENT_TEXT, suggestion, path, begin, end,
                                   context)

        print("Got response", response)
        pprint(response.__dict__)
Esempio n. 5
0
    def from_filename(filename):
        """Reads diff from the filename provided.

    This fills self._mapping with a mapping for each file the represents that
    map from target filename and line number to source filename and line number.
    """
        patchset = PatchSet.from_filename(filename)
        return DiffMapper.from_patchset(patchset)
Esempio n. 6
0
def run(project, bugid, patch_no, tmp_tracefile='tmp_a'):
    #v=line[2:].split(' ')
    #project=v[0]
    #bugid=v[1]
    tmp_tracefile += project + bugid + patch_no + 'get_randoop_coverage'
    tmp_tracefile = os.path.join(os.getcwd(), tmp_tracefile)
    w_buggy = project + bugid + 'b'
    test = 'randoop'
    #
    testfiledir = '../test_gen_randoop/' + project + '/randoop/' + bugid + '/'
    targetfile = testfiledir + project + '-' + bugid + 'b-randoop.' + bugid + '.instr.tar.bz2'
    testfile = testfiledir + project + '-' + bugid + 'b-randoop.' + bugid + '.tar.bz2'
    targetdir = testfiledir + 'suite'
    os.system('mkdir ' + targetdir)
    os.system('tar xvf ' + testfile + ' -C ' + targetdir)
    os.system('make TestCaseInstr ARGS="' + targetdir + ' ' + tmp_tracefile +
              ' Randoop"')
    os.system('cd ' + targetdir + ' && tar -c ./* | bzip2 > ../' + project +
              '-' + bugid + 'b-randoop.' + bugid + '.instr.tar.bz2')
    #patch_no=line[3:].split(' ')[1][1:]
    #print(patch_no)

    #os.system('cp -r '+v+' '+v+'_Patch'+line[3:].split(' ')[1][1:])
    #print(line)
    #if line[0]=='[':
    #    bug_loc=line.split(']')[0][1:]
    #else :
    #    bug_loc=line.split(' (')[0]
    #bug_loc=bug_loc.split/(':')
    #bug_loc=bug_loc[0].replace('.','/')+'.java:'+bug_loc[1]
    print(w_buggy + '_' + patch_no)
    patch = PatchSet.from_filename('../patches/' + patch_no)
    souce_file_list = []
    for filei in range(len(patch)):
        source_file = patch[filei].source_file
        souce_file_list.append(source_file)
        line_no_list = []
        for hunki in range(len(patch[filei])):
            for i in range(len(patch[filei][hunki])):
                if not patch[filei][hunki][i].is_context:
                    line_no_list.append(
                        str(patch[filei][hunki][i - 1].source_line_no + 1))
                    break
        os.system('cp ' + source_file + ' ' + source_file + '.bak')
        os.system('make MthdInstr ARGS="' + source_file + ' ' + tmp_tracefile +
                  ' ' + ','.join(line_no_list) + '"')
    if (os.path.exists(tmp_tracefile)):
        os.system('rm ' + tmp_tracefile)
    os.system('defects4j test -s ' + targetfile + ' -w ' + w_buggy)
    os.system('mv ' + tmp_tracefile + ' ../randoop_cover/' + w_buggy + '_' +
              patch_no + '.txt')
    for source_file in souce_file_list:
        os.system('rm ' + source_file)
        os.system('mv ' + source_file + '.bak ' + source_file)
    os.system('rm -rf ' + w_buggy)
    os.system('defects4j checkout -p ' + project + ' -v ' + bugid + 'b -w ' +
              project + bugid + 'b')
Esempio n. 7
0
def load_diff(commit1, commit2, repo):
    """
    Get a list of changed files between two commits
    """
    diff_file = str(commit1) + "diff" + str(commit2)
    cmd.run_cmd(["git", "-C", repo, "diff", commit1, commit2], diff_file)
    patchset = PatchSet.from_filename(diff_file)
    cmd.run_cmd(["rm", diff_file])
    return patchset
Esempio n. 8
0
def create_module_from_diff(module_name):

    if (subprocess.call(["git", "branch"],
                        stderr=subprocess.STDOUT,
                        stdout=open(os.devnull, "w")) != 0):
        click.Abort("Current directory is not a git repository")

    patch_file = f".rony_{module_name}.patch"
    module_path = os.path.join(os.path.expanduser("~"), "MyRonyModules",
                               module_name)

    p = subprocess.call(["git", "diff", "--no-prefix"],
                        stdout=open(patch_file, "w"))

    patch_set = PatchSet.from_filename(patch_file)

    for patched_file in patch_set:

        file_path = patched_file.path
        added_lines = []
        for hunk in patched_file:
            for line in hunk:
                if line.is_added:
                    added_lines.append(line.value)

        module_file_path = os.path.join(module_path, file_path)
        module_dir_path = os.path.dirname(module_file_path)

        if not os.path.exists(module_dir_path):
            os.makedirs(module_dir_path)

        with open(module_file_path, "w") as f:
            f.write("".join(added_lines))

    os.remove(patch_file)

    info = click.prompt("Please your modules description", default="")
    inst = click.prompt(
        "Please instructions to be displayed to the users after they add this module",
        default="",
    )
    developer = click.prompt("Please enter your email", default="")

    with open(module_path + ".json", "w") as f:
        f.write(
            json.dumps(
                {
                    "info": info,
                    "instructions": [inst],
                    "developers": [developer],
                    "input_info": [],
                    "version": "0.0.0",
                },
                sort_keys=True,
                indent=4,
                separators=(",", ": "),
            ))
Esempio n. 9
0
def create_module_from_diff(module_name):

    if subprocess.call(["git", "branch"],
                       stderr=subprocess.STDOUT,
                       stdout=open(os.devnull, 'w')) != 0:
        click.Abort('Current directory is not a git repository')

    patch_file = f'.rony_{module_name}.patch'
    module_path = os.path.join(os.path.expanduser('~'), 'MyRonyModules',
                               module_name)

    p = subprocess.call(["git", "diff", '--no-prefix'],
                        stdout=open(patch_file, 'w'))

    patch_set = PatchSet.from_filename(patch_file)

    for patched_file in patch_set:

        file_path = patched_file.path
        added_lines = []
        for hunk in patched_file:
            for line in hunk:
                if line.is_added:
                    added_lines.append(line.value)

        module_file_path = os.path.join(module_path, file_path)
        module_dir_path = os.path.dirname(module_file_path)

        if not os.path.exists(module_dir_path):
            os.makedirs(module_dir_path)

        with open(module_file_path, 'w') as f:
            f.write(''.join(added_lines))

    os.remove(patch_file)

    info = click.prompt('Please your modules description', default='')
    inst = click.prompt(
        'Please instructions to be displayed to the users after they add this module',
        default='')
    developer = click.prompt('Please enter your email', default='')

    with open(module_path + '.json', 'w') as f:
        f.write(
            json.dumps(
                {
                    "info": info,
                    'instructions': inst,
                    "developers": developer,
                    "input_info": [],
                    "version": "0.0.0"
                },
                sort_keys=True,
                indent=4,
                separators=(',', ': ')))
Esempio n. 10
0
def real_patch(diffdata, encfile, key, tostdout):
    if isinstance(diffdata, str):
        p = PatchSet.from_filename(diffdata)
    else:
        p = PatchSet(diffdata)

    if len(p) == 0:
        raise Exception(
            "No patched files in this diff, sure you remembered the -u for diff?"
        )
    # TODO read file name from PatchedFile and let this function process file/directory trees instead of just one filr
    if len(p) != 1:
        raise Exception("can't handle multiple files")
    for patchedfile in p:
        with open(encfile, "r+b") as encf:
            alllines = encf.readlines()
            lines = []

            def seelines():
                """ for debugging in pdb """
                return list(map(line_decryptor(key), lines))

            def addline(val):
                iv = os.urandom(16)
                aes = AES.new(key, AES.MODE_CFB, iv)
                lines.append(
                    hexlify(iv) + b"," + hexlify(aes.encrypt(val + "\n")) +
                    b"\n")

            for hunk in patchedfile:
                for line in hunk:

                    def useoldline():
                        crypted = alllines[line.source_line_no - 1]
                        decrypted = line_decryptor(key)(crypted)
                        assert decrypted == line.value.encode(
                            "utf-8") + b"\n", (decrypted, line.value)
                        lines.append(crypted)

                    if line.is_removed: continue
                    if line.is_context:
                        useoldline()
                    else:
                        if line.source_line_no is not None:
                            useoldline()
                        else:
                            assert line.is_added
                            addline(line.value)

            if tostdout:
                print(b''.join(lines).decode("utf-8").rstrip())
            else:
                encf.truncate(0)
                encf.seek(0)
                encf.write(b''.join(lines).rstrip())
Esempio n. 11
0
def getChangedLines(patchfile, source_name):
    patch = PatchSet.from_filename(patchfile, encoding='utf-8')
    changed_lines = []
    for p in patch:  #patchset
        fname = os.path.basename(p.source_file)
        if source_name in fname:
            for h in p:  #hunk
                for line in h:  #line
                    if line.is_added:
                        changed_lines.append(str(line.target_line_no + 1))

    return changed_lines
Esempio n. 12
0
    def __init__(self, filename):
        self.filename = filename
        diff = PatchSet.from_filename(filename)
        date = None
        author = None
        subject = ''

        subject_last = False
        with open(self.filename, 'r') as f:
            lines = f.read().splitlines()
        lines = list(takewhile(lambda line: line != '---', lines))
        for line in lines:
            if line.startswith(DATE_PREFIX):
                date = parse(line[len(DATE_PREFIX):])
            elif line.startswith(FROM_PREFIX):
                author = GitCommit.format_git_author(line[len(FROM_PREFIX):])
            elif line.startswith(SUBJECT_PREFIX):
                subject = line[len(SUBJECT_PREFIX):]
                subject_last = True
            elif subject_last and line.startswith(' '):
                subject += line
            elif line == '':
                break
            else:
                subject_last = False

        if subject:
            subject = subject_patch_regex.sub('', subject)
        header = list(takewhile(lambda line: line != '', lines))
        # Note: commit message consists of email subject, empty line, email body
        message = [subject] + lines[len(header):]

        modified_files = []
        for f in diff:
            # Strip "a/" and "b/" prefixes
            source = decode_path(f.source_file)[2:]
            target = decode_path(f.target_file)[2:]

            if f.is_added_file:
                t = 'A'
            elif f.is_removed_file:
                t = 'D'
            elif unidiff_supports_renaming and f.is_rename:
                # Consider that renamed files are two operations: the deletion
                # of the original name and the addition of the new one.
                modified_files.append((source, 'D'))
                t = 'A'
            else:
                t = 'M'
            modified_files.append((target if t != 'D' else source, t))
        git_info = GitInfo(None, date, author, message, modified_files)
        super().__init__(git_info, commit_to_info_hook=lambda x: None)
Esempio n. 13
0
def main():
    parser = argparse.ArgumentParser(description='Утилита для проверки ошибочно изменных файлов в индексе')
    parser.add_argument('--version', action='version', version='%(prog)s {}'.format(__version__))
    parser.add_argument('-v', '--verbose', dest='verbose_count', action='count', default=0,
                        help='Increases log verbosity for each occurence.')
    parser.add_argument('--g', action='store_true', default=False,
                        help='Запустить чтение индекса из git и определить список файлов для разбора')
    
    args = parser.parse_args()

    log.setLevel(max(3 - args.verbose_count, 0) * 10)
    
    taglistchange = ('<d3p1:id>', '<d3p1:fullIntervalBegin>',
                     '<d3p1:fullIntervalEnd>', '<d3p1:visualBegin>',
                     '<xr:TypeId>',
                     '<xr:ValueId>'
                     )

    if args.g is True:
        files = get_list_of_comitted_files()
        for file in files:
            if not file[-12:] == "Template.xml":
                continue
                
            data = get_diff_forfile(file)
            if data is None:
                log.error("diff file not exists {}".format(file))
                continue
            pathc = PatchSet.from_filename(data, encoding='utf-8')
            for f in pathc.modified_files:
                log.debug('file is {}'.format(f))
                modifiedsource, modifiedtarget = [],[]
                for hunk in f:
                    modifiedsource = modifiedsource + list(filter(lambda x: not x[:1] == " ", hunk.source))
                    modifiedtarget = modifiedtarget + list(filter(lambda x: not x[:1] == " ", hunk.target))
                
                
                sourcetags = list(filter(lambda x: x[1:].strip().startswith(taglistchange), modifiedsource))
                targettags = list(filter(lambda x: x[1:].strip().startswith(taglistchange), modifiedtarget))
                log.debug(sourcetags)
                log.debug(targettags)
                
                if not (len(sourcetags) == len(modifiedsource) and \
                    len(targettags) == len(modifiedtarget) and \
                    len(sourcetags) == len(targettags)):
                    continue
            
                #Теперь надо будет отменить изменения в индексе для файла. 
                git_reset_file(file, 'HEAD')
                break
        replace_old_form_attr(files)
def run(project,bugid,patch_no,tmp_tracefile='tmp_a'):
        #v=line[2:].split(' ')
        #project=v[0]
        #bugid=v[1]
        tmp_tracefile+=project+bugid+patch_no+'get_randoop_coverage'
        tmp_tracefile=os.path.join(os.getcwd(),tmp_tracefile)
        w_buggy=project+bugid+'b'
        test='randoop'
        #
        testfiledir='../test_gen_randoop/'+project+'/randoop/'+bugid+'/'
        targetfile=testfiledir+project+'-'+bugid+'b-randoop.'+bugid+'.instr.tar.bz2'
        testfile=testfiledir+project+'-'+bugid+'b-randoop.'+bugid+'.tar.bz2'
        targetdir=testfiledir+'suite'
        os.system('mkdir '+targetdir)
        os.system('tar xvf '+testfile+' -C '+targetdir)
        os.system('make TestCaseInstr ARGS="'+targetdir+' '+tmp_tracefile+' Randoop"')
        os.system('cd '+targetdir+' && tar -c ./* | bzip2 > ../'+project+'-'+bugid+'b-randoop.'+bugid+'.instr.tar.bz2')
        #patch_no=line[3:].split(' ')[1][1:]
        #print(patch_no)

        #os.system('cp -r '+v+' '+v+'_Patch'+line[3:].split(' ')[1][1:])
        #print(line)
        #if line[0]=='[':
        #    bug_loc=line.split(']')[0][1:]
        #else :
        #    bug_loc=line.split(' (')[0]
        #bug_loc=bug_loc.split/(':')
        #bug_loc=bug_loc[0].replace('.','/')+'.java:'+bug_loc[1]
        print(w_buggy+'_'+patch_no)
        patch = PatchSet.from_filename('../patches/'+patch_no)
        souce_file_list=[]
        for filei in range(len(patch)):
            source_file=patch[filei].source_file
            souce_file_list.append(source_file)
            line_no_list=[]
            for hunki in range(len(patch[filei])):
                for i in range(len(patch[filei][hunki])):
                    if not patch[filei][hunki][i].is_context:
                        line_no_list.append(str(patch[filei][hunki][i-1].source_line_no+1))
                        break
            os.system('cp '+source_file+' '+source_file+'.bak')
            os.system('make MthdInstr ARGS="'+source_file+' '+tmp_tracefile+' '+','.join(line_no_list)+'"')
        if(os.path.exists(tmp_tracefile)):
            os.system('rm '+tmp_tracefile)
        os.system('defects4j test -s '+targetfile+' -w '+w_buggy)
        os.system('mv '+tmp_tracefile+' ../randoop_cover/'+w_buggy+'_'+patch_no+'.txt')
        for source_file in souce_file_list:
            os.system('rm '+source_file)
            os.system('mv '+source_file+'.bak '+source_file)
        os.system('rm -rf '+w_buggy)
        os.system('defects4j checkout -p '+project+' -v '+bugid+'b -w '+project+bugid+'b')
def run(project,bugid,patch_no,randoop_tests,tmp_tracefile='tmp_d'):
    tmp_tracefile+=project+bugid+patch_no+'run_trace_randoop.py'
    tmp_tracefile=os.path.join(os.getcwd(),tmp_tracefile)
    w_buggy=project+str(bugid)+'b'
    w_patched=w_buggy+'_'+patch_no
    #
    patchfile=os.path.join('../patches',patch_no)
    patch = PatchSet.from_filename(patchfile)
    source_file=patch[0].source_file
    target_file=patch[0].target_file
    
    os.system('cp '+source_file+' '+source_file+'.bak')
    os.system('cp '+target_file+' '+target_file+'.bak')

    os.system('make instru_class ARGS="-S '+source_file+' -T '+tmp_tracefile+'"')
    os.system('make instru_class ARGS="-S '+target_file+
            ' -T '+tmp_tracefile+' '+
            ' -P '+patchfile+
            ' -F '+target_file+'"')
    #
    dir_path='../traces/'+w_patched
    if(os.path.exists(tmp_tracefile)):
        os.system('rm '+tmp_tracefile)
    os.system('mkdir '+dir_path)
    os.system('mkdir '+os.path.join(dir_path,'buggy'))
    os.system('mkdir '+os.path.join(dir_path,'patched'))

    test='randoop'
    #
    testfile='../test_gen_randoop/'+project+'/randoop/'+bugid+'/'+project+'-'+bugid+'b-randoop.'+bugid+'.tar.bz2'
    
    comp_flag=True
    for Test_Case in randoop_tests:
        test='Randoop.'+Test_Case.strip()
        if comp_flag:
            status=os.system('timeout 90 defects4j test -s '+testfile+' -t '+Test_Case.strip()+' -w '+w_buggy)
        else:
            status=os.system('timeout 90 defects4j test -s '+testfile+' -t '+Test_Case.strip()+' -n -w '+w_buggy)
        if status==0:
            os.system('mv '+tmp_tracefile+' '+os.path.join(dir_path,'buggy','__'.join(test.split('::'))))
        
        if comp_flag:
            status=os.system('timeout 90 defects4j test -s '+testfile+' -t '+Test_Case.strip()+' -w '+w_patched)
        else:
            status=os.system('timeout 90 defects4j test -s '+testfile+' -t '+Test_Case.strip()+' -n -w '+w_patched)
        if status==0:
            os.system('mv '+tmp_tracefile+' '+os.path.join(dir_path,'patched','__'.join(test.split('::'))))
        comp_flag=False
    
    os.system('mv '+source_file+'.bak '+source_file)
    os.system('mv '+target_file+'.bak '+target_file)
Esempio n. 16
0
    def get_vulnerable_lines(dir):
        flaw_dict = {}
        patch_file = join(dir, 'patch/file.patch')
        patch = PatchSet.from_filename(patch_file, encoding='latin-1')

        for file in patch:
            filename = abspath(join(dir, 'app', file.path))

            # Produces a list of vulnerable line numbers
            vulnerable_lines = [
                line.source_line_no for hunk in file for line in hunk
                if line.is_removed
            ]

            flaw_dict[filename] = vulnerable_lines

        return flaw_dict
def get_patched_class(patch_no):
    patchfile=os.path.join('../../patches',patch_no)
    patch = PatchSet.from_filename(patchfile,encoding='utf-8')
    source_file=patch[0].source_file
    #print(source_file)
    line_no_list=[]
    tmp_file='tmp_result'+patch_no
    for hunki in range(len(patch[0])):
        for i in range(len(patch[0][hunki])):
            if not patch[0][hunki][i].is_context:
                line_no_list.append(str(patch[0][hunki][i-1].source_line_no+1))
                break
    os.system('cd .. && make PatchInfo ARGS="'+os.path.join('../source/',source_file)+' '+tmp_file+' '+','.join(line_no_list)+'" >/dev/null')
    f=open('../'+tmp_file)
    res=f.readlines()[0].strip()
    f.close()
    os.system('rm ../'+tmp_file)
    return res
Esempio n. 18
0
def run(project,bugid,patch_no,tmp_tracefile='tmp_b'):
        w_buggy=project+bugid+'b'
        test='randoop'
        
        tmp_tracefile+=project+bugid+patch_no+'get_test_coverage'
        tmp_tracefile=os.path.join(os.getcwd(),tmp_tracefile)
        #
        testdir=os.path.join(w_buggy,get_path_to_test(w_buggy))
        os.system('cp -r '+testdir+' '+testdir+'_bak')
        
        
        os.system('make TestCaseInstr ARGS="'+testdir+' '+tmp_tracefile+' '+project+'"')
        
        print(w_buggy+'_'+patch_no)
        #
        patch = PatchSet.from_filename('../patches/'+patch_no)
        souce_file_list=[]
        for filei in range(len(patch)):
            source_file=patch[filei].source_file
            souce_file_list.append(source_file)
            line_no_list=[]
            for hunki in range(len(patch[filei])):
                for i in range(len(patch[filei][hunki])):
                    if not patch[filei][hunki][i].is_context:
                        line_no_list.append(str(patch[filei][hunki][i-1].source_line_no+1))
                        break
            os.system('cp '+source_file+' '+source_file+'.bak')
            os.system('make MthdInstr ARGS="'+source_file+' '+tmp_tracefile+' '+','.join(line_no_list)+'"')

        os.system('defects4j compile -w '+w_buggy)
        if(os.path.exists(tmp_tracefile)):
            os.system('rm '+tmp_tracefile)
        os.system('defects4j test -n -r -w '+w_buggy)
        os.system('mv '+tmp_tracefile+' ../test_coverage/'+w_buggy+'_'+patch_no+'.txt')
        for source_file in souce_file_list:
            os.system('rm '+source_file)
            os.system('mv '+source_file+'.bak '+source_file)
        os.system('rm -rf '+testdir)
        os.system('mv '+testdir+'_bak '+testdir)
        
        os.system('rm -rf '+w_buggy)
        os.system('defects4j checkout -p '+project+' -v '+bugid+'b -w '+project+bugid+'b')
Esempio n. 19
0
    def __init__(self, filename, strict=False):
        self.filename = filename
        diff = PatchSet.from_filename(filename)
        date = None
        author = None

        with open(self.filename, 'r') as f:
            lines = f.read().splitlines()
        lines = list(takewhile(lambda line: line != '---', lines))
        for line in lines:
            if line.startswith(DATE_PREFIX):
                date = parse(line[len(DATE_PREFIX):])
            elif line.startswith(FROM_PREFIX):
                author = GitCommit.format_git_author(line[len(FROM_PREFIX):])
        header = list(takewhile(lambda line: line != '', lines))
        body = lines[len(header) + 1:]

        modified_files = []
        for f in diff:
            # Strip "a/" and "b/" prefixes
            source = f.source_file[2:]
            target = f.target_file[2:]

            if f.is_added_file:
                t = 'A'
            elif f.is_removed_file:
                t = 'D'
            elif f.is_rename:
                # Consider that renamed files are two operations: the deletion
                # of the original name and the addition of the new one.
                modified_files.append((source, 'D'))
                t = 'A'
            else:
                t = 'M'
            modified_files.append((target, t))
        super().__init__(None,
                         date,
                         author,
                         body,
                         modified_files,
                         strict=strict,
                         commit_to_date_hook=lambda x: date)
Esempio n. 20
0
File: D2C.py Progetto: lirenlin/D2C
def scanDiff (repo, diffFile):
  if not os.path.isfile (diff_file):
    print "%s is not found" % (diff_file)
    exit ()

  try:
    patch = PatchSet.from_filename (diff_file, encoding='utf-8')
  except:
    print "invlid diff file"
    exit ()

  string = ""
  for patchedFile in patch:
    fileName = patchedFile.path

    entry = CLEntry (fileName)
    if patchedFile.is_added_file:
      entry.setChanges (["New"])
    elif patchedFile.is_removed_file:
      entry.setChanges (["Remove"])
    else:
      if fileName.split ('.')[-1] not in ["c", "cpp", "c++", "h", "hpp"]:
        sectionList = list ()
        for hunk in patchedFile:
          sectionList.append (hunk.section_header)
        entry.setChanges (sectionList)
      else:
        locList = list ()
        for hunk in patchedFile:
          start = hunk.source_start
          end = start + hunk.source_length
          loc = Loc(start, end)
          locList.append (loc)
        sectionList = clangFind (repo + fileName, locList)
        entry.setChanges (sectionList)

    string += entry.genEntry ()
  print string
Esempio n. 21
0
def main():
    base = sys.argv[1]
    patches = sys.argv[2]
    output = sys.argv[3]
    counter = 1
    index = []
    files = os.listdir(patches)
    pc = 0
    for pfile in sorted(files, reverse=True):
        sys.stdout.write("\r%d" % pc)
        pc += 1
        pfile = os.path.join(patches, pfile)
        try:
            patch = PatchSet.from_filename(pfile)
        except:
            continue
        base_counter = counter
        for f in patch.modified_files:
            fn = "%05d_after_%s" % (counter, f.path.replace("/", "_"))
            srcf = os.path.join(base, f.path)
            if os.path.exists(srcf):
                index.append([fn])
                copyfile(srcf, os.path.join(output, fn))
                counter += 1
        call(["git", "apply", "-R", "--reject", pfile], cwd=base)
        counter = base_counter
        for f in patch.modified_files:
            fn = "%05d_before_%s" % (counter, f.path.replace("/", "_"))
            srcf = os.path.join(base, f.path)
            if os.path.exists(srcf):
                index[counter - 1].insert(0, fn)
                copyfile(srcf, os.path.join(output, fn))
                counter += 1
    with open(os.path.join(output, "index.txt"), "w") as fout:
        for p in index:
            fout.write("%s %s\n" % tuple(p))
    print()
Esempio n. 22
0
def run(project, bugid, patch_no, tests, tmp_tracefile='tmp_c'):

    tmp_tracefile += project + bugid + patch_no + 'run_print_trace'
    tmp_tracefile = os.path.join(os.getcwd(), tmp_tracefile)
    w_buggy = project + str(bugid) + 'b'
    w_patched = w_buggy + '_' + patch_no

    patchfile = os.path.join('../patches', patch_no)
    patch = PatchSet.from_filename(patchfile)

    source_file = patch[0].source_file
    target_file = patch[0].target_file

    os.system('cp ' + source_file + ' ' + source_file + '.bak')
    os.system('cp ' + target_file + ' ' + target_file + '.bak')

    os.system('make instru_class ARGS="-S ' + source_file + ' -T ' +
              tmp_tracefile + '"')
    os.system('make instru_class ARGS="-S ' + target_file + ' -T ' +
              tmp_tracefile + ' ' + ' -P ' + patchfile + ' -F ' + target_file +
              '"')
    #
    dir_path = '../traces/' + w_patched
    if (os.path.exists(tmp_tracefile)):
        os.system('rm ' + tmp_tracefile)
    os.system('mkdir ' + dir_path)
    os.system('mkdir ' + os.path.join(dir_path, 'buggy'))
    os.system('mkdir ' + os.path.join(dir_path, 'patched'))
    if project == 'Time':
        os.system('defects4j compile -w ' + w_buggy)
        os.system('defects4j compile -w ' + w_patched)
    # clone
    for test in tests:
        test = test.strip()
        testfile = os.path.join(
            w_buggy, get_path_to_test(w_buggy),
            test.split('::')[0].replace('.', '/') + '.java')
        if project == 'Time':
            os.system('rm ' + tmp_tracefile)
            status = os.system('timeout 90 defects4j test -t ' + test +
                               ' -w  ' + w_buggy)
        else:
            os.system('cp ' + testfile + ' ' + testfile + '.bak')
            os.system('make GetSingleTest_Chart ARGS="' + testfile + ' ' +
                      test.split('::')[1] + '"')
            status = os.system('timeout 90 defects4j test -t ' + test +
                               ' -w ' + w_buggy)
            os.system('mv ' + testfile + '.bak ' + testfile)
        print(testfile)
        if status == 0:
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'buggy', '__'.join(test.split('::'))))

        testfile = os.path.join(
            w_patched, get_path_to_test(w_patched),
            test.split('::')[0].replace('.', '/') + '.java')
        if project == 'Time':
            os.system('rm ' + tmp_tracefile)
            status = os.system('timeout 90 defects4j test -t ' + test +
                               ' -w  ' + w_patched)
        else:
            os.system('cp ' + testfile + ' ' + testfile + '.bak')
            os.system('make GetSingleTest_Chart ARGS="' + testfile + ' ' +
                      test.split('::')[1] + '"')
            status = os.system('timeout 90 defects4j test -t ' + test +
                               ' -w ' + w_patched)
            os.system('mv ' + testfile + '.bak ' + testfile)
        if status == 0:
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'patched', '__'.join(test.split('::'))))
    # clone
    os.system('mv ' + source_file + '.bak ' + source_file)
    os.system('mv ' + target_file + '.bak ' + target_file)
Esempio n. 23
0
    def import_patch_file(self):
        """Import one single patch into the database.

        Importing means:
        1) Creating a new node for the patch (if it doesn't exist yet).
        2) Dry applying this patch on the current code base (to get updated line offsets).
        3) Parsing the patchfile and determining all effects.
        4) Retrieving all nodes of affected source code from the database.
        5) Showing some basic statistics about how well the patch could be integrated.

        Args:
          patch_filepath: str The patch's filepath.
        """
        # Initialize the database connection here since this method is started as one single thread.
        self._init_db()

        self._print_indented("[~] Importing: " + self.patch_filepath + " - ",
                             0, 1)

        # Create a node for this patch if it doesn't already exist.
        patch_description = self._get_patch_file_descripiton(
            self.patch_filepath)
        if "$/" in patch_description or "/$" in patch_description:
            raise Exception(
                "Invalid content detected in patch description. Please adjust the code to support this."
            )
        patch_node_id = self._query("createPatchNode('{}', $/{}/$)".format(
            self.patch_filepath, patch_description))[0]

        if not patch_node_id:
            raise Exception("[!] Can't create a new node.")
        if patch_node_id == "-1":
            raise Exception(
                "[!] A node for this patch existed more than once. Removed all instances. Please retry."
            )
        self._print_indented("created/resolved (" + str(patch_node_id) + ")")

        # Remove any previous operation and file nodes of this patch-node.
        self._query("cleanupPatchEffects('" + str(patch_node_id) + "')")

        # Parse the patch and read general information.
        patches = PatchSet.from_filename(self.patch_filepath, encoding='utf-8')
        number_of_patches = len(patches)
        self._print_indented(
            "[~] Patchfile consists of {} patch/es.".format(number_of_patches))

        is_reversed_patch = False
        import_vulnerable_code = False

        # Check if a corresponding directory exists in the same directory.
        patch_directory = os.path.dirname(self.patch_filepath)
        patch_name = os.path.basename(self.patch_filepath)
        patch_raw_name = os.path.splitext(patch_name)[0]
        patch_vulnerable_code = patch_directory + "/" + patch_raw_name
        if os.path.isdir(patch_vulnerable_code):
            # Since vulnerable code already exists we can run a dry patch application on this code.
            self._copy_affected_files(patches, patch_vulnerable_code)
            fuzzed_line_offsets = self._apply_patch(self.patch_filepath, False,
                                                    True)
            self._print_indented(
                "[!] Patch was not applied as Vulnerable code is already provided for this patch."
            )
            import_vulnerable_code = True
        else:
            # Iterate over all affected files and copy them into a temporary location.
            # Copy all affected files into the temporary directory.
            self._copy_affected_files(patches)

            # Apply this patch to the temporary location.
            # Fetch adjusted/fuzzed lines in case the patch doesn't match perfectly.
            fuzzed_line_offsets = self._apply_patch(self.patch_filepath)
            if fuzzed_line_offsets == -1:
                # This seems to be a patch that should be applied in reverse.
                self._copy_affected_files(patches)
                is_reversed_patch = True
                fuzzed_line_offsets = self._apply_patch(
                    self.patch_filepath, True)
                self._print_indented(
                    "[!] This patch was already applied to the project. Treating as reversed patch."
                )

        #print(temp_dir)
        #time.sleep(30)

        # Store some meta information about this patch.
        self._query("g.v('{}').reversed = \"{}\"".format(
            patch_node_id, is_reversed_patch))
        self._query("g.v('{}').originalFilesAffected = {}".format(
            patch_node_id, number_of_patches))

        # TODO: merge this loop and the one below...
        patch_original_lines_added = 0
        patch_original_lines_removed = 0
        patch_original_hunks = 0
        for patch in patches:
            patch_original_lines_added += patch.added
            patch_original_lines_removed += patch.removed
            patch_original_hunks += len(patch)
        # TODO: remove static string "linesAdded" here...
        self._query("g.v('{}').originalLinesAdded = {}".format(
            patch_node_id, patch_original_lines_added))
        self._query("g.v('{}').originalLinesRemoved = {}".format(
            patch_node_id, patch_original_lines_removed))
        self._query("g.v('{}').originalHunks = {}".format(
            patch_node_id, patch_original_hunks))

        amount_hunks_successful = 0
        amount_patchfile_connected_nodes = 0
        amount_patchfile_added_nodes = 0
        patch_i = -1
        # Each patch refers to effects on one single file.
        for patch in patches:
            patch_i += 1

            filepath = self.code_base_location + "/" + patch.path
            number_of_hunks = len(patch)
            self._print_indented(
                "[->] " + filepath + " (" + str(number_of_hunks) + " hunk/s)",
                1, 1)

            file_node_id = -1
            if import_vulnerable_code:
                extension = os.path.splitext(filepath)[1]
                # TODO: very ugly workaround. We need a way to check if the current file really contains code.
                if extension in [".c", ".cpp", ".h"]:
                    # We don't need to search for the file inside the database.
                    self._print_indented(" - importing from local file")
                else:
                    self._print_indented(
                        " - invalid extension: {}, skipping".format(extension))
                    continue
            else:
                # TODO: I have no idea why we need to do this. OrientDB is giving us a hard time with indices :(.
                filepath = filepath[-100:]
                results = self._query(
                    "queryFileByPath('{}', true).toList().id".format(filepath))
                # TODO: remove this == [''] once the OrientDB Gremlin "List instead element" return mystery is resolved.

                # Check if this file exists in the code base (only if not provided by extracted repository content).
                if len(results) == 0 or results == ['']:
                    self._print_indented(" skipped (not found)")
                    continue
                elif len(results) > 1:
                    raise Exception("The file: " + filepath +
                                    " exists more than once in the database.")
                file_node_id = results[0]
                self._print_indented(" - resolved ({})".format(file_node_id))

            # Create a node for the affected file and connect the patch-node with it.
            patch_file_node_id = self._query("g.addVertex().id")[0]
            # Connect the patch with this newly created patch file node.
            self._query(
                "g.addEdge(g.v('{}'), g.v('{}'), 'affects'); g.commit();".
                format(patch_node_id, patch_file_node_id))

            # Process all hunks contained in the current patch.
            patch_hunks = self._process_patch_hunks(patch, fuzzed_line_offsets,
                                                    patch_i, is_reversed_patch)
            hunks_successful = len(patch_hunks)

            self._print_indented("[!] Effects:", 2)
            self._print_indented(str(patch_hunks), 3)

            if import_vulnerable_code:
                vulnerable_file_node_id = self._import_patched_file(
                    self.patch_filepath, patch_file_node_id, patch)
                # TODO: Add support for any effects regarding content being added i.e. handling with patched files.
                patched_file_node_id = None
                file_node_id = vulnerable_file_node_id
            elif is_reversed_patch:
                vulnerable_file_node_id = self._import_patched_file(
                    self.patch_filepath, patch_file_node_id, patch)
                patched_file_node_id = file_node_id
            else:
                vulnerable_file_node_id = file_node_id
                patched_file_node_id = None
                self._print_indented(
                    "[~] Skipping patched (vulnerable) code import for non-reversed patch.",
                    2)

            # Connect the patch file node with the corresponding affected file node.
            self._query(
                "g.addEdge(g.v('{}'), g.v('{}'), 'isFile'); g.commit();".
                format(patch_file_node_id, file_node_id))

            # Connect the node with all code parts that it affects in the database.
            (amount_connected_nodes, amount_supposed_added_nodes) = \
                self._connect_patch(patch_file_node_id, patch_hunks, vulnerable_file_node_id, patched_file_node_id)

            amount_patchfile_connected_nodes += amount_connected_nodes
            amount_patchfile_added_nodes += amount_supposed_added_nodes
            total_effects = amount_connected_nodes + amount_supposed_added_nodes
            amount_hunks_successful += hunks_successful

            if total_effects > 0:
                self._print_indented(
                    "[+] Connected patch node with {} CPG node/s (with {} applied hunks)."
                    .format(amount_connected_nodes, hunks_successful), 2)
            else:
                self._print_indented(
                    "[-] Patch can't be connected to any CPG nodes of the current code base.",
                    2)
                # Remove patch file node again.
                self._query("g.v('{}').remove(); g.commit();".format(
                    patch_file_node_id))

        number_of_total_effects = amount_patchfile_connected_nodes + amount_patchfile_added_nodes
        if number_of_total_effects > 0:
            self._print_indented(
                "[+] Patchnode was connected to {} CPG node/s (supposed total {} nodes)."
                .format(amount_patchfile_connected_nodes,
                        number_of_total_effects))

            # Save some statistics about this patch.
            self._query(
                "pn = g.v('{}'); pn.actualFilesAffected = pn.out.toList().size"
                .format(patch_node_id))
            self._query("g.v('{}').actualLinesAdded = {}".format(
                patch_node_id, amount_patchfile_added_nodes))
            self._query("g.v('{}').actualLinesRemoved = {}".format(
                patch_node_id, amount_patchfile_connected_nodes))
            self._query("g.v('{}').actualHunks = {}".format(
                patch_node_id, amount_hunks_successful))

            # Compute the average patch hunk complexity by dividing all effects by the number of hunks.
            original_total_effects = patch_original_lines_added + patch_original_lines_removed
            average_patch_hunk_complexity = round(
                original_total_effects / patch_original_hunks, 3)
            self._print_indented(
                "[!] Average original patch hunk complexity is: {} (#total_effects: {} / #hunks_contained: {})"
                .format(average_patch_hunk_complexity, original_total_effects,
                        patch_original_hunks))
            # TODO: we might want to use the actual average patch hunk complexity instead.
            #average_patch_hunk_complexity = round(number_of_total_effects / amount_hunks_successful, 3)
            #self._print_indented(
            #    "[!] Average patch hunk complexity is: {} (#total_effects: {} / #hunks_contained: {})".format(
            #        average_patch_hunk_complexity, number_of_total_effects, amount_hunks_successful))
            self._query("g.v('{}').avgHunkComplexity = {}".format(
                patch_node_id, average_patch_hunk_complexity))
        else:
            self._print_indented(
                "[-] Patchfile can't be connected to the current database (no effects)."
            )
            self._query(
                "g.v('{}').actualFilesAffected = 0".format(patch_node_id))
            self._query("g.v('{}').actualLinesAdded = 0".format(patch_node_id))
            self._query(
                "g.v('{}').actualLinesRemoved = 0".format(patch_node_id))
            self._query("g.v('{}').actualHunks = 0".format(patch_node_id))
            # Remove patch node again.
            # TODO: later we should remove this node again...
            #self._query("g.v('{}').remove(); g.commit();".format(patch_node_id))
        self._print_indented(
            "------------------------------------------------------------")
        self.flush_message_queue()
Esempio n. 24
0
import os
from unidiff import PatchSet
import json
projects=['Chart','Time','Lang','Closure','Math','Mockito']
l=os.listdir('.')
for patch_file in l:
    info={}
    try:
        patch = PatchSet.from_filename(patch_file)
        target_file=patch[0].source_file
    
    except:
        continue
    s=target_file.split('/')[0]
    info['ID']=patch_file
    s=s.split('_')[0]
    info['tool']='SimGen'
    info['correctness']='Incorrect'
    for p in projects:
        if p in s:
            info['project']=p
            info['bug_id']=s[len(p):-1]
    
    #print(info)
    f=open('INFO/%s.json'%patch_file,'w')
    json.dump(info,f)
    f.close()
Esempio n. 25
0
def tokenize_pre_and_post(tmpfile, path_to_diff):
    try:
        patch = PatchSet.from_filename(tmpfile, encoding='utf-8')

        pre_version_function = [line[1:] for line in patch[0][0].source]
        post_version_function = [line[1:] for line in patch[0][0].target]

        pre_version_function_str = ''.join(pre_version_function)
        post_version_function_str = ''.join(post_version_function)

        if ''.join(pre_version_function_str) == ''.join(
                post_version_function_str):
            return

        # Comments have been removed so add comment tokens for line delimiters
        pre_version_function_str = pre_version_function_str.replace(
            '\n', ' //<S2SV>\n ').replace('\\ //<S2SV>\n', '\\\n')
        post_version_function_str = post_version_function_str.replace(
            '\n', ' //<S2SV>\n').replace('\\ //<S2SV>\n', '\\\n')

        index = clang.cindex.Index.create()
        tu_pre = index.parse('tmp.c',
                             unsaved_files=[('tmp.c', pre_version_function_str)
                                            ])
        tu_post = index.parse('tmp.c',
                              unsaved_files=[('tmp.c',
                                              post_version_function_str)])

        pre_version_function_path = path_to_diff.parent / 'pre_version' / (
            path_to_diff.stem + '.tokens')
        post_version_function_path = path_to_diff.parent / 'post_version' / (
            path_to_diff.stem + '.tokens')

        pre_version_function_path = Path(
            str(pre_version_function_path).replace('BugFixFunction',
                                                   'BugFixToken'))
        post_version_function_path = Path(
            str(post_version_function_path).replace('BugFixFunction',
                                                    'BugFixToken'))

        pre_version_function_path.parent.mkdir(parents=True, exist_ok=True)
        post_version_function_path.parent.mkdir(parents=True, exist_ok=True)

        pre_tokens = ""
        post_tokens = ""
        for token in tu_pre.cursor.get_tokens():
            pre_tokens += repr(token.spelling.replace(
                ' ', '<S2SV_blank>'))[1:-1] + ' '
        for token in tu_post.cursor.get_tokens():
            post_tokens += repr(token.spelling.replace(
                ' ', '<S2SV_blank>'))[1:-1] + ' '
        if pre_tokens == post_tokens:
            return

        with codecs.open(pre_version_function_path, 'w', 'utf-8') as f:
            f.write(pre_tokens)

        with codecs.open(post_version_function_path, 'w', 'utf-8') as f:
            f.write(post_tokens)
    except Exception as e:
        print("Tokenize error: " + str(e))
        print(traceback.format_exc())
        return
def run(project,bugid,patch_no,tests,tmp_tracefile='tmp_c'):

    tmp_tracefile+=project+bugid+patch_no+'run_print_trace'
    tmp_tracefile=os.path.join(os.getcwd(),tmp_tracefile)
    w_buggy=project+str(bugid)+'b'
    w_patched=w_buggy+'_'+patch_no

    patchfile=os.path.join('../patches',patch_no)
    patch = PatchSet.from_filename(patchfile)
    
    source_file=patch[0].source_file
    target_file=patch[0].target_file
    
    os.system('cp '+source_file+' '+source_file+'.bak')
    os.system('cp '+target_file+' '+target_file+'.bak')

    os.system('make instru_class ARGS="-S '+source_file+' -T '+tmp_tracefile+'"')
    os.system('make instru_class ARGS="-S '+target_file+
              ' -T '+tmp_tracefile+' '+
              ' -P '+patchfile+
              ' -F '+target_file+'"')
    #
    dir_path='../traces/'+w_patched
    if(os.path.exists(tmp_tracefile)):
        os.system('rm '+tmp_tracefile)
    os.system('mkdir '+dir_path)
    os.system('mkdir '+os.path.join(dir_path,'buggy'))
    os.system('mkdir '+os.path.join(dir_path,'patched'))
    if project=='Time':
        os.system('defects4j compile -w '+w_buggy)
        os.system('defects4j compile -w '+w_patched)    
    # clone
    for test in tests:
        test=test.strip()
        testfile=os.path.join(w_buggy,get_path_to_test(w_buggy),test.split('::')[0].replace('.','/')+'.java')
        if project=='Time':
            os.system('rm '+tmp_tracefile)
            status=os.system('timeout 90 defects4j test -t '+test+' -w  '+w_buggy)
        else:
            os.system('cp '+testfile+' '+testfile+'.bak')
            os.system('make GetSingleTest_Chart ARGS="'+testfile+' '+test.split('::')[1]+'"')
            status=os.system('timeout 90 defects4j test -t '+test+' -w '+w_buggy)
            os.system('mv '+testfile+'.bak '+testfile)
        print(testfile)
        if status==0:
            os.system('mv '+tmp_tracefile+' '+os.path.join(dir_path,'buggy','__'.join(test.split('::'))))
    
        testfile=os.path.join(w_patched,get_path_to_test(w_patched),test.split('::')[0].replace('.','/')+'.java')
        if project=='Time':
            os.system('rm '+tmp_tracefile)
            status=os.system('timeout 90 defects4j test -t '+test+' -w  '+w_patched)
        else:
            os.system('cp '+testfile+' '+testfile+'.bak')
            os.system('make GetSingleTest_Chart ARGS="'+testfile+' '+test.split('::')[1]+'"')
            status=os.system('timeout 90 defects4j test -t '+test+' -w '+w_patched)
            os.system('mv '+testfile+'.bak '+testfile)        
        if status==0:
            os.system('mv '+tmp_tracefile+' '+os.path.join(dir_path,'patched','__'.join(test.split('::'))))
    # clone
    os.system('mv '+source_file+'.bak '+source_file)
    os.system('mv '+target_file+'.bak '+target_file)
from __future__ import print_function, unicode_literals

from unidiff import PatchSet

repos = open(
    "C:/Users/happyuser/Desktop/GitHub_Scraper/1000000/scraper/1000000_Packages_SearchHits_Ruby.txt",
    "r")
for name in repos:
    name = name.replace('/', '-')
    name = name.replace('\n', '')

    try:
        patches = PatchSet.from_filename('K:/cloned_searchFiles_rb/' + name +
                                         '_patches.diff',
                                         encoding='utf-8')

        with open(
                'C:/Users/happyuser/Desktop/GitHub_Scraper/1000000/codeCSV_rb/'
                + name + '.csv',
                'w',
                newline='') as file:
            file.write(
                "projectName, commitID, author, date, fileName, modifications, codechangetype"
            )
            file.write("\n")
        print(name)

        for patch in patches:
            stringPath = str(patch.path)
            arr = str(patch).splitlines()
            codeModified = []
Esempio n. 28
0
def run(project, bugid, patch_no, randoop_tests, tmp_tracefile='tmp_d'):
    tmp_tracefile += project + bugid + patch_no + 'run_trace_randoop.py'
    tmp_tracefile = os.path.join(os.getcwd(), tmp_tracefile)
    w_buggy = project + str(bugid) + 'b'
    w_patched = w_buggy + '_' + patch_no
    #
    patchfile = os.path.join('../patches', patch_no)
    patch = PatchSet.from_filename(patchfile)
    source_file = patch[0].source_file
    target_file = patch[0].target_file

    os.system('cp ' + source_file + ' ' + source_file + '.bak')
    os.system('cp ' + target_file + ' ' + target_file + '.bak')

    os.system('make instru_class ARGS="-S ' + source_file + ' -T ' +
              tmp_tracefile + '"')
    os.system('make instru_class ARGS="-S ' + target_file + ' -T ' +
              tmp_tracefile + ' ' + ' -P ' + patchfile + ' -F ' + target_file +
              '"')
    #
    dir_path = '../traces/' + w_patched
    if (os.path.exists(tmp_tracefile)):
        os.system('rm ' + tmp_tracefile)
    os.system('mkdir ' + dir_path)
    os.system('mkdir ' + os.path.join(dir_path, 'buggy'))
    os.system('mkdir ' + os.path.join(dir_path, 'patched'))

    test = 'randoop'
    #
    testfile = '../test_gen_randoop/' + project + '/randoop/' + bugid + '/' + project + '-' + bugid + 'b-randoop.' + bugid + '.tar.bz2'

    comp_flag = True
    for Test_Case in randoop_tests:
        test = 'Randoop.' + Test_Case.strip()
        if comp_flag:
            status = os.system('timeout 90 defects4j test -s ' + testfile +
                               ' -t ' + Test_Case.strip() + ' -w ' + w_buggy)
        else:
            status = os.system('timeout 90 defects4j test -s ' + testfile +
                               ' -t ' + Test_Case.strip() + ' -n -w ' +
                               w_buggy)
        if status == 0:
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'buggy', '__'.join(test.split('::'))))

        if comp_flag:
            status = os.system('timeout 90 defects4j test -s ' + testfile +
                               ' -t ' + Test_Case.strip() + ' -w ' + w_patched)
        else:
            status = os.system('timeout 90 defects4j test -s ' + testfile +
                               ' -t ' + Test_Case.strip() + ' -n -w ' +
                               w_patched)
        if status == 0:
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'patched', '__'.join(test.split('::'))))
        comp_flag = False

    os.system('mv ' + source_file + '.bak ' + source_file)
    os.system('mv ' + target_file + '.bak ' + target_file)
Esempio n. 29
0
def main():
    parser = argparse.ArgumentParser(
        description='Утилита для проверки ошибочно изменных файлов в индексе')
    parser.add_argument('--version',
                        action='version',
                        version='%(prog)s {}'.format(__version__))
    parser.add_argument('-v',
                        '--verbose',
                        dest='verbose_count',
                        action='count',
                        default=0,
                        help='Increases log verbosity for each occurence.')
    parser.add_argument(
        '--g',
        action='store_true',
        default=False,
        help=
        'Запустить чтение индекса из git и определить список файлов для разбора'
    )

    args = parser.parse_args()

    log.setLevel(max(3 - args.verbose_count, 0) * 10)

    taglistchange = ('<d3p1:id>', '<d3p1:fullIntervalBegin>',
                     '<d3p1:fullIntervalEnd>', '<d3p1:visualBegin>',
                     '<xr:TypeId>', '<xr:ValueId>', '<d4p1:id>')
    typefiles = ('template.xml', 'form.xml')

    if args.g is True:
        files = get_list_of_comitted_files()
        for file in files:
            filename = os.path.basename(file)
            if not (filename.lower() in typefiles or filename[-3:] == "xml"):
                log.debug("пропускаем файл {} расширение {}".format(
                    file, filename[-3:]))
                continue

            data = get_diff_forfile(file)
            if data is None:
                log.error("diff file not exists {}".format(file))
                continue
            pathc = PatchSet.from_filename(data, encoding='utf-8')
            for f in pathc.modified_files:
                log.debug('file is {}'.format(f))
                modifiedsource, modifiedtarget = [], []
                for hunk in f:
                    modifiedsource = modifiedsource + list(
                        filter(lambda x: not x[:1] == " ", hunk.source))
                    modifiedtarget = modifiedtarget + list(
                        filter(lambda x: not x[:1] == " ", hunk.target))

                sourcetags = list(
                    filter(lambda x: x[1:].strip().startswith(taglistchange),
                           modifiedsource))
                targettags = list(
                    filter(lambda x: x[1:].strip().startswith(taglistchange),
                           modifiedtarget))
                log.debug("sourcetags:{} targettags:{}".format(
                    sourcetags, targettags))

                if not (len(sourcetags) == len(modifiedsource) and \
                    len(targettags) == len(modifiedtarget) and \
                    len(sourcetags) == len(targettags)):
                    continue

                #Теперь надо будет отменить изменения в индексе для файла.
                log.info("удалем из индекса файл {}".format(file))
                git_reset_file(file, 'HEAD')
                break
        replace_old_form_attr(files)
Esempio n. 30
0
def run(project,
        bugid,
        patch_no,
        tests,
        randoop_tests=[],
        tmp_tracefile='tmp_c'):
    tmp_tracefile += project + bugid + patch_no + 'run_print_trace'
    tmp_tracefile = os.path.join(os.getcwd(), tmp_tracefile)
    w_buggy = project + str(bugid) + 'b'
    w_patched = w_buggy + '_' + patch_no

    patchfile = os.path.join('../patches', patch_no)
    patch = PatchSet.from_filename(patchfile)

    source_file = patch[0].source_file
    target_file = patch[0].target_file
    line_no_list = []
    for hunki in range(len(patch[0])):
        for i in range(len(patch[0][hunki])):
            if not patch[0][hunki][i].is_context:
                line_no_list.append(
                    str(patch[0][hunki][i - 1].source_line_no + 1))
                break

    dir_path = '../traces/' + w_patched
    if (os.path.exists(tmp_tracefile)):
        os.system('rm ' + tmp_tracefile)
    os.system('mkdir ' + dir_path)
    os.system('mkdir ' + os.path.join(dir_path, 'buggy'))
    os.system('mkdir ' + os.path.join(dir_path, 'patched'))
    os.system('mkdir ' + os.path.join(dir_path, 'buggy_e'))
    os.system('mkdir ' + os.path.join(dir_path, 'patched_e'))

    patch_info_file = "fdsa.txt"
    os.system("rm -rf " + patch_info_file)
    os.system('make PatchInfo ARGS="' +
              os.path.join('../source/', source_file) + ' ' + patch_info_file +
              ' ' + ','.join(line_no_list) + '" >/dev/null')
    f = open(patch_info_file)
    lines = f.readlines()
    patched_class = lines[-1].strip()
    patched_method, method_signature, start_line, end_line = lines[0].strip(
    ).split('\t')
    f.close()
    start_line = int(start_line)
    end_line = int(end_line)

    os.system('defects4j compile -w ' + w_buggy)
    os.system('defects4j compile -w ' + w_patched)

    f = open("%s/AllLines_pattern.java" % (btrace_home))
    s = f.read()
    f.close()
    s = s.replace('__CLASS__NAME__', patched_class)
    f = open("%s/AllLines.java" % (btrace_home), 'w')
    f.write(s)
    f.close()
    os.system("cd %s && ./btracec AllLines.java" % (btrace_home))

    jvmargs = " -a -Djvmargs=\-javaagent:%s/btrace\-agent.jar=noserver,debug=true,scriptOutputFile=%s,script=%s/AllLines.class" % (
        btrace_home, tmp_tracefile, btrace_home)

    for test in tests:
        test = test.strip()

        os.system('timeout 90 defects4j test -n -t ' + test + ' -w ' +
                  w_buggy + jvmargs)
        if os.path.exists(tmp_tracefile):
            extract_trace(
                tmp_tracefile,
                os.path.join(dir_path, 'buggy_e', '__'.join(test.split('::'))),
                start_line, end_line)
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'buggy', '__'.join(test.split('::'))))

        os.system('timeout 90 defects4j test -n -t ' + test + ' -w  ' +
                  w_patched + jvmargs)
        if os.path.exists(tmp_tracefile):
            extract_trace(
                tmp_tracefile,
                os.path.join(dir_path, 'patched_e',
                             '__'.join(test.split('::'))), start_line,
                end_line)
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'patched', '__'.join(test.split('::'))))

    cmpl_flag = True
    testfile = '../test_gen_randoop/' + project + '/randoop/' + bugid + '/' + project + '-' + bugid + 'b-randoop.' + bugid + '.tar.bz2'
    for Test_Case in randoop_tests:
        test = 'Randoop.' + Test_Case.strip()
        if (cmpl_flag):
            os.system('timeout 90 defects4j test -s ' + testfile + ' -t ' +
                      Test_Case.strip() + ' -w ' + w_buggy + jvmargs)
        else:
            os.system('timeout 90 defects4j test -n -s ' + testfile + ' -t ' +
                      Test_Case.strip() + ' -w ' + w_buggy + jvmargs)
        if os.path.exists(tmp_tracefile):
            extract_trace(
                tmp_tracefile,
                os.path.join(dir_path, 'buggy_e', '__'.join(test.split('::'))),
                start_line, end_line)
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'buggy', '__'.join(test.split('::'))))
        if (cmpl_flag):
            os.system('timeout 90 defects4j test -s ' + testfile + ' -t ' +
                      Test_Case.strip() + ' -w ' + w_patched + jvmargs)
        else:
            os.system('timeout 90 defects4j test -n -s ' + testfile + ' -t ' +
                      Test_Case.strip() + ' -w ' + w_patched + jvmargs)
        if os.path.exists(tmp_tracefile):
            extract_trace(
                tmp_tracefile,
                os.path.join(dir_path, 'patched_e',
                             '__'.join(test.split('::'))), start_line,
                end_line)
            os.system(
                'mv ' + tmp_tracefile + ' ' +
                os.path.join(dir_path, 'patched', '__'.join(test.split('::'))))
        cmpl_flag = False
 def __init__(self, filename, language):
     self.patch = PatchSet.from_filename(filename)
     self.language = language
     self.clean_patch()