def diff_compare(in_lines1, in_lines2): l1 = in_lines1.split("\n") l2 = in_lines2.split("\n") d = Differ() result = list(d.compare(l1, l2)) result = "\n".join(result) return result
def str_diffs(self): if self.actual == self.expected: return '' differ = Differ() diffs = differ.compare(self.actual.splitlines(), self.expected.splitlines()) return 'Diffs:\n{0}'.format('\n'.join(diffs))
def test_dx2cube(tmp_path): """Test conversion of OpenDX files to Cube files.""" pqr_path = DATA_DIR / "dx2cube.pqr" dx_path = DATA_DIR / "dx2cube.dx" cube_gen = tmp_path / "test.cube" cube_test = DATA_DIR / "dx2cube.cube" _LOGGER.info(f"Reading PQR from {pqr_path}...") with open(pqr_path, "rt") as pqr_file: atom_list = read_pqr(pqr_file) _LOGGER.info(f"Reading DX from {dx_path}...") with open(dx_path, "rt") as dx_file: dx_dict = read_dx(dx_file) _LOGGER.info(f"Writing Cube to {cube_gen}...") with open(cube_gen, "wt") as cube_file: write_cube(cube_file, dx_dict, atom_list) _LOGGER.info(f"Reading this cube from {cube_gen}...") this_lines = [line.strip() for line in open(cube_gen, "rt")] _LOGGER.info(f"Reading test cube from {cube_test}...") test_lines = [line.strip() for line in open(cube_test, "rt")] differ = Differ() differences = [ line for line in differ.compare(this_lines, test_lines) if line[0] != " " ] if differences: for diff in differences: _LOGGER.error(f"Found difference: {diff}") raise ValueError() _LOGGER.info("No differences found in output")
def before_after_diff_list(self, before, after, ignore_regex=''): d = Differ() str1_list = [] str2_list = [] with open(before, 'r') as f: str1 = f.readlines() for i in str1: #if i.find(ignore_str) != -1: # replaced_str1 = i.replace(ignore_str, '') # str1_list.append(replaced_str1) if re.findall(ignore_regex, i): replace_regex = re.sub(ignore_regex, '', i) str1_list.append(replace_regex) else: str1_list.append(i) with open(after, 'r') as f: str2 = f.readlines() for i in str2: #if i.find(ignore_str) != -1: # replaced_str2 = i.replace(ignore_str, '') # str2_list.append(replaced_str2) if re.findall(ignore_regex, i): replace_regex = re.sub(ignore_regex, '', i) str2_list.append(replace_regex) else: str2_list.append(i) #result_list = list(d.compare(str1, str2)) result_list = list(d.compare(str1_list, str2_list)) return result_list
def getFirstLettersTheSame(wd1, wd2): #возращает одинаковые первые буквы у двух идущих друг за другом слов d = Differ() diff = ''.join(d.compare(wd1, wd2)) diff = patternSpace.sub('',diff) return patternLetters.match(diff)
def _(success, output): d = Differ() assert world.out.value() == output, '\n' + '\n'.join(d.compare(world.out.value().split('\n'), output.split('\n'))) if success == 'fail': assert world.status.value() != 0 else: assert world.status.value() == 0
def print_diff(suppressed_kinds, kind, context, before, after, to): d = Differ() diffs = "".join( d.compare( before.decode("utf8").splitlines(True), after.decode("utf8").splitlines(True))) diffs = diffs.splitlines(True) logger.debug("".join(diffs)) if kind in suppressed_kinds: string = "+ Changes suppressed on sensitive content of type %s\n" % kind to.write(string) return if context >= 0: distances = calculate_distances(diffs) omitting = False for i, diff in enumerate(diffs): if distances[i] > context: if not omitting: to.write("...") omitting = True else: omitting = False print_diff_record(diff, to) return for diff in diffs: print_diff_record(diff, to)
def get_changelist(old_text, new_text): # copy and make sure we don't register changes simply # because the endline characters changed (which happens # when syncing windows and linux files, and when an otw # unchanged line at the end of the file now has an endline # character) # splitlines() eliminate the last line if it's empty, and # removes the endline characters, so they won't be considered # for the diffs old_text = old_text.splitlines() old_text = [l for l in old_text] new_text = new_text.splitlines() new_text = [l for l in new_text] lines_with_changes = None differ = Differ() differ_output = differ.compare(old_text, new_text) lc = 0 lines_with_changes = [] for l in differ_output: if l[0] == '+': lines_with_changes.append(lc) lc += 1 elif l[0] == '-': pass elif l[0] == ' ': lc += 1 # there might also be the '?' case, but this doesn't affect the linecount return "\n".join([str(l) for l in lines_with_changes])
def get_programming_language_for_one_block(acom_code_blocks, block): code_lines = [line.strip() for line in block[0].split("\n")] transits = [] matchings = [] for acom_block in acom_code_blocks: acom_code_lines = [line.strip() for line in acom_block[1].split("\n")] differ = Differ() diff = list(differ.compare(code_lines, acom_code_lines)) same_count = 0 for diff_line in diff: if diff_line[0] == " ": same_count += 1 if same_count != len(code_lines): same_count += get_similarity(diff) line_num_max = max(len(code_lines), len(acom_code_lines)) line_num_min = min(len(code_lines), len(acom_code_lines)) if same_count / len(code_lines) > 0.7 and ( line_num_max - line_num_min) / line_num_max < 0.5: return acom_block[0] return ""
def compare_structure(self, structure, slug): """Compare provided data structure with layout defined in Grafana. Args: structure (object): structure of grafana dashboard for comparison slug (str): Slug of dashboard uri. Slug is the url friendly version of the dashboard title. """ layout = self.get_dashboard(slug) pytest.check( len(layout) > 0, "{} dashboard should not be empty".format(slug)) structure_grafana = {} for row in layout["dashboard"]["rows"]: structure_grafana[row["title"]] = [] for panel in row["panels"]: if panel["title"]: structure_grafana[row["title"]].append(panel["title"]) elif "displayName" in panel.keys() and panel["displayName"]: structure_grafana[row["title"]].append( panel["displayName"]) LOGGER.debug("defined layout structure = {}".format(structure)) LOGGER.debug( "layout structure in grafana = {}".format(structure_grafana)) d = Differ() LOGGER.debug("reduced diff between the layouts: {}".format("".join([ x.strip() for x in d.compare(json.dumps(structure, sort_keys=True), json.dumps(structure_grafana, sort_keys=True)) ]))) pytest.check( structure == structure_grafana, "defined structure of panels should " + "be equal to structure in grafana")
def display_output(self, output, *args): if args: output = ''.join(line.rstrip() + '\n' for line in output.splitlines()) expected_output = ''.join(line.rstrip() + '\n' for line in args[0].splitlines()) if (output == expected_output): print(colored.green(f"Sample testcase passed.")) print() print(colored.yellow("Output:")) print(output) else: print(colored.red(f"Sample testcase failed !")) print() print(colored.yellow("Output:")) print(output) print(colored.yellow("Changes needed:")) diff = Differ() diffed_output = self.color_diff( diff.compare(output.splitlines(), expected_output.splitlines())) print('\n'.join(diffed_output)) else: print(colored.yellow(f"Output: ")) print(output)
async def run(bot, sess): subs = sess.query(SiteSub).all() for sub in subs: # type: SiteSub async with client_session() as session: try: async with session.get(sub.url) as res: new_body: str = await res.text() if sub.body != new_body: old_body_lines = sub.body.splitlines(keepends=True) new_body_lines = new_body.splitlines(keepends=True) d = Differ() diff = [ SPACE_RE.sub(' ', x).rstrip() for x in d.compare(old_body_lines, new_body_lines) if x[0] not in [' ', '?'] ] await bot.say( sub.user, '`{}` 에서 변경이 발생했어요!\n```\n{}\n```'.format( sub.url, '\n'.join(diff), )) sub.body = new_body with sess.begin(): sess.add(sub) except aiohttp.client_exceptions.ClientConnectorError: await bot.say(sub.user, f'`{sub.url}` 에 접속할 수 없어요!')
def compare_config(self, other=None, text=False): """ Compares running config with another config. This other config can be either the *running* config or a :class:`~pyFG.forticonfig.FortiConfig`. The result of the comparison will be how to reach\ the state represented in the target config (either the *candidate* or *other*) from the *running*\ config. Args: * **other** (:class:`~pyFG.forticonfig.FortiConfig`) -- This parameter, if specified, will be used for the\ comparison. If it is not specified the candidate config will be used. * **text** (bool): * If ``True`` this method will return a text diff showing how to get from the running config to\ the target config. * If ``False`` this method will return all the exact commands that needs to be run on the running\ config to reach the target config. Returns: See the explanation of the *text* arg in the section Args. """ if other is None: other = self.candidate_config if not text: return self.running_config.compare_config(other) else: diff = Differ() result = diff.compare( self.running_config.to_text().splitlines(), other.to_text().splitlines() ) return '\n'.join(result)
def mergeDiff(global_, mooncake): if global_.strip() == "": return ["<!-- keep by customization: begin -->\n", mooncake+"\n", "<!-- keep by customization: end -->\n"] leadingBlank = global_.find(global_.strip()) if global_.strip() == mooncake.strip(): return [global_ + "\n"] globalText = handlePunctuation(global_.strip().split(" ")) mooncakeText = handlePunctuation(mooncake.strip().split(" ")) differ = Differ() diff = list(differ.compare(globalText, mooncakeText)) i = 0 result = [] while i < len(diff): if diff[i][0] == " ": result.append(diff[i][2:]) i += 1 elif diff[i][0] == "-": result.append("<!-- deleted by customization") while i < len(diff) and diff[i][0] == "-": result.append(diff[i][2:]) i += 1 result.append("-->") elif diff[i][0] == "+": result.append("<!-- keep by customization: begin -->") while i < len(diff) and diff[i][0] == "+": result.append(diff[i][2:]) i += 1 result.append("<!-- keep by customization: end -->") else: i += 1 text, count, percentage = mergeDeleteAndKeep2(result) if ((count < 0.1*float(len(globalText)) or count == 1) and percentage < 0.5) or global_.strip().find(mooncake.strip()) != -1 or mooncake.strip().find(global_.strip()) != -1: return [global_[:leadingBlank]+text.replace(" \b","").replace("\b","").replace("--> <!--", "--><!--")] return ["<!-- deleted by customization\n", global_+"\n", "-->\n", "<!-- keep by customization: begin -->\n", mooncake+"\n", "<!-- keep by customization: end -->\n"]
def testcode(self): skipped_methods = [] fname = "examples/android/TestsAndroguard/bin/classes.dex" parsed = parse_dex.read_dex(fname) with open(fname, "rb") as f: d = DalvikVMFormat(f.read()) dif = Differ() for m in d.get_methods(): if not m.get_code(): continue if m.get_method_idx() in skipped_methods: continue code = hexlify(m.get_code().get_raw()) self.assertEqual( parsed.methods[m.get_method_idx()], code, "incorrect code for " "[{}]: {} --> {}:\n" "{}\ntries_size: {}, insns_size: {}\nSHOULD BE {}\n{}\n{}". format( m.get_method_idx(), m.get_class_name(), m.get_name(), "".join( dif.compare(parsed.methods[m.get_method_idx()], code)), m.get_code().tries_size, m.get_code().insns_size, hexlify(m.get_code().get_raw()), parsed.methods[m.get_method_idx()], hexlify(m.get_code().code.get_raw())))
def main(file1: str, file2: str): dir_route = Path().resolve() required_modules = ['sql_detector', 'output'] for dir_name in required_modules: if dir_name not in str(dir_route): dir_route = dir_route / dir_name if not dir_route.exists(): raise FileNotFoundError( 'Not found module path. try change current directory.') module_dir = dir_route try: f1 = open(f"{module_dir.resolve()}/{file1}", 'r', encoding='utf-8') f2 = open(f"{module_dir.resolve()}/{file2}", 'r', encoding='utf-8') except FileNotFoundError as e: print(e) exit(1) with f1, f2: text1 = f1.read() text2 = f2.read() diff = Differ() result = list( diff.compare(text1.splitlines(keepends=True), text2.splitlines(keepends=True))) sys.stdout.writelines(result)
def __init__(self, obj, snapshottest): if isinstance(obj, dict): obj = SortedDict(**obj) self.obj = obj self.pretty = Formatter() self.differ = Differ() self.snapshottest = snapshottest
def difflib_comparison(source, source_asr, workdir, **kwargs): with open(source, 'r', encoding='utf-8') as source, \ open(source_asr, 'r', encoding='utf-8') as source_asr, \ open(workdir / 'diff_source_and_asr', 'w', encoding='utf-8') as result: clean = source.readlines() clean_asr = source_asr.readlines() # Define comparison function and apply it generating output data d = Differ() i = 0 for sent_nb in tqdm.tqdm(range(len(clean))): diff_output = d.compare(clean[sent_nb], clean_asr[sent_nb]) diff = "".join(diff_output).strip() + '\n' corr = re.sub(r' ', 'X', diff) corr1 = re.sub(r' ', 'S', corr) corr15 = re.sub(r' ', 'X', corr1) corr2 = re.sub(r' ', '', corr15) corr3 = re.sub(r'X', ' ', corr2) corr4 = re.sub(r'--', ' - -', corr3) corr5 = re.sub(r'\+\+', ' + +', corr4) result.write(corr5) i += 1
def sortimports_linenum_msg(self, sort_result): """Parses isort.SortImports for line number changes and message Uses a diff.Differ comparison of SortImport `in_lines`:`out_lines` to yield the line numbers of import lines that have been moved or blank lines added. Args: sort_imports (isort.SortImports): The isorts results object. Yields: tuple: A tuple of the specific isort line number and message. """ if sort_result.skipped: return self._fixup_sortimports_wrapped(sort_result) self._fixup_sortimports_eof(sort_result) differ = Differ() diff = differ.compare(sort_result.in_lines, sort_result.out_lines) line_num = 0 for line in diff: if line.startswith(' ', 0, 2): line_num += 1 # Ignore unchanged lines but increment line_num. elif line.startswith('- ', 0, 2): line_num += 1 if line.strip() == '-': yield line_num, self.isort_blank_unexp else: yield line_num, self.isort_unsorted elif line.strip() == '+': # Include newline additions but do not increment line_num. yield line_num + 1, self.isort_blank_req
def diff(request, page_info): try: id0 = request.GET['diff_1'] id1 = request.GET['diff_2'] submit0 = Web.get_result_details(submit=Submit(int(id0))) submit1 = Web.get_result_details(submit=Submit(int(id1))) submits = [submit0,submit1] except: return HttpResponseRedirect(reverse('results',args=[unicode(page_info.contest.id)])) bar = StatusBar() codes = [] ok = True for i in [0,1]: codes.append(submits[i].data) if codes[i]==None or codes[i]=="": ok = False bar.errors.append('Could not render submit '+unicode(submits[i].submit.id)+'!') diff_html = "" if ok: d = Differ() for line in d.compare(codes[1].splitlines(True),codes[0].splitlines(True)): if line[0]=='?': continue css = 'highlight_back' if line[0]=='+': css = 'highlight_pos' if line[0]=='-': css = 'highlight_neg' diff_html += '<span class="'+css+'">'+escape(line)+'</span>' return render_to_response('viewdiff.html',{'page_info' : page_info, 'submits' : submits, 'diff' : diff_html, 'status_bar' : bar})
def compare_config(self, other=None, text=False): """ Compares running config with another config. This other config can be either the *running* config or a :class:`~pyFG.forticonfig.FortiConfig`. The result of the comparison will be how to reach\ the state represented in the target config (either the *candidate* or *other*) from the *running*\ config. Args: * **other** (:class:`~pyFG.forticonfig.FortiConfig`) -- This parameter, if specified, will be used for the\ comparison. If it is not specified the candidate config will be used. * **text** (bool): * If ``True`` this method will return a text diff showing how to get from the running config to\ the target config. * If ``False`` this method will return all the exact commands that needs to be run on the running\ config to reach the target config. Returns: See the explanation of the *text* arg in the section Args. """ if other is None: other = self.candidate_config if not text: return self.running_config.compare_config(other) else: diff = Differ() result = diff.compare(self.running_config.to_text().splitlines(), other.to_text().splitlines()) return '\n'.join(result)
def sortimports_linenum_msg(self, sort_result): """Parses isort.SortImports for line number changes and message Uses a diff.Differ comparison of SortImport `in_lines`:`out_lines` to yield the line numbers of import lines that have been moved or blank lines added. Args: sort_imports (isort.SortImports): The isorts results object. Yields: tuple: A tuple of the specific isort line number and message. """ if sort_result.skipped: raise StopIteration self._fixup_sortimports_wrapped(sort_result) self._fixup_sortimports_eof(sort_result) differ = Differ() diff = differ.compare(sort_result.in_lines, sort_result.out_lines) line_num = 0 for line in diff: if line.startswith(' ', 0, 2): line_num += 1 # Ignore unchanged lines but increment line_num. elif line.startswith('- ', 0, 2): line_num += 1 if line.strip() == '-': yield line_num, self.isort_blank_unexp else: yield line_num, self.isort_unsorted elif line.strip() == '+': # Include newline additions but do not increment line_num. yield line_num + 1, self.isort_blank_req
def get_similarity(diff): try: diff_set = compare_result_split2(diff) except: return 0 result = 0 for a_diff in diff_set: if len(a_diff) >= 3 and a_diff[len(a_diff) - 1] == REPLACEMENT_MARKER_ONELINE: removed = [] added = [] for line in a_diff[:len(a_diff) - 1]: if line[0] == "-": removed.append(line[2:]) elif line[0] == "+": added.append(line[2:]) removed_words = " ".join(removed).split(" ") added_words = " ".join(added).split(" ") differ = Differ() if len(removed_words) < len(added_words): inline_diff = differ.compare(removed_words, added_words) else: inline_diff = differ.compare(added_words, removed_words) transit, matching = get_transit_and_matching(inline_diff) if transit <= 2 and matching > 0.7: result += matching return result
def main(file1, file2): d1 = read(file1) d2 = read(file2) differ = Differ() d = differ.compare(d1, d2) hd = HtmlDiff().make_file() sys.stdout.writelines(d)
def get_config_diff(module, running=None, candidate=None): conn = get_connection(module) if is_cliconf(module): try: response = conn.get("show commit changes diff") except ConnectionError as exc: module.fail_json(msg=to_text(exc, errors="surrogate_then_replace")) return response elif is_netconf(module): if running and candidate: # ignore rpc-reply root node and diff from data element onwards running_data_ele = etree.fromstring( to_bytes(running.strip()), ).getchildren()[0] candidate_data_ele = etree.fromstring( to_bytes(candidate.strip()), ).getchildren()[0] running_data = to_text(etree.tostring(running_data_ele)).strip() candidate_data = to_text( etree.tostring(candidate_data_ele), ).strip() if running_data != candidate_data: d = Differ() diff = list( d.compare( running_data.splitlines(), candidate_data.splitlines(), ), ) return "\n".join(diff).strip() return None
def compare_texts(original_text, corrected_text): d = Differ() comp = list(d.compare(original_text, corrected_text)) mistakes = [] correction = [] for c in comp: if c == ' ': mistakes.append(' ') correction.append(' ') elif c[0] is '+': correction.append('**{}**'.format(c[-1])) elif c[0] is '-': mistakes.append('~~{}~~'.format(c[-1])) else: mistakes.append(c[-1]) correction.append(c[-1]) mistakes = ''.join(mistakes) mistakes = mistakes.replace('~~~~', '') # Remove in-between strike-throughs correction = ''.join(correction) correction = correction.replace('****', '') # Remove in-between highlights return mistakes, correction
def getFirstLettersTheSame(wd1, wd2): #возращает одинаковые первые буквы у двух идущих друг за другом слов d = Differ() diff = ''.join(d.compare(wd1, wd2)) diff = patternSpace.sub('', diff) return patternLetters.match(diff)
def diff(self, oldobj, obj): """Create a diff of `obj` vs. `oldobj`; description is handled using difflib module.""" difflist = [] skip = "description_html".split() nl = '\n' for fld in obj._meta.fields: name = fld.name if name not in skip: oldval = getattr(oldobj, fld.name) val = getattr(obj, fld.name) if name == "description": olddesc = oldobj.description.splitlines(1) desc = obj.description.splitlines(1) if olddesc: olddesc[-1] = olddesc[-1].strip() + '\r\n' if desc: desc[-1] = desc[-1].strip() + '\r\n' d = Differ() result = list(d.compare(olddesc, desc)) # note: Differ returns full(?) content when there are no changes!!!? if olddesc != desc: difflist.extend( [nl + "Description diff:" + nl] + result + [nl] ) else: if oldval != val: difflist.append("%s: changed from '%s' to '%s'" % (fld.name, oldval, val) + nl) diff = ''.join(difflist) return diff
def editFile(IO): print("OPENING FILE:") file = IO[6::] print(file) try: f = open(file, "r") t = f.read() text = t.splitlines() f.close() except Exception: print("failed to read file") # print(e) subprocess.run(f"notepad {file}") try: nf = open(file, "r") nt = nf.read() ntext = nt.splitlines() dif = Differ() df = list(dif.compare(text, ntext)) for i in df: if i[0] == "+": print(f"{fg('green')}{i}{attr('reset')}") elif i[0] == "-": print(f"{fg('red_1')}{i}{attr('reset')}") else: pass print("Succesfully edited with exit code 0") except FileNotFoundError: print("Could not load file changes (file unexistent)")
def validate(self, expected_file_name=None): # type: (str) -> Response if expected_file_name is None: expected_file_name = os.path.join(dirname(inspect.getfile(self.__class__)), 'expected.data.yml') time.sleep(10) with open(expected_file_name) as expected_data_file: expected_data = os.linesep.join(expected_data_file.readlines()) response = requests.post(url='http://0.0.0.0:12800/dataValidate', data=expected_data) if response.status_code != 200: res = requests.get('http://0.0.0.0:12800/receiveData') actual_data = yaml.dump(yaml.load(res.content, Loader=Loader)) differ = Differ() diff_list = list(differ.compare( actual_data.splitlines(keepends=True), yaml.dump(yaml.load(expected_data, Loader=Loader)).splitlines(keepends=True) )) print('diff list: ') sys.stdout.writelines(diff_list) assert response.status_code == 200 return response
def isSame(self): # get fields schema schema1 = self._dataReader1.getSchema() schema2 = self._dataReader2.getSchema() # compare field by field # check if fieldname are the same, respect to the ignored lists fields1 = schema1['fields'] fields2 = schema2['fields'] ignoreList1 = self._ignoredFields1 ignoreList2 = self._ignoredFields2 for index in range(len(fields1) - 1, -1, -1): if fields1[index]['name'] in ignoreList1: fields1.pop(index) for index in range(len(fields2) - 1, -1, -1): if fields2[index]['name'] in ignoreList2: fields2.pop(index) fields1 = [str(i) for i in sorted(fields1, key=lambda x: x.items())] fields2 = [str(i) for i in sorted(fields2, key=lambda x: x.items())] # fields2 = sorted(list(fields1.items())) d = Differ() diff = d.compare(fields1, fields2) result = [line for line in diff if re.match('^[-+] ', line)] if len(result) > 0: if self._model: self._model.result = 'Fields are inconsistent!' self._model.result_detail = ( settings.RESULT_SPLITTING_TOKEN.join(result)) self._model.save() return False return True
def testcode(self): skipped_methods = [] fname = "examples/android/TestsAndroguard/bin/classes.dex" parsed = parse_dex.read_dex(fname) with open(fname, "rb") as f: d = DalvikVMFormat(f.read()) dif = Differ() for m in d.get_methods(): if not m.get_code(): continue if m.get_method_idx() in skipped_methods: continue code = hexlify(m.get_code().get_raw()) self.assertEqual(parsed[m.get_method_idx()], code, "incorrect code for " "[{}]: {} --> {}:\n" "{}\ntries_size: {}, insns_size: {}\nSHOULD BE {}\n{}\n{}".format(m.get_method_idx(), m.get_class_name(), m.get_name(), "".join(dif.compare(parsed[m.get_method_idx()], code)), m.get_code().tries_size, m.get_code().insns_size, hexlify(m.get_code().get_raw()), parsed[m.get_method_idx()], hexlify(m.get_code().code.get_raw())))
def diff_xml(self, old, new): """Find the differences between the old and new versions of a doc. Pass: old - the serialized XML for the old version of the document new - the serialized XML for the new version of the document Return: an HTML `pre` element containing the deltas between old and new, or None if there are no differences after normalization """ differ = Differ() before = old.replace("\r", "").splitlines() after = new.replace("\r", "").splitlines() diffs = differ.compare(before, after) lines = [] changes = False for line in diffs: line = line.rstrip("\n") color = self.COLORS.get(line[0], "white") if line and line[0] in self.COLORS: changes = True bgcolor = f"background-color: {color}" span = self.HTMLPage.B.SPAN(f"{line}\n", style=bgcolor) lines.append(span) elif self.verbose: lines.append(self.HTMLPage.B.SPAN(line)) if changes: pre = self.HTMLPage.B.PRE(*lines) pre.set("class", "fixed") return pre return None
def diffdump(buf1, buf2, header1='Buffer 1', header2='Buffer 2', startaddr=None, fd=None): """Print the difference of 2 buffers""" from difflib import Differ from StringIO import StringIO f1 = StringIO() f2 = StringIO() dump(buf1, header=header1, startaddr=startaddr, fd=f1) dump(buf2, header=header2, startaddr=startaddr, fd=f2) f1.seek(0) # reposition to file start f2.seek(0) d = Differ() result = list(d.compare(f1.readlines(), f2.readlines())) """ print('results of dump()') print(f1.readlines()) print(f2.readlines()) print(result) """ if fd == None: sys.stdout.writelines(result) else: fd.writelines(result)
def __init__(self, obj, pysnap): self.pretty = Formatter() self.differ = Differ() self.pysnap = pysnap if isinstance(obj, dict): obj = SortedDict(**obj) self.obj = self.pretty(obj)
def assert_templates(template_type, pre, post): """Alternates the result of assert by diff comparing the template data Again, Matches the difference with expected difference and returns True if its expected else returns Fail The expected template differences are derived from varients.py['template_varients'] e.g IF template has addition in 6.2 from 6.1 as '+ RedHat' , then It returns true to pass the test if the change is listed in template_varients :param template_type: Has to be one of 'partition-table', 'template' and 'job-template' :param pre: The preupgrade template of template_type same as postupgrade template :param post: The postupgrade template of template_type same as preupgrade template :return: True if the templates difference is expected else False """ diff = Differ() difference = list(diff.compare(pre.splitlines(), post.splitlines())) del diff added_elements = [added for added in difference if added.startswith('+')] removed_elements = [added for added in difference if added.startswith('-')] for changed_element in added_elements + removed_elements: for expected_varients in template_varients[template_type]: if changed_element in expected_varients: return True pprint(difference) return False
def diff_lines(old, new): d = Differ() diff = list(d.compare(old, new)) for line in diff[:]: if line.startswith(" "): diff.remove(line) return diff
def response_dif(response1, response2): d = Differ() diff = d.compare(response1, response2) i = 0 for el in diff: if re.match(r'\+|-', el): i += 1 return i
def diff(dir1, dir2, fn1, fn2): filenames_list1 = sorted(map(fn1, os.listdir(dir1))) filenames_list2 = sorted(map(fn2, os.listdir(dir2))) df = Differ() result = df.compare(filenames_list1, filenames_list2) with open(OUTPUT_FILE, 'w') as f: f.write('\n'.join(result))
def diff(self, func, entry1, entry2): d = Differ() if type(func) != str: func = func.__name__ s1 = self.versions[func][entry1]["source"].split('\n') s2 = self.versions[func][entry2]["source"].split('\n') return list(d.compare(s1, s2))
def compareWithMooncake(relativePath, text): try: mooncakefile = open(setting["compare"]["path"]+relativePath) except IOError: return text mooncakeLines = mooncakefile.readlines() mooncakefile.close() if len(mooncakeLines) == 0 or mooncakeLines[0].strip() == "<!-- not suitable for Mooncake -->": return "".join(mooncakeLines) i=0 remodeKeepLines = [] keepCount = 0 keeps = {} while i < len(mooncakeLines): if mooncakeLines[i].strip() == "<!-- keep by customization: begin -->": remodeKeepLines.append("<!-- keep by customization: begin -->\n") remodeKeepLines.append(str(keepCount)+"\n") keepCount+=1 i+=1 keepStr = "" while mooncakeLines[i].strip() != "<!-- keep by customization: end -->": keepStr += mooncakeLines[i] i+=1 keeps["<!-- keep by customization: begin -->\n"+str(keepCount-1)+"\n"+mooncakeLines[i]] = "<!-- keep by customization: begin -->\n"+keepStr+"<!-- keep by customization: end -->\n" remodeKeepLines.append(mooncakeLines[i]) i+=1 differ = Differ() lines = [x+"\n" for x in text.split("\n")] lines[len(lines)-1] = lines[len(lines)-1][0:len(lines[len(lines)-1])-1] diff = list(differ.compare(remodeKeepLines, lines)) i = 0 result = "" while i<len(diff): if diff[i][0] == " ": result+=diff[i][2:] elif diff[i][0] == "+": if diff[i][1:] == " ": if i+1<len(diff) and diff[i+1][0] == "-" and diff[i+1][1:].strip() == "": result+="\n" i+=1 continue elif i+1<len(diff) and diff[i+1].strip() == "- <!-- keep by customization: begin -->": i+=1 continue elif i+1<len(diff) and diff[i+1][0] == "-": result+=handleOneLine3(diff, i)[2:] i+=1 else: result+=diff[i][2:] elif diff[i][0] == "-": update = checkUpdate(diff, i) result+=update[0][2:] i+=update[1] i+=1 for k, v in keeps.items(): result = result.replace(k,v); return result
def diff_inside(_dir, type1, type2): files = os.listdir(_dir) filenames_list1 = sorted(map(lambda x:x.replace(type1, ''), filter(lambda f: f.endswith(type1), files))) filenames_list2 = sorted(map(lambda x:x.replace(type2, ''), filter(lambda f: f.endswith(type2), files))) df = Differ() result = df.compare(filenames_list1, filenames_list2) with open(OUTPUT_FILE, 'w') as f: f.write('\n'.join(result))
def assertFileMatches(self, expected, actual): areSame = filecmp.cmp(expected, actual) if not areSame: d = Differ() expectedLines = open(expected).readlines() actualLines = open(actual).readlines() result = list(d.compare(expectedLines, actualLines)) sys.stdout.writelines(result) self.assertTrue(areSame)
def compare(expected, obtained): expected = clean(expected) obtained = clean(obtained) if expected == obtained: return '' else: d = Differ() diff = d.compare(expected, obtained) return ''.join(color(line) for line in diff)
def update(): """ Capture diff between stored history and new history and upload the delta. """ web_history = _get_web_setup() # If we aren't setup for Web usage, just bomb out. if not web_history: print_b(UNCONFIGURED_ERROR) sys.exit(1) current_hist_file = os.path.expanduser( os.environ.get('HISTFILE', '~/.bash_history') ) # Create our diff file if it doesn't exist if not os.path.exists(HISTORY_FILE): open(HISTORY_FILE, 'a').close() # Compare the current history to our previously stored one, # upload any additions and copy the file over. commands = {} with codecs.open(HISTORY_FILE, encoding='UTF-8') as cached, \ codecs.open(current_hist_file, encoding='UTF-8') as current: differ = Differ() results = differ.compare(cached.readlines(), current.readlines()) # use diff lib "codes" to see if we need to upload differences for diff in results: if diff[:2] == '+ ' or diff[:2] == '? ': if diff[2:-1]: commands[diff[2:-1]] = None # Warn if we are doing a large upload num_commands = len(list(commands.keys())) if num_commands > LARGE_UPDATE_COUNT: print_b('Beginning upload of {} history items. ' 'This may take a while...\n'.format(num_commands)) try: success = True commands = [x for x in list(commands.keys()) if x] # To ease testing, sort commands commands.sort() if len(commands) > 0: success, response = web_history.bulk_add( commands ) except ArcheloncException as ex: print_b(ex) sys.exit(3) if not success: print_b('Failed to upload commands, got:\n {}'.format( response )) sys.exit(2) shutil.copy(current_hist_file, HISTORY_FILE)
def DisplayResult(testObj, outTuple, outFile): try:#attempt to open a result file outFile_Read = open(outFile, 'r') except Exception as err: #some error happens while attempting to get a result, inform user return {"StdoutMatch": False, "StderrMatch": False} stdoutMatch = True stderrMatch = True #initialize some values diffObj = Differ()#apparently we can use a single differ object outFile_Obj = loads(outFile_Read.read())#parse output file outFile_Read.close() realStdOut = outTuple[0].decode("UTF-8").splitlines(1)#outTuple has old strings, we need to decode expectedStdOut = outFile_Obj['stdout'].splitlines(1)#the JSON parser returns 3.x strs, no decode realStdErr = outTuple[1].decode("UTF-8").splitlines(1)#outTuple has old strings, we need to decode expectedStdErr = outFile_Obj['stderr'].splitlines(1)#the JSON parser returns 3.x strs, no decode stdOutDiffs = diffObj.compare(realStdOut, expectedStdOut) stdErrDiffs = diffObj.compare(realStdErr, expectedStdErr) #differs output sucks, use DetectDiffs to clean it up and make it readable stdOutResults = DetectDiffs(stdOutDiffs) stdErrResults = DetectDiffs(stdErrDiffs) if len(stdOutResults) > 0 or len(stdErrResults) > 0:#if we have some type of failed test currentResultDir = join(currentResults, testObj['name']) SafeMakeDir(currentResultDir) stdOutRaw = open(join(currentResultDir, "stdout.txt"), 'w') stdOutRaw.write(outTuple[0].decode("UTF-8")) stdOutRaw.close() stdErrRaw = open(join(currentResultDir, "stderr.txt"), 'w') stdErrRaw.write(outTuple[1].decode("UTF-8")) stdErrRaw.close() diffsOut = open(join(currentResultDir, "diff.txt"), 'w') if len(stdOutResults) > 0:#output the diff for stdout to the file stdoutMatch = False diffsOut.write("STDOUT RESULTS\n") diffsOut.write("------------------------------------\n") for line in stdOutResults: diffsOut.write(line) if len(stdErrResults) > 0:#output the diff for the stderr to the file stderrMatch = False diffsOut.write("STDERR RESULTS\n") diffsOut.write("------------------------------------\n") for line in stdErrResults: diffsOut.write(line) diffsOut.close() passed = False return {"StdoutMatch": stdoutMatch, "StderrMatch": stderrMatch}
def _helper(self, source, expected): blocks = self.parser.parse(source) generated = '\n'.join(map(str, blocks)) if isinstance(expected, list): expected = '\n'.join(map(str, expected)) if generated != expected: d = Differ() result = list(d.compare(expected.split('\n'), generated.split('\n'))) print '\n'.join(result) print source.encode('utf8') assert generated == expected
def _helper(self, source, expected): blocks = self.parser.parse(source) generated = b'\n'.join([bytes(b) for b in blocks]) if isinstance(expected, list): expected = b'\n'.join([bytes(b) for b in expected]) if generated != expected: d = Differ() result = list(d.compare(expected.decode('utf8').split('\n'), generated.decode('utf8').split('\n'))) print('\n'.join(result)) print(source.encode('utf8')) self.assertEquals(generated, expected)
def diff(self, func, module): from difflib import Differ d = Differ() before = str(func) self.run(func) self.runmodule(module) after = str(func) diff = d.compare(before.splitlines(), after.splitlines()) for line in diff: print line
def createComment(globalText, mooncakeText): differ = Differ() mooncakeIndex = mooncakeText.find("/>")+2 mooncakeIndex = mooncakeIndex + mooncakeText[mooncakeIndex:].find("/>")+2 globalIndex = globalText.find("/>")+2 globalIndex = globalIndex + globalText[globalIndex:].find("/>")+2 mooncakeTags = re.search("\<tags\s*\n?(.*\n)*.*/\>", mooncakeText[:mooncakeIndex]) globalTags = re.search("\<tags\s*\n?(.*\n)*.*/\>", globalText[:globalIndex]) tagS = "" if globalTags != None: tagS = globalText[:globalTags.span()[1]] globalText = globalText[globalTags.span()[1]:] mooncakeText = mooncakeText[mooncakeTags.span()[1]:] globalLines = [line+"\n" for line in globalText.split("\n")] mooncakeLines = [line+"\n" for line in mooncakeText.split("\n")] diff = list(differ.compare(globalLines, mooncakeLines)) if len(diff) == 0: return "" i = 0 result = [] while i < len(diff): if diff[i][0] == " ": result.append(diff[i][2:]) i += 1 elif diff[i][0] == "-": if i+1 < len(diff) and diff[i+1][0] == "?": result.extend(mergeDiff(diff[i][2:len(diff[i])-1], diff[i+2][2:len(diff[i+2])-1])) i += 3 elif i+1 < len(diff) and diff[i+1][0] == "+": result.extend(mergeDiff(diff[i][2:len(diff[i])-1], diff[i+1][2:len(diff[i+1])-1])) i += 2 else: result.append("<!-- deleted by customization\n") while i < len(diff) and diff[i][0] == "-": result.append(diff[i][2:]) i += 1 result.append("-->\n") elif diff[i][0] == "+": if i+1 < len(diff) and diff[i][0+1] == "-": result.extend(mergeDiff(diff[i+1][2:len(diff[i+1])-1], diff[i][2:len(diff[i])-1])) i += 2 else: result.append("<!-- keep by customization: begin -->\n") while i < len(diff) and diff[i][0] == "+": result.append(diff[i][2:]) i += 1 result.append("<!-- keep by customization: end -->\n") else: i += 1 return tagS + mergeDeleteAndKeep(result)
def _do_test_diffs(self, env, filename, testfun): self._do_test(env, filename, testfun) from os.path import join, dirname testdir = join(dirname(dirname(dirname(testfolder))), 'test') import sys from difflib import Differ d = Differ() def readall(ext): return open(join(testdir, filename + ext), 'rb').readlines() result = d.compare(readall('.' + testfun.__name__ + '.ctl'), readall('.' + testfun.__name__ + '.out')) lines = [ line for line in result if line[0] != ' '] sys.stdout.writelines(lines) self.assertEquals(0, len(lines))
def differ(fn1, fn2): d = Differ() f1 = open(fn1) f2 = open(fn2) for each in d.compare(f1.readlines(), f2.readlines()): if each[0] == '-' or each[0] == '+' or each[0] == '?': f1.close() f2.close() return True f1.close() f2.close() return False
def bobsim(self): path_a = self.args.text_one path_b = self.args.text_two lines_a = loadlines(path_a, character_allcaps_split) lines_b = loadlines(path_b, character_allcaps_split) lines_a = [l for l in lines_a if len(l.words) > 74] lines_b = [l for l in lines_b if len(l.words) > 74] bobs_a = [bag_of_ngrams(l.words) for l in lines_a] bobs_b = [bag_of_ngrams(l.words) for l in lines_b] vocab = set() for bob in bobs_a: vocab |= set(bob) for bob in bobs_b: vocab |= set(bob) vectors_a = [[bob[bg] for bg in vocab] for bob in bobs_a] vectors_b = [[bob[bg] for bg in vocab] for bob in bobs_b] rows = [] for v in vectors_a: row = [cosine_sim(v, u) for u in vectors_b] rows.append(row) for col in row: print "{:0.1f}".format(col), print for i, (line_a, row) in enumerate(zip(lines_a, rows)): for j, csim in enumerate(row): if csim > 0.1: line_b = lines_b[j] print "__________________________" print "Similar pair:" print print line_a.character print line_a.text print print line_b.character print line_b.text print sm = SequenceMatcher(line_a.words, line_b.words) print "Similarity: {}".format(sm.ratio()) print print "____________" print "Edits:" df = Differ() for diffline in df.compare(line_a.text.splitlines(), line_b.text.splitlines()): print diffline print print
def main(code_path, jy_exe="jython", testfile=False, print_diff=True, print_fail=False, print_success=False, print_diff_lines=False): from pprint import pprint from popen2 import popen2 from StringIO import StringIO from difflib import Differ if testfile: pyfiles = [f.rstrip() for f in file(code_path)] elif os.path.isdir(code_path): pyfiles = globwalk.GlobDirectoryWalker(code_path, "*.py") else: pyfiles = [code_path] for pyfile in pyfiles: ast = compile(open(pyfile).read(), pyfile, "exec", _ast.PyCF_ONLY_AST) lispified = lispify_ast(ast) sio = StringIO() pprint(lispified, stream=sio) fin, fout = popen2("%s jastlib.py %s" % (jy_exe, pyfile)) sio.seek(0) pstr = sio.readlines() jstr = fin.readlines() differs = False diffstr = [] difflines = 0 diff = Differ() results = diff.compare(pstr, jstr) for d in results: diffstr.append(d) if d[0] in ['+', '-']: differs = True difflines += 1 if print_success and not differs: print "SUCCESS: %s" % pyfile if print_fail and differs: print "FAIL: %s" % pyfile if print_diff_lines: print "%s diff lines in %s" % (difflines, pyfile) if print_diff and differs: print "---------- ouput -------------" print "py: %s" % sio.getvalue() print "jy: %s" % "".join(jstr) print "---------- DIFF -------------" print "".join(diffstr)
def cutFileNames(l): from difflib import Differ d = Differ() text = sortext(l) indexes = [] for i in l: indexes.append(l[i]) newl = [] for li in l: newl.append(cutStr(li[0:len(li) - 1 - len(li.split('.')[-1])])) l = newl text1 = cutStr(text[0][0:len(text[0]) - 1 - len(text[0].split('.')[-1])]) text2 = cutStr(text[1][0:len(text[1]) - 1 - len(text[1].split('.')[-1])]) sep_file = " " result = list(d.compare(text1.split(sep_file), text2.split(sep_file))) debug('[cutFileNames] ' + unicode(result)) start = '' end = '' for res in result: if str(res).startswith('-') or str(res).startswith('+') or str(res).startswith('.?'): break start = start + str(res).strip() + sep_file result.reverse() for res in result: if str(res).startswith('-') or str(res).startswith('+') or str(res).startswith('?'): break end = sep_file + str(res).strip() + end newl = l l = {} debug('[cutFileNames] [start] ' + start) debug('[cutFileNames] [end] ' + end) for i, fl in enumerate(newl): if cutStr(fl[0:len(start)]) == cutStr(start): fl = fl[len(start):] if cutStr(fl[len(fl) - len(end):]) == cutStr(end): fl = fl[0:len(fl) - len(end)] try: isinstance(int(fl.split(sep_file)[0]), int) fl = fl.split(sep_file)[0] except: pass l[fl] = indexes[i] debug('[cutFileNames] [sorted l] ' + unicode(sorted(l, key=lambda x: x))) return l
def compare_two_xml(benchmark_file_path,target_file_path): #make pretty xml. all xmls are one line xml file. this will create a standard xml structurexl xmltree1=ET.parse(benchmark_file_path) xmltree1_str=ET.tostring(xmltree1,pretty_print=True) #this is to remove b'string value' the b prefix b stands for bytes literal xmltree1_str=xmltree1_str.decode('utf-8') xmltree2=ET.parse(target_file_path) xmltree2_str=ET.tostring(xmltree2,pretty_print=True) xmltree2_str=xmltree2_str.decode('utf-8') d=Differ() result=list(d.compare(xmltree1_str.splitlines(1), xmltree2_str.splitlines(1))) return result
def get_value(self): """Concatenation of Unified Diffs of resources between the revisions.""" # getting the revision id of the element to be diffed. self.rev1_num = self._rev1.properties.revision_id self.rev2_num = self._rev2.properties.revision_id resource1 = str(self._rev1.get_resource().data) resource2 = str(self._rev2.get_resource().data) differ = Differ() result = list(differ.compare(resource1, resource2)) return ''.join(result)
def show_string_diff(s1, s2): """ Writes differences between strings s1 and s2 """ d = Differ() diff = d.compare(s1.splitlines(), s2.splitlines()) diffList = [el for el in diff if el[0] != ' ' and el[0] != '?'] for l in diffList: if l[0] == '+': print('+' + bcolors.GREEN + l[1:] + bcolors.ENDC) elif l[0] == '-': print('-' + bcolors.RED + l[1:] + bcolors.ENDC) else: assert False, 'Error, diffList entry must start with + or -'