ó
ýGpNc           @   sÉ   d  Z  d d l Z d d l Z d Z d „  Z d „  Z d „  Z e d k rÅ 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 e j f d	 „  ƒ  YZ e j ƒ  n  d S(
   s:   Python sys.excepthook hook to generate apport crash dumps.iÿÿÿÿNs   /etc/default/apportc          C   sW   d d l  }  y2 t t ƒ j ƒ  } |  j d | |  j ƒ d k SWn t k
 rR t SXd S(   s4   Return whether Apport should generate crash reports.iÿÿÿÿNs   ^\s*enabled\s*=\s*0\s*$(	   t   ret   opent   CONFIGt   readt   searcht   Mt   Nonet   IOErrort   True(   R    t   conf(    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   enabled   s     c         C   sÙ  zµ|  t  f k r d St ƒ  s# d Sy d d l m } Wn! t k
 rZ d d l m } n Xd d l } d d l } d d l } d d l	 m
 } y2 t j j t j j t j ƒ  t j d ƒ ƒ } WnL t t t f k
 ry t j d t j ƒ  ƒ } Wqt k
 rd SXn Xt j | t j ƒ s9t j j | ƒ r=d S| | ƒ sMd Sd d l }	 |	 j j ƒ  }
 | ƒ  } | j |  | | d | ƒ| j ƒ  j  ƒ  |
 d <|
 j! ƒ  |
 j" ƒ  | |
 d	 <y d
 t j |
 d <Wn t k
 ræn X|
 j# ƒ  r÷d S| j$ d d | ƒ } t j% ƒ  } d | | f } t j j& | ƒ rc|	 j' j( | ƒ r\t j) | ƒ qcd Sn  t j* t j+ | t j, t j- Bt j. Bd ƒ d ƒ } z |
 j/ | ƒ Wd | j0 ƒ  XWd t rÔt j1 |  | | ƒ n  Xd S(   s1   Catch an uncaught exception and make a traceback.Niÿÿÿÿ(   t   StringIO(   t   likely_packagedi    s   /proc/%i/exet   filet	   Tracebackt   ExecutablePaths   %rt
   PythonArgst   /t   _s   /var/crash/%s.%i.crashi€  t   w(2   t   KeyboardInterruptR
   t	   cStringIOR   t   ImportErrort   ioR    t   tempfilet	   tracebackt   apport.fileutilsR   t   ost   patht   realpatht   joint   getcwdut   syst   argvt	   TypeErrort   AttributeErrort
   IndexErrort   readlinkt   getpidt   OSErrort   accesst   X_OKt   isfilet   apport.reportt   reportt   Reportt   print_exceptiont   getvaluet   stript   add_proc_infot   add_user_infot   check_ignoredt   subt   getuidt   existst	   fileutilst   seen_reportt   unlinkt   fdopenR   t   O_WRONLYt   O_CREATt   O_EXCLt   writet   closet   __excepthook__(   t   exc_typet   exc_objt   exc_tbR   R    R   R   R   t   binaryt   apportt   prt   tb_filet   mangled_programt   usert   pr_filenamet   report_file(    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   apport_excepthook   sd    	$2	)	


#c           C   s   t  t _ d S(   s   Install the python apport hook.N(   RL   R    t
   excepthook(    (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   install|   s    t   __main__t   _Tc           B   sV   e  Z d  „  Z d d	 d „ Z d „  Z d „  Z d „  Z d „  Z d „  Z	 d „  Z
 RS(
   c         C   s    |  j  t j j ƒ  g  d ƒ d S(   s   Check the test environment.s    No crash reports already presentN(   t   assertEqualRE   R7   t   get_all_reports(   t   self(    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_envŠ   s    t    c         C   s  | r+ | } t  j | t  j t  j Bƒ } n t j d t j j ƒ \ } } z® t  j	 | d | ƒ t  j
 | ƒ t  j | d ƒ t j | d d g d t j d t j ƒ} | j ƒ  d } |  j | j d d	 ƒ |  j d
 | k ƒ |  j d | k | ƒ Wd t  j | ƒ X| S(   s   Create a test crash.t   dirsW   #!/usr/bin/python
def func(x):
    raise Exception, 'This should happen.'

%s
func(42)
ií  t   testarg1t   testarg2t   stdoutt   stderri   s4   crashing test python program exits with failure codes   Exception: This should happen.R'   N(   R   R   R<   R;   R   t   mkstempRE   R7   t
   report_dirR>   R?   t   chmodt
   subprocesst   Popent   PIPEt   communicateRQ   t
   returncodet
   assertTruet   assertFalseR9   (   RS   t	   extracodet
   scriptnamet   scriptt   fdt   pt   err(    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   _test_crash   s$    	c         C   sy  |  j  ƒ  } t j j ƒ  } d } zl |  j t | ƒ d d ƒ |  j t j t	 j | d ƒ j
 ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ Wd x | D] } t	 j | ƒ q— WXd d d	 d
 d d d d d d d g } |  j t | ƒ j t | j ƒ  ƒ ƒ d ƒ |  j d | d k ƒ |  j | d | ƒ |  j | d d | ƒ |  j | d	 j d	 ƒ ƒ |  j d | d	 k ƒ d S(   s+   general operation of the Python crash hook.i   s(   crashed Python program produced a reporti    i€  s   report has correct permissionsNt   InterpreterPathR   R   t   ProblemTypet   ProcEnviront
   ProcStatust   ProcCmdlinet   DateR   t   ProcMapst
   UserGroupss   report has necessary fieldss
   bin/pythons   ['%s', 'testarg1', 'testarg2']s.   func
    raise Exception, 'This should happen.(   Rk   RE   R7   t   get_new_reportsR   RQ   t   lent   statt   S_IMODER   t   st_modet   problem_reportt   ProblemReportt   loadR   R9   Rc   t   sett   issubsett   keyst
   startswith(   RS   Rg   t   reportsRF   t   rt   expected_keys(    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_general±   s,    "
	$c         C   sn  |  j  ƒ  } t ƒ  } z3t j j ƒ  } | j | ƒ |  j t | ƒ d d ƒ |  j t j	 t
 j | d ƒ j ƒ d d ƒ t j j | d ƒ t j j ƒ  } | j | ƒ |  j t | ƒ d ƒ |  j  d | ƒ } t j j ƒ  } | j | ƒ |  j t | ƒ d ƒ |  j  d | ƒ } t j j ƒ  } |  j t | ƒ d ƒ | j | ƒ Wd x | D] } t
 j | ƒ qRWXd S(   s1   Python crash hook overwrites seen existing files.i   s(   crashed Python program produced a reporti    i€  s   report has correct permissionsRf   N(   Rk   R|   RE   R7   Rt   t   updateRQ   Ru   Rv   Rw   R   Rx   t   mark_report_seenR9   (   RS   Rg   t   to_delR€   R   (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_existingÑ   s,    	"
c      
   C   s4  |  j  d ƒ t j j ƒ  } d } zl |  j t | ƒ d d ƒ |  j t j t	 j | d ƒ j
 ƒ d d ƒ t j ƒ  } | j t | d ƒ ƒ Wd x | D] } t	 j | ƒ q˜ WXd d	 d
 d d d d d d d g
 } |  j t | ƒ j t | j ƒ  ƒ ƒ d ƒ |  j d | d k ƒ |  j | d	 j d	 ƒ ƒ d S(   s   with zapped sys.argv.s   import sys
sys.argv = Nonei   s(   crashed Python program produced a reporti    i€  s   report has correct permissionsNRl   R   Rm   Rn   Ro   Rp   Rq   R   Rr   Rs   s   report has necessary fieldss
   bin/python(   Rk   RE   R7   Rt   R   RQ   Ru   Rv   Rw   R   Rx   Ry   Rz   R{   R   R9   Rc   R|   R}   R~   R   (   RS   R€   RF   R   R‚   (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_no_argvô   s&    "
	$c         C   sO   t  j j ƒ  } z' |  j t | ƒ d d t j ƒ  ƒ Wd x | D] } q@ WXd S(   s'   Assert that there are no crash reports.i    s"   no crash reports present (cwd: %s)N(   RE   R7   Rt   RQ   Ru   R   t   getcwd(   RS   R€   R   (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   _assert_no_reports  s    c      	   C   sÈ   t  j ƒ  } z§ x  d D]˜ } t  j | ƒ t j d g d t j d t j d t j ƒ} | j d ƒ \ } } | j d	 k s€ t ‚ | d
 k s’ t ‚ d | k s¤ t ‚ |  j	 ƒ  q WWd t  j | ƒ Xd S(   s4   interactive Python sessions never generate a report.s   /tmps
   /usr/locals   /usrt   pythont   stdinRY   RZ   s   raise ValueErrori    RU   t
   ValueErrorN(   s   /tmps
   /usr/locals   /usr(
   R   R‰   t   chdirR^   R_   R`   Ra   Rb   t   AssertionErrorRŠ   (   RS   t   orig_cwdt   dRi   t   outRj   (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_interactive  s    c   
      C   sÉ  t  j d t j j ƒ \ } } t j j t j j	 ƒ } d } zì t j | d ƒ t j | ƒ t j | d ƒ t j j | ƒ r› | d } t j | | ƒ n  t j j ƒ  } | | d <| j ƒ  d } t j | d d g d t j d	 t j ƒ} | j ƒ  d
 } |  j | j d
 d ƒ |  j d | k ƒ Wd t j | ƒ t j j | ƒ rZt j | ƒ n  | rst j | | ƒ n  Xt j j ƒ  } d }	 z |  j t | ƒ d ƒ Wd x | D] } t j | ƒ q­WXd S(   s/   the Python crash hook respects the ignore list.RV   sT   #!/usr/bin/python
def func(x):
    raise Exception, 'This should happen.'

func(42)
ií  s   .apporttestR   RW   RX   RY   RZ   i   s4   crashing test python program exits with failure codes   Exception: This should happen.Ni    (   R   R[   RE   R7   R\   R   R   t
   expanduserR,   t   _ignore_fileR   R>   R?   R]   R6   t   renameR-   t   mark_ignoreR^   R_   R`   Ra   RQ   Rb   Rc   R9   Rt   Ru   (
   RS   Rh   Rg   t   ifpatht   orig_ignore_fileR   Ri   Rj   R€   RF   (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   test_ignoring/  s@    	


N(   t   __name__t
   __module__RT   R   Rk   Rƒ   R‡   Rˆ   RŠ   R“   Rš   (    (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyRP   ‰   s   	!	 	#			(   t   __doc__R   R    R   R
   RL   RN   R›   t   unittestR   R^   t   os.pathRv   R   RE   Ry   t   TestCaseRP   t   main(    (    (    s6   /usr/lib/python2.7/dist-packages/apport_python_hook.pyt   <module>   s   		]		<Û