Ejemplo n.º 1
0
def genbase(command, args):
    prefix = command[len("genbase") :]
    if prefix and prefix[0] == "=":
        prefix = prefix[1:]

    conf = ScriptConfig(None)
    for path in args:
        conf.add_item("DIR", path)
    base = ScriptBase(conf)
    base.reload()
    if prefix:
        base.set_url_prefix(prefix)
    base.save("nsebase")
def genbase(command, args):
    prefix = command[len("genbase"):]
    if prefix and prefix[0] == '=':
        prefix = prefix[1:]

    conf = ScriptConfig(None)
    for path in args:
        conf.add_item('DIR', path)
    base = ScriptBase(conf)
    base.reload()
    if prefix:
        base.set_url_prefix(prefix)
    base.save('nsebase')
Ejemplo n.º 3
0
                callback(src, all, current)

            if not callback:
                return None
            return subcallback_impl

        scripts = []
        for i, src in enumerate(self.sources):
            subcall = subcallback(callback, src, (i, len(self.sources)))
            if subcall: subcall(None)  # send current=None for every new source
            scripts.extend(src.reload_scripts(subcall))
        return scripts


if __name__ == "__main__":
    from nseBase import ScriptBase

    def callback(src, all, current):
        print src, all, current
        return True

    config = ScriptConfig()
    config.load()
    base = ScriptBase(config)
    base.reload(callback)
    base.save()
    config.save()
    #print base
    #base.save()
    #config.save()
