
MNc           @   s  d  Z  d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l Z d d l	 Z	 d d l
 Z
 d d l Z d d l Z d d l Z d d l m Z e j d d  Z d   Z d e d  Z d   Z d e d	  Z d d d
  Z d   Z d   Z d   Z d   Z d   Z d   Z  d e j! d  Z" d   Z# d e j! d  Z$ d   Z% d   Z& d   Z' d   Z( d Z) d Z* d Z+ d Z, d Z- d Z. d Z/ d Z0 d  Z1 d! Z2 d" Z3 d# Z4 d$   Z5 d%   Z6 d d&  Z7 d'   Z8 d(   Z9 d)   Z: d*   Z; d+   Z< d,   Z= d-   Z> d.   Z? d/   Z@ d0   ZA d1 d2  ZB d3   ZC d4   ZD d5   ZE eF d6 k rd d lG ZG d7 eG jH f d8     YZI eG jJ   n  d S(9   s/   Convenience functions for use in package hooks.iN(   t   impls   #/-_+ s   ....._c         C   s   |  j  t  S(   s   Generate a valid report key name from a file path.
        
    This will replace invalid punctuation symbols with valid ones.
    (   t	   translatet   _path_key_trans(   t   path(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   path_to_key    s    c         C   sA   | s t  |  } n  t j j |  r= t |  | | |  n  d S(   s  Attach file contents if file exists.

    If key is not specified, the key name will be derived from the file
    name with path_to_key().

    If overwrite is True, an existing key will be updated. If it is False, a
    new key with '_' appended will be added instead.
    N(   R   t   osR   t   existst   attach_file(   t   reportR   t   keyt	   overwrite(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_file_if_exists'   s    	c         C   s?   y t  |   j   j   SWn t k
 r: } d t |  SXd S(   s   Return the contents of the specified path. 
        
    Upon error, this will deliver a text representation of the error,
    instead of failing.
    s   Error: N(   t   opent   readt   stript	   Exceptiont   str(   R   t   e(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt	   read_file6   s    c         C   sR   | s t  |  } n  | s> x  |  j |  r: | d 7} q Wn  t |  |  | <d S(   s
  Attach a file to the report.

    If key is not specified, the key name will be derived from the file
    name with path_to_key().

    If overwrite is True, an existing key will be updated. If it is False, a
    new key with '_' appended will be added instead.
    t   _N(   R   t   has_keyR   (   R   R   R	   R
   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyR   A   s    	c         C   s  y. t  j d d d | g d t  j d t } Wn t k
 rN } d t |  SX| j   d } | j d k rr d SxP| j   D]B} | s q n  | j	   j
   d	  \ } }	 | r | | k r q n  d
 t |  }
 t j j |  rt |  j   } t j   } | j |  | j   } | |	 k r| rc| j d |  } | rV| |  |
 <qmd |  |
 <n
 d |  |
 <t j |  } t j j | j  } d t |  } | j   |  | <qq d |  |
 <q Wd S(   s:   Attach information about any modified or deleted conffiless
   dpkg-querys   -Ws   --showformat=${Conffiles}t   stdoutt	   close_fdss   Error: i    Ni   s   modified.conffile.sn   It seems you have modified the contents of '%s'.  Would you like to add the contents of it to your bug report?s
   [modified]s   mtime.conffile.s	   [deleted](   t
   subprocesst   Popent   PIPEt   Truet   OSErrorR   t   communicatet
   returncodet
   splitlinesR   t   splitR   R   R   R   R   R   t   hashlibt   md5t   updatet	   hexdigestt   yesnot   statt   datetimet   fromtimestampt   st_mtimet	   isoformat(   R   t   packaget	   conffilest   uit   dpkgR   t   outt   lineR   t   default_md5sumR	   t   contentst   mt   calculated_md5sumt   responset   statinfot   mtimet	   mtime_key(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_conffilesS   s@    
c   
      C   s%  y. t  j d d d | g d t  j d t } Wn t k
 rN } d t |  SX| j   d } | j d k rr d Sx | j   D] } | s q n  | j	   j
   d } t j j |  r d	 | k r | j d
 d  } | j d	 d  } t j j |  rt |  j   }	 |	 |  | <qq q Wd S(   s3   Attach information about any Upstart override filess
   dpkg-querys   -Ws   --showformat=${Conffiles}R   R   s   Error: i    Ns
   /etc/init/s   .confs	   .overridet    (   R   R   R   R   R   R   R   R   R   R   R   R   R   R   t   replaceR   R   (
   R   R*   R-   R   R.   R/   t   conffilet   overridet   titleR1   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_upstart_overrides   s$    c         C   s   y5 |  j  d d  j   s4 t d  j   |  d <n  Wn t k
 rH n X|  j  d d  j   s t d d d g  } | j d  } | d	 k r | | d
 } n  | |  d <n  d S(   sw   Attach information from the kernel ring buffer (dmesg).

    This will not overwrite already existing information.
    t	   BootDmesgR9   s   /var/log/dmesgt   CurrentDmesgt   shs   -cs1   dmesg | comm -13 --nocheck-order /var/log/dmesg -s   
[ii   N(   t   getR   R   R   t   IOErrort   command_outputt   find(   R   t   dmesgt   first_newline(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_dmesg   s    c         C   s   d } t  j j |  r x t  j |  D] } d | | f } t  j |  } t j | j  s( | j d @d k ry q( n  | d
 k r q( n  y t |  j   j	   } Wn t
 t f k
 r q( n X| r( | |  d | j d d	  <q( q( Wn  d  S(   Ns   /sys/class/dmi/ids   %s/%si   i    t	   subsystemt   uevents   dmi.R   t   .(   RI   RJ   (   R   R   t   isdirt   listdirR%   t   S_ISREGt   st_modeR   R   R   R   RC   R:   (   R   t   dmi_dirt   ft   pt   stt   value(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt
   attach_dmi   s    &c         C   s+  t  |   t |  d d  t |  d d  t |  d d  t |  d d  t |  d	 d
  t j j d  r t d d g  |  d <n  t d g  |  d <t d d d g  |  d <t j d d |  d
  |  d
 <t j d d |  d
  |  d
 <t j d d |  d
  |  d
 <t j d d |  d  |  d <t j d d |  d  |  d <t j d d |  d  |  d <t |   d |  k rd |  k rd |  d |  d f |  d <n  t	 d  rt d g  |  d  <n  t	 d!  r't d! d" g  j
   } | r| |  d# <n  t d! d$ g  j
   } | r'| |  d% <q'n  d& S('   sP  Attach a standard set of hardware-related data to the report, including:

    - kernel dmesg (boot and current)
    - /proc/interrupts
    - /proc/cpuinfo
    - /proc/cmdline
    - /proc/modules
    - lspci -vvnn
    - lsusb
    - devices from udev
    - DMI information from /sys
    - prtconf (sparc)
    - pccardctl status/ident
    s   /proc/interruptst   ProcInterruptss   /proc/cpuinfot   ProcCpuinfos   /proc/cmdlinet   ProcKernelCmdLines   /proc/modulest   ProcModuless   /var/log/udevt   UdevLogs   /sys/bus/pcit   lspcis   -vvnnt   Lspcit   lsusbt   Lsusbt   udevadmt   infos   --export-dbt   UdevDbs   ID_FS_LABEL=(.*)s   ID_FS_LABEL=<hidden>s   ID_FS_LABEL_ENC=(.*)s   ID_FS_LABEL_ENC=<hidden>s   by-label/(.*)s   by-label/<hidden>s   dmi.sys.vendors   dmi.product.names   %s %st   MachineTypet   prtconft   Prtconft	   pccardctlt   statust   PccardctlStatust   identt   PccardctlIdentN(   RH   R   R   R   R   RD   t   ret   subRU   t   command_availableR   (   R   R.   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_hardware   s:    


c   
      C   s  t  |  t j j d  d  t  |  t j j d  d  t  |  d  t  |  d d  t |  d d	  t d
 d d g  |  d <t d d g  |  d <t d d g  |  d <t t  |  d <g  } t j j d  r$xO t	 d  D]> } d | k r | j
   j   } | j t | d   q q Wn  xV| D]N} d | } t d d t |  d g  |  | <d | } t d d t |  g  |  | <x t j d |  D] } t j j |  rt j j |  } d | t |  f } t |  | d | qt j j |  rt j j |  } x^ t j |  D]J } t j j | |  }	 d | t |  t |  f } t |  |	 |  q$WqqWq+Wt d d  g t j d!  t j d"  t j d#   |  d$ <t j j d%  rt d& d' g  |  d( <t d& d) g  |  d* <n  t |   t |   d+ S(,   sv   Attach ALSA subsystem information to the report.

    (loosely based on http://www.alsa-project.org/alsa-info.sh)
    s   ~/.asoundrct   UserAsoundrcs   ~/.asoundrc.asoundconft   UserAsoundrcAsoundconfs   /etc/asound.confs   /proc/asound/versiont   AlsaVersions   /proc/cpuinfoRW   t   lss   -ls	   /dev/snd/t   AlsaDevicest   aplayt   AplayDevicest   arecordt   ArecordDevicest   PciMultimedias   /proc/asound/cardss   ]:i    s   Card%d.Amixer.infot   amixers   -cR`   s   Card%d.Amixer.valuess   /proc/asound/card%d/codec*s   Card%d.Codecs.%sR	   s   Card%d.Codecs.%s.%st   fusers   -vs	   /dev/dsp*s
   /dev/snd/*s	   /dev/seq*t   AudioDevicesInUses   /usr/bin/pacmdt   pacmds
   list-sinkst
   PulseSinkss   list-sourcest   PulseSourcesN(   R   R   R   t
   expanduserR   RD   t   pci_devicest   PCI_MULTIMEDIAR   R   t   lstripR   t   appendt   intR   t   globt   isfilet   basenameR   RL   RM   t   joinRU   RH   (
   R   t   cardsR/   t   fieldst   cardR	   t	   codecpatht   codect   nameR   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_alsa   sP    !
"
:
c         C   s   d t  j k r t St  j d } xf | j t  j  D]R } | sE q3 n  t  j j | |   } t  j j |  r3 t  j | t  j	  r3 t
 Sq3 Wt S(   s/   Is given command on the executable search path?t   PATH(   R   t   environt   FalseR   t   pathsepR   R   R   t   accesst   X_OKR   (   t   commandR   t   elementt   filename(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyRl   2  s    'c      
   C   s   t  j j   } d | d <y. t j |  d t j d | d t d | } Wn t k
 rg } d t |  SX| j	 |  d } | j
 d k r | j   Sd	 t |   | j
 | f Sd
 S(   s   Try to execute given command (array) and return its stdout. 
    
    In case of failure, a textual error gets returned. This function forces
    LC_MESSAGES to C, to avoid translated output in bug reports.
    t   Ct   LC_MESSAGESR   t   stderrR   t   envs   Error: i    s.   Error: command %s failed with exit code %i: %sN(   R   R   t   copyR   R   R   R   R   R   R   R   R   (   R   t   inputR   R   t   spR   R.   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyRD   ?  s    

c       
   C   sN  t  j   d k r g  }  n/t  j d  r t j d d g d t j d t j d k r t j d d d	 t t  j    d
 g d t j d t j d k r d d d d g }  n t  j d  rAt j d d g d t j d t j d k rAt j d d d	 t t  j    d g d t j d t j d k rAd d d d g }  n	 d g }  |  S(   Ni    t   DISPLAYt   whicht   kdesudoR   R   t   pgreps   -xs   -ut	   ksmservers	   --desktops/   /usr/share/applications/apport-kde-mime.desktops   --t   gksus   gnome-panel|gconfd-2s   -Dt   Apportt   sudo(   R   t   getuidt   getenvR   t   callR   R   (   t   prefix(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   _root_command_prefixT  s     	''	c         C   s8   t  |   t  g   k s! t d  t t   |  | |  S(   s   Try to execute given command (array) as root and return its stdout. 

    This passes the command through gksu, kdesudo, or sudo, depending on the
    running desktop environment.
    
    In case of failure, a textual error gets returned.
    s   command must be a list(   t   typet   AssertionErrorRD   R   (   R   R   R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   root_command_outputh  s    !c         C   s^  t  j   } z=t j j | d  } t | d  } xX | j   D]J \ } } t | d  sd t d  | j	 d | t j j | |  f  q@ W| j
   t j j   } d | d <d | d	 <t j t   d
 | g d t d | } | j   xX | D]P } t t j j | |   }	 |	 j   j   }
 |
 r:|
 |  | <n  |	 j
   q WWd t j |  Xd S(   sP  Execute multiple commands as root and put their outputs into report.

    command_map is a keyname -> 'shell command' dictionary with the commands to
    run. They are all run through /bin/sh, so you need to take care of shell
    escaping yourself. To include stderr output of a command, end it with
    "2>&1".

    Just like root_command_output() this will use gksu, kdesudo, or sudo for
    gaining root privileges, depending on the running desktop environment.

    This is preferrable to using root_command_output() multiple times, as that
    will ask for the password every time.
    s   :script:t   wR   s(   command must be a string (shell command)s   %s | cat > %s
R   R   R9   t   LANGUAGEs   /bin/shR   R   N(   t   tempfilet   mkdtempR   R   R   R   t   itemst   hasattrR   t   writet   closeR   R   R   R   R   R   t   waitR   R   t   shutilt   rmtree(   R   t   command_mapt   workdirt   script_patht   scriptt   keynameR   R   R   RQ   t   buf(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_root_command_outputss  s*    *



c         C   s   t  d |   S(   sc   Extract recent messages from syslog which match a regex.

    pattern should be a "re" object.
    s   /var/log/syslog(   t   recent_logfile(   t   pattern(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   recent_syslog  s    c         C   sV   d } y7 x0 t  |   D]" } | j |  r | | 7} q q WWn t k
 rQ g  SX| S(   sf   Extract recent messages from a logfile which match a regex.

    pattern should be a "re" object.
    R9   (   R   t   searchRC   (   t   logfileR   t   linesR/   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyR     s    c         C   se   t  j j d  } t  j j |  s( d Sd } x0 t |  D]" } |  j |  r; | | 7} q; q; W| S(   sp   Extract messages from ~/.xsession-errors which match a regex.
        
    pattern should be a "re" object.
    s   ~/.xsession-errorsR9   N(   R   R   R~   R   t   NoneR   R   (   R   R   R   R/   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   xsession_errors  s    i   i   i   i   i   i   i   i   i	   i
   i   i   c          G   sG  |  s t  d d g  Sd } t  d d g  } x| j d  D]} d } d } d } x | j d  D] } | j d d  \ } }	 |	 j   }	 | j   } | d	 k r t |	 d
 d !d  }
 |
 d @d ?} |
 d @} qf | d k rf |	 } qf qf W| r> | r> | |  k r> | r| d 7} n  | t  d d | g  j   7} q> q> W| S(   s9   Return a text dump of PCI devices attached to the system.R[   s   -vvnnR9   s   -vvmmnns   

s   
t   :i   t   Classiii   i   i   i   t   Slots   -vvnnsN(   RD   R   R   R   R   (   t   pci_classest   resultt   outputt	   paragrapht	   pci_classt   pci_subclasst   slotR/   R	   RT   t   n(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyR     s.    &c           C   s   t  d d g  S(   s9   Return a text dump of USB devices attached to the system.R]   s   -v(   RD   (    (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   usb_devices  s    c         C   sP   t  j |   } | rF g  | D]! } t j j | |  r | ^ q } n | } | S(   sF   Retrieve a list of files owned by package, optionally matching globpat(   t	   packagingt	   get_filesR   t   fnmatch(   R*   t   globpatt   filesRQ   R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   files_in_package  s
    1c         C   s|  d d l  } d d l } | j   } i  } x t | d  D] } x t |  j   D] \ } } yK | j |  }	 |	 s} wS n  |	 j   }	 | s |	 | k r |	 | | <n  WqS | j k
 r t	 d d | g  }	 |	 | k r |	 | | <q qS XqS Wq: W| rxd }
 | j
   } | j   x, | D]$ } | | }	 |
 d | |	 f 7}
 q Wd |  k rkd	 |  d |
 f }
 n  |
 |  d <n  d S(
   s>   Attach information about gconf keys set to non-default values.iNs"   /usr/share/gconf/schemas/*.schemass   gconftool-2s   -gR9   s   %s=%s
t   GConfNonDefaults   %s
%s(   t   gconft   glibt   client_get_defaultR   t   _parse_gconf_schemaR   RB   t	   to_stringt   GErrorRD   t   keyst   sort(   R   R*   R   R   t   clientt   non_defaultst   schema_fileR	   t   default_valueRT   t   sR   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_gconf   s8    	

c         C   s   t  d d g  |  d <t  d d g  |  d <t t  |  d <t |  d d d	 x1 d D]) } | t j k rV t j | |  | <qV qV Wd S(   s5   Attach generic network-related information to report.t   ipt   routet   IpRoutet   addrt   IpAddrt
   PciNetworks   /etc/network/interfacesR	   t   IfupdownConfigt
   http_proxyt	   ftp_proxyt   no_proxyN(   R   R   R   (   RD   R   t   PCI_NETWORKR   R   R   (   R   t   var(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_network)  s    c         C   s   t  t j d   |  d <t j d d t d g   |  d <t d d g  |  d	 <t d
 d d g  |  d <t |  d d d d S(   s5   Attach wireless (WiFi) network information to report.sH   (NetworkManager|modem-manager|dhclient|kernel|wpa_supplicant)(\[\d+\])?:t
   WifiSyslogs   Encryption key:(.*)s   Encryption key: <hidden>t   iwconfigt   IwConfigt   rfkillt   listt   RfKillt   iwt   regRB   t   CRDAs   /var/log/wpa_supplicant.logR	   t   WpaSupplicantLogN(   R   Rj   t   compileRk   RD   R   (   R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_wifi5  s
    "c      !   C   s  t  |  d d  t  |  d d  t d g  |  d <t d d g  |  d	 <t j d
  } | r t d d d g |  } t j d d |  |  d <n  t d d d d d d d d d d d d d d d d  d! d" d# d$ d% d& d' d( d d) d* d+ d, d- d. d/   |  d0 <d1 S(2   sk   Attach printing information to the report.

    Based on http://wiki.ubuntu.com/PrintingBugInfoScript.
    s   /etc/papersizet	   Papersizes   /var/log/cups/error_logt   CupsErrorLogt   localet   Localet   lpstats   -vt   Lpstats   /etc/cups/ppd/*.ppdt   fgreps   -Hs	   *NickNames*   /etc/cups/ppd/(.*).ppd:\*NickName: *"(.*)"s   \g<1>: \g<2>t   PpdFilest   foo2zjss   foomatic-dbs   foomatic-db-engines   foomatic-db-gutenprints   foomatic-db-hpijss   foomatic-filterss   foomatic-guit   hpijst   hplipt   m2300wt   min12xxwt   c2050t   hpojt   pxljrt   pnm2ppat   splixs   hp-ppds
   hpijs-ppdss   linuxprinting.org-ppdss   openprinting-ppdss   openprinting-ppds-extrat   ghostscriptt   cupss   cups-driver-gutenprintt   ijsgutenprints   cupsys-driver-gutenprints   gimp-gutenprints   gutenprint-docs   gutenprint-localess   system-config-printer-commont   kdeprintt   PrintingPackagesN(   R   RD   R   Rj   Rk   t   package_versions(   R   t   ppdst	   nicknames(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_printing?  s$    					c         C   sM  d } t  j | t  j  } d } t  j | t  j  } t j j d  r^ t d |  |  d <n( t j j d  r t d |  |  d <n  t j j d  r t |  i d | d d	 6 n  t |  d
 d  t |  d d  t  j	 | |  j
 d d   st  j	 | |  j
 d	 d   rI|  j
 d d  } | r8| d 7} n  | d |  d <n  d S(   s0   Attach MAC information and events to the report.s!   audit\(|apparmor|selinux|securitys   apparmor="DENIED"s   /var/log/kern.logt   KernLogs   /var/log/messagess   /var/run/auditd.pids   egrep "s   " /var/log/audit/audit.logt   AuditLogs   /proc/version_signaturet   ProcVersionSignatures   /proc/cmdlinet   ProcCmdlineR9   t   Tagst    t   apparmorN(   Rj   R  t
   IGNORECASER   R   R   R   R   R   R   RB   (   R   t	   mac_regext   mac_ret   aa_denied_regext   aa_denied_ret   tags(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_mac_eventsY  s"    <c         C   s   t  |   |  d <d S(   si   Attach version information for related packages

    In the future, this might also run their hooks.
    t   RelatedPackageVersionsN(   R  (   R   t   packages(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_related_packagesr  s    c          G   s  g  } x |  D] } | s q n  t  j |  } | sJ | j | d f  n  xi t |  D][ } y t  j |  } Wn t k
 r d } n X| d k r d } n  | j | | f  qW Wq Wt t g  t	 |   D] } t t
 |  ^ q  \ } } d | }	 d j g  | D] }
 |	 |
 ^ q S(   sx   Return a text listing of package names and versions.
    
    Arguments may be package names or globs, e. g. "foo*"
    s   N/As
   %%-%ds %%ss   
N(   R   t   package_name_globR   t   sortedt   get_versiont
   ValueErrorR   t   mapt   maxt   zipt   lenR   (   R-  t   versionst   package_patternt   matching_packagesR*   t   versiont   tt   package_widtht   version_widtht   fmtt   v(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyR  y  s$    
	7
c         C   s   t    } xo t d |  g  j d  D]R } y | j d d  \ } } Wn t k
 r] q% n X| j   } | j |  q% W| S(   ss   Returns a list of strings containing the sonames of shared libraries
    with which the specified binary is linked.t   ldds   
s   =>i   (   t   setRD   R   R2  R   t   add(   R   t   libsR/   t   libt   rest(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   shared_libraries  s    	"c         C   sH   t  |   } | | k r t Sx% | D] } | j | d  r# t Sq# Wt S(   s   Returns True if the binary at path links with the library named lib.

    path should be a fully qualified path (e.g. report['ExecutablePath'])
    lib may be of the form 'lib<name>' or 'lib<name>.so.<version>'
    s   .so.(   RF  R   t
   startswithR   (   R   RD  RC  t
   linked_lib(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   links_with_shared_library  s      c         C   s   yN t  j d |  g d t  j d t  j } | j   d } | j d k rM d SWn t k
 rb d
 SXx\ | j   D]N } | j d d  } t	 |  d k  r qp n  | d d	 k rp | d j
   Sqp Wd
 S(   s-   Return the license for a given kernel module.s   /sbin/modinfoR   R   i    t   invalidR   i   i   t   licenseN(   R   R   R   R   R   R   R   R   R   R6  R   (   t   modulet   modinfoR.   t   lR   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   _get_module_license  s    s   /proc/modulesc         C   s   y- g  t  |   D] } | j   d ^ q } Wn t k
 rA g  SXg  } xa | D]Y } t |  } | rO d | k p d | k p d | k p d | k rO | j |  qO qO W| S(   sC   Check loaded modules and return a list of those which are not free.i    t   GPLt   BSDt   MPLt   MIT(   R   R   RC   RO  R   (   t   module_listRN  t   modst   nonfreeR2   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   nonfree_kernel_modules  s    -7c   	      C   sy  i  } t  j j j |   } xW| j d  D]F} x=| j d  D],} x#| j d  D]} y! | j d  d j d j } WnJ t k
 r | j d  d j d j } | j d  r | d } q n X| j d	  d j d j } yc | j d
  d j d j } | d k r@| j	   d k r3d | | <qJd | | <n
 | | | <WqW t k
 rhd | | <qW XqW WqA Wq+ W| S(   Nt   gconfschemafilet
   schemalistt   schemat   applytoi    R	   s	   /schemas/i   R   t   defaultt   boolt   truet   falseR9   (
   t   xmlt   domt   minidomt   parset   getElementsByTagNamet
   childNodest   datat
   IndexErrorRG  t   lower(	   R   t   retRa  RX  RY  RZ  R	   R   R\  (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyR     s,    !c         C   s   d } x t  j |   D] } t  j j |  |  } | d k s t  j j |  rV q n  t |  j   j   } | d k r | j d d  } n  | d k r t	 j
 |  } | d 7} n  | d | | f 7} q W| S(	   NR9   RJ   t   modess   
R#  t   edids   -base64s   %s: %s
(   R   RM   R   R   R   R   R   R   R:   t   base64t	   b64encode(   t   conR`   RQ   R   t   val(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   __drm_con_info  s    c         C   s   d } t  j j |  s d Sxj t  j |  D]Y } t  j j | |  } t  j j t  j j | d   r, t |  |  d t |  <q, q, Wd S(   sW   Add information about DRM hardware.

    Collect information from /sys/class/drm/.
    s   /sys/class/drmNt   enableds   DRM.(   R   R   RL   RM   R   R   Rp  R   (   R   t   drm_dirRQ   Rn  (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   attach_drm_info
  s    !t   __main__t   _Tc           B   s>   e  Z d    Z d   Z d   Z d   Z d   Z d   Z RS(   c         C   s   d   } | d  } | d  } |  j  d t d  k  |  j t d  d  |  j  d t | j  k  |  j  d t | j  k  t j   } | j d | j | j f  | j   t | j  } |  j	 d | k  |  j  d | k  |  j	 | j | k  |  j  | j | k  d S(	   s+   module licenses can be validated correctly.c         S   sv   t  j d d |  d d  } | j d |   | j   t  j d d |  d d  } t j d | j d | j g  | S(	   NR   s   %s-t   suffixs   .Ss'   .section .modinfo
.string "license=%s"
s   .kos   /usr/bin/ass   -o(   R   t   NamedTemporaryFileR   t   flushR   R   R   (   RK  t   asmt   ko(    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt	   _build_ko$  s    	
	RP  t   BADt   isofss   does-not-existRJ  s   isofs
does-not-exist
%s
%s
N(
   t
   assertTrueRO  t   assertEqualR   R   Rw  R   Rx  RW  t   assertFalse(   t   selfR{  t   good_kot   bad_koRQ   RV  (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_module_license_evaluation!  s     	
		
c         C   se   i  } t  |  |  j | d j d   |  j t | d  d k  |  j | d j d   d S(   s7   attach_dmesg() does not overwrite already existing dataR?   t   [i  R@   N(   RH   R~  RG  R6  (   R  R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_attach_dmesgH  s
    
c         C   s   i d d 6} t  |  |  j | d d  d  |  j | d j d   i d d 6d d 6} t  |  |  j | d d  |  j | d d  d S(   s7   attach_dmesg() does not overwrite already existing datat   existingbootR?   i2   R@   R  t   existingcurrentN(   RH   R  R~  RG  (   R  R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_dmesg_overwriteR  s    

c         C   s  t  d   } | j   j   } Wd QXt  d   } | j   j   } Wd QXi  } t | d  |  j | j   d g  |  j | d |  i  } t | d d  |  j | j   d g  |  j | d |  i  } t | d  |  j | j   d g  |  j | d j d   i  } t | d  t | d  |  j | j   d g  |  j | d |  t | d d d	 t |  j t	 | j    d d
 g  |  j | d |  |  j | d
 |  d S(   s   attach_file()s	   /etc/motdNs
   /etc/issues	   .etc.motdt   Motds   /nonexistings   .nonexistings   Error: R
   s
   .etc.motd_(
   R   R   R   R   R  R   R~  RG  R   R0  (   R  RQ   t   motd_contentst   issue_contentsR   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_attach_filea  s2    "c         C   s   t  d   } | j   j   } Wd QXi  } t | d  |  j | j   d g  |  j | d |  i  } t | d d  |  j | j   d g  |  j | d |  i  } t | d  |  j | j   g   d S(   s   attach_file_if_exists()s	   /etc/motdNs	   .etc.motdR  s   /nonexisting(   R   R   R   R   R  R   (   R  RQ   R  R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_attach_file_if_exists  s    c         C   s<   i  } t  |  t |  t |  t |  t |  d S(   s%   functions do not crash (very shallow)N(   Rm   R   R   R  R  (   R  R   (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   test_no_crashes  s    



(   t   __name__t
   __module__R  R  R  R  R  R  (    (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyRu     s   	'	
		&	(K   t   __doc__R   R    R   R&   R   Rj   t   stringR%   Rl  R   R   t   xml.domR`  t   xml.dom.minidomt   packaging_implR    R   t	   maketransR   R   R   R   R   R   R   R8   R>   RH   RU   Rm   R   Rl   t   STDOUTRD   R   R   R   R   R   R   t   PCI_MASS_STORAGER   t   PCI_DISPLAYR   t
   PCI_MEMORYt
   PCI_BRIDGEt   PCI_SIMPLE_COMMUNICATIONSt   PCI_BASE_SYSTEM_PERIPHERALSt   PCI_INPUT_DEVICESt   PCI_DOCKING_STATIONSt   PCI_PROCESSORSt   PCI_SERIAL_BUSR   R   R   R   R   R  R  R+  R.  R  RF  RI  RO  RW  R   Rp  Rs  R  t   unittestt   TestCaseRu  t   main(    (    (    s4   /usr/lib/python2.7/dist-packages/apport/hookutils.pyt   <module>   s   		0				6	;			,					
	)		
										