
(qh                @   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 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 j d k rAd d l m Z e d Z n d d l m Z d Z d Z d	 d
   Z e j  j! d d  Z" d Z# d d   Z$ d d   Z% d d d d  Z& Gd d   d  Z' Gd d   d  Z( d S)zAbstract Apport user interface.

This encapsulates the workflow and common code for any user interface
implementation (like GTK, Qt, or CLI).
    N)unicode_gettext   )ConfigParserFTz2.20.1c             C   s8   t  j j d k r. t |   j t j   d  St |   S)z$Return exception message as unicode.r   replace)sysversion_infomajorstrdecodelocalegetpreferredencoding)Z	exception r   +/usr/lib/python3/dist-packages/apport/ui.pyexcstr'   s    r   ZAPPORT_SYMPTOMS_DIRz/usr/share/apport/symptomsi    c             C   sX   y5 t  j d |  j d d   j d  } t |  SWn t t f k
 rS d  SYn Xd  S)Nz
Pid:	(.*)
Z
ProcStatus    )researchgetgroupint
IndexErrorAttributeError)reportpidr   r   r   get_pid3   s
    'r   c             C   s\   y t  j t |   d  Wn; t k
 rW } z | j t j k rE d SWYd  d  } ~ Xn Xd S)Nr   FT)oskillr   OSErrorerrnoESRCH)r   er   r   r   still_running;   s    r"   c          4   C   s  |  j    |  j   | r i  } y t |  & } t t | j   | d  |  Wd QRX| d |  |  } | s t j d |  d St j	 j
 t j	 j |   d |  d <WnM t k
 r t j d  Yn/ t j d |  t j   t j d  Yn X| s3d |  k r't j j |  d  } n t d	   y |  j |  WnO t k
 ra| s]  Yn5 t k
 r} z t |  |  d
 <WYd d } ~ Xn Xd
 |  k r~|  j |  rt j d  y d |  k r_d t j k r_d |  k rt d  |  d
 <nd t j j |  d j   d  r_t j j |  d j   d  r_t d  |  d j   d |  d
 <Wn t k
 r}| sy  Yn Xd |  k r|  j   }	 |	 r|	 |  d <|  j  d  d k rd t j k r|  j!   }
 |
 rt d  d j" |
  |  d
 <| ry@ t | d  * } t j# | d  |  j$ | d d Wd QRXWn< t% k
 r}} z t j d | | f  WYd d } ~ Xn Xt j j& |  t j# | d  d S)ay  Collect information about report.

    Encapsulate calls to add_*_info() and update given report, so that this
    function is suitable for threading.

    ui must be a HookUI instance, it gets passed to add_hooks_info().

    If reportfile is not None, the file is written back with the new data.

    If symptom_script is given, it will be run first (for run_symptom()).
    execNrunz8symptom script %s did not determine the affected packager   ZSymptomzsymptom script %s crashed:ExecutablePathzAcalled without a package, and report does not have ExecutablePathUnreportableReasonCrashDBZAPPORT_DISABLE_DISTRO_CHECKPackagez4This package does not seem to be installed correctlyzXThis is not an official %s package. Please remove any third party package and try again.ZDistroReleaseZTitleProblemTypeCrashZAPPORT_IGNORE_OBSOLETE_PACKAGESzYou have some obsolete package versions installed. Please upgrade the following packages and check if the problem still occurs:

%sz, ZabZonly_newTzCannot update %s: %si  )'Zadd_gdb_infoadd_os_infoopenr#   compilereadapporterrorr   pathsplitextbasenameStopIterationr   exit	traceback	print_exc	fileutilsfind_file_packageKeyErroradd_package_info
ValueErrorSystemErrorr   Zadd_hooks_infoenviron_	packagingZis_distro_packagesplitZis_native_origin_packageZstandard_titler   Zobsolete_packagesjoinchmodwriteIOErrormark_report_seen)r   Z
reportfilepackageuisymptom_scriptignore_uninstalledsymbfr!   titleZold_pkgsr   r   r   thread_collect_infoD   sz    

&*
#!!#
$*rN   c               @   s;  e  Z d  Z d Z d d   Z d d   Z d d d  Z d	 d
   Z d d   Z d d   Z	 d d   Z
 d d d  Z d d   Z d d   Z d d   Z d d   Z d d   Z d d   Z d  d!   Z d" d#   Z d$ d%   Z d& d'   Z d( d)   Z d* d+   Z d, d-   Z d d. d d/ d0  Z d1 d2   Z d3 d4   Z d5 d6   Z d7 d8   Z d9 d:   Z d; d<   Z d= d>   Z  d d d? d@  Z! dA dB   Z" dC dD   Z# dE dF   Z$ dG dH   Z% dI dJ   Z& dK dL   Z' dM dN   Z( dO dP   Z) dQ dR   Z* dS dT   Z+ dU dV   Z, dW dX   Z- dY dZ   Z. d[ d\   Z/ d S)]UserInterfacea  Apport user interface API.

    This provides an abstract base class for encapsulating the workflow and
    common code for any user interface implementation (like GTK, Qt, or CLI).

    A concrete subclass must implement all the abstract ui_* methods.
    c             C   s   d |  _  d |  _ d |  _ d |  _ d |  _ y t j j d  |  _ WnY t k
 r } z t j	 d t
 |   WYd d } ~ Xn t k
 r t j	 d  Yn Xt j |  j   |  j   d S)z8Initialize program state and parse command line options.r/   NFzDCould not import module, is a package upgrade in progress? Error: %sz8/etc/apport/crashdb.conf is damaged: No default database)Zgettext_domainr   report_filecur_packageoffer_restartr/   crashdbget_crashdbImportErrorfatalr	   r:   gettext