Ejemplo n.º 4
0
        and current source parsing in form (current item, total items)
        """
        def subcallback(callback, src, all):
            def subcallback_impl(current):
                callback(src, all, current)
            if not callback:
                return None
            return subcallback_impl
        scripts = []
        for i, src in enumerate(self.sources):
            subcall = subcallback(callback, src, (i, len(self.sources)))
            if subcall: subcall(None) # send current=None for every new source
            scripts.extend(src.reload_scripts(subcall))
        return scripts

if __name__ == "__main__":
    from nseBase import ScriptBase
    def callback(src, all, current):
        print src, all, current
        return True
    config = ScriptConfig()
    config.load()
    base = ScriptBase(config)
    base.reload(callback)
    base.save()
    config.save()
    #print base
    #base.save()
    #config.save()
    
Ejemplo n.º 5
0
class ScriptManagerWindow(ScriptWindow):
    columns_dict = {
        'I' : (gtk.CellRendererPixbuf, str, _status_pixbuf.get_status_pixbuf),
        'Name' : (gtk.CellRendererText, str, lambda i, s: s.name),
        'ID': (gtk.CellRendererText, str, lambda i, s: s.id),
        'Description': (gtk.CellRendererText, str, lambda i, s: s.description),
        'Type'  : (gtk.CellRendererText, str, lambda i, s: s.type),
        'Author' : (gtk.CellRendererText, str, lambda i, s: s.author),
        'License' : (gtk.CellRendererText, str, lambda i, s: s.license),
        'Categories' : (gtk.CellRendererText, str, lambda i, s: ",".join(s.categories)),
        'Rule' : (gtk.CellRendererText, str, lambda i, s: s.rule),
        'Version' : (gtk.CellRendererText, str, lambda i, s: "%d.%d.%d" % s.version),
        'Path' : (gtk.CellRendererText, str, lambda i, s: s.path),
        'URL' : (gtk.CellRendererText, str, lambda i, s: s.url),
        'Size' : (gtk.CellRendererText, int, lambda i, s: s.size),
        'MD5' : (gtk.CellRendererText, str, lambda i, s: s.md5),
        'SHA1' : (gtk.CellRendererText, str, lambda i, s: s.sha1),
        'GPG' : (gtk.CellRendererText, str, lambda i, s: s.gpg)
        }
    
    def __init__(self):
        ScriptWindow.__init__(self)

	print 'ScriptManager: load preferences'
        self.preferences = NSEManagerConfig()
        # update to full column list
        columns_order = self.preferences.columns_order
        columns_order.extend(set(self.columns_dict.keys()) - set(columns_order))
        self.preferences.columns_order = columns_order

        self.config = ScriptConfig()
        self.config.load()
        self.base = ScriptBase(self.config)
        self.base.load()
        self.search_filter = None
        
        self.set_title(_("Script Manager"))
        self.set_position(gtk.WIN_POS_CENTER)
        self.set_size_request(600, 400)
        self.create_widgets()
        self.update_model(0)

    def create_widgets(self):
        # ui_manager
        actions = [
            ('Script', None, _('_Script'), None),

            ('New', gtk.STOCK_NEW,
             _('New'), "<Ctrl>N",
             _('Create new script file'), self._new_file_cb),

            ('New Wizard', gtk.STOCK_NEW,
             _('_New Wizard...'), "",
             _('Create new script file with wizard'), self._new_wizard_file_cb),

            ('Install', gtk.STOCK_CONVERT,
             _('_Install'), "<Control>I",
             _('Install script from site'), self._install_cb),

            ('Upgrade', gtk.STOCK_EXECUTE,
             _('_Upgrade'), "<Control>U",
             _('Upgrade script to newest version'), self._upgrade_cb),

            ('Remove', gtk.STOCK_DELETE,
             _('_Remove'), "<Control>R",
             _('Remove script from computer'), self._remove_cb),

            ('Install All', gtk.STOCK_CONVERT,
             _('Install All'), None,
             _('Install all new scripts from site'), self._install_all_cb),

            ('Upgrade All', gtk.STOCK_EXECUTE,
             _('Upgrade All'), None,
             _('Upgrade all script to newest versions'), self._upgrade_all_cb),

            ('Tools', None, _('_Tools'), None),

            ('Add Source', gtk.STOCK_ADD,
             _('_Add Source...'), "<Control>A",
             _('Add new source to configuration'), self._add_source_cb),

            ('Quit', gtk.STOCK_QUIT,
             _('_Quit'), "<Ctrl>Q",
             _('Quit from Script Manager'), self._quit_cb),
	    
	    ('Edit Templates', gtk.STOCK_PREFERENCES,
             _('_Edit Templates'), None,
             _('Edit User Templates'), self._templates_cb),

            ('View', None, _('_View'), None),

            ('Settings', None, _('Se_ttings'), None),

            ('Reload', gtk.STOCK_REFRESH,
             _('_Reload'), "<Ctrl>R",
             _('Reload scripts list'), self._reload_cb),

            ('Search', gtk.STOCK_FIND,
             _('_Search'), "<Ctrl>S",
             _('Search necessory scripts'), self._search_cb),

            ('Edit', gtk.STOCK_EDIT,
             _('_Edit'), "<Ctrl>E",
             _('Edit selected script'), self._edit_cb),

            ('Preferences', gtk.STOCK_PREFERENCES,
             _('_Preferences'), "<Ctrl>P",
             _('Script Manager settings'), self._preferences_cb),
            
            ('Help', None, _('_Help'), None),

            ('Report a bug', gtk.STOCK_DIALOG_INFO,
             _('_Report a bug'), "<Ctrl>b",
             _('Report a bug'), self._show_bug_report_cb),

            ('Show Help', gtk.STOCK_HELP,
             _('_Help'), None,
             _('Show the application help'), self._show_help_cb)
            
        ]

        toggle_actions = [
            ('Toolbar', None,
             _('Toolbar'), None,
             _('Show/hide toolbar'), self._toolbar_cb, True),

            ('Statusbar', None,
             _('Statusbar'), None,
             _('Show/hide statusbar'), self._statusbar_cb, True),

            ('Categories', None,
             _('Categories'), None,
             _('Show/hide Categories sidebar'), self._categories_cb, True),

            ('Description', None,
             _('Description'), None,
             _('Show/hide Description sidebar'), self._description_cb, True)
        ]

        ui = """<menubar>
        <menu action='Script'>
            <menuitem action='Install'/>
            <menuitem action='Upgrade'/>
            <menuitem action='Remove'/>
            <separator/>
            <menuitem action='New'/>
            <menuitem action='New Wizard'/>
            <menuitem action='Edit'/>
            <separator/>
            <menuitem action='Quit'/>
        </menu>
        <menu action='Tools'>
            <menuitem action='Reload'/>
            <menuitem action='Install All'/>
            <menuitem action='Upgrade All'/>
            <separator/>
            <menuitem action='Search'/>
            <separator/>
            <menuitem action='Add Source'/>
	    <separator/>
	    <menuitem action='Edit Templates'/>
        </menu>
        <menu action='View'>
            <menuitem action='Categories'/>
            <menuitem action='Description'/>
            <menuitem action='Toolbar'/>
            <menuitem action='Statusbar'/>
        </menu>
        <menu action='Settings'>
            <menuitem action='Preferences'/>
        </menu>
        <menu action='Help'>
            <menuitem action='Show Help'/>
            <menuitem action='Report a bug'/>
        </menu>
        </menubar>

        <toolbar>
            <toolitem action='Reload'/>
            <toolitem action='Install All'/>
            <toolitem action='Upgrade All'/>
            <separator/>
            <toolitem action='Search'/>
            <toolitem action='Edit'/>
            <separator/>
            <toolitem action='Preferences'/>
        </toolbar>
        """

        self.create_layout(actions, toggle_actions, ui)

        self.action_group.get_action('Categories').set_active(self.preferences.view_categories)
        self.action_group.get_action('Description').set_active(self.preferences.view_description)
        self.action_group.get_action('Toolbar').set_active(self.preferences.view_toolbar)
        self.action_group.get_action('Statusbar').set_active(self.preferences.view_statusbar)

        # popup menu
        self.popup_menu = gtk.Menu()
        install_item = gtk.ImageMenuItem(_("Install"))
        install_item.set_image(gtk.image_new_from_stock(gtk.STOCK_CONVERT, gtk.ICON_SIZE_MENU))
        install_item.connect("activate", self._install_cb)
        self.popup_menu.append(install_item)
        upgrade_item = gtk.ImageMenuItem(_("Upgrade"))
        upgrade_item.set_image(gtk.image_new_from_stock(gtk.STOCK_EXECUTE, gtk.ICON_SIZE_MENU))
        upgrade_item.connect("activate", self._upgrade_cb)
        self.popup_menu.append(upgrade_item)
        remove_item = gtk.ImageMenuItem(_("Remove"))
        remove_item.set_image(gtk.image_new_from_stock(gtk.STOCK_DELETE, gtk.ICON_SIZE_MENU))
        remove_item.connect("activate", self._remove_cb)
        self.popup_menu.append(remove_item)
        # TODO: Properties dialog
        #self.popup_menu.append(gtk.SeparatorMenuItem())
        #properties_item = gtk.ImageMenuItem(_("Properties"))
        #properties_item.set_image(gtk.image_new_from_stock(gtk.STOCK_PROPERTIES, gtk.ICON_SIZE_MENU))
        #self.popup_menu.append(properties_item)
        self.popup_menu.show_all()

    def show_all(self):
        ScriptWindow.show_all(self)
        (self.categories_scroll.hide,
         self.categories_scroll.show)[self.preferences.view_categories]()
        (self.desc_scroll.hide,
         self.desc_scroll.show)[self.preferences.view_description]()
        (self.toolbar.hide,
         self.toolbar.show)[self.preferences.view_toolbar]()
        (self.statusbar.hide,
         self.statusbar.show)[self.preferences.view_statusbar]()
        
    def create_categories(self):
        self.categories_model = gtk.ListStore(object, str, int)
        self.categories_view = gtk.TreeView(self.categories_model)
        self.categories_model.set_sort_column_id(2, gtk.SORT_ASCENDING)
        self.categories_view.set_headers_visible(False)
        cell = gtk.CellRendererText()
        col = gtk.TreeViewColumn('Category', cell, markup=1)
        self.categories_view.append_column(col)
        self.categories_view.set_search_column(1)
        self.categories_view.connect('cursor-changed', self._select_category_cb)
        self.categories_view.set_size_request(100, -1)
        self.categories_scroll = scroll_wrap(self.categories_view)
        return self.categories_scroll

    def create_list(self):
        self.list_model = gtk.ListStore(object)
        self.list_view = gtk.TreeView(self.list_model)
        #self.list_view.get_selection().set_mode(gtk.SELECTION_MULTIPLE)
        self.list_view.set_property('rules-hint', True)
        self.list_columns = []
        self.list_view.connect('cursor-changed', self._select_script_cb)
        self.list_view.connect('button-press-event', self._button_press_script_cb)
        return scroll_wrap(self.list_view)

    def create_desc(self):
        self.desc_buffer = gtk.TextBuffer()
        desc_view = gtk.TextView(self.desc_buffer)
        desc_view.set_editable(False)
        desc_view.set_wrap_mode(gtk.WRAP_WORD)
        desc_view.set_size_request(-1, 100);
        self.desc_scroll = scroll_wrap(desc_view)
        return self.desc_scroll
    
    def create_main(self):
        hpaned = gtk.HPaned()
        hpaned.pack1(self.create_categories(), False)
        vpaned = gtk.VPaned()
        vpaned.pack1(self.create_list(), True)
        vpaned.pack2(self.create_desc(), False)
        hpaned.pack2(vpaned, True)
        return hpaned

    def update_model(self, category = -1):
        self.update_categories(category)
        self.update_columns()
        self.update_list(self.get_selected_filter())

    def update_columns(self):
        for col in self.list_columns:
            self.list_view.remove_column(col)
        self.list_columns = []
        model_list = [object]
        i = 1
        pixbuf_id = None
        search_id = None
        for col in self.preferences.columns_order:
            if col in self.preferences.columns_visible:
                col_data = self.columns_dict[col]
                cell = col_data[0]()
                if col_data[0] == gtk.CellRendererToggle:
                    column = gtk.TreeViewColumn(col, cell, active=i)
                elif col_data[0] == gtk.CellRendererPixbuf:
                    column = gtk.TreeViewColumn(col, cell, icon_name=i)
                    pixbuf_id = i
                else:
                    if not search_id:
                        search_id = i
                    column = gtk.TreeViewColumn(col, cell, text=i)
                column.set_sort_column_id(i)
                self.list_view.append_column(column)
                self.list_columns.append(column)
                i += 1
                model_list.append(col_data[1])
        self.list_model = gtk.ListStore(*tuple(model_list))
        self.list_view.set_model(self.list_model)
        self.list_view.set_headers_visible(True)
        self.list_model.set_sort_column_id(1, gtk.SORT_ASCENDING)
        if search_id:
            self.list_view.set_search_column(search_id)
        if pixbuf_id:
            self.list_model.set_sort_func(pixbuf_id, _status_pixbuf.compare, pixbuf_id)

    def append_filter(self, i, filter_):
        self.categories_model.append((filter_, filter_.get_name(), i))
        
    def update_categories(self, selected = -1):
        self.categories_model.clear()
        self.append_filter(0, AllFilter())
        shift = 1
        if self.search_filter:
            self.append_filter(1, self.search_filter)
            shift += 1
        for i, name in enumerate(sorted(self.base.categories)):
            self.append_filter(i + shift, CategoryFilter(name))
        if selected == -1:
            self.update_list(NoneFilter())
        else:
            self.categories_view.get_selection().select_path((selected,))

    def update_list(self, filter_):
        self.list_model.clear()
        for item in self.base.get_script_items():
	    s = item.get_last_installed()
            if s is None:
                s = item.get_last_version()
            if filter_(s):
                model_list = [(item, s)]
                for col in self.preferences.columns_order:
                    if col in self.preferences.columns_visible:
                        col_data = self.columns_dict[col]
                        model_list.append(col_data[2](item, s))
                self.list_model.append(tuple(model_list))
        self.update_desc(None)
        self.update_statusbar()

    def update_statusbar(self):
        context = self.statusbar.get_context_id("statusbar")
        self.statusbar.pop(context)
        self.statusbar.push(context, _("%d scripts listened") % len(self.list_model))

    _state_choose = {
        #state -> (Installable, Upgradable, Removable)
        ScriptItem.STATE_NOT_INSTALLED: (True, False, False),
        ScriptItem.STATE_UPGRADABLE: (False, True, True),
        ScriptItem.STATE_INSTALLED: (False, False, True)
        }
    
    def update_avaliables(self):
        item = self.get_selected_item()
        if not item:
            res = (False, False, False)
        else:
            res = self._state_choose[item.get_state()]
        self.action_group.get_action('Install').set_sensitive(res[0])
        self.action_group.get_action('Upgrade').set_sensitive(res[1])
        self.action_group.get_action('Remove').set_sensitive(res[2])

    def update_desc(self, script):
        self.action_group.get_action('Edit').set_sensitive(bool(script))
        self.update_avaliables()
        if script:
            self.desc_buffer.set_text(script.description)
        else:
            self.desc_buffer.set_text("")

    def get_selected_item(self):
        (model, it) = self.list_view.get_selection().get_selected()
        if not it:
            return None
        return self.list_model[it][0][0]
        
    def get_selected_script(self):
        (model, it) = self.list_view.get_selection().get_selected()
        if not it:
            return None
        return self.list_model[it][0][1]

    def get_selected_filter(self):
        (model, it) = self.categories_view.get_selection().get_selected()
        if not it:
            return NoneFilter()
        return self.categories_model[it][0]

    def _new_wizard_file_cb(self, p):
        editor = ScriptEditorWindow()
        editor.new_file_wizard()
        editor.show_all()

    def _new_file_cb(self, p):
        editor = ScriptEditorWindow()
        editor.open_file()
        editor.show_all()

    def _install_cb(self, p):
        script = self.get_selected_script()
        if script:
            self.base.install(script.name)
            self.update_list(self.get_selected_filter())
            self.base.save()
            
    def _upgrade_cb(self, p):
        script = self.get_selected_script()
        if script:
            self.base.upgrade(script.name)
            self.update_list(self.get_selected_filter())
            self.base.save()

    def _remove_cb(self, p):
        script = self.get_selected_script()
        if script:
            self.base.remove(script.name)
            self.update_list(self.get_selected_filter())
            self.base.save()
    
    def _install_all_cb(self, p):
        self.base.install_all()
        self.update_list(self.get_selected_filter())
        self.base.save()

    def _upgrade_all_cb(self, p):
        self.base.upgrade_all()
        self.update_list(self.get_selected_filter())
        self.base.save()
    
    def _add_source_cb(self, p):
        dialog = AddSourceDialog()
        result = dialog.run()
        dialog.destroy()
        if result == gtk.RESPONSE_OK:
            result = dialog.get_result()
            if result:
                (type_, path) = result
                self.config.add_item(type_, path)
                self.config.save()

    def _toolbar_cb(self, toggle):
        (self.toolbar.hide, self.toolbar.show)[toggle.get_active()]()
        self.preferences.view_toolbar = toggle.get_active()

    def _statusbar_cb(self, toggle):
        (self.statusbar.hide, self.statusbar.show)[toggle.get_active()]()
        self.preferences.view_statusbar = toggle.get_active()

    def _categories_cb(self, toggle):
        if toggle.get_active():
            self.update_list(self.get_selected_filter())
            self.categories_scroll.show()
            self.preferences.view_categories = True
        else:
            self.update_list(AllFilter())
            self.categories_scroll.hide()
            self.preferences.view_categories = False

    def _description_cb(self, toggle):
        if toggle.get_active():
            self.update_desc(self.get_selected_script())
            self.desc_scroll.show()
            self.preferences.view_description = True
        else:
            self.desc_scroll.hide()
            self.preferences.view_description = False

    def _reload_cb(self, p):
        self.base.reload()
        self.base.save()
        self.update_model(0)
        # TODO: testing and bugfixing
        #progress = ScriptManagerProgressWindow(self)
        #progress.show_all()

    def _search_cb(self, p):
        dialog = SearchFilterDialog(self)
        if dialog.run() == gtk.RESPONSE_OK:
            self.search_filter = SearchFilter(dialog.get_pattern())
            self.update_model(1)
        dialog.destroy()
    
    def _edit_cb(self, p):
        script = self.get_selected_script()
        if script and script.path:
            if self.preferences.use_internal_editor:
                editor = ScriptEditorWindow()
                editor.open_file(script.path)
                editor.show_all()
            else:
                # XXX : find better way to run shell command from Python
                if not os.fork():
                    os.system(self.preferences.external_command % script.path)
                    sys.exit(0)

    def _preferences_cb(self, p):
        dialog = ScriptManagerPreferencesDialog(self)
        dialog.run()
        dialog.destroy()
        #self.preferences.save_changes()
        self.config.save()
        self.update_columns()
        self.update_list(self.get_selected_filter())

    def _show_bug_report_cb(self, p):
        BugReport().show_all()

    def _show_help_cb(self, p):
        import webbrowser
        webbrowser.open("file://%s" % os.path.join(Path.docs_dir, "nse_facilitator.html#script_manager"), new=2)

    def _select_category_cb(self, categories_view):
        self.update_list(self.get_selected_filter())

    def _button_press_script_cb(self, list_view, e):
        if e.type != gtk.gdk.BUTTON_PRESS or e.button != 3:
            return
        path = self.list_view.get_path_at_pos(int(e.x), int(e.y))[0]
        item = self.list_model[path][0][0]
        state = self._state_choose[item.get_state()]
        for menu_item, sensitive in zip(self.popup_menu.get_children(), state):
            menu_item.set_sensitive(sensitive)
        self.popup_menu.popup(None, None, None, e.button, e.get_time())
        
    def _select_script_cb(self, list_view):
        self.update_desc(self.get_selected_script())
    
    def _quit_cb(self, p):
        self.destroy()
	
    def _templates_cb(self, p):
	template_manager = ScriptManagerTemplates(self)
        template_manager.show_all()