Back to index

system-config-printer  1.3.9+20120706
ErrorLogFetch.py
Go to the documentation of this file.
00001 #!/usr/bin/python
00002 
00003 ## Printing troubleshooter
00004 
00005 ## Copyright (C) 2008, 2010 Red Hat, Inc.
00006 ## Authors:
00007 ##  Tim Waugh <twaugh@redhat.com>
00008 
00009 ## This program is free software; you can redistribute it and/or modify
00010 ## it under the terms of the GNU General Public License as published by
00011 ## the Free Software Foundation; either version 2 of the License, or
00012 ## (at your option) any later version.
00013 
00014 ## This program is distributed in the hope that it will be useful,
00015 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017 ## GNU General Public License for more details.
00018 
00019 ## You should have received a copy of the GNU General Public License
00020 ## along with this program; if not, write to the Free Software
00021 ## Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00022 
00023 import cups
00024 import os
00025 import tempfile
00026 import time
00027 from timedops import TimedOperation
00028 from base import *
00029 class ErrorLogFetch(Question):
00030     def __init__ (self, troubleshooter):
00031         Question.__init__ (self, troubleshooter, "Error log fetch")
00032         troubleshooter.new_page (gtk.Label (), self)
00033         self.persistent_answers = {}
00034 
00035     def display (self):
00036         answers = self.troubleshooter.answers
00037         parent = self.troubleshooter.get_window ()
00038         self.answers = {}
00039         try:
00040             checkpoint = answers['error_log_checkpoint']
00041         except KeyError:
00042             checkpoint = None
00043 
00044         if self.persistent_answers.has_key ('error_log'):
00045             checkpoint = None
00046 
00047         def fetch_log (c):
00048             prompt = c._get_prompt_allowed ()
00049             c._set_prompt_allowed (False)
00050             c._connect ()
00051             (tmpfd, tmpfname) = tempfile.mkstemp ()
00052             os.close (tmpfd)
00053             success = False
00054             try:
00055                 c.getFile ('/admin/log/error_log', tmpfname)
00056                 success = True
00057             except cups.HTTPError:
00058                 try:
00059                     os.remove (tmpfname)
00060                 except OSError:
00061                     pass
00062 
00063             c._set_prompt_allowed (prompt)
00064             if success:
00065                 return tmpfname
00066             return None
00067 
00068         self.authconn = self.troubleshooter.answers['_authenticated_connection']
00069         if answers.has_key ('error_log_debug_logging_set'):
00070             try:
00071                 self.op = TimedOperation (self.authconn.adminGetServerSettings,
00072                                           parent=parent)
00073                 settings = self.op.run ()
00074             except cups.IPPError:
00075                 return False
00076 
00077             settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '0'
00078             orig_settings = answers['cups_server_settings']
00079             settings['MaxLogSize'] = orig_settings.get ('MaxLogSize', '2000000')
00080             success = False
00081             def set_settings (connection, settings):
00082                 connection.adminSetServerSettings (settings)
00083 
00084                 # Now reconnect.
00085                 attempt = 1
00086                 while attempt <= 5:
00087                     try:
00088                         time.sleep (1)
00089                         connection._connect ()
00090                         break
00091                     except RuntimeError:
00092                         # Connection failed
00093                         attempt += 1
00094 
00095             try:
00096 
00097                 self.op = TimedOperation (set_settings,
00098                                           (self.authconn, settings),
00099                                           parent=parent)
00100                 self.op.run ()
00101                 self.persistent_answers['error_log_debug_logging_unset'] = True
00102             except cups.IPPError:
00103                 pass
00104 
00105         if checkpoint != None:
00106             self.op = TimedOperation (fetch_log,
00107                                       (self.authconn,),
00108                                       parent=parent)
00109             tmpfname = self.op.run ()
00110             if tmpfname != None:
00111                 f = file (tmpfname)
00112                 f.seek (checkpoint)
00113                 lines = f.readlines ()
00114                 os.remove (tmpfname)
00115                 self.answers = { 'error_log': map (lambda x: x.strip (),
00116                                                    lines) }
00117 
00118         return False
00119 
00120     def collect_answer (self):
00121         answers = self.persistent_answers.copy ()
00122         answers.update (self.answers)
00123         return answers
00124 
00125     def cancel_operation (self):
00126         self.op.cancel ()
00127 
00128         # Abandon the CUPS connection and make another.
00129         answers = self.troubleshooter.answers
00130         factory = answers['_authenticated_connection_factory']
00131         self.authconn = factory.get_connection ()
00132         self.answers['_authenticated_connection'] = self.authconn