home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / system-config-printer / troubleshoot / ErrorLogCheckpoint.py < prev    next >
Encoding:
Python Source  |  2009-05-05  |  7.8 KB  |  227 lines

  1. #!/usr/bin/env python
  2.  
  3. ## Printing troubleshooter
  4.  
  5. ## Copyright (C) 2008 Red Hat, Inc.
  6. ## Copyright (C) 2008 Tim Waugh <twaugh@redhat.com>
  7.  
  8. ## This program is free software; you can redistribute it and/or modify
  9. ## it under the terms of the GNU General Public License as published by
  10. ## the Free Software Foundation; either version 2 of the License, or
  11. ## (at your option) any later version.
  12.  
  13. ## This program is distributed in the hope that it will be useful,
  14. ## but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. ## GNU General Public License for more details.
  17.  
  18. ## You should have received a copy of the GNU General Public License
  19. ## along with this program; if not, write to the Free Software
  20. ## Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  
  22. import cups
  23. import os
  24. import tempfile
  25. import time
  26. from timedops import TimedOperation
  27. from base import *
  28. class ErrorLogCheckpoint(Question):
  29.     def __init__ (self, troubleshooter):
  30.         Question.__init__ (self, troubleshooter, "Error log checkpoint")
  31.         page = self.initial_vbox (_("Debugging"),
  32.                                   _("I would like to enable debugging output "
  33.                                     "from the CUPS scheduler.  This may "
  34.                                     "cause the scheduler to restart.  Click "
  35.                                     "the button below to enable debugging."))
  36.         button = gtk.Button (_("Enable Debugging"))
  37.         buttonbox = gtk.HButtonBox ()
  38.         buttonbox.set_border_width (0)
  39.         buttonbox.set_layout (gtk.BUTTONBOX_START)
  40.         buttonbox.pack_start (button, False, False, 0)
  41.         self.button = button
  42.         page.pack_start (buttonbox, False, False, 0)
  43.         self.label = gtk.Label ()
  44.         self.label.set_alignment (0, 0)
  45.         self.label.set_line_wrap (True)
  46.         page.pack_start (self.label, False, False, 0)
  47.         troubleshooter.new_page (page, self)
  48.         self.persistent_answers = {}
  49.  
  50.     def __del__ (self):
  51.         if not self.persistent_answers.get ('error_log_debug_logging_set',
  52.                                             False):
  53.             return
  54.  
  55.         c = self.troubleshooter.answers['_authenticated_connection']
  56.         c._set_lock (False)
  57.         settings = c.adminGetServerSettings ()
  58.         if len (settings.keys ()) == 0:
  59.             return
  60.  
  61.         settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '0'
  62.         answers = self.troubleshooter.answers
  63.         orig_settings = self.persistent_answers['cups_server_settings']
  64.         settings['MaxLogSize'] = orig_settings.get ('MaxLogSize', '2000000')
  65.         c.adminSetServerSettings (settings)
  66.  
  67.     def display (self):
  68.         self.answers = {}
  69.         answers = self.troubleshooter.answers
  70.         if not answers['cups_queue_listed']:
  71.             return False
  72.  
  73.         self.authconn = answers['_authenticated_connection']
  74.         parent = self.troubleshooter.get_window ()
  75.  
  76.         def getServerSettings ():
  77.             # Fail if auth required.
  78.             cups.setPasswordCB (lambda x: '')
  79.             cups.setServer ('')
  80.             c = cups.Connection ()
  81.             return c.adminGetServerSettings ()
  82.  
  83.         try:
  84.             self.op = TimedOperation (getServerSettings, parent=parent)
  85.             settings = self.op.run ()
  86.         except RuntimeError:
  87.             return False
  88.         except cups.IPPError:
  89.             settings = {}
  90.  
  91.         self.forward_allowed = False
  92.         self.label.set_text ('')
  93.         if len (settings.keys ()) == 0:
  94.             # Requires root
  95.             return True
  96.         else:
  97.             self.persistent_answers['cups_server_settings'] = settings
  98.  
  99.         try:
  100.             if int (settings[cups.CUPS_SERVER_DEBUG_LOGGING]) != 0:
  101.                 # Already enabled
  102.                 return False
  103.         except KeyError:
  104.             pass
  105.         except ValueError:
  106.             pass
  107.  
  108.         return True
  109.  
  110.     def connect_signals (self, handler):
  111.         self.button_sigid = self.button.connect ('clicked', self.enable_clicked,
  112.                                                  handler)
  113.  
  114.     def disconnect_signals (self):
  115.         self.button.disconnect (self.button_sigid)
  116.  
  117.     def collect_answer (self):
  118.         answers = self.troubleshooter.answers
  119.         if not answers['cups_queue_listed']:
  120.             return {}
  121.  
  122.         parent = self.troubleshooter.get_window ()
  123.         self.answers.update (self.persistent_answers)
  124.         (tmpfd, tmpfname) = tempfile.mkstemp ()
  125.         os.close (tmpfd)
  126.         try:
  127.             self.op = TimedOperation (self.authconn.getFile,
  128.                                       args=('/admin/log/error_log', tmpfname),
  129.                                       parent=parent)
  130.             self.op.run ()
  131.         except RuntimeError:
  132.             try:
  133.                 os.remove (tmpfname)
  134.             except OSError:
  135.                 pass
  136.  
  137.             return self.answers
  138.         except cups.IPPError:
  139.             try:
  140.                 os.remove (tmpfname)
  141.             except OSError:
  142.                 pass
  143.  
  144.             return self.answers
  145.  
  146.         statbuf = os.stat (tmpfname)
  147.         os.remove (tmpfname)
  148.         self.answers['error_log_checkpoint'] = statbuf[6]
  149.         return self.answers
  150.  
  151.     def can_click_forward (self):
  152.         return self.forward_allowed
  153.  
  154.     def enable_clicked (self, button, handler):
  155.         parent = self.troubleshooter.get_window ()
  156.         self.troubleshooter.busy ()
  157.         try:
  158.             self.op = TimedOperation (self.authconn.adminGetServerSettings,
  159.                                       parent=parent)
  160.             settings = self.op.run ()
  161.         except cups.IPPError:
  162.             self.troubleshooter.ready ()
  163.             self.forward_allowed = True
  164.             handler (button)
  165.             return
  166.  
  167.         self.persistent_answers['cups_server_settings'] = settings.copy ()
  168.         MAXLOGSIZE='MaxLogSize'
  169.         try:
  170.             prev_debug = int (settings[cups.CUPS_SERVER_DEBUG_LOGGING])
  171.         except KeyError:
  172.             prev_debug = 0
  173.         try:
  174.             prev_logsize = int (settings[MAXLOGSIZE])
  175.         except (KeyError, ValueError):
  176.             prev_logsize = -1
  177.  
  178.         if prev_debug == 0 or prev_logsize != '0':
  179.             settings[cups.CUPS_SERVER_DEBUG_LOGGING] = '1'
  180.             settings[MAXLOGSIZE] = '0'
  181.             success = False
  182.  
  183.             def set_settings (connection, settings):
  184.                 connection.adminSetServerSettings (settings)
  185.  
  186.                 # Now reconnect.
  187.                 attempt = 1
  188.                 while attempt <= 5:
  189.                     try:
  190.                         time.sleep (1)
  191.                         connection._connect ()
  192.                         break
  193.                     except RuntimeError:
  194.                         # Connection failed
  195.                         attempt += 1
  196.  
  197.             try:
  198.                 debugprint ("Settings to set: " + repr (settings))
  199.                 self.op = TimedOperation (set_settings,
  200.                                           args=(self.authconn, settings,),
  201.                                           parent=parent)
  202.                 self.op.run ()
  203.                 success = True
  204.             except cups.IPPError:
  205.                 pass
  206.             except RuntimeError:
  207.                 pass
  208.  
  209.             if success:
  210.                 self.persistent_answers['error_log_debug_logging_set'] = True
  211.                 self.label.set_text (_("Debug logging enabled."))
  212.         else:
  213.             self.label.set_text (_("Debug logging was already enabled."))
  214.  
  215.         self.forward_allowed = True
  216.         self.troubleshooter.ready ()
  217.         handler (button)
  218.  
  219.     def cancel_operation (self):
  220.         self.op.cancel ()
  221.  
  222.         # Abandon the CUPS connection and make another.
  223.         answers = self.troubleshooter.answers
  224.         factory = answers['_authenticated_connection_factory']
  225.         self.authconn = factory.get_connection ()
  226.         self.answers['_authenticated_connection'] = self.authconn
  227.