def findex(): fform = FileForm() tform = TextForm() set_fform = 'True' if fform.validate_on_submit(): filename1 = secure_filename(fform.File1.data.filename) fform.File1.data.save(uppath + filename1) filename2 = secure_filename(fform.File2.data.filename) fform.File2.data.save(uppath + filename2) with open(uppath + filename1, 'r') as f1, open(uppath + filename2, 'r') as f2: cmp = difflib.HtmlDiff() cmp_result = cmp.make_file(f1, f2) os.remove(uppath + filename1) os.remove(uppath + filename2) return cmp_result.encode('utf-8') return render_template('index.html', fform=fform, tform=tform, set_fform=set_fform)
def compareFile(filename1, filename2): if filename1 == "" or filename2 == "": print "Usage:om_file_comp.py comfile1 comfile2" sys.exit(1) lines1 = readFile(filename1) lines2 = readFile(filename2) d = difflib.HtmlDiff() try: htmlFile = str(filename1).split("/")[-1] + 'temp.html' fileHandle = open(htmlFile, 'w') t = d.make_file(lines1, lines2) fileHandle.write(t) fileHandle.close() except IOError as error: print('Read file Error:' + str(error)) sys.exit(1) #调用修改 changeCode(filename1 + 'temp.html')
def diff_table(content_from, content_to, prev_id, curr_id): """Creates an HTML diff of the passed in content_from and content_to.""" tidy_from, errors = _massage_diff_content(content_from) tidy_to, errors = _massage_diff_content(content_to) html_diff = difflib.HtmlDiff(wrapcolumn=DIFF_WRAP_COLUMN) from_lines = tidy_from.splitlines() to_lines = tidy_to.splitlines() try: diff = html_diff.make_table( from_lines, to_lines, _("Revision %s") % prev_id, _("Revision %s") % curr_id, context=True, numlines=constance.config.DIFF_CONTEXT_LINES) except RuntimeError: # some diffs hit a max recursion error message = _(u'There was an error generating the content.') diff = '<div class="warning"><p>%s</p></div>' % message return jinja2.Markup(diff)
def compare_file(self): """比较两个文件并把结果生成一份html文本""" name_list = self.get_same_name_file() for file_name in name_list: text1_lines = self.read_file(os.path.join(self.file_dir1, file_name)) text2_lines = self.read_file(os.path.join(self.file_dir2, file_name)) diff = difflib.HtmlDiff() # 创建HtmlDiff 对象 result = diff.make_file(text1_lines, text2_lines) # 通过make_file 方法输出 html 格式的对比结果 current_path = os.path.dirname(os.path.abspath(__file__)) report_path = os.path.join(current_path, 'resultreport') report_file_path = os.path.join(report_path, f'result_({file_name}).html') # 将结果写入到result_.html文件中 try: with open(report_file_path, 'w') as result_file: result_file.write(result) print(f"{file_name} Successfully Finished") except IOError as error: print('写入html文件错误:{0}'.format(error))
def perform_diff(modeladmin, request, queryset): opts = modeladmin.model._meta count = queryset.count() if count != 2: raise Exception("You can only compare 2 outputs") else: from_output = queryset[0] to_output = queryset[1] import difflib d = difflib.HtmlDiff() table = d.make_table( from_output.result.splitlines(), to_output.result.splitlines(), "%s<br/>%s<br/>%s" % (from_output.job.command.command, from_output.node, from_output.updated_on), "%s<br/>%s<br/>%s" % (to_output.job.command.command, to_output.node, to_output.updated_on)) return render_to_response("admin/ncm/full_diff.html", {'diff': table}) perform_diff.short_description = description
def compare_results(self, act_res_path, exp_res_path): dcmp = dircmp(act_res_path, exp_res_path) match_files_list = [] diff_files_list = [] for name in dcmp.same_files: #print "Matching Data file \"%s\", Expected Res:\"%s\" and Actual Res \"%s\":-" %(name, dcmp.left, dcmp.right) match_files_list.append(name) for name in dcmp.diff_files: #print "Matching Data file \"%s\", Expected Res:\"%s\" and Actual Res \"%s\":-" %(name, dcmp.left, dcmp.right) diff_files_list.append(name) res_file = open("results.html", "wa", 0) sys.stdout = res_file print "Data files with inconsistency in results are: <br>" for counter in range(len(diff_files_list)): print "(", counter + 1, ") ", diff_files_list[counter], "<br>" for i in range(len(diff_files_list)): act_res_file_path = act_res_path + diff_files_list[i] exp_res_file_path = exp_res_path + diff_files_list[i] act_res_file = open(act_res_file_path, 'r') exp_res_file = open(exp_res_file_path, 'r') act_res_line = act_res_file.readlines() exp_res_line = exp_res_file.readlines() diff = difflib.HtmlDiff().make_file(act_res_line, exp_res_line) print "<br><br>Displaying the Difference in Result Set <br>" print "Actual Result File : %s <br>" % (act_res_file_path) print "Expected Result File: %s <br>" % (exp_res_file_path) print "<br>" sys.stdout.writelines(diff) act_res_file.close() exp_res_file.close()
def findOriginalpath(FindpathFilename, f1, f2, log_path): # print "++++现实文档文件++++" for root, dirs, files in os.walk(f2): for fileName in files: compare_filename = fileName #对比的文件名 try: if compare_filename == FindpathFilename: #difflib生成报告 ori = readfile(f2 + "/" + fileName) stan = readfile(f1 + "/" + fileName) diff = difflib.HtmlDiff() p_txt = diff.make_file(stan, ori, context=True, numlines=0) #创建报告文件 f = open(log_path + "/" + "diff.html", 'a') #............ f.write("<meta charset='UTF-8'>") f.write("<a>这是" + fileName + "的对比不同的地方:</a>") #解决中文乱码 f.write(p_txt) f.close() #------------- #创建报告文件HTML #--------- #创建log文档方便shell查看 stan_lines = stan ori_lines = ori d = difflib.Differ() log_diff = d.compare(stan_lines, ori_lines) open_log = open(log_path + "/" + "diff.log", 'a') open_log.write('\n' + "#这是" + fileName + "的对比不同的地方:<->" + '\n') open_log.write('\n'.join(list(log_diff))) open_log.close() #创建log文档方便shell查看 else: exit() except: StandardError
def FileDiff(file1, file2, HtmlFileName, options): #File1 = FileName of File1 / #File2 = FileName of File2 fromdate = time.ctime(os.stat(file1).st_mtime) todate = time.ctime(os.stat(file2).st_mtime) fromlines = open(file1, 'U').readlines() tolines = open(file2, 'U').readlines() if options == 'u': diff = difflib.unified_diff(fromlines, tolines, file1, file2, fromdate, todate, n=n) elif options == 'n': diff = difflib.ndiff(fromlines, tolines) elif options == 'm': diff = difflib.HtmlDiff().make_file(fromlines, tolines, file1, file2, context=options, numlines=3) else: diff = difflib.context_diff(fromlines, tolines, file1, file2, fromdate, todate, n=n) orig_stdout = sys.stdout #f = file('index.html', 'w') # Old Index.html File here f = file(HtmlFileName, 'w') sys.stdout = f sys.stdout.writelines(diff) sys.stdout = orig_stdout f.close()
def diff(self, url, original_code, new_code): diff_data = "" # Prep the two code blocks, after expanding into readable segments options = { "keep_function_indentation": True, "keep_array_indentation": True, "jslint_happy": True } beautiful_old_code = jsbeautifier.beautify(original_code, options).splitlines() beautiful_new_code = jsbeautifier.beautify(new_code, options).splitlines() if self.mode == CodeDifferMode.UNIFIED: delta = difflib.unified_diff( beautiful_old_code, beautiful_new_code, fromfile="before", tofile="after", lineterm="") # Create copies of the iterator so we can manipulate the colored one # seperately without storing it to the database with the ansi chars raw_delta, color_delta = tee(delta) diff_data = '\n'.join(raw_delta) if self.console: print( "[CHANGED FILE] {0}".format(url)) color_diff = self.__color_diff(color_delta) print('\n'.join(color_diff)) return diff_data else: # Generate an HTML diff file differ = difflib.HtmlDiff() diff_data = differ.make_file( beautiful_old_code, beautiful_new_code ) if self.console: print( "[CHANGED FILE] {0}".format(url)) print(diff_data) return diff_data
def view_paste(request, uniqid): try: # Teste la présence du paste paste = Pastebin.objects.get(uniqid=uniqid) except Pastebin.DoesNotExist: # il n'existe pas, on renvoie sur l'accueil return HttpResponseRedirect('pastebin-1.html') author_m = paste.author_m # L'utilisateur peut-il modifier le paste ? if request.user.is_authenticated(): can_edit_paste = (request.user.has_perm('pastebin.edit_all_pastes') or paste.author == request.user.get_profile()) else: can_edit_paste = False # Passage à la template data = { 'uniqid': uniqid, 'contents': paste.contents, 'can_edit_paste': can_edit_paste, 'paste': paste } # Gestion du diff if paste.author_m_id: contents_old = paste.contents_m # Generation du diff Html df = difflib.HtmlDiff().make_table(paste.contents_m.split('\n'), paste.contents.split('\n'), _('Ancienne version'), _('Nouvelle version')) # On enlève les nowrap df = df.replace('nowrap="nowrap"', '') # Template pour le diff data['contents_old'] = contents_old data['difftable'] = df return tpl('pastebin/view.html', data, request)
def json_compare(ret_obj1, ret_obj2, deleted_keys=[]): ret_obj = { 'rc': 0, 'data': { 'matches': False, 'difference': '', 'deleted_keys': deleted_keys }, 'changed': False, 'warnings': [] } # Pick the highest RC to return (should not matter either way) if ret_obj1['rc'] > ret_obj2['rc']: ret_obj['rc'] = ret_obj1['rc'] else: ret_obj['rc'] = ret_obj2['rc'] if 'warnings' in ret_obj1 and ret_obj1['warnings']: ret_obj['warnings'].append(ret_obj1['warnings']) if 'warnings' in ret_obj2 and ret_obj2['warnings']: ret_obj['warnings'].append(ret_obj2['warnings']) sorted_json1 = json_sort(ret_obj1['data']) sorted_json2 = json_sort(ret_obj2['data']) if sorted_json1 == sorted_json2: ret_obj['data']['matches'] = True else: psj1 = pprint.pformat(sorted_json1) logger.debug('Sorted JSON1 to Compare: \n' + psj1) psj2 = pprint.pformat(sorted_json2) logger.debug('Sorted JSON2 to Compare: \n' + psj2) diff = difflib.ndiff(psj1.split('\n'), psj2.split('\n')) ret_obj['data']['difference'] = '\n'.join(diff) ret_obj['data']['context_difference'] = list( difflib.context_diff(psj1.split('\n'), psj2.split('\n'))) ret_obj['data']['html_difference'] = difflib.HtmlDiff().make_file( psj1.split('\n'), psj2.split('\n'), context=True) return ret_obj
def run(self): logger = self.logger first_file = self.options['FIRSTFILE'] first_file = open(first_file, 'r') second_file = self.options['SECONDFILE'] second_file = open(second_file, 'r') self.report = open(self.options['REPORTFILE'], 'w', 1) self.differ = difflib.HtmlDiff() self.tables_to_skip = [ c1219.constants.PROC_INITIATE_TBL, c1219.constants.PROC_RESPONSE_TBL, c1219.constants.PRESENT_REGISTER_DATA_TBL ] self.report.write(HTML_HEADER) self.report.write(HTML_TABLE_LEGEND) self.report.write('<br />\n') self.report.write(HTML_TABLE_HEADER) self.highlight_table = True self.frmwk.print_status('Generating Diff...') fid, fline = self.get_line(first_file) sid, sline = self.get_line(second_file) while (fid != None) or (sid != None): if (fid == None or sid == None) or (fid == sid): self.report_line(fline, sline, (fid or sid)) fid, fline = self.get_line(first_file) sid, sline = self.get_line(second_file) elif fid < sid: self.report_line(fline, '', fid) fid, fline = self.get_line(first_file) elif sid < fid: self.report_line('', sline, sid) sid, sline = self.get_line(second_file) self.report.write(HTML_TABLE_FOOTER) self.report.write(HTML_FOOTER) self.report.close() second_file.close() first_file.close() return
def compare(self, session=None): conf1 = {} conf2 = {} conf = None dcmp_dag = None dag_name = request.args.get("dag_name") if dag_name and len(dag_name) > 0: # 增加限制非有权限的用户dagid,不能查看 if (not wwwutils.get_filter_by_user_dagid(dag_name)): return render_template('airflow/circles.html', hostname=socket.getfqdn()) version1 = request.args.get("version1") version2 = request.args.get("version2") if dag_name: dcmp_dag = session.query(DcmpDag).filter( DcmpDag.dag_name == dag_name, ).first() if dcmp_dag: conf = dcmp_dag.get_conf(session=session) conf1 = dcmp_dag.get_conf(version=version1, session=session) conf2 = dcmp_dag.get_conf(version=version2, session=session) dcmp_dag_confs = session.query(DcmpDagConf).filter( DcmpDagConf.dag_id == dcmp_dag.id, ).order_by( DcmpDagConf.version.desc()) diff_table = difflib.HtmlDiff().make_table( self.conf_diff_preprocess(conf1), self.conf_diff_preprocess(conf2), ) if conf is None: conf = self.DEFAULT_CONF return self.render("dcmp/compare.html", can_access_approver=can_access_approver(), diff_table=diff_table, conf=conf, version1=version1, version2=version2, dcmp_dag=dcmp_dag, dcmp_dag_confs=dcmp_dag_confs, command_render=command_render)
def _get_diff(self, v1, v2): """Return the difference between two version of document version.""" text1 = v1 and self.browse(v1).content or "" text2 = v2 and self.browse(v2).content or "" # Include line breaks to make it more readable # TODO: consider using a beautify library directly on the content text1 = text1.replace("</p><p>", "</p>\r\n<p>") text2 = text2.replace("</p><p>", "</p>\r\n<p>") line1 = text1.splitlines(True) line2 = text2.splitlines(True) if line1 == line2: return _("There are no changes in revisions.") else: diff = difflib.HtmlDiff() return diff.make_table( line1, line2, "Revision-{}".format(v1), "Revision-{}".format(v2), context=True, )
def diff_table(content_from, content_to, prev_id, curr_id, tidy=False): """ Creates an HTML diff of the passed in content_from and content_to. """ if tidy: content_from, errors = tidy_content(content_from) content_to, errors = tidy_content(content_to) html_diff = difflib.HtmlDiff(wrapcolumn=DIFF_WRAP_COLUMN) try: diff = html_diff.make_table(content_from.splitlines(), content_to.splitlines(), ugettext('Revision %s') % prev_id, ugettext('Revision %s') % curr_id, context=True, numlines=config.DIFF_CONTEXT_LINES) except RuntimeError: # some diffs hit a max recursion error message = ugettext('There was an error generating the content.') diff = '<div class="warning"><p>%s</p></div>' % message return jinja2.Markup(diff)
def run2(year, month, t1, t2, mf_dict, ss_dict, ss, lock): t1.join() t2.join() lock.acquire() print(str(year) + '/' + str(month)) mfdata = mf_dict[str(year) + '/' + str(month)] sdata = ss_dict[str(year) + '/' + str(month)] if sdata == mfdata: print('SAME') elif len(mfdata) == 0: print('MoneyForward No Data') else: print('There is diff\nUpdate sheet') fname = 'diff' + str(month) + '.html' d = difflib.HtmlDiff() with open(fname, mode='w') as f: f.write( d.make_file([', '.join(map(str, i)) for i in mfdata], [', '.join(map(str, i)) for i in sdata])) ss.merge(year, month, mfdata) lock.release()
class FileDiff(GeoffreyPlugin): diff = difflib.HtmlDiff() files = {} def run(self, filename): with magic.Magic(flags=magic.MAGIC_MIME_TYPE) as m: file_type = m.id_filename(filename) if not fnmatch(file_type, '*text*'): return False with open(filename, 'r') as f: content = f.readlines() if filename in self.files: if self.files[filename] != content: output = HTML( self.diff.make_table(self.files[filename], content)) yield from self.output_queue.put(output) self.files[filename] = content
def file_cmp(path1, path2): filename1 = get_filename(path1) filename2 = get_filename(path2) file1_line = open(path1).readlines() file2_line = open(path2).readlines() str1 = open(path1).read() str2 = open(path2).read() f = open("tmp/" + filename1 + "_" + filename2 + "_cmp.html", 'w') d = difflib.HtmlDiff() f.write(d.make_file(file1_line, file2_line)) f.close() matcher = difflib.SequenceMatcher(None, file1_line, file2_line).ratio() # 计算莱文斯坦比 sim2 = Levenshtein.ratio(str1, str2) # 计算jaro距离 sim3 = Levenshtein.jaro(str1, str2) # Jaro–Winkler距离 sim4 = Levenshtein.jaro_winkler(str1, str2) matcher = matcher * 0.25 + sim2 * 0.25 + sim3 * 0.25 + sim4 * 0.25 return matcher
def html_diff(self, context=True, wrapcolumn=40): """Return an HTML table showing differences""" # difflib is not Unicode-aware, so we need to force everything to # utf-8 manually a = [ safe_unicode(i) for i in self._parseField(self.oldValue, filename=self.oldFilename) ] b = [ safe_unicode(i) for i in self._parseField(self.newValue, filename=self.newFilename) ] vis_diff = difflib.HtmlDiff(wrapcolumn=wrapcolumn) diff = vis_diff.make_table(a, b, safe_unicode(self.id1), safe_unicode(self.id2), context=context) if six.PY2: diff = safe_utf8(diff) return diff
def make_html_diff(fromfile, tofile): import difflib with open(fromfile) as fh: fromlines = fh.readlines() with open(tofile) as fh: tolines = fh.readlines() diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile, tofile, context=True, numlines=3) # writelines because diff is a generator _, tmp_path = tempfile.mkstemp(text=True, suffix=".html") with open(tmp_path, "w") as fh: fh.writelines(diff) return tmp_path
def create_diff_csv(referenced_msg, compared_msgs): hd = difflib.HtmlDiff() html = hd.make_file(referenced_msg, compared_msgs) lines = parse_side_by_side(html) csvlist = [] for i in range(0, len(lines), 2): if lines[i].text == '': origline = ',' else: origline = lines[i].text if lines[i + 1].text == '': revline = ',' else: revline = lines[i + 1].text L = [] L.append(origline.replace('\xa0', '\x20')) L.append(',') L.append(revline.replace('\xa0', '\x20')) L.append('\n') csvlist.append(''.join(L)) return csvlist
def diffTextFiles(fFile,tFile,options): # we're passing these as arguments to the diff function fDate = time.ctime(stat(fFile).st_mtime) tDate = time.ctime(stat(tFile).st_mtime) fLines = getFileContent(fFile) tLines = getFileContent(tFile) if options.unified: return difflib.unified_diff(fLines, tLines, fFile, tFile, fDate, tDate, n=options.ablines) elif options.ndiff: return difflib.ndiff(fLines, tLines) elif options.html: return difflib.HtmlDiff().make_file(fLines, tLines, fFile, tFile, context=options.context, numlines=options.ablines) else: return difflib.context_diff(fLines, tLines, fFile, tFile, fDate, tDate, n=options.ablines) return
def run_compare(f1, f2): """主函数""" # try: # # 获取文件名 # f1 = sys.argv[1] # f2 = sys.argv[2] # except Exception as e: # print("Error: " + str(e)) # print("Usage : python compareFile.py filename1 filename2") # sys.exit() # # 参数不够 # if f1 == "" or f2 == "": # print("Usage : python compareFile.py filename1 filename2") # sys.exit() tf1 = read_file(f1) tf2 = read_file(f2) # 创建一个实例difflib.HtmlDiff d = difflib.HtmlDiff() # 生成一个比较后的报告文件,格式为html write_file(d.make_file(tf1, tf2))
def test_tex_file(fn=None, htmldiff=None): if fn is None: testdir = os.path.split(os.path.realpath(__file__))[0] fn = testdir + os.sep + 'test_pub.tex' with open(fn) as f: orig = f.read().split('\n') tfstr = publication.TeXFile(fn)().split('\n') diffres = list(difflib.unified_diff(orig, tfstr)) if htmldiff: hd = difflib.HtmlDiff() htmlres = hd.make_file(orig, tfstr) with open(htmldiff, 'w') as f: f.write(htmlres) else: for l in diffres: print l assert len( diffres ) == 0, 'TeXFile string does not match original file ' + os.path.split( fn)[1]
def main(): parser = argparse.ArgumentParser() parser.add_argument('fromfile') parser.add_argument('tofile') options = parser.parse_args() fromfile = options.fromfile tofile = options.tofile with open(fromfile) as ff: fromlines = ff.readlines() with open(tofile) as tf: tolines = tf.readlines() diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile, tofile, charset='utf-8') sys.stdout.writelines(diff)
def getDiff(self, cr, uid, v1, v2, context=None): """ @param cr: the current row, from the database cursor, @param uid: the current user’s ID for security checks, """ history_pool = self.pool.get('wiki.wiki.history') text1 = history_pool.read(cr, uid, [v1], ['text_area'])[0]['text_area'] text2 = history_pool.read(cr, uid, [v2], ['text_area'])[0]['text_area'] line1 = line2 = '' if text1: line1 = tools.ustr(text1.splitlines(1)) if text2: line2 = tools.ustr(text2.splitlines(1)) if (not line1 and not line2) or (line1 == line2): raise osv.except_osv(_('Warning !'), _('There are no changes in revisions')) diff = difflib.HtmlDiff() return diff.make_file(line1, line2, "Revision-%s" % (v1), "Revision-%s" % (v2), context=False)
def compare(self, new): new_lines = new.splitlines() state_lines = self.state.splitlines() d = difflib.Differ() diff = d.compare(state_lines, new_lines) diff = [line for line in diff if str.startswith(line, ("+", "-"))] if diff: diff_html = difflib.HtmlDiff().make_table(state_lines, new_lines) self.logger.info("Creating diff.html") with open(self.path + "/resources/diff.html", "w") as f: f.write(diff_html) self.logger.info("Rewriting state") self.state = new with open(self.path + "/resources/state.html", "w") as f: f.write(self.state) return diff
def write(self, test, outdir, result, actual, expected, diff): import difflib actual_file = open(os.path.join(outdir, actual), 'r') expected_file = open(expected, 'r') html = difflib.HtmlDiff().make_file(actual_file.readlines(), expected_file.readlines(), "Actual", "Expected", context=True) actual_file.close() expected_file.close() diffdir = self._create_diff_for_test(outdir, test) pretty_diff_name = result + '-pretty-diff.html' pretty_diff = os.path.abspath(os.path.join(diffdir, pretty_diff_name)) f = open(pretty_diff, 'w') f.write(html) f.close() return os.path.join(test, pretty_diff_name)
def compare_two_files(textfile1,textfile2): text1_lines = readfile(textfile1) text2_lines = readfile(textfile2) if textfile1=="" or textfile2=="": print ("有文件不存在") #输出html格式 d = difflib.HtmlDiff() html=d.make_file(text1_lines, text2_lines) #直接输出结果 d2 = difflib.Differ() diff = d2.compare(text1_lines, text2_lines) compare_line='\n'.join(list(diff)) #print(compare_line) print ('type(html)') #保存到html文件中 save_tempHtml(html) #替换文件中的charset=ISO-8859-1为charset=UTF-8 replace_tempHtml(''' content=\"text/html; charset=ISO-8859-1"''','''content="text/html; charset=UTF-8"''') conut_str('''td''')
def index(request): context = { 'answer': '1', 'tx1': '1', 'tx2': '1', 'issame': "", # 是否显示 'hmdistance': '', # 海明距离, 'dedistance': '', # 判断距离, 'diffe': '' # 对比结果 } try: simhash = SimHash() tx1 = (request.POST['tx1']) tx2 = (request.POST['tx2']) context['tx1'] = tx1 context['tx2'] = tx2 if len(tx1) <= 2 or len(tx2) <= 2: context['answer'] = "对比文本不能小于2字符" else: hash1 = simhash.hash(tx1) hash2 = simhash.hash(tx2) distince = simhash.getDistince(hash1, hash2) value = 20 context['hmdistance'] = distince context['dedistance'] = value context['issame'] = distince <= value res = "海明距离:" + str(distince) + " 判定距离:" + str( value) + " 是否相似 " + str(distince <= value) context['answer'] = res dtx1 = tx1.splitlines() dtx2 = tx2.splitlines() d = difflib.HtmlDiff() dres = d.make_file(dtx1, dtx2, charset="utf-8") context['diffe'] = dres except KeyError: context['answer'] = '空字符串' return render(request, 'simhash.html', context) return render(request, 'simhash.html', context)