textdomain
parse_argv)selfr!   r   r   r   __init__   s    					(zUserInterface.__init__c             C   s_  d } d |  _  t j   d k r9 t j j   } d } nr t j j   } t r t j d t j	   t j
 t j Bt j B } t j j d |  } n t j j t j	    } x | D] } |  j |  s q | r!d |  j k r!d |  j k r!| d |  j d k s | d	 |  j j   k r!q |  j d
 d k rD|  j |  n |  j |  d } q W| S)a  Present all currently pending crash reports.

        Ask the user what to do about them, and offer to file bugs for them.

        Crashes that occurred in a different desktop (logind) session than the
        one that is currently running are not processed. This skips crashes
        that happened during logout, which are uninteresting and confusing to
        see at the next login.

        Return True if at least one crash report was processed, False
        otherwise.
        FTr   Nz/proc/%sproc_pid_fdZ_LogindSessionDater   r)   Hang)rR   r   geteuidr/   r8   Zget_new_system_reportsZget_new_reportsPY3r,   getpidO_RDONLYO_PATHO_DIRECTORYReportZget_logind_sessionload_reportr   Zget_timestampfinish_hang	run_crash)rZ   resultZreportsZlogind_sessionr\   rL   r   r   r   run_crashes   s.    		-
zUserInterface.run_crashesTc             C   sQ  | |  _  y(y t j j |  Wn t k
 r4 Yn X|  j rS |  j |  rS d Sd |  j k rf d S|  j j d  d k rd |  j k rd |  j k rd |  j k rt j	 j
 |  j j d t d	    } t d
  | } |  j t d  | d | t d  f  d St j j   } |  j |  } | d sB| d r[d |  j k r[|  j   |  j d k rnd S| d r|  j   d S| d r|  j   | d r|  j j   | d sd Sd |  j k r|  j d d k rt j j |  |  j j |  j  r0|  j   rd S|  j   r&d S|  j   Wnt k
 r} z | j t j t j f k r|  j t d  t d   t j d  nd | j t j k r|  j t d  t d   t j d  n& |  j t d  | j   t j d  WYd d } ~ XnJ t k
 rL} z* | j t j! k r7t j" d  n   WYd d } ~ Xn Xd S)zPresent and report a particular crash.

        If confirm is True, ask the user what to do about it, and offer to file
        a bug for it.

        If confirm is False, the user will not be asked, and the crash is
        reported right away.
        NIgnorer)   r*   ZSignalCoreDump
Stacktracer%   zunknown programz+Sorry, the program "%s" closed unexpectedlyzProblem in %sz%s

%szxYour computer does not have enough free memory to automatically analyze the problem and send a report to the developers.r   examineDependenciesrestartZ	blacklist_MarkForUploadFalsezInvalid problem reportz2You are not allowed to access this problem report.r   Errorz@There is not enough disk space available to process this report.zOut of memory, aborting)#rP   r/   r8   rF   r   r   rf   r   r   r1   r3   r?   ui_error_messageallowed_to_reportui_present_report_detailscollect_inforn   rp   Zmark_ignoremark_report_uploadrS   acceptshandle_duplicatecheck_unreportablefile_reportrE   r   ZEPERMEACCESr   r5   ZENOSPCstrerrorZENOMEMrV   )rZ   rP   ZconfirmZsubjectZheadingru   responser!   r   r   r   rh      sr    		E'






