home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / apport / package-hooks / source_network-manager.py < prev    next >
Encoding:
Python Source  |  2009-04-14  |  3.2 KB  |  97 lines

  1. '''Apport package hook for Network Manager
  2.  
  3. (c) 2008 Canonical Ltd.
  4. Contributors:
  5. Matt Zimmerman <mdz@canonical.com>
  6. Martin Pitt <martin.pitt@canonical.com>
  7.  
  8. This program is free software; you can redistribute it and/or modify it
  9. under the terms of the GNU General Public License as published by the
  10. Free Software Foundation; either version 2 of the License, or (at your
  11. option) any later version.  See http://www.gnu.org/copyleft/gpl.html for
  12. the full text of the license.
  13. '''
  14.  
  15. import os
  16. import subprocess
  17.  
  18. # TODO:
  19. # - HAL info for modem devices
  20. # - Trim down HAL dumps to only the relevant info
  21.  
  22. attach_files = { 'IfupdownConfig' : '/etc/network/interfaces',
  23.                  'WpaSupplicantLog' : '/var/log/wpa_supplicant.log' }
  24.                  
  25. attach_commands = { 'Syslog' : ['egrep',' (NetworkManager|dhclient|kernel):','/var/log/syslog'],
  26.                     'Gconf' : ['gconftool-2','-R','/system/networking']
  27.                     }
  28.  
  29. def _command_output(command, input = None, stderr = subprocess.STDOUT):
  30.     '''Try to execute given command (array) and return its stdout, or return
  31.     a textual error if it failed.'''
  32.  
  33.     try:
  34.        sp = subprocess.Popen(command,
  35.                              stdout=subprocess.PIPE, stderr=stderr, close_fds=True)
  36.     except OSError, e:
  37.        return 'Error: ' + str(e)
  38.  
  39.     out = sp.communicate(input)[0]
  40.     if sp.returncode == 0:
  41.        return out.strip()
  42.     else:
  43.        return 'Error: command %s failed with exit code %i: %s' % (
  44.            str(command), sp.returncode, out)
  45.  
  46. def _read_file(file):
  47.     try:
  48.         return open(file).read().strip()
  49.     except Exception, e:
  50.         return 'Error: ' + str(e)
  51.  
  52. def _network_interfaces():
  53.     output = _command_output(['hal-find-by-capability','--capability','net'])
  54.     udis = output.split('\n')
  55.     interfaces = {}
  56.     for udi in udis:
  57.         interface = _command_output(['hal-get-property','--udi',udi,'--key','net.interface'])
  58.         device = _command_output(['hal-get-property','--udi',udi,'--key','net.originating_device'])
  59.  
  60.         interfaces[interface] = device
  61.  
  62.     return interfaces
  63.                 
  64. def _device_details(device):
  65.     return _command_output(['lshal','-u',device])
  66.  
  67. def add_info(report):
  68.     for name, path in attach_files.items():
  69.         if os.path.exists(path):
  70.             report[name] = _read_file(path)
  71.  
  72.     for name, command in attach_commands.items():
  73.         output = _command_output(command)
  74.         report[name] = output
  75.  
  76.     for interface, device in _network_interfaces().items():
  77.         key = 'NetDevice.%s' % interface
  78.         report[key] = _device_details(device)
  79.  
  80.     # It would be neat if we could just set the driver-* tags in LP
  81.     # per https://wiki.ubuntu.com/DebuggingNetworkManager
  82.     interesting_modules = ['ndiswrapper','ath_hal','b44']
  83.     interesting_modules_loaded = []
  84.     for line in open('/proc/modules'):
  85.         module = line.split()[0]
  86.         if module in interesting_modules:
  87.             interesting_modules_loaded.append(module)
  88.  
  89.     if interesting_modules_loaded:
  90.         report['InterestingModules'] = ' '.join(interesting_modules_loaded)
  91.  
  92. if __name__ == '__main__':
  93.     report = {}
  94.     add_info(report)
  95.     for key in report:
  96.         print '%s: %s' % (key, report[key].split('\n', 1)[0])
  97.