
    !f{-                     ^    d Z ddlZddlZddlZ G d dej
                  j                        Zy)zISimple in-memory CrashDatabase implementation, mainly useful for testing.    Nc                       e Zd ZdZd ZddZd Zd Zd Zd Z	d	 Z
d
 Z	 	 	 ddZd Zd Zd Zd Zd Zd Zd Zd ZddZd Zd Zd Zd Zy)CrashDatabasezSimple implementation of crash database interface which keeps everything
    in memory.

    This is mainly useful for testing and debugging.
    c                     t         j                  j                  j                  | ||       g | _        t               | _        t               | _        d| _        d| _	        d|v r| j                          yy)ztInitialize crash database connection.

        This class does not support bug patterns and authentication.
        r   Nsample_data)apportcrashdbr   __init__reportsset
unretraceddup_uncheckedupload_delay
upload_msgadd_sample_data)self	auth_fileoptionss      </usr/lib/python3/dist-packages/apport/crashdb_impl/memory.pyr	   zCrashDatabase.__init__   sd    
 	$$--dIwG % UG#  " $    Nc                    | j                  |      sJ |r/| j                  r# || j                  d   | j                  d          | j                  j                  |dddd       t	        | j                        dz
  }d|v r| j
                  j                  |       n| j                  j                  |       | j                  r5|r	 |dd       t        j                  | j                         |r	 |dd       |S )zStore the report and return a handle number (starting from 0).

        This does not support (nor need) progress callbacks.
        r      N )reportfixed_versiondup_ofcomment	Tracebackd   )acceptsr   r
   appendlenr   addr   r   timesleep)r   r   progress_callbackuser_message_callbackcrash_ids        r   uploadzCrashDatabase.upload/   s    
 ||F### T__!$//!"4dooa6HIQST	
 t||$q(& ""8,OO)  !!S)JJt(() !#s+r   c                 *    d|v rd|d    d| S d| S )zReturn http://<sourcepackage>.bugs.example.com/<handle> for package
        bugs or http://bugs.example.com/<handle> for reports without a
        SourcePackage.
        SourcePackagezhttp://z.bugs.example.com/zhttp://bugs.example.com/ )r   r   handles      r   get_comment_urlzCrashDatabase.get_comment_urlL   s3    
 f$VO455GxPP)&22r   c                 &    | j                  ||      S )zReturn URL for a given report ID.

        The report is passed in case building the URL needs additional
        information from it, such as the SourcePackage name.

        Return None if URL is not available or cannot be determined.
        )r-   )r   r   r'   s      r   
get_id_urlzCrashDatabase.get_id_urlU   s     ##FH55r   c                 &    | j                   |   d   S )z>Download the problem report from given ID and return a Report.r   r
   r   r'   s     r   downloadzCrashDatabase.download_   s    ||H%h//r   c                 .    | j                   |   d   d   gS )z5Return list of affected source packages for given ID.r   r*   r1   r2   s     r   get_affected_packagesz#CrashDatabase.get_affected_packagesc   s    X&x0ABBr   c                      y)z3Check whether the user is the reporter of given ID.Tr+   r2   s     r   is_reporterzCrashDatabase.is_reporterg   s    r   c                 $    | j                  |      S )aB  Check whether the user is eligible to update a report.

        A user should add additional information to an existing ID if (s)he is
        the reporter or subscribed, the bug is open, not a duplicate, etc. The
        exact policy and checks should be done according to  the particular
        implementation.
        )r7   r2   s     r   
can_updatezCrashDatabase.can_updatek   s     ))r   c                     | j                   |   }||d<   |r|D ]  }||v s||   |d   |<    y|d   j                  |       y)ah  Update the given report ID with all data from report.

        This creates a text comment with the "short" data (see
        ProblemReport.write_mime()), and creates attachments for all the
        bulk/binary data.

        If change_description is True, and the crash db implementation supports
        it, the short data will be put into the description instead (like in a
        new bug).

        comment will be added to the "short" data. If attachment_comment is
        given, it will be added to the attachment uploads.

        If key_filter is a list or set, then only those keys will be added.
        r   r   N)r
   update)	r   r'   r   r   change_descriptionattachment_comment
key_filterrfs	            r   r;   zCrashDatabase.updateu   sY    0 LL") /;%+AYAhKN/ hKv&r   c                 ,    | j                   |   d   d   S )zNGet 'DistroRelease: <release>' from the given report ID and return
        it.r   DistroReleaser1   r2   s     r   get_distro_releasez CrashDatabase.get_distro_release   s     ||H%h/@@r   c                     t               }t        | j                        D ]"  \  }}|d   |d   |j                  |       $ |S )aX  Return an ID set of all crashes which are not yet fixed.

        The list must not contain bugs which were rejected or duplicate.

        This function should make sure that the returned list is correct. If
        there are any errors with connecting to the crash database, it should
        raise an exception (preferably OSError).
        r   r   )r   	enumerater
   r"   )r   resultir   s       r   get_unfixedzCrashDatabase.get_unfixed   sN     "4<<0 	IAvh'F?,C,K

1	 r   c                 l    	 | j                   |   d   y| j                   |   d   S # t        $ r Y yw xY w)a  Return the package version that fixes a given crash.

        Return None if the crash is not yet fixed, or an empty string if the
        crash is fixed, but it cannot be determined by which version. Return
        'invalid' if the crash report got invalidated, such as closed a
        duplicate or rejected.

        This function should make sure that the returned result is correct. If
        there are any errors with connecting to the crash database, it should
        raise an exception (preferably OSError).
        r   invalidr   )r
   