zUserInterface.run_crashc             C   s$   t  j j |  t  j j |  d S)zFinish processing a hanging application after the core pipe handler
        has handed the report back.

        This will signal to whoopsie that the report needs to be uploaded.
        N)r/   r8   rx   rF   )rZ   rL   r   r   r   rg   ]  s    zUserInterface.finish_hangc             C   s0  t  j d  |  _ |  j j s> |  j t d  t d   d S|  j j |  |  j j   |  j j	 d d  } t  j
 j |  |  _ |  j j   t  j
 j   } |  j | d | } | d r t  j
 j |  j |  t j t |  t j  n t j t |  t j  | d	 r,|  j |  |  j   d
 S)a  Report an application hanging.

        This will first present a dialog containing the information it can
        collect from the running application (everything but the trace) with
        the option of terminating or restarting the application, optionally
        reporting that this error occurred.

        A SIGABRT will then be sent to the process and a series of
        noninteractive processes will collect the remaining information and
        mark the report for uploading.
        r^   zNo PID specifiedz;You need to specify a PID. See --help for more information.Fr%   r   	modal_forr   rp   N)r/   re   r   optionsr   rt   r?   add_proc_infor;   r   r8   r9   rQ   r+   ru   rv   Zmark_hanging_processr   r   r   signalSIGABRTSIGKILLwait_for_pidrp   )rZ   r   r1   ru   r   r   r   r   run_hangf  s(    	

zUserInterface.run_hangc             C   ss   xl y t  j t |  d  Wn> t k
 r] } z | j t j k rH Pn   WYd d } ~ Xn Xt j d  q Wd S)zwaitpid() does not work for non-child processes. Query the process
        state in a loop, waiting for "no such process."
        r   Nr   )r   r   r   r   r   r    timeZsleep)rZ   r   r!   r   r   r   r     s    zUserInterface.wait_for_pidc             C   s   t  j t |  t j  d  S)N)r   r   r   r   SIGSEGV)rZ   r   r   r   r   	kill_segv  s    zUserInterface.kill_segvNc       
   4   C   s4  |  j  j rQ |  j  j rQ | rQ |  j   r1 d S|  j t d  t d   d St j d  |  _ |  j  j ry d } t	 r t
 j d |  j  j t
 j t
 j Bt
 j B } t
 j d t
 j t
 j Bd	 | } n d
 |  j  j } t j |   } | j   j   } Wd QRXt | d  } | t @r4d |  j  _ n |  j j d |  j  j d |  Wqt t t f k
 r} z| t | d  r| j t j k rd S| j t j k r|  j t d  t d   d S|  j t d  t d   d SWYd d } ~ XqXn |  j j   |  j  j r0|  j  j j   |  j  _ |  j  j d k rWt j j    |  _! n |  j  j |  _! y |  j" |  Wn t k
 r} z d t# |  k rd t# |  k r|  j! s|  j t d  t d  |  n# |  j t d  t d  |  j!  d S  WYd d } ~ Xn X|  j$   r)d S|  j%   |  j&   rCd Sy |  j d =Wn t' k
 reYn X|  j  j( ry? t t
 j) j* |  j  j(  d   } |  j j+ |  Wd QRXWq0t t f k
 r} z! |  j t d  t, |   WYd d } ~ Xq0Xn2 t j- j.   } |  j/ |  }	 |	 d r0|  j0   d S)a@  Report a bug.

        If a pid is given on the command line, the report will contain runtime
        debug information. Either a package or a pid must be specified; if none
        is given, show a list of symptoms.

        If a symptom script is given, this will be run first (used by
        run_symptom()).
        TzNo package specifiedzHYou need to specify a package or a PID. See --help for more information.FBugNz/proc/%sstatdir_fdz/proc/%s/stat   linuxr   r\   r   zPermission deniedzfThe specified process does not belong to you. Please run this program as the process owner or as root.zInvalid PIDz6The specified process ID does not belong to a program.rG   zdoes not existzInvalid problem reportz7Symptom script %s did not determine an affected packagezPackage %s does not existProcCmdlinewbzCannot create reportr   )1r   rG   r   run_symptomsrt   r?   r/   re   r   r`   r   r,   rb   rc   rd   	O_CLOEXECior.   rA   r   
PF_KTHREADr   r<   rE   r   hasattrr   ENOENTr}   add_proc_environstripr@   Zget_kernel_packagerQ   rw   r	   r{   add_extra_tagsrz   r:   saver1   
expanduserrD   r   r8   ru   rv   r|   )
rZ   rI   r\   Z	stat_filerL   r   flagsr!   ru   r   r   r   r   run_report_bug  s    
-%
#$	
$2

zUserInterface.run_report_bugc             C   s  |  j  j |  j j  s8 |  j t d  t d   d S|  j  j |  j j  } | su |  j t d   } | su d St j	 d  |  _
 |  j j r |  j j j   g } n |  j  j |  j j  } d } x | D] } | |  _ | |  j
 d <| |  j
 d <y t j j |  WnN t k
 r]t j j t j j t j
 j d |   sYt d	 |  w Yn X|  j d
 d  d } q W| s|  j t d  t d   d S|  j
 j   |  j
 j   |  j   |  j
 d =|  j
 d =y |  j
 d =Wn t k
 rYn Xt |  j
  d k r.|  j t d  t d   d St j j    } |  j! |  } | d r|  j  j" |  j j |  j
 d d | d d d Sd S)z:Update an existing bug with locally collected information.zUpdating problem reportzYou are not the reporter or subscriber of this problem report, or the report is a duplicate or already closed.

Please create a new report using "apport-bug".FaH  You are not the reporter of this problem report. It is much easier to mark a bug as a duplicate of another than to move your comments and attachments to a new bug.

Subsequently, we recommend that you file a new bug report using "apport-bug" and make a comment in this bug about the one you file.

Do you really want to proceed?r   SourcePackager(   zsource_%s.pyz8Package %s not installed and no hook available, ignoringrJ   Tz$No additional information collected.r)   r]   r   r   zapport informationZchange_descriptionZattachment_comment)#rS   Z
can_updater   update_reportrt   r?   is_reporterui_question_yesnor/   re   r   rG   r   Zget_affected_packagesrQ   r@   Zget_versionr<   r   r1   existsrB   Z	_hook_dirprintrw   ui_info_messageadd_user_infor   r   r:   lenr8   ru   rv   update)rZ   r   rZpkgsZinfo_collectedpru   r   r   r   r   run_update_report  sh    	+




zUserInterface.run_update_reportc       	      C   s  t  j  t j j t d   } g  } g  } x | D] } t j j |  j d  rU q1 i  } y9 t |  & } t t	 | j
   | d  |  Wd QRXWn% t j d |  t j   w1 Yn Xd | k r t j d |  q1 | j t j j t j j |   d  | j | j d	 | d   q1 W| s/d St t t | |     \ } } t |  } t |  } | j d  | j d  |  j t d  | d  } | d k	 r| | d } | r|  j t j j t | d   n d Sd S)zlReport a bug from a list of available symptoms.

        Return False if no symptoms are available.
        z*.pyr?   r#   Nzsymptom script %s is invalidr$   z0symptom script %s does not define run() functionr   Zdescriptionr   FzOther problemz+What kind of problem do you want to report?z.pyT)globr   r1   rB   symptom_script_dirr3   
startswithr,   r#   r-   r.   r/   r0   r6   r7   appendr2   r   zipsortedlistui_question_choicer?   r   )	rZ   scriptsZsymptom_namesZsymptom_descriptionsscriptrK   rL   Zchsymptomr   r   r   r   X  sD    *
)!!#zUserInterface.run_symptomsc             C   sl   t  j j t |  j j d  } t  j j |  s[ |  j t d  t d  |  j j  d S|  j	 |  d S)z#Report a bug with a symptom script.z.pyzUnknown symptomzThe symptom "%s" is not known.N)
r   r1   rB   r   r   r   r   rt   r?   r   )rZ   r   r   r   r   run_symptom  s    zUserInterface.run_symptomc             C   s  |  j  j r |  j   d S|  j  j r= |  j |  j  j  d S|  j  j rS |  j   S|  j  j d k	 ro |  j	   S|  j  j
 r t t  d S|  j  j r y |  j |  j  j d  WnA t k
 r } z! |  j t d  t |   WYd d } ~ Xn Xd S|  j  j r|  j d t d   t j d d g d	 t j d
 t j } | j   \ } } | j d k ry  t | j   d  |  j  _ Wn2 t k
 r|  j t d  t d   d SYn X|  j   S|  j t d  t d  d |  d Sn
 |  j   Sd S)zCall appopriate run_* method according to command line arguments.

        Return True if at least one report has been processed, and False
        otherwise.
        TNFzInvalid problem reportr   z^After closing this message please click on an application window to report a problem about it.xpropZ_NET_WM_PIDstdoutstderrr   r   zCannot create reportz2xprop failed to determine process ID of the windowz

r   )r   r   r   hangingr   r   filebugr   r   r   versionr   __version__
crash_filerh   r   rt   r?   r   windowr   
subprocessPopenPIPEZcommunicate
returncoder   rA   r<   rj   )rZ   r!   r   outerrr   r   r   run_argv  sH    



/ 	
zUserInterface.run_argvc          	   C   s  t  j t d   } | j d d d t d  | j d d d d	 g  d t d
  | j   \ |  _ |  _ t |  j  d k s |  j d j   r | j	 d  t
 j d  t |  j d  |  j _ d |  j _ d |  j _ d |  j _ d |  j _ d |  j _ g  |  _ d S)zgParse command line options when being invoked in update mode.

        Return (options, args).
        z%prog <report number>z-pz	--packagehelpzSpecify package name.z--tagactionr   defaultz@Add an extra tag to the report. Can be specified multiple times.r   r   z-You need to specify a report number to updateNF)optparseOptionParserr?   
add_option
parse_argsr   argsr   isdigitr0   r   r5   r   r   r   r   r   r   r   )rZ   	optparserr   r   r   parse_argv_update  s     )zUserInterface.parse_argv_updatec             C   s  t  t j  d k r d t j k rf t j j t j j t j d  t j j t j d   t j d <t j d } | j	 d  s | j	 d  r |  j
   d St j t d   } | j d d d	 d
 d d d d d t d  | j d d d	 d
 d d d t d  | j d d d d d d d t d  | j d d d d d t d  | j d d  d t d!  | j d" d# d d d t d$  | j d% d	 d
 d d d t d&  | j d' d( d d) d t d*  t j j | j d+ d d) d t d,  | j d- d	 d. d g  d t d/  | j d0 d1 d	 d
 d t d2  t  t j  d k r| j	 d3  rx# d< D] } t j | j |  _ qtW| j   \ |  _ |  _ |  j j r|  j j s|  j j s|  j j s|  j j s|  j j s|  j j r| j d4  t  t j  d k r%d St  t j  d5 k rY| j	 d3  rYd6 |  j _ d St  |  j  d5 k rrd St j j  t j j t! |  j d d7   rd6 |  j _ |  j d |  j _ g  |  _ n,|  j d j	 d8  s|  j d j	 d9  r|  j d |  j _ g  |  _ n |  j d j"   rRd6 |  j _ |  j d |  j _ g  |  _ n d: |  j d k rt j# j$ |  j d  } | s| j d; |  j d  t j% d5  g  |  _ d6 |  j _ | |  j _& n( d6 |  j _ |  j d |  j _& g  |  _ d S)=zParse command line options.

        If a single argument is given without any options, this tries to "do
        what I mean".
        r   ZAPPORT_INVOKED_ASz-update-bugz-collectNzF%prog [options] [symptom|pid|package|program path|.apport/.crash file]-fz
--file-bugr   
store_truedestr   r   Fr   zStart in bug filing mode. Requires --package and an optional --pid, or just a --pid. If neither is given, display a list of known symptoms. (Implied if a single argument is given.)z-wz--windowz7Click a window as a target for filing a problem report.-uz--update-bugtyper   r   z;Start in bug updating mode. Can take an optional --package.-sz	--symptommetavarZSYMPTOMzWFile a bug report about a symptom. (Implied if symptom name is given as only argument.)-pz	--packagezSpecify package name in --file-bug mode. This is optional if a --pid is specified. (Implied if package name is given as only argument.)-Pz--pidzSpecify a running program in --file-bug mode. If this is specified, the bug report will contain more information.  (Implied if pid is given as only argument.)z	--hangingz*The provided pid is a hanging application.-cz--crash-filePATHzReport the crash from given .apport or .crash file instead of the pending ones in %s. (Implied if file is given as only argument.)z--savezIn bug filing mode, save the collected information into a file instead of reporting it. This file can then be reported later on from a different machine.z--tagr   z@Add an extra tag to the report. Can be specified multiple times.z-vz	--versionz Print the Apport version number.z-bugzL-u/--update-bug option cannot be used together with options for a new reportr   Tz.pyz.crashz.apport/z %s does not belong to a package.)r   r   r   r   r   r   )'r   r   argvr   r>   r1   rB   dirnamer3   endswithr   r   r   r?   r   r/   r8   Z
report_dirZSUPPRESS_HELPZ
get_optionr   r   r   r   r   r   r   r   r   r   r   r0   r   r   r   r@   Zget_file_packager5   rG   )rZ   cmdr   opkgr   r   r   rY     s    #
$$$$,,	zUserInterface.parse_argvc             C   sf   | d k  r$ t  j d | d  d S| d k  rH t  j d | d  d St  j d | t d   d S)	zCFormat the given integer as humanly readable and i18n'ed file size.i@B z%.1fg     @@z KBi ʚ;g    .Az MBz GB)r   formatfloat)rZ   sizer   r   r   format_filesizeD  s
    zUserInterface.format_filesizec             C   s}   d } xp |  j  D]e } |  j  | r y& | t |  j  | j  d d 7} Wq t k
 rt | t |  j  |  7} Yq Xq W| S)z'Return the size of the complete report.r   r      )r   r   	gzipvaluer   )rZ   r   kr   r   r   get_complete_sizeM  s    & zUserInterface.get_complete_sizec             C   s   d } x| |  j  D]q } | d k r |  j  | r y& | t |  j  | j  d d 7} Wq t k
 r | t |  j  |  7} Yq Xq W| S)z&Return the size of the reduced report.r   rl   r   r   )r   r   r   r   )rZ   r   r   r   r   r   get_reduced_size\  s    & zUserInterface.get_reduced_sizec             C   s   |  j  s d |  j  k r d Sy5 t j d d g d t j d t j d k rQ d SWn t k
 rj d SYn Xy |  j d  SWn t k
 r d SYn Xd S)	zCheck whether to offer the "Examine locally" button.

        This will be true if the report has a core dump, apport-retrace is
        installed and a terminal is available (see ui_run_terminal()).
        rl   FZwhichzapport-retracer   r   r   N)r   r   callr   ZSTDOUTr   ui_run_terminalNotImplementedError)rZ   r   r   r   can_examine_locallyl  s    		z!UserInterface.can_examine_locallyc             C   sn   d |  j  k s t  t j   d k rj t j   t j d d d |  j  j d |  j  d   t j d  d S)zReopen the crashed application.r   r   Zshz-cZRespawnCommandr   N)	r   AssertionErrorr   forksetsidexeclpr   r   r5   )rZ   r   r   r   rp     s
    
,zUserInterface.restartc             C   s   |  j  t d  t d  t d  t d  |  j g d  } | d k rM d Sd t j j d  } d	 } d
 |  j j d
 d  d
 } d | d | d | d | d | d | d | i } |  j | | d  d S)zLocally examine crash report.zJThis will launch apport-retrace in a terminal window to examine the crash.zRun gdb sessionz1Run gdb session without downloading debug symbolsz)Update %s with fully symbolic stack traceFNz"apport-retrace -S system -C %s -v z~/.cache/apport/retracezapport-retrace 'z'\''r   z--gdb r   r   z	--output  )r   r?   rP   r   r1   r   r   r   )rZ   r   Zretrace_with_downloadZretrace_no_downloadZfileargZcmdsr   r   r   rn     s     				
zUserInterface.examinec             C   sj  d |  j  k r d S|  j  d j   j d  ryA t j |  j  d  } t | t  s] t  d | k so t  WnG t k
 r } z' d |  j  d | f |  j  d <d SWYd d } ~ Xn Xy t	 j
 j d |  |  _
 Wqft t f k
 rd	 |  j  d |  j  d <YqfXnZ y# t	 j
 j d |  j  d  |  _
 Wn4 t t f k
 red	 |  j  d |  j  d <d SYn Xd S)
z*Process reports' CrashDB field, if presentr'   T{implzBA package hook defines an invalid crash database definition:
%s
%sr&   FNzYA package hook wants to send this report to the crash database "%s" which does not exist.)r   lstripr   astZliteral_eval
isinstancedictr   	Exceptionr/   rS   Zload_crashdbrU   r:   rT   )rZ   specr!   r   r   r   check_report_crashdb  s(     #	z"UserInterface.check_report_crashdbFc          '   C   s  d |  j  d <|  j  j d  d k r4 d |  j  k s[ |  j  j d  d k rl d |  j  k rl | rh |   d S|  j  j d  d k rU|  j  j d d	  } t j j |  s t d
  } | r d | |  j  d f } | |  j  d <| r |   d Sd |  j  k rUt j j |  j  d  sUt d
  } d | |  j  d f |  j  d <| rQ|   d Sd |  j  k rd |  j  k rt |  j  d  } |  j  d =t t j |  j  d  j  } | | k rd |  j  d <t d  |  j  d |  j  d <d S|  j	 rd |  j  k r| r|  j  j
   n|  j   |  j j |  j   rHt |   } n d } d |  j  k r4t j j   }	 t j j d t d d d |  j  |  j |  j	 | | | f  }
 |
 j   xP |
 j   r|  j   | ry | j   Wqt k
 r t j d  YqXqW|
 j   t j j   t j j |	  y |
 j   Wn t  t! t" j# f k
 r} z5 d t d  t$ |  f |  j  d <d |  j  d <WYd d } ~ Xn t% k
 rt d  |  j  d <d |  j  d <Ynf t& k
 r3} zF t j# t$ |   t d  d t' |  |  j  d <d |  j  d <WYd d } ~ Xn Xd |  j  k sP|  j(   rk|  j)   | rg|   d S|  j  j d  d k s|  j  j d  d k sd |  j  k rt j j d |  j  j* d |  j j+   f  } | j   xM | j   r0|  j   y | j d  Wqt k
 r,t j d  YqXqWy | j   WnT t  t! t" j# f k
 r} z( d t d  t$ |  f |  j  d <WYd d } ~ Xn X| j,   r| j,   |  j  d <|  j  j d  d  k rt j j d |  j j- d |  j  f  } | j   xM | j   rM|  j   y | j d  Wqt k
 rIt j d  YqXqW| j   | j,   } | d k	 r| d! k rd" |  j  d <n | |  j  d <t j j d |  j  j.  } | j   xM | j   r|  j   y | j d  Wqt k
 r	t j d  YqXqW| j   |  j)   d |  j  k rd# |  j  k rOd |  j  k s}|  j  j d d	  j/ d$  rd |  j  k r|  j0 t d%  t d&   |  j1   t j d  | r|   d S)'aH  Collect additional information.

        Call all the add_*_info() methods and display a progress dialog during
        this.

        In particular, this adds OS, package and gdb information and checks bug
        patterns.

        If a symptom script is given, this will be run first (used by
        run_symptom()).
        Truerq   r)   r*   rm   ro   Nr%   r   zIThis problem report applies to a program which is not installed any more.z%s (%s)r&   ZInterpreterPathExecutableTimestamprr   zPThe problem happened with the program %s which changed since the crash occurred.targetnamerN   r   r   z%s

%sz7This problem report is damaged and cannot be processed.z6The report belongs to a package that is not installed.zBAn error occurred while attempting to process this problem report:z

ZKernelCrashZ
KernelOopsr(   g?_KnownReportr   T1r   ZKernelzInvalid problem reportz7Could not determine the package or source package name.)2r   r   r   r1   r   r?   r   r   st_mtimerQ   r+   !ui_start_info_collection_progressrS   ry   HookUIr>   copyr/   REThreadrN   rP   startisAlive!ui_pulse_info_collection_progressprocess_eventKeyboardInterruptr   r5   rB   clearr   	exc_raiserE   EOFErrorzlibr0   reprr<   r   r	   r   ui_stop_info_collection_progressZsearch_bug_patternsZget_bugpattern_baseurlreturn_valueZknownZ	anonymizer   rt   ui_shutdown)rZ   rI   rJ   Zon_finishedZexe_pathmsgZ	orig_timeZcur_timeZhookuiZorig_envZicthreadr!   ZbpthreadZknown_threadvalZanonymize_threadr   r   r   rw     s    N
	



	! 
?

	*







zUserInterface.collect_infoc          3   C   s  t  j   \ } } t  j   d k r t  j |  t  j   \ } } | r t d  } t d |  } t  j |  j   } | r | d | 7} |  j | |  y t  j |  Wn t	 k
 r Yn Xd St  j
   t  j |  y't t  j d t  j d    }	 d d	 d
 d t |	  g }
 y t j d d d d
 t |	  d g  j d  } | j   d j   d } t d | d   } | j   j d  } Wd QRXxE | D]= } | j d  r|
 j d | j d d  d j    PqWWn t j t f k
 rYn XWn t k
 r g  }
 Yn Xym y t j |
 d | g  WnK t	 k
 r} z+ t j | d d d d t j d  WYd d } ~ Xn XWnH t k
 r} z( t  j | t |   t j d  WYd d } ~ Xn Xt  j  d  d S)zjOpen the given URL in a new browser window.

        Display an error dialog if everything fails.
        r   zUnable to start web browserz'Unable to start web browser to open %s.
NZ
PKEXEC_UIDZSUDO_UIDZsudoz-Hz-u#Zpgrepz-az-xZgvfsdzUTF-8z/proc/%s/environrbs    s   DBUS_SESSION_BUS_ADDRESS=zDBUS_SESSION_BUS_ADDRESS=   =r   zxdg-opennewTZ	autoraise)!r   piper   closewaitr?   fdopenreadlinert   r   r   r   getenvr	   r   Zcheck_outputr
   
splitlinesrA   r,   r.   r   r   ZCalledProcessErrorrE   	TypeErrorr   
webbrowserr   r5   r   rD   _exit)rZ   urlr   wr   ZstatusrM   r0   messageZuidZsudo_prefixr   rL   envr!   r   r   r   open_url~  sZ    
!*'		$ zUserInterface.open_urlc          "   C   s  |  j  j |  j  s d S|  j j d  |  j j d  k r` y |  j d =Wn t k
 r_ Yn Xy |  j d =Wn t k
 r Yn Xd a d d   } x0 t |  j  D] } | j d  r |  j | =q W|  j   t	 j
 j
 d |  j  j d	 |  j | f  } | j   xy| j   r|  j t  y | j d
  | j   Wq	t k
 r^t j d  Yq	t	 j  j k
 r} z t d  } |  j | t |   } | d k	 r| \ } } |  j  j | |  t	 j
 j
 d |  j  j d	 |  j | f  } | j   WYd d } ~ Xq	t t t f k
 r)  Yq	t k
 r}} z5 |  j t d  d t d  t |  f  d SWYd d } ~ Xq	Xq	W| j   | j   }	 |  j   |  j  j  |  j |	  }
 |
 r|  j! |
  d S)zGUpload the current report and guide the user to the reporting web page.NZPackageArchitectureZArchitectureZStacktraceAddressSignaturec             S   s   t  |   | a d  S)N)r   _UserInterface__upload_progress)ZsentZtotalr   r   r   progress_callback  s    z4UserInterface.file_report.<locals>.progress_callbackr?   r  r   g?r   zDPlease enter your account information for the %s bug tracking systemzNetwork problemz%s

