diffcompare.py
来自「Ubuntu packages of security software。 相」· Python 代码 · 共 1,081 行 · 第 1/3 页
PY
1,081 行
#!/usr/bin/env python# -*- coding: utf-8 -*-# Copyright (C) 2005 Insecure.Com LLC.## Author: Adriano Monteiro Marques <py.adriano@gmail.com>## This program is free software; you can redistribute it and/or modify# it under the terms of the GNU General Public License as published by# the Free Software Foundation; either version 2 of the License, or# (at your option) any later version.## This program is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU General Public License for more details.## You should have received a copy of the GNU General Public License# along with this program; if not, write to the Free Software# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USAimport gtkimport pangoimport osimport os.pathimport webbrowserfrom higwidgets.higdialogs import HIGAlertDialog, HIGDialogfrom higwidgets.higboxes import HIGVBox, HIGHBox, hig_box_space_holderfrom higwidgets.higlabels import HIGEntryLabel, HIGSectionLabelfrom higwidgets.higtables import HIGTablefrom higwidgets.higbuttons import HIGButtonfrom zenmapCore.Name import APP_DISPLAY_NAME, NMAP_DISPLAY_NAMEfrom zenmapCore.Diff import Difffrom zenmapCore.UmitConf import UmitConf, DiffColorsfrom zenmapCore.NmapParser import NmapParser, HostInfofrom zenmapCore.Paths import check_access, Pathfrom zenmapCore.UmitLogging import logfrom zenmapCore.I18N import _from tempfile import mktempfrom types import StringTypesfrom zenmapGUI.FileChoosers import RegularDiffiesFileFilter, HtmlDiffiesFileFiltertry: from zenmapCore.DiffHtml import DiffHtml from zenmapGUI.FileChoosers import AllFilesFileChooserDialog,\ ResultsFileChooserDialog,\ FullDiffiesFileChooserDialog as DiffiesFileChooserDialog use_html = Trueexcept: from zenmapGUI.FileChoosers import AllFilesFileChooserDialog,\ ResultsFileChooserDialog,\ SingleDiffiesFileChooserDialog as DiffiesFileChooserDialog use_html = Falseclass ScanChooser(HIGVBox): def __init__(self, scan_dict, num=""): HIGVBox.__init__(self) self.num = num self.scan_dict = scan_dict # Setting HIGVBox self.set_border_width(5) self.set_spacing(6) self._create_widgets() self._pack_hbox() self._attaching_widgets() self._set_scrolled() self._set_text_view() self._set_open_button() for scan in scan_dict: self.list_scan.append([scan]) self.combo_scan.connect('changed', self.show_scan) self._pack_noexpand_nofill(self.lbl_scan) self._pack_expand_fill(self.hbox) def _create_widgets(self): self.lbl_scan = HIGSectionLabel("%s %s"%(_("Scan Result"), str(self.num))) self.hbox = HIGHBox() self.table = HIGTable() self.list_scan = gtk.ListStore(str) self.combo_scan = gtk.ComboBoxEntry(self.list_scan, 0) self.btn_open_scan = gtk.Button(stock=gtk.STOCK_OPEN) self.exp_scan = gtk.Expander(_("Scan Result Visualization")) self.scrolled = gtk.ScrolledWindow() self.txt_scan_result = gtk.TextView() self.txg_tag = gtk.TextTag("scan_style") def get_buffer(self): return self.txt_scan_result.get_buffer() def show_scan (self, widget): self.txt_scan_result.get_buffer().\ set_text(self.normalize_output(self.scan_dict[widget.child.get_text()].nmap_output)) def normalize_output(self, output): return "\n".join(output.split("\\n")) def _pack_hbox (self): self.hbox._pack_noexpand_nofill(hig_box_space_holder()) self.hbox._pack_expand_fill(self.table) def _attaching_widgets (self): self.table.attach(self.combo_scan, 0,1,0,1, yoptions=0) self.table.attach(self.btn_open_scan, 1,2,0,1, yoptions=0, xoptions=0) self.table.attach(self.exp_scan, 0,2,1,2) def _set_scrolled(self): self.scrolled.set_border_width(5) self.scrolled.set_size_request(-1, 160) # Packing scrolled window into expander self.exp_scan.add(self.scrolled) # Packing text view into scrolled window self.scrolled.add_with_viewport(self.txt_scan_result) # Setting scrolled window self.scrolled.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC) def _set_text_view (self): self.txg_table = self.txt_scan_result.get_buffer().get_tag_table() self.txg_table.add(self.txg_tag) self.txg_tag.set_property("family", "Monospace") self.txt_scan_result.set_wrap_mode(gtk.WRAP_WORD) self.txt_scan_result.set_editable(False) self.txt_scan_result.get_buffer().connect("changed", self._text_changed_cb) def _set_open_button (self): self.btn_open_scan.connect('clicked', self.open_file) def open_file (self, widget): file_chooser = ResultsFileChooserDialog(_("Select Scan Result")) file_chooser.run() file_chosen = file_chooser.get_filename() file_chooser.destroy() if check_access(file_chosen, os.R_OK): try: parser = NmapParser(file_chosen) parser.parse() except: alert = HIGAlertDialog( message_format='<b>%s</b>' % _('File is not a Umit Scan Result'), secondary_text=_("Selected file is not a Umit Scan Result file. \%s can not parse this file. Please, select another." % APP_DISPLAY_NAME)) alert.run() alert.destroy() return False scan_name = os.path.split(file_chosen)[-1] self.add_scan(scan_name, parser) self.combo_scan.set_active(len(self.list_scan) - 1) else: alert = HIGAlertDialog( message_format='<b>%s</b>' % _('Can not open selected file'), secondary_text=_("%s can not open selected file. Please, select \another." % APP_DISPLAY_NAME)) alert.run() alert.destroy() def add_scan(self, scan_name, parser): scan_id = 1 new_scan_name = scan_name while new_scan_name in self.scan_dict.keys(): new_scan_name = "%s (%s)" % (scan_name, scan_id) scan_id += 1 self.list_scan.append([new_scan_name]) self.scan_dict[new_scan_name] = parser def _text_changed_cb (self, widget): buff = self.txt_scan_result.get_buffer () buff.apply_tag(self.txg_tag, buff.get_start_iter(), buff.get_end_iter()) def get_nmap_output(self): parsed = self.parsed_scan if parsed: return parsed.nmap_output return False def get_parsed_scan(self): selected_scan = self.combo_scan.child.get_text() if selected_scan: return self.scan_dict[selected_scan] return False nmap_output = property(get_nmap_output) parsed_scan = property(get_parsed_scan)class DiffWindow(gtk.Window): def __init__(self, scans): """scans in the format: {"scan_title":parsed_scan} """ gtk.Window.__init__(self) self.set_title(_("Compare Results")) self.scans = scans self.umit_conf = UmitConf() self.colors = Colors() # Diff views self.text_view = DiffText(self.colors, self.umit_conf.colored_diff) self.compare_view = DiffTree(self.colors) self._create_widgets() self._pack_widgets() self._connect_widgets() # Settings if self.umit_conf.diff_mode == "text": self.text_mode.set_active(True) else: self.compare_mode.set_active(True) self.check_color.set_active(self.umit_conf.colored_diff) # Initial Size Request self.initial_size = self.size_request() def _show_help(self, action): webbrowser.open("file://%s" % os.path.join(Path.docs_dir, "help.html"), new=2) def _create_widgets(self): self.main_vbox = HIGVBox() self.hbox_mode = HIGHBox() self.hbox_settings = HIGHBox() self.hbox_buttons = HIGHBox() self.hbox_result = HIGHBox() self.btn_open_browser = HIGButton(_("Open in Browser"), stock=gtk.STOCK_EXECUTE) self.btn_help = HIGButton(stock=gtk.STOCK_HELP) self.btn_close = HIGButton(stock=gtk.STOCK_CLOSE) self.check_color = gtk.CheckButton(_("Enable colored diffies")) self.btn_legend = HIGButton(_("Color Descriptions"), stock=gtk.STOCK_SELECT_COLOR) self.text_mode = gtk.ToggleButton(_("Text Mode")) self.compare_mode = gtk.ToggleButton(_("Compare Mode")) self.vpaned = gtk.VPaned() self.hpaned = gtk.HPaned() self.scan_chooser1 = ScanChooser(self.scans, "1") self.scan_chooser2 = ScanChooser(self.scans, "2") self.scan_buffer1 = self.scan_chooser1.get_buffer() self.scan_buffer2 = self.scan_chooser2.get_buffer() def _pack_widgets(self): self.main_vbox.set_border_width(6) self.vpaned.pack1(self.hpaned, True, False) self.vpaned.pack2(self.hbox_result) self.hpaned.pack1(self.scan_chooser1, True, False) self.hpaned.pack2(self.scan_chooser2, True, False) self.hbox_buttons._pack_expand_fill(self.btn_help) self.hbox_buttons._pack_expand_fill(self.btn_legend) self.hbox_buttons._pack_expand_fill(self.btn_open_browser) self.hbox_buttons._pack_expand_fill(self.btn_close) self.hbox_buttons.set_homogeneous(True) self.hbox_mode.set_homogeneous(True) self.hbox_mode.pack_start(self.text_mode) self.hbox_mode.pack_start(self.compare_mode) self.hbox_settings._pack_noexpand_nofill(self.hbox_mode) self.hbox_settings._pack_expand_fill(self.check_color) self.main_vbox._pack_expand_fill(self.vpaned) self.main_vbox._pack_noexpand_nofill(self.hbox_settings) self.main_vbox._pack_noexpand_nofill(self.hbox_buttons) self.add(self.main_vbox) def _connect_widgets(self): self.connect("delete-event", self.close) self.btn_legend.connect("clicked", self.show_legend_window) self.btn_help.connect("clicked", self._show_help) self.btn_close.connect("clicked", self.close) self.btn_open_browser.connect("clicked", self.open_browser) self.check_color.connect("toggled", self._set_color) self.text_mode.connect("clicked", self._change_to_text) self.compare_mode.connect("clicked", self._change_to_compare) self.scan_chooser1.exp_scan.connect('activate', self.resize_vpane) self.scan_chooser2.exp_scan.connect('activate', self.resize_vpane) self.scan_buffer1.connect('changed', self.text_changed) self.scan_buffer2.connect('changed', self.text_changed) def open_browser(self, widget): text1=self.scan_buffer1.get_text(self.scan_buffer1.get_start_iter(),\ self.scan_buffer1.get_end_iter()) text2=self.scan_buffer2.get_text(self.scan_buffer2.get_start_iter(),\ self.scan_buffer2.get_end_iter()) if not text1 or not text2: alert = HIGAlertDialog( message_format='<b>'+_('Select Scan')+'</b>', secondary_text=_("You must select two different scans to \generate diff.")) alert.run() alert.destroy() return False text1 = text1.split('\n') text2 = text2.split('\n') self.temp_view = mktemp('.html') text1 = [text+'\n' for text in text1] text2 = [text+'\n' for text in text2] if use_html: diff = DiffHtml(text1, text2) diff = diff.generate() file_desc = open(self.temp_view, 'w') file_desc.write(''.join(diff)) # Closing file to avoid problems with file descriptors file_desc.close() else: diff = Diff(text1, text2) diff = diff.generate () diff.insert(0, '''<pre>(This diff is been shown in pure text \because you dont have Python 2.4 or higher.)\n''') diff.append('</pre>') file_desc = open(self.temp_view, 'w') file_desc.writelines(diff) # Closing file to avoid problems with file descriptors file_desc.close() webbrowser.open("file://" + self.temp_view, autoraise=1) def show_legend_window(self, widget): legend_window = DiffLegendWindow(self.colors) legend_window.run() legend_window.destroy() self.text_changed(None) def text_changed (self, widget): text1 = self.scan_buffer1.get_text(self.scan_buffer1.get_start_iter(),\ self.scan_buffer1.get_end_iter()) text2 = self.scan_buffer2.get_text(self.scan_buffer2.get_start_iter(),\ self.scan_buffer2.get_end_iter()) if text1 != '' and text2 != '': if self.compare_mode.get_active(): self.compare_view.make_diff(self.scan_chooser1.parsed_scan, self.scan_chooser2.parsed_scan) self.compare_view.activate_color(self.check_color.get_active()) else: self.text1 = text1.split ('\n') self.text2 = text2.split ('\n')
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?