IndexErrorr2   s     r   get_fixed_versionzCrashDatabase.get_fixed_version   sD    	||H%h/; <<)/:: 		s   ' ' 	33c                 &    | j                   |   d   S )zcReturn master ID for a duplicate bug.

        If the bug is not a duplicate, return None.
        r   r1   r2   s     r   duplicate_ofzCrashDatabase.duplicate_of   s    
 ||H%h//r   c                 (    || j                   |   d<   y)zlMark a crash id as duplicate of given master ID.

        If master is None, id gets un-duplicated.
        r   Nr1   )r   r   r'   	master_ids       r   close_duplicatezCrashDatabase.close_duplicate   s    
 ,5Xx(r   c                 V    | j                   |   d   J d| | j                   |   d<   y)zyMark a crash id as reintroducing an earlier crash which is
        already marked as fixed (having ID 'master').
        r   Nzregression, already fixed in #r   r1   )r   r'   masters      r   mark_regressionzCrashDatabase.mark_regression   s8     ||F#O4@@@.LVH,UXy)r   c                 Z    	 | j                   j                  |       y# t        $ r Y yw xY w)z/Mark crash id as checked for being a duplicate.N)r   removeKeyError)r   r'   r   s      r   _mark_dup_checkedzCrashDatabase._mark_dup_checked   s-    	%%h/ 		s    	**c                 :    | j                   j                  |       y)zMark crash id as retraced.N)r   rV   r2   s     r   mark_retracedzCrashDatabase.mark_retraced   s    x(r   c                      y)ztMark crash id as 'failed to retrace'.

        This is a no-op since this crash DB is not interested in it.
        Nr+   )r   r'   invalid_msgs      r   mark_retrace_failedz!CrashDatabase.mark_retrace_failed   s    r   c                     | j                   S )z}Return an ID set of all crashes which have not been retraced yet and
        which happened on the current host architecture.)r   r   s    r   get_unretracedzCrashDatabase.get_unretraced   s     r   c                     | j                   S )a,  Return an ID set of all crashes which have not been checked for
        being a duplicate.

        This is mainly useful for crashes of scripting languages such as
        Python, since they do not need to be retraced. It should not return
        bugs that are covered by get_unretraced().
        )r   r_   s    r   get_dup_uncheckedzCrashDatabase.get_dup_unchecked   s     !!!r   c                 2    t        | j                        dz
  S )z0Return the ID of the most recently filed report.r   )r!   r
   r_   s    r   	latest_idzCrashDatabase.latest_id   s    4<< 1$$r   c                    t         j                  j                         }d|d<   d|d<   d|d<   d|d<   d	|d
<   d|d<   | j                  |       t         j                  j                         }d|d<   d|d<   d|d<   d|d<   d	|d
<   d|d<   | j                  |       t         j                  j                         }d|d<   d|d<   d|d<   d|d<   d|d
<   d|d<   | j                  |       t         j                  j                         }d|d<   d|d<   d|d<   d|d
<   d|d<   | j                  |       t         j                  j                         }d|d<   d|d<   d|d<   d|d
<   d|d<   | j                  |       y)zWAdd some sample crash reports.

        This is mostly useful for test suites.
        zlibfoo1 1.2-3Packagefoor*   zFooLinux Pi/2rB   11Signalz
/bin/crashExecutablePathzfoo_bar (x=1) at crash.c:28
d01 (x=1) at crash.c:29
raise () from /lib/libpthread.so.0
<signal handler called>
__frob (x=1) at crash.c:30StacktraceTopzlibfoo1 1.2-4z
Testux 1.0zfoo_bar (x=2) at crash.c:28
d01 (x=3) at crash.c:29
raise () from /lib/libpthread.so.0
<signal handler called>
__frob (x=4) at crash.c:30zbar 42-4barz/usr/bin/brokenzuh (p=0x0) at crash.c:25
g (x=1, y=42) at crash.c:26
f (x=1) at crash.c:27
e (x=1) at crash.c:28
d (x=1) at crash.c:29zpython-goo 3epsilon1pygooz
Testux 2.2z/usr/bin/pygoozTraceback (most recent call last):
  File "test.py", line 7, in <module>
    print(_f(5))
  File "test.py", line 5, in _f
    return g_foo00(x+1)
  File "test.py", line 2, in g_foo00
    return x/0
ZeroDivisionError: integer division or modulo by zeror   zpython-goo 5N)r   r   Reportr(   )r   r?   s     r   r   zCrashDatabase.add_sample_data   s    MM  "&)"/,/(*
 	
	
 	A MM  "&)"/)/(*
 	
	
 	A MM  "!)"/)/(/
 	
	
 	A MM  "-)$/)/.
9 	
	
 	A MM  "%)$/)/.
9 	
	
 	Ar   )NN)FNN)N)__name__
__module____qualname____doc__r	   r(   r-   r/   r3   r5   r7   r9   r;   rC   rH   rL   rN   rQ   rT   rX   rZ   r]   r`   rb   rd   r   r+   r   r   r   r      s    #&:360C* ! 'DA
 &05V)
"%\r   r   )rr   r#   apport.crashdbr   apport.reportr   r   r+   r   r   <module>ru      s+    O   FNN00 r   