%szHCannot connect to crash database, please check your Internet connection.)"rS   ry   r   r   r:   r0  r   r   ui_start_upload_progressr/   r  Zuploadr  r  ui_set_upload_progressrB   r  r  r   r5   ZNeedsCredentialsr?   ui_question_userpassr   Zset_credentialsr(  SyntaxErrorr<   r   rt   r  ui_stop_upload_progressZget_comment_urlr/  )rZ   r1  r   Zupthreadr!   r-  datauserZpasswordZticketr+  r   r   r   r|     sd    $

	

zUserInterface.file_reportc             C   s  y] t  j   |  _ t | d   } |  j j | d d Wd QRXd |  j k r\ t d   Wn t k
 r d |  _ |  j t d  t d   d	 SYn t	 k
 r } z+ d |  _ |  j t d
  | j
  d	 SWYd d } ~ Xnm t t t t j f k
 rP} z> d |  _ |  j t d
  d t d  t |  f  d	 SWYd d } ~ Xn Xd |  j k r}|  j d j   d |  _ n$ t  j j |  j j d d   |  _ d S)zLoad report from given path and do some consistency checks.

        This might issue an error message and return False if the report cannot
        be processed, otherwise self.report is initialized and True is
        returned.
        r  binaryZ
compressedNr)   z+Report does not contain "ProblemType" fieldzMemory exhaustionzEYour system does not have enough memory to process this crash report.FzInvalid problem reportz%s

