Back to index

system-config-printer  1.3.9+20120706
ErrorLogCheckpoint.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008, 2009 Red Hat, Inc.
00006 ## Author: Tim Waugh <twaugh@redhat.com>
00007 
00008 ## This program is free software; you can redistribute it and/or modify
00009 ## it under the terms of the GNU General Public License as published by
00010 ## the Free Software Foundation; either version 2 of the License, or
00011 ## (at your option) any later version.
00012 
00013 ## This program is distributed in the hope that it will be useful,
00014 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 ## GNU General Public License for more details.
00017 
00018 ## You should have received a copy of the GNU General Public License
00019 ## along with this program; if not, write to the Free Software
00020 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00021 
00022 import cups
00023 import os
00024 import tempfile
00025 import time
00026 from timedops import TimedOperation, OperationCanceled
00027 from base import *
00028 class ErrorLogCheckpoint(Question):
00029     def __init__ (self, troubleshooter):
00030         Question.__init__ (self, troubleshooter, "Error log checkpoint")
00031         page = self.initial_vbox (_("Debugging"),
00032                                   _("This step will enable debugging output "
00033                                     "from the CUPS scheduler.  This may "
00034                                     "cause the scheduler to restart.  Click "
00035                                     "the button below to enable debugging."))
00036         button = gtk.Button (_("Enable Debugging"))
00037         buttonbox = gtk.HButtonBox ()
00038         buttonbox.set_border_width (0)
00039         buttonbox.set_layout (gtk.BUTTONBOX_START)
00040         buttonbox.pack_start (button, False, False, 0)
00041         self.button = button
00042         page.pack_start (buttonbox, False, False, 0)
00043         self.label = gtk.Label ()
00044         self.label.set_alignment (0, 0)
00045         self.label.set_line_wrap (True)
00046         page.pack_start (self.label, False, False, 0)
00047         troubleshooter.new_page (page, self)
00048         self.persistent_answers = {}
00049 
00050     def __del__ (self):
00051         if not self.persistent_answers.get ('error_log_debug_logging_set',
00052                                             False):
00053             return
00054 
00055         c = self.troubleshooter.answers['_authenticated_connection']
00056         c._set_lock (False)
00057         settings = c.adminGetServerSettings ()
00058         if len (settings.keys ()) == 0:
00059             return
00060 
00061         settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '0'
00062         answers = self.troubleshooter.answers
00063         orig_settings = self.persistent_answers['cups_server_settings']
00064         settings['MaxLogSize'] = orig_settings.get ('MaxLogSize', '2000000')
00065         c.adminSetServerSettings (settings)
00066 
00067     def display (self):
00068         self.answers = {}
00069         answers = self.troubleshooter.answers
00070         if not answers['cups_queue_listed']:
00071             return False
00072 
00073         self.authconn = answers['_authenticated_connection']
00074         parent = self.troubleshooter.get_window ()
00075 
00076         def getServerSettings ():
00077             # Fail if auth required.
00078             cups.setPasswordCB (lambda x: '')
00079             cups.setServer ('')
00080             c = cups.Connection ()
00081             return c.adminGetServerSettings ()
00082 
00083         try:
00084             self.op = TimedOperation (getServerSettings, parent=parent)
00085             settings = self.op.run ()
00086         except RuntimeError:
00087             return False
00088         except cups.IPPError:
00089             settings = {}
00090 
00091         self.forward_allowed = False
00092         self.label.set_text ('')
00093         if len (settings.keys ()) == 0:
00094             # Requires root
00095             return True
00096         else:
00097             self.persistent_answers['cups_server_settings'] = settings
00098 
00099         try:
00100             if int (settings[cups.CUPS_SERVER_DEBUG_LOGGING]) != 0:
00101                 # Already enabled
00102                 return False
00103         except KeyError:
00104             pass
00105         except ValueError:
00106             pass
00107 
00108         return True
00109 
00110     def connect_signals (self, handler):
00111         self.button_sigid = self.button.connect ('clicked', self.enable_clicked,
00112                                                  handler)
00113 
00114     def disconnect_signals (self):
00115         self.button.disconnect (self.button_sigid)
00116 
00117     def collect_answer (self):
00118         answers = self.troubleshooter.answers
00119         if not answers['cups_queue_listed']:
00120             return {}
00121 
00122         parent = self.troubleshooter.get_window ()
00123         self.answers.update (self.persistent_answers)
00124         if self.answers.has_key ('error_log_checkpoint'):
00125             return self.answers
00126 
00127         (tmpfd, tmpfname) = tempfile.mkstemp ()
00128         os.close (tmpfd)
00129         try:
00130             self.op = TimedOperation (self.authconn.getFile,
00131                                       args=('/admin/log/error_log', tmpfname),
00132                                       parent=parent)
00133             self.op.run ()
00134         except RuntimeError:
00135             try:
00136                 os.remove (tmpfname)
00137             except OSError:
00138                 pass
00139 
00140             return self.answers
00141         except cups.IPPError:
00142             try:
00143                 os.remove (tmpfname)
00144             except OSError:
00145                 pass
00146 
00147             return self.answers
00148 
00149         statbuf = os.stat (tmpfname)
00150         os.remove (tmpfname)
00151         self.answers['error_log_checkpoint'] = statbuf[6]
00152         self.persistent_answers['error_log_checkpoint'] = statbuf[6]
00153         return self.answers
00154 
00155     def can_click_forward (self):
00156         return self.forward_allowed
00157 
00158     def enable_clicked (self, button, handler):
00159         parent = self.troubleshooter.get_window ()
00160         self.troubleshooter.busy ()
00161         try:
00162             self.op = TimedOperation (self.authconn.adminGetServerSettings,
00163                                       parent=parent)
00164             settings = self.op.run ()
00165         except (cups.IPPError, OperationCanceled):
00166             self.troubleshooter.ready ()
00167             self.forward_allowed = True
00168             handler (button)
00169             return
00170 
00171         self.persistent_answers['cups_server_settings'] = settings.copy ()
00172         MAXLOGSIZE='MaxLogSize'
00173         try:
00174             prev_debug = int (settings[cups.CUPS_SERVER_DEBUG_LOGGING])
00175         except KeyError:
00176             prev_debug = 0
00177         try:
00178             prev_logsize = int (settings[MAXLOGSIZE])
00179         except (KeyError, ValueError):
00180             prev_logsize = -1
00181 
00182         if prev_debug == 0 or prev_logsize != '0':
00183             settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '1'
00184             settings[MAXLOGSIZE] = '0'
00185             success = False
00186 
00187             def set_settings (connection, settings):
00188                 connection.adminSetServerSettings (settings)
00189 
00190                 # Now reconnect.
00191                 attempt = 1
00192                 while attempt <= 5:
00193                     try:
00194                         time.sleep (1)
00195                         connection._connect ()
00196                         break
00197                     except RuntimeError:
00198                         # Connection failed
00199                         attempt += 1
00200 
00201             try:
00202                 debugprint ("Settings to set: " + repr (settings))
00203                 self.op = TimedOperation (set_settings,
00204                                           args=(self.authconn, settings,),
00205                                           parent=parent)
00206                 self.op.run ()
00207                 success = True
00208             except cups.IPPError:
00209                 pass
00210             except RuntimeError:
00211                 pass
00212 
00213             if success:
00214                 self.persistent_answers['error_log_debug_logging_set'] = True
00215                 self.label.set_text (_("Debug logging enabled."))
00216         else:
00217             self.label.set_text (_("Debug logging was already enabled."))
00218 
00219         self.forward_allowed = True
00220         self.troubleshooter.ready ()
00221         handler (button)
00222 
00223     def cancel_operation (self):
00224         self.op.cancel ()
00225 
00226         # Abandon the CUPS connection and make another.
00227         answers = self.troubleshooter.answers
00228         factory = answers['_authenticated_connection_factory']
00229         self.authconn = factory.get_connection ()
00230         self.answers['_authenticated_connection'] = self.authconn