%sz7This problem report is damaged and cannot be processed.r(   r   r%   r   T)r/   re   r   r,   loadr<   MemoryErrorrt   r?   rE   r~   r(  r   r  r0   r  rA   rQ   r8   r9   r   )rZ   r1   rL   r!   r   r   r   rf     s4    		!		$zUserInterface.load_reportc             C   s   |  j  j |  j  s d Sd |  j k r t |  j d  t k r^ |  j d j d  |  j d <d |  j k r t d  |  j d j   d } n d } |  j | t d  |  j d  d	 Sd S)
znCheck if the current report is unreportable.

        If so, display an info message and return True.
        Fr&   zUTF-8r(   zProblem in %sr   r   z#The problem cannot be reported:

%sT)	rS   ry   r   r   bytesr
   r?   rA   r   )rZ   rM   r   r   r   r{   ,  s    $z UserInterface.check_unreportablec             C   sv  d |  j  k r8 t j j |  j  d  r8 |  j  d } n2 y t j j |  j  } Wn t k
 ri d SYn X| st d St	 r t
 d d d d  } d d i } n t
   } i  } y | j | |  Wnv t k
 r9} zV d t | j  k r$d	 t | j  k r$t j j d
 | t |  f  d S  WYd d } ~ Xn X| j d  sMd St | j d   } d | k rrd S| S)zReturn a .desktop info dictionary for the current report.

        Return None if report cannot be associated to a .desktop file.
        ZDesktopFileNinterpolationstrictFencodingzUTF-8ZonfigZarserzWarning! %s is broken: %s
zDesktop Entryr  )r   r   r1   r   r/   r8   Zfind_package_desktopfilerQ   r<   r`   r   r.   r   r	   	__class__r   r   rD   Zhas_sectionr   items)rZ   Zdesktop_filecpkwargsr!   ri   r   r   r   get_desktop_entry?  s4    (		* zUserInterface.get_desktop_entryc             C   s   |  j  j |  j  s d Sd |  j k r, d S|  j d j d  ru |  j t d  t d   |  j |  j d  n |  j t d  t d   d S)zCheck if current report matches a bug pattern.

        If so, tell the user about it, open the existing bug in a browser, and
        return True.
        Fr  ZhttpzProblem already knownzThis problem was already reported in the bug report displayed in the web browser. Please check if you can add any further information that might be helpful for the developers.z;This problem was already reported to developers. Thank you!T)rS   ry   r   r   r   r?   r/  )rZ   r   r   r   rz   d  s    
zUserInterface.handle_duplicatec             C   sd   |  j  s t  |  j j r` |  j  j d d  } | r@ | d 7} | d j |  j j  |  j  d <d S)z6Add extra tags to report specified with --tags on CLI.ZTagsr   r   N)r   r   r   tagr   rB   )rZ   Ztagsr   r   r   r   ~  s    
zUserInterface.add_extra_tagsc             C   s   t  d   d S)a  Show details of the bug report.

        Return the action and options as a dictionary:

        - Valid keys are: report the crash ('report'), restart
          the crashed application ('restart'), or blacklist further crashes
          ('blacklist').
        z.this function must be overridden by subclassesN)r   )rZ   ru   r   r   r   r   rv     s    	z'UserInterface.ui_present_report_detailsc             C   s   t  d   d S)z:Show an information message box with given title and text.z.this function must be overridden by subclassesN)r   )rZ   rM   textr   r   r   r     s    zUserInterface.ui_info_messagec             C   s   t  d   d S)z4Show an error message box with given title and text.z.this function must be overridden by subclassesN)r   )rZ   rM   rF  r   r   r   rt     s    zUserInterface.ui_error_messagec             C   s   t  d   d S)zOpen a indefinite progress bar for data collection.

        This tells the user to wait while debug information is being
        collected.
        z.this function must be overridden by subclassesN)r   )rZ   r   r   r   r	    s    z/UserInterface.ui_start_info_collection_progressc             C   s   t  d   d S)zaAdvance the data collection progress bar.

        This function is called every 100 ms.
        z.this function must be overridden by subclassesN)r   )rZ   r   r   r   r    s    z/UserInterface.ui_pulse_info_collection_progressc             C   s   t  d   d S)z,Close debug data collection progress window.z.this function must be overridden by subclassesN)r   )rZ   r   r   r   r    s    z.UserInterface.ui_stop_info_collection_progressc             C   s   t  d   d S)z{Open progress bar for data upload.

        This tells the user to wait while debug information is being uploaded.
        z.this function must be overridden by subclassesN)r   )rZ   r   r   r   r2    s    z&UserInterface.ui_start_upload_progressc             C   s   t  d   d S)zUpdate data upload progress bar.

        Set the progress bar in the debug data upload progress window to the
        given ratio (between 0 and 1, or None for indefinite progress).

        This function is called every 100 ms.
        z.this function must be overridden by subclassesN)r   )rZ   Zprogressr   r   r   r3    s    z$UserInterface.ui_set_upload_progressc             C   s   t  d   d S)z(Close debug data upload progress window.z.this function must be overridden by subclassesN)r   )rZ   r   r   r   r6    s    z%UserInterface.ui_stop_upload_progressc             C   s   d S)zdCalled right before terminating the program.

        This can be used for for cleaning up.
        Nr   )rZ   r   r   r   r    s    zUserInterface.ui_shutdownc             C   s   t  d   d S)a  Run command in, or check for a terminal window.

        If command is given, run command in a terminal window; raise an exception
        if terminal cannot be opened.

        If command is None, merely check if a terminal application is available
        and can be launched.
        z.this function must be overridden by subclassesN)r   )rZ   Zcommandr   r   r   r     s    	zUserInterface.ui_run_terminalc             C   s   t  d   d S)zShow a yes/no question.

        Return True if the user selected "Yes", False if selected "No" or
        "None" on cancel/dialog closing.
        z.this function must be overridden by subclassesN)r   )rZ   rF  r   r   r   r     s    zUserInterface.ui_question_yesnoc             C   s   t  d   d S)am  Show an question with predefined choices.

        options is a list of strings to present. If multiple is True, they
        should be check boxes, if multiple is False they should be radio
        buttons.

        Return list of selected option indexes, or None if the user cancelled.
        If multiple == False, the list will always have one element.
        z.this function must be overridden by subclassesN)r   )rZ   rF  r   multipler   r   r   r     s    
z UserInterface.ui_question_choicec             C   s   t  d   d S)zmShow a file selector dialog.

        Return path if the user selected a file, or None if cancelled.
        z.this function must be overridden by subclassesN)r   )rZ   rF  r   r   r   ui_question_file  s    zUserInterface.ui_question_filec             C   s   t  d   d S)zRequest username and password from user.

        message is the text to be presented to the user when requesting for
        username and password information.

        Return a tuple (username, password), or None if cancelled.
        z.this function must be overridden by subclassesN)r   )rZ   r-  r   r   r   r4    s    z"UserInterface.ui_question_userpass)0__name__
__module____qualname____doc__r[   rj   rh   rg   r   r   r   r   r   r   r   r   r   rY   r   r   r   r   rp   rn   r  rw   r/  r|   rf   r{   rD  rz   r   rv   r   rt   r	  r  r  r2  r3  r6  r  r   r   r   rH  r4  r   r   r   r   rO      s\   3_	%iS12f	
@I%%
rO   c               @   sm   e  Z d  Z d Z d d   Z d d   Z d d   Z d d	 d
  Z d d   Z d d   Z	 d d   Z
 d S)r
  ag  Interactive functions which can be used in package hooks.

    This provides an interface for package hooks which need to ask interactive
    questions. Directly passing the UserInterface instance to the hooks needs
    to be avoided, since we need to call the UI methods in a different thread,
    and also don't want hooks to be able to poke in the UI.
    c             C   sF   | |  _  t j   |  _ t j   |  _ d |  _ d |  _ d |  _ d S)zSCreate a HookUI object.

        ui is the UserInterface instance to wrap.
        N)rH   	threadingZEvent_request_event_response_event_request_fn_request_args	_response)rZ   rH   r   r   r   r[     s    			zHookUI.__init__c             C   s   |  j  d d |  S)zShow an information with OK/Cancel buttons.

        This can be used for asking the user to perform a particular action,
        such as plugging in a device which does not work.
        r   r   )_trigger_ui_request)rZ   rF  r   r   r   information"  s    zHookUI.informationc             C   s   |  j  d |  S)zShow a yes/no question.

        Return True if the user selected "Yes", False if selected "No" or
        "None" on cancel/dialog closing.
        r   )rS  )rZ   rF  r   r   r   yesno*  s    zHookUI.yesnoFc             C   s   |  j  d | | |  S)am  Show an question with predefined choices.

        options is a list of strings to present. If multiple is True, they
        should be check boxes, if multiple is False they should be radio
        buttons.

        Return list of selected option indexes, or None if the user cancelled.
        If multiple == False, the list will always have one element.
        r   )rS  )rZ   rF  r   rG  r   r   r   choice2  s    
zHookUI.choicec             C   s   |  j  d |  S)zmShow a file selector dialog.

        Return path if the user selected a file, or None if cancelled.
        rH  )rS  )rZ   rF  r   r   r   file>  s    zHookUI.filec             G   s   |  j  j   s t  |  j j   s, t  |  j d k sA t  d |  _ | |  _ | |  _ |  j  j   |  j j   d |  _ |  j j	   |  j S)z5Called by HookUi functions in info collection thread.N)
rN  is_setr   rO  rP  rR  rQ  setr#  r  )rZ   fnr   r   r   r   rS  I  s    				zHookUI._trigger_ui_requestc             C   sx   |  j  j d  |  j  j   s# d S|  j j   s9 t  |  j  j   t |  j |  j  |  j	   |  _
 |  j j   d S)z;Called by GUI thread to check and process hook UI requests.g?N)rN  r#  rX  rO  r   r  getattrrH   rP  rQ  rR  rY  )rZ   r   r   r   r  \  s    !zHookUI.process_eventN)rI  rJ  rK  rL  r[   rT  rU  rV  rW  rS  r  r   r   r   r   r
    s   r
  ))rL  r   r   os.pathr   r   r6   r   rW   r   r   r   r  r   rM  r)  r   r   r   r/   apport.fileutilsZapport.REThreadZapport.crashdbr   r?   r   r   r   r`   configparserr   r   r>   r   r   r   r   r"   rN   rO   r
  r   r   r   r   <module>   s>   l$$			f     b