o
    åvŽf•N ã                   @   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mZ ddlmZ ejjdkr[ddlmZmZmZ ddlmZ ddlmZmZ eeeeeef d	Zndd
lmZmZmZmZ ddlmZ ddlmZ dZzddlmZ ddl m!Z! e! W n e"y   dZ!Y nw ddl#Z$ddl$Z$ej% &d¡Z'dd„ Z(dd„ Z)G dd„ de$j*j+ƒZ+da,G dd„ deƒZ-G dd„ deƒZ.d!dd„Z/e0dkrñddl1Z1ddl2Z2ddl3m4Z4 da*da5da6da7G dd „ d e1j8ƒZ9e1 :¡  dS dS )"z,Crash database implementation for Launchpad.é    N)ÚFailedToDecompressContent)ÚBytesIOé   )ÚHTTPSHandlerÚRequestÚbuild_opener)ÚHTTPSConnection)Ú	urlencodeÚurlopenT)r   r   r   r
   )r	   F)Ú	HTTPError)Ú	Launchpadz%~/.cache/apport/launchpad.credentialsc              
   c   s`    | D ]*}z|j  ¡ }W n ttfy   t d¡ Y qw |j}| d¡s*| d¡r-|V  qd S )Nz"Broken attachment on bug, ignoringú.txtú.gz)ÚdataÚopenr   r   ÚapportÚerrorÚfilenameÚendswith)ÚattachmentsÚ
attachmentÚfÚname© r   ú?/usr/lib/python3/dist-packages/apport/crashdb_impl/launchpad.pyÚfilter_filename,   s   €
þ€ør   c                 C   s   t dd„ | D ƒƒS )Nc                 s   s$    | ]}t |j d ¡ ¡ ƒV  qdS )ú/N)ÚintÚ	self_linkÚsplitÚpop)Ú.0Úir   r   r   Ú	<genexpr>:   s   €" zid_set.<locals>.<genexpr>)Úset)Útasksr   r   r   Úid_set8   s   r&   c                   @   s  e Zd ZdZdd„ Zedd„ ƒZdd„ Zedd	„ ƒZdAdd„Z	dd„ Z
dd„ Zdd„ Zdd„ Z		
dBdd„ZdCd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/d0„ Zd1d2„ Zd3d4„ ZdAd5d6„Zd7d8„ Zd9d:„ Zd;d<„ Z d=d>„ Z!e"d?d@„ ƒZ#d
S )DÚCrashDatabasez5Launchpad implementation of crash database interface.c                 C   s  t  d¡rt  d¡|d< |s$| d¡}|r"td | dd¡d  }nt}tjj | ||¡ | d¡| _	| j	r?d|vs>J d	ƒ‚nd|v sGJ d
ƒ‚d|v rSd|d  | _
ndtj ¡  | _
|| _|| _| jsfJ ‚d| _d| _t  d| d¡¡| _| jsŒtjdd| _tjtj| jdd dS dS )a  Initialize Launchpad crash database.

        You need to specify a launchpadlib-style credentials file to
        access launchpad. If you supply None, it will use
        default_credentials_path (~/.cache/apport/launchpad.credentials).

        Recognized options are:
        - distro: Name of the distribution in Launchpad
        - project: Name of the project in Launchpad
        (Note that exactly one of "distro" or "project" must be given.)
        - launchpad_instance: If set, this uses the given launchpad instance
          instead of production (optional). This can be overriden or set by
          $APPORT_LAUNCHPAD_INSTANCE environment.
        - cache_dir: Path to a permanent cache directory; by default it uses a
          temporary one. (optional). This can be overridden or set by
          $APPORT_LAUNCHPAD_CACHE environment.
        - escalation_subscription: This subscribes the given person or team to
          a bug once it gets the 10th duplicate.
        - escalation_tag: This adds the given tag to a bug once it gets more
          than 10 duplicates.
        - initial_subscriber: The Launchpad user which gets subscribed to newly
          filed bugs (default: "apport"). It should be a bot user which the
          crash-digger instance runs as, as this will get to see all bug
          details immediately.
        - triaging_team: The Launchpad user/team which gets subscribed after
          updating a crash report bug by the retracer (default:
          "ubuntu-crashes-universe")
        - architecture: If set, this sets and watches out for needs-*-retrace
          tags of this architecture. This is useful when being used with
          apport-retrace and crash-digger to process crash reports of foreign
          architectures. Defaults to system architecture.
        ÚAPPORT_LAUNCHPAD_INSTANCEÚlaunchpad_instanceÚ.z://é   éÿÿÿÿÚdistroÚprojectz/Must not set both "project" and "distro" optionz0Need to have either "project" or "distro" optionÚarchitecturezneed-%s-retraceNÚAPPORT_LAUNCHPAD_CACHEÚ	cache_dirzlaunchpadlib.cache.)ÚprefixT)Úignore_errors)ÚosÚgetenvÚgetÚdefault_credentials_pathr   r   Úcrashdbr'   Ú__init__r-   Úarch_tagÚ	packagingÚget_system_architectureÚoptionsÚauthÚ_CrashDatabase__launchpadÚ_CrashDatabase__lp_distroÚ_CrashDatabase__lpcacheÚtempfileÚmkdtempÚatexitÚregisterÚshutilÚrmtree)Úselfr>   r=   Úlp_instancer   r   r   r9   @   s6   
!ÿ

ýzCrashDatabase.__init__c              
   C   s  | j r| j S tdu rtrtj d¡ ntj d¡ t d¡ | j d¡r+| j d¡}nd}t	j
 | j¡}|rAt	j
 |¡sAt	 |¡ ztjd|| jdg| jd	d
| _ W | j S  ty‚ } z"t|dƒre|j}nt|ƒ}t d|| j¡ t d¡ W Y d}~| j S d}~ww )zReturn Launchpad instance.Nz^ERROR: The python-launchpadlib package is not installed. This functionality is not available.
z_ERROR: The python3-launchpadlib package is not installed. This functionality is not available.
r+   r)   Ú
productionzapport-collectÚWRITE_PRIVATEz1.0)Úlaunchpadlib_dirÚallow_access_levelsÚcredentials_fileÚversionÚcontentzZconnecting to Launchpad failed: %s
You can reset the credentials by removing the file "%s"éc   )r?   r   Ú_python2ÚsysÚstderrÚwriteÚexitr=   r6   r4   ÚpathÚdirnamer>   ÚisdirÚmakedirsÚ
login_withrA   Ú	ExceptionÚhasattrrP   Ústrr   r   )rH   r)   Úauth_dirÚeÚmsgr   r   r   Ú	launchpad‚   s>   


ûø
€øzCrashDatabase.launchpadc                 c   sH    | j st‚|D ]}|j ¡ | j kst d| j  |j ¡ ¡r!|V  qd S )Nz^.+\(%s.*\)$)r-   ÚStopIterationÚbug_target_nameÚlowerÚreÚmatch)rH   r%   Útr   r   r   Ú_get_distro_tasks«   s   €ÿ€ýzCrashDatabase._get_distro_tasksc                 C   sX   | j d u r)| jr| jj| j | _ | j S d| jv r%| jj| jd  | _ | j S tdƒ‚| j S )Nr.   z:distro or project needs to be specified in crashdb options)r@   r-   rb   Údistributionsr=   ÚprojectsÚSystemError©rH   r   r   r   Ú	lp_distro´   s   

ûþzCrashDatabase.lp_distroNc                 C   s>   |   |¡sJ ‚|  |¡}t|||  ¡ d}| ¡  |sJ ‚|S )aŸ  Upload given problem report return a handle for it.

        This should happen noninteractively.

        If the implementation supports it, and a function progress_callback is
        passed, that is called repeatedly with two arguments: the number of
        bytes already sent, and the total number of bytes to send. This can be
        used to provide a proper upload progress indication on frontends.
        )Úhostname)ÚacceptsÚ_generate_upload_blobÚupload_blobÚget_hostnameÚclose)rH   ÚreportÚprogress_callbackÚ	blob_fileÚticketr   r   r   ÚuploadÀ   s   

zCrashDatabase.uploadc                 C   s0   | j  d¡}|r|dkrd}|S d}|S d}|S )z/Return the hostname for the Launchpad instance.r)   Ústagingzstaging.launchpad.netzlaunchpad.devúlaunchpad.net)r=   r6   )rH   r)   ro   r   r   r   rs   Ò   s   ýÿzCrashDatabase.get_hostnamec                 C   s¬   i }|  d| ¡ ¡}|rt|tƒs| d¡}||d< |  ¡ }d|v r'|d }n| j  d¡}|sLd|v rAd|| j|d |t|ƒf S d|| j|t|ƒf S d|||t|ƒf S )	a/  Return an URL that should be opened after report has been uploaded
        and upload() returned handle.

        Should return None if no URL should be opened (anonymous filing without
        user comments); in that case this function should do whichever
        interactive steps it wants to perform.ÚTitleúUTF-8zfield.titleÚ
SnapSourcer.   ÚSourcePackagez,https://bugs.%s/%s/+source/%s/+filebug/%s?%sz!https://bugs.%s/%s/+filebug/%s?%s)	r6   Ústandard_titleÚ
isinstanceÚbytesÚencoders   r=   r-   r	   )rH   ru   ÚhandleÚargsÚtitlero   r.   r   r   r   Úget_comment_urlß   s*   


ÿÿÿzCrashDatabase.get_comment_urlc                 C   s   dt |ƒ S )zõReturn 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.
        z https://bugs.launchpad.net/bugs/)r^   )rH   ru   Úidr   r   r   Ú
get_id_url  s   zCrashDatabase.get_id_urlc                 C   s¢  t  ¡ }| jj| }t d|jtj¡}|s!t d|jtjtjB ¡}|s'J dƒ‚| 	d¡ 
d¡ dd¡ dd	¡}d
|v r`d|v rX| dd¡\}}| d
d	¡d | d
d¡d  }n| d
d¡d }| t|ƒ¡ d|vrƒz	|j ¡ |d< W n ty‚   |j|d< Y nw d|vr¸d|jv r‘d|d< n'd|jv r›d|d< nd|jv r¥d|d< nd|jv r¯d|d< n	tdt|jƒ ƒ‚d |j¡|d< d|v rÊ|d |d< |j|d< t|jƒD ]z}tj |j¡\}	}
zd||	< W n	 tyî   Y qÔw |
dkr| ¡ ||	< z||	  d¡||	< W qÔ t y   Y qÔw |
dkrHzt!j"|d ¡ ||	< W qÔ t#yG } zd t|ƒvr2‚ | $d¡ | ¡ ||	< W Y d!}~qÔd!}~ww td"|j ƒ‚|S )#z>Download the problem report from given ID and return a Report.z(ProblemType:.*)$z^--- \r?$[\r\n]*(.*)z8bug description must contain standard apport format datar+   r}   s   Â ó    s   
ó   
s   

s   Uname:r   ÚDateÚProblemTypez
apport-bugÚBugúapport-crashÚCrashzapport-kernelcrashÚKernelCrashzapport-packageÚPackagez(cannot determine ProblemType from tags: ú ÚTagsr|   ÚOriginalTitleÚ r   r   )Úfileobjz
Not a gzipNzUnknown attachment type: )%r   ÚReportrb   Úbugsrf   ÚsearchÚdescriptionÚSÚMÚgrouprƒ   Úreplacer   Úloadr   Údate_createdÚctimeÚAttributeErrorÚtagsÚ
ValueErrorr^   Újoinr†   r   r   r4   rW   Úsplitextr   r\   ÚreadÚdecodeÚUnicodeDecodeErrorÚgzipÚGzipFileÚIOErrorÚseek)rH   rˆ   ru   ÚbÚmr›   Úpart1Úpart2r   ÚkeyÚextr`   r   r   r   Údownload  sz    ÿþ








ÿ
ÿ

€úzCrashDatabase.downloadFc              	   C   s¦  | j j| }|rt| ¡ ƒt|ƒ }nd}t ¡ }	|j|	|d |	 ¡  |	 d¡ t	r1t
 |	¡}
nt
 |	¡}
|
 ¡ }t	r@| ¡ pC| ¡ }| ¡ sJJ ‚t	rP| ¡ pS| ¡ }| ¡ rZJ ‚| ¡ dksbJ ‚|sŒ|jdd… }| d¡ d|v r||  |d ¡ ¡ 7 }||_| ¡  | j j| }|jdd d	d
¡}|r³|r§|jd | |_| ¡  n|s¬|j}|j||d |D ]}|j|p¼d| ¡ d|jdd| ¡ dd qµ|	 ¡  dS )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.
        N)Ú	skip_keysr   ú
text/plainúapport-collectedr”   T©r©   r}   rŸ   z
--- 
©rP   Úsubjectr–   F©Úcommentr›   Úcontent_typer   r   Úis_patch)rb   r™   r$   ÚkeysrB   ÚTemporaryFileÚ
write_mimeÚflushr®   rR   ÚemailÚmessage_from_fileÚmessage_from_binary_fileÚwalkÚnextÚ__next__Úis_multipartÚget_content_typer¤   ÚappendÚ_filter_tag_namesr   Úlp_saveÚget_payloadr©   r›   r†   Ú
newMessageÚaddAttachmentÚget_filenamert   )rH   rˆ   ru   r½   Úchange_descriptionÚattachment_commentÚ
key_filterÚbugr¶   Úmimera   Úmsg_iterÚpartÚxÚtextr   r   r   Úupdatea  sR   





üzCrashDatabase.updater–   c           	   	   C   s€  t jj | |||¡ | jj| }d|v r7|jD ]}|jj 	d¡r6| j
j|d d|_| ¡  | jj| } nq| ¡ r¸|jD ]}|jdkrUz| ¡  W q> tyT   Y q>w q>z|  |j¡}trd| ¡ }n| ¡ }|jdkrtd|_| ¡  W n	 ty~   Y nw | ¡ }|r¸t d|j¡}|r¸| d¡|kr¸| d	¡| | d
¡ |_z| ¡  W n	 ty±   Y nw | jj| }|  ||¡ dS )zÆUpdate the given report ID for retracing results.

        This updates Stacktrace, ThreadStacktrace, StacktraceTop,
        and StacktraceSource. You can also supply an additional comment.
        r   ú#distribution©r   úCoreDump.gzÚ	UndecidedÚMediumz,^(.*crashed with SIG.* in )([^( ]+)(\(\).*$)r   r+   é   N)r   r8   r'   Úupdate_tracesrb   r™   Ú	bug_tasksÚtargetÚresource_type_linkr   rn   ÚgetSourcePackagerÎ   Úhas_useful_stacktracer   r†   ÚremoveFromBugr   ri   rR   rÈ   rÉ   Ú
importancerc   Ústacktrace_top_functionrf   rg   rž   Ú_subscribe_triaging_team)	rH   rˆ   ru   r½   rÖ   ÚtaskÚaÚfnr°   r   r   r   rã   ´  sV   
ü

ÿý

€ÿÿzCrashDatabase.update_tracesc                 C   s0   | j j| }t d|j¡}|r| d¡S tdƒ‚)zNGet 'DistroRelease: <release>' from the given report ID and return
        it.z"DistroRelease: ([-a-zA-Z0-9.+/ ]+)r+   z)URL does not contain DistroRelease: field)rb   r™   rf   rš   r›   rž   r¥   )rH   rˆ   rÖ   r°   r   r   r   Úget_distro_releaseè  s
   
z CrashDatabase.get_distro_releasec                 C   s`   t  d| j ¡}| jj| }g }|jD ]}| |jj¡}|sq|j	dv r%q| 
| d¡¡ q|S )z5Return list of affected source packages for given ID.z5/%s/(?:(?P<suite>[^/]+)/)?\+source/(?P<source>[^/]+)$)ÚInvalidú	Won't FixúFix ReleasedÚsource)rf   Úcompiler-   rb   r™   rä   rš   rå   r   ÚstatusrÌ   rž   )rH   rˆ   Úbug_target_rerÖ   Úresultrí   rg   r   r   r   Úget_affected_packagesñ  s   ÿ

z#CrashDatabase.get_affected_packagesc                 C   s   | j j| }|jj| j jjkS )z3Check whether the user is the reporter of given ID.)rb   r™   Úownerr   Úme©rH   rˆ   rÖ   r   r   r   Úis_reporter  s   zCrashDatabase.is_reporterc                 C   sZ   | j j| }|jrdS |jj| j jjkrdS | j jj}|jjD ]}|d |kr* dS qdS )aA  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.
        FTÚperson_link)	rb   r™   Úduplicate_ofrú   r   rû   r   ÚsubscriptionsÚentries)rH   rˆ   rÖ   rû   Úsubr   r   r   Ú
can_update	  s   
ÿzCrashDatabase.can_updatec              
   C   s^   z| j j| jdd}t|ƒW S  ty. } zt dt|ƒ¡ t 	d¡ W Y d}~dS d}~ww )z}Return an ID set of all crashes which have not been retraced yet and
        which happened on the current host architecture.ú
2011-08-01©r¤   Úcreated_sinceú"connecting to Launchpad failed: %srQ   N)
rn   ÚsearchTasksr:   r&   r\   r   r   r^   rS   rV   ©rH   r™   r`   r   r   r   Úget_unretraced   s   
€þzCrashDatabase.get_unretracedc              
   C   s\   z| j jddd}t|ƒW S  ty- } zt dt|ƒ¡ t d¡ W Y d}~dS d}~ww )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().úneed-duplicate-checkr  r  r  rQ   N)	rn   r  r&   r\   r   r   r^   rS   rV   r	  r   r   r   Úget_dup_unchecked*  s   
€þzCrashDatabase.get_dup_uncheckedc                 C   s   | j jdd}t|ƒS )aO  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 IOError).r   )r¤   )rn   r  r&   )rH   r™   r   r   r   Úget_unfixed9  s   	zCrashDatabase.get_unfixedc                 C   s"   | j jjd|| j jd}|d jS )z­Return the version of given source package in the latest release of
        given distribution.

        If 'distro' is None, we will look for a launchpad project .
        T)Úexact_matchÚsource_nameÚdistro_seriesr   )rn   Úmain_archiveÚgetPublishedSourcesÚcurrent_seriesÚsource_package_version)rH   ÚpackageÚsourcesr   r   r   Ú_get_source_versionE  s   ý
z!CrashDatabase._get_source_versionc           	         sN  zˆj j| }W n
 ty   Y dS w |jrdS t|jƒ}ˆjrdˆj ¡  ‰ tt‡ fdd„|ƒƒ}|sCtt‡fdd„|ƒƒ}|rCdS t	|ƒdkrSt
 dˆj|¡ dS |ro| ¡ }zˆ |j ¡ d	 ¡W S  tyn   Y dS w tt‡ fd
d„|ƒƒ}|r‹tt‡ fdd„|ƒƒ}|s‹dS dS ttdd„ |ƒƒ}|ršdS ttdd„ |ƒƒr¥dS dS )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 IOError).
        Úinvalidz(%s)c                    s   | j dkoˆ | j ¡ v S ©Nró   ©rö   Úbug_target_display_namere   ©rí   ©Údistro_identifierr   r   Ú<lambda>o  ó   
 z1CrashDatabase.get_fixed_version.<locals>.<lambda>c                    s   | j dko| j ¡ ˆ j ¡ kS r  )rö   rd   re   r-   r  rm   r   r   r  s  s   
 r–   r+   zVThere is more than one task fixed in %s %s, using first one to determine fixed versionr   c                    s   | j dv oˆ | j ¡ v S ©N)rñ   rò   ÚExpiredr  r  r  r   r   r  †  r   c                    s   | j dvoˆ | j ¡ v S r!  r  r  r  r   r   r  Š  r   c                 S   ó
   | j dkS r  ©rö   r  r   r   r   r    ó   
 c                 S   r#  )Nrñ   r$  r  r   r   r   r  ”  r%  N)rb   r™   ÚKeyErrorrÿ   Úlisträ   r-   re   ÚfilterÚlenr   Úwarningr    r  r  r   Ú
IndexError)	rH   rˆ   r¯   r%   Úfixed_tasksÚfixed_distrorí   Úinvalid_tasksÚnon_invalid_tasksr   )r  rH   r   Úget_fixed_versionS  s\   ÿ
ÿÿþÿ
þ
øzCrashDatabase.get_fixed_versionc                 C   s   | j j| j}|r|jS dS )zcReturn master ID for a duplicate bug.

        If the bug is not a duplicate, return None.
        N)rb   r™   rÿ   rˆ   )rH   rˆ   r¯   r   r   r   rÿ   ™  s   zCrashDatabase.duplicate_ofc              	   C   sÎ  | j j| }|rT||ksJ dt|ƒ ƒ‚| j j| }|jr2|j}|j}|j|kr2t d||¡ dS |jD ]}|jdv rLz| 	¡  W q5 t
yK   Y q5w q5| j j| }|jd| dd | j j| }|jrhd|_|jsn||_|j}t|jƒd	kr·d
| jv rš| jd
 |vrš| j dd¡|vrš|| jd
 g |_| ¡  d| jv r·| j dd¡|vr·| j j| jd  }|j|d | ¡ rd|v sÅd|v r| j||d| g d¢d | j j| }|jdd… }	z|	 d¡ W n	 tyï   Y nw z|	 d¡ W n
 ty   Y nw |	|_z| ¡  W n
 t
y   Y nw dg}
| jjD ]}|jdvr'q|
 |j¡ qt|jƒ}|j}| |¡}|D ]}||
v rJ| |¡ q>||_| ¡  n|jr[d|_|j re| ¡  dS dS )zlMark a crash id as duplicate of given master ID.

        If master is None, id gets un-duplicated.
        z+cannot mark bug %s as a duplicate of itselfzNBug %i was manually marked as a dupe of newer bug %i, not closing as duplicateN)rß   zStacktrace.txtzThreadStacktrace.txtzProcMaps.txtzProcStatus.txtzRegisters.txtzDisassembly.txtað  Thank you for taking the time to report this crash and helping to make this software better.  This particular crash has already been reported and is a duplicate of bug #%i, so is being marked as such.  Please look at the other bug report to see if there is any missing information that you can provide, or to see if there is a workaround for the bug.  Additionally, any further discussion regarding the bug should occur in the other report.  Please continue to report any other bugs you may find.zThis bug is a duplicaterº   Fé
   Úescalation_tagÚescalated_tagz	 invalid Úescalation_subscription©Úpersonzapport-request-retraceúapport-failed-retracez)Updated stack trace from duplicate bug %i)Ú
StacktraceÚThreadStacktracer’   ÚDependenciesÚProcMapsÚProcCmdline)rÕ   zbugpattern-needed)zActive DevelopmentzCurrent Stable ReleaseÚ	SupportedzPre-release Freeze)!rb   r™   r^   rÿ   rˆ   r   r*  r   r†   ré   r   rÐ   Úprivater¤   r)  Ú
duplicatesr=   r6   rÎ   ÚpeopleÚ	subscriberè   rÜ   Úremover¥   rn   Úseriesrö   rÌ   r   r$   Ú
differenceÚ_dirty_attributes)rH   ru   rˆ   Ú	master_idrÖ   Úmasterrî   Úmaster_tagsÚprÚ   Útags_to_copyrC  Ú	dupe_tagsÚmissing_tagsÚtagr   r   r   Úclose_duplicate¤  s¢   
ÿ

ÿú	úù	*
ÿÿÿÿ



€
ÿzCrashDatabase.close_duplicatec                 C   sD   | j j| }|jd| dd | j j| }|jdg |_| ¡  dS )zpMark a crash id as reintroducing an earlier crash which is
        already marked as fixed (having ID 'master').zíThis crash has the same stack trace characteristics as bug #%i. However, the latter was already fixed in an earlier package version than the one in this report. This might be a regression or because the problem is in a dependent package.zPossible regression detectedrº   úregression-retracerN)rb   r™   rÐ   r¤   rÎ   )rH   rˆ   rG  rÖ   r   r   r   Úmark_regression  s   ýüzCrashDatabase.mark_regressionc                 C   s`   | j j| }| j|jv r.|jdd… }| | j¡ ||_z| ¡  W dS  ty-   Y dS w dS )zMark crash id as retraced.N)rb   r™   r:   r¤   rB  rÎ   r   )rH   rˆ   rÖ   rÚ   r   r   r   Úmark_retraced#  s   ÿúzCrashDatabase.mark_retracedc              	   C   sÒ   | j j| }|rUz|  |j¡}tr| ¡ }n| ¡ }W n ty)   |jd }Y nw d|_| 	¡  |j
|dd |jD ]}|jdkrRz| ¡  W q; tyQ   Y q;w q;dS d|jvrg|jdg |_| 	¡  dS dS )z%Mark crash id as 'failed to retrace'.r   rñ   z Crash report cannot be processedrº   rß   r7  N)rb   r™   ri   rä   rR   rÈ   rÉ   rc   rö   rÎ   rÐ   r   r†   ré   r   r¤   )rH   rˆ   Úinvalid_msgrÖ   rí   rî   r   r   r   Úmark_retrace_failed0  s:   
€þÿ

ÿýÿ
þz!CrashDatabase.mark_retrace_failedc              	   C   sê   | j j| }d|v r9|jD ]+}|jj d¡r8| jj|d d|_z| ¡  | j j| }W n	 t	y5   Y nw  nqd|j
v rm|j
dd… }| d¡ ||_
| ¡  d|v rm|jD ]}d|jjv rl|jdkrld|_| ¡  qX|  ||¡ dS )	z/Mark crash id as checked for being a duplicate.r   rÝ   rÞ   r  NÚ	Tracebackrà   rá   )rb   r™   rä   rå   ræ   r   rn   rç   rÎ   r   r¤   rB  rê   rì   )rH   rˆ   ru   rÖ   rí   rÚ   r   r   r   Ú_mark_dup_checkedN  s8   
ÿþ÷



€zCrashDatabase._mark_dup_checkedc                 C   s    t jj | |¡}|s|S ||d< zt|d ƒ}W n ty#   | Y S w | ¡ }| d¡s/dS |D ]}| d¡rFd|v s@d|v rC dS  |S | ¡ sM |S q1|S )	a¦  Check if the crash db already knows about the crash signature.

        Check if the report has a DuplicateSignature, crash_signature(), or
        StacktraceAddressSignature, and ask the database whether the problem is
        already known. If so, return an URL where the user can check the status
        or subscribe (if available), or just return True if the report is known
        but there is no public URL. In that case the report will not be
        uploaded (i. e. upload() will not be called).

        Return None if the report does not have any signature or the crash
        database does not support checking for duplicates on the client side.

        The default implementation uses a text file format generated by
        duplicate_db_publish() at an URL specified by the "dupdb_url" option.
        Subclasses are free to override this with a custom implementation, such
        as a real database lookup.
        ÚDuplicateOfz/+texts   bug:Ts   tags:s   apport-failed-retraces   apport-request-retraceN)	r   r8   r'   Úknownr
   r­   ÚreadlineÚ
startswithÚstrip)rH   ru   Úurlr   Úliner   r   r   rW  n  s.   ý

ýýzCrashDatabase.knownc                 C   sx   d|v r|d   ¡ d dkrdS d| jj| j dd¡f }| t| jjƒd¡ d	¡d
d„ |jD ƒvr:|j	|d dS dS )z-Subscribe the right triaging team to the bug.ÚDistroReleaser   ÚUbuntuNz%s~%sÚtriaging_teamzubuntu-crashes-universer–   ú~c                 S   s   g | ]}t |ƒ d ¡d ‘qS )r   r,   )r^   r   )r!   r  r   r   r   Ú
<listcomp>²  s    z:CrashDatabase._subscribe_triaging_team.<locals>.<listcomp>r5  )
r   rb   Ú	_root_urir=   r6   rŸ   r^   rZ  r   rA  )rH   rÖ   ru   r6  r   r   r   rì   ¥  s   ÿÿþz&CrashDatabase._subscribe_triaging_teamc                 C   sn  i }d|d   ¡  |d< | d¡}|r|dkr| d¡}|r(|d  d| 7  < d|v r;|d  d|  |d ¡ 7  < d|v r}|rid	|v sMd
|v sMd|v rid|d< | d| j dd¡¡|d< |d  d| 7  < nd|v r}d|d< d|d< |d  d7  < d|v rd|d vr|d  d7  < d|v r™|d |d< g d¢}t ¡ }|j||g d¢|d | ¡  | d¡ |S )zGenerate a multipart/MIME temporary file for uploading.

        You have to close the returned file object after you are done with it.
        z	apport-%sr   r”   ÚPackageArchitectureÚallÚArchitecturer“   r]  ÚVmCoreÚCoreDumpÚLaunchpadPrivateÚyesÚPrivateÚLaunchpadSubscribeÚinitial_subscriberr   ÚSubscribersz need-%s-retracerT  z need-duplicate-checkÚDuplicateSignaturer  ÚCheckboxSubmissionzHWDB-Submission)	r   r]  r’   Ú
RegressionÚReproducibleÚTestedUpstreamÚProcVersionSignatureÚUnameÚNonfreeKernelModules)r”   rh  rk  )Úextra_headersr¶   Úpriority_fieldsr   )	re   r6   rÍ   r=   rB   rÁ   rÂ   rÃ   r®   )rH   ru   Úhdrrî   Úorderr×   r   r   r   rq   µ  sB   

ÿþ
z#CrashDatabase._generate_upload_blobc                 C   s`   d}|  ¡ jdddD ]"}|dv st|ƒdkr)|dv r)tr"||7 }q|t|ƒ7 }q|d7 }q|S )	zAReplace characters from tags which are not palatable to Launchpadr–   ÚASCIIÚignore)Úerrorss%   abcdefghijklmnopqrstuvwxyz0123456789 r   s   +-.r*   )re   rƒ   r)  rR   Úchr)Úklassr¤   ÚresÚchr   r   r   rÍ   ç  s   

zCrashDatabase._filter_tag_names©N)FNN)r–   )$Ú__name__Ú
__module__Ú__qualname__Ú__doc__r9   Úpropertyrb   ri   rn   ry   rs   r‡   r‰   rµ   rÜ   rã   rð   rù   rý   r  r
  r  r  r  r0  rÿ   rN  rP  rQ  rS  rU  rW  rì   rq   ÚclassmethodrÍ   r   r   r   r   r'   =   sJ    B
(	

#
U
ÿ
S4	
Fq
 72r'   c                   @   s   e Zd ZdZdd„ ZdS )ÚHTTPSProgressConnectionzWImplement a HTTPSConnection with an optional callback function for
    upload progress.c                 C   s¨   t s
t | |¡ d S d}t|ƒ}d}||k rRt ||ƒ t ¡ }t | |||| … ¡ ||7 }t ¡ }|dkrL|| dk rB|dK }n
|| dkrL|dL }||k sd S d S )Nr   i   g      à?r+   r   )Ú_https_upload_callbackr   Úsendr)  Útime)rH   r   ÚsentÚtotalÚ	chunksizeÚt1Út2r   r   r   rŠ    s$   

ózHTTPSProgressConnection.sendN)r‚  rƒ  r„  r…  rŠ  r   r   r   r   rˆ    s    rˆ  c                   @   s   e Zd Zdd„ ZdS )ÚHTTPSProgressHandlerc                 C   s   |   t|¡S r  )Údo_openrˆ  )rH   Úreqr   r   r   Ú
https_open$  s   zHTTPSProgressHandler.https_openN)r‚  rƒ  r„  r”  r   r   r   r   r‘  "  s    r‘  r{   c                 C   s  d}d| }|a tjj ¡ }tjj d¡}| dd¡ | |¡ tjj	 
dd¡}| dd¡ | |  ¡  d	¡¡ | |¡ tƒ }tjjd
krOtjj|dd}	ntjj|dd}	|	 |¡ t|| ¡ ƒ}
|
 dd| ¡  ¡ ttƒ}| |
¡}| ¡  d¡}|sJ ‚|S )a  Upload blob (file-like object) to Launchpad.

    progress_callback can be set to a function(sent, total) which is regularly
    called with the number of bytes already sent and total number of bytes to
    send. It is called every 0.5 to 2 seconds (dynamically adapted to upload
    bandwidth).

    Return None on error, or the ticket number on success.

    By default this uses the production Launchpad hostname. Set
    hostname to 'launchpad.dev' or 'staging.launchpad.net' to use another
    instance for testing.
    Nzhttps://%s/+storeblobÚ1zContent-Dispositionzform-data; name="FORM_SUBMIT"Úapplicationzoctet-streamz*form-data; name="field.blob"; filename="x"Úasciir   F)Úmangle_from_zContent-Typezmultipart/form-data; boundary=zX-Launchpad-Blob-Token)r‰  rÄ   r×   Ú	multipartÚMIMEMultipartrÛ   ÚMIMETextÚ
add_headerÚattachÚbaseÚMIMEBaseÚset_payloadr¨   r©   r   rS   Úversion_infoÚmajorÚ	generatorÚ	GeneratorÚBytesGeneratorÚflattenr   ÚgetvalueÚget_boundaryr   r‘  r   Úinfor6   )Úblobrv   ro   rx   r[  r   ÚsubmitÚ	form_blobÚ	data_flatÚgenr“  Úopenerrø   r   r   r   rr   (  s.   



rr   Ú__main__)Úpatchc                   @   sT  e Zd ZdZdZdd„ Zdd„ Zedd„ ƒZdNd	d
„Z	dd„ Z
dNd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*„ Zd+d,„ Zd-d.„ Ze ed/¡d0d1„ ƒZe d2d3„ ƒZ!d4d5„ Z"dOd7d8„Z#d9d:„ Z$d;d<„ Z%d=d>„ Z&d?d@„ Z'dAdB„ Z(dCdD„ Z)dEdF„ Z*dGdH„ Z+dIdJ„ Z,e dPdLdM„ƒZ-d6S )QÚ_TÚ	coreutilsc                 C   sH   t s|  ¡ a t | _ t ¡ | _| j ¡  | j ¡  d| jd< |  d¡ d S )Nr³  r   úlangpack-o-matic)r8   Ú_get_instancer   r˜   Ú
ref_reportÚadd_os_infoÚadd_user_infoÚ_create_projectrm   r   r   r   ÚsetUpr  s   



z_T.setUpc                 C   s@   | j jj| }|s| j jjj|d |||d |d d dS dS )z8Create a project using launchpadlib to be used by tests.r›   Úsummaryr†   )r›   Údisplay_namer   r»  r†   N)r8   rb   rk   Únew_project)rH   r   r.   r   r   r   r¹  ‚  s   

ûÿz_T._create_projectc                 C   s
   | j  ¡ S )z1Get the Launchpad hostname for the given crashdb.)r8   rs   rm   r   r   r   ro   Ž  s   
z_T.hostnameFc                 C   s°   |st durt S |  ¡ }| | j¡ | ¡  | ¡  | ¡  |  | ¡ d¡ d|d< d|d< |  	| j
|¡}|  |¡ |  ||¡}|  |dk¡ tj d| j|f ¡ |sV|a |S )	zÇGenerate SEGV crash report.

            This is only done once, subsequent calls will return the already
            existing ID, unless force_fresh is True.

            Return the ID.
            Nú!crash crashed with SIGSEGV in f()u    "]Â¶"
ÚShortGibberishõ   a
b
c
d
e
Ã¿Ã¿Ã¿
ÚLongGibberishr   z*(Created SEGV report: https://%s/bugs/%i) )Ú_segv_reportÚ_generate_sigsegv_reportÚadd_package_infoÚtest_packager·  Úadd_gdb_infor¸  ÚassertEqualr€   Ú_get_bug_targetr8   Ú
assertTrueÚ	_file_bugrS   rT   rU   ro   )rH   Úforce_freshÚrÚ
bug_targetrˆ   r   r   r   Úget_segv_report”  s$   	
z_T.get_segv_reportc                 C   sª   t durt S t d¡}d|d< d|d< d|d< | | j¡ | ¡  | ¡  |  | ¡ d	¡ |  	| j
|¡}|  |¡ |  ||¡}|  |d
k¡ tj d| j|f ¡ |a |S )zFGenerate Python crash report.

            Return the ID.
            Nr   ú/bin/fooÚExecutablePathú…Traceback (most recent call last):
  File "/bin/foo", line 67, in fuzz
    print(weird)
NameError: global name 'weird' is not definedrT  zboogus pybogusr”   úHfoo crashed with NameError in fuzz(): global name 'weird' is not definedr   z,(Created Python report: https://%s/bugs/%i) )Ú_python_reportr   r˜   rÄ  rÅ  r·  r¸  rÇ  r€   rÈ  r8   rÉ  rÊ  rS   rT   rU   ro   )rH   rÌ  rÍ  rˆ   r   r   r   Úget_python_report·  s&   

ÿ
z_T.get_python_reportc                 C   sV   |st durt S d}| jjjjd|| jjd}tj d| j	|j
f ¡ |s(|j
a |j
S )a­  File a bug report with an uncommon description.

            This is only done once, subsequent calls will return the already
            existing ID, unless force_fresh is True.

            Example taken from real LP bug 269539. It contains only
            ProblemType/Architecture/DistroRelease in the description, and has
            free-form description text after the Apport data.

            Return the ID.
            Nzbproblem

ProblemType: Package
Architecture: amd64
DistroRelease: Ubuntu 8.10

more text

and more
zmixed description bug)r†   r›   rå   z3(Created uncommon description: https://%s/bugs/%i) )Ú_uncommon_description_reportr8   rb   r™   Ú	createBugrn   rS   rT   rU   ro   rˆ   )rH   rË  ÚdescrÖ   r   r   r   Úget_uncommon_description_reportÖ  s   

ýz"_T.get_uncommon_description_reportc              
   C   sÒ  | j  |  ¡ ¡}|  |d d¡ |  |d d¡ |  |d | jd ¡ |  |d | jd ¡ |  |d | jd ¡ |  | d¡| j d¡¡ |  | d	¡| j d	¡¡ t|d
  ¡ ƒ}|  |t| j jdt	j
 ¡ gƒ¡ |  |d d¡ |  |d  d¡¡ |  |d | j¡ |  |d  | jd ¡¡ |  d|d ¡ |  d|d ¡ |  d|d ¡ |  t|d ƒd¡ |  d|¡ |  d|¡ |  d|¡ | j  |  ¡ ¡}t|d
  ¡ ƒ}|  |tddddt	j
 ¡ gƒ¡ dS ) z
download()r   r   r|   r¾  r]  re  rt  ru  Ú
UserGroupsr”   r   ÚSignalÚ11rÐ  z/crashr   r’   r“   zf (x=42)r8  ÚStacktraceTopr9  rg  iè  r:  ÚDisassemblyÚ	RegistersÚboogusÚpybogusr  N)r8   rµ   rÎ  rÇ  r¶  r6   r$   r   r:   r   r;   r<   rÉ  r   Útest_srcpackagerY  rÅ  ÚassertInÚassertGreaterr)  rÔ  )rH   rÌ  r¤   r   r   r   Útest_1_downloadú  s<   
ÿÿ
ÿz_T.test_1_downloadc                 C   sø  | j  |  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  |d d¡ d	|d
< d|d< d|d< d|d< | j  |  ¡ |d¡ | j  |  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  |d d¡ | j jj|  ¡  j	}|  d|¡ |  d|¡ d|d
< d|d< d|d< | j  |  ¡ |d¡ | j  |  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d|¡ |  |d d¡ | j jj|  ¡  }d|_
z| ¡  W n
 ty
   Y nw d|d
< | j  |  ¡ |d¡ | j  |  ¡ ¡}|  |d d¡ | j jj|  ¡  }d|_
z| ¡  W n
 tyE   Y nw d|d
< | j  |  ¡ |d¡ | j  |  ¡ ¡}|  |d d¡ d|d< d|d< d|d< | j  |  ¡ |d¡ dS ) zupdate_traces()rg  r:  rÝ  rÞ  r8  r9  r|   r¾  z?? ()rÜ  ú
long
traceúthread
even longer
traceÚbogusÚFooBarzI can has a better retrace?r   r¸   ú=read () from /lib/libc.6.so
foo (i=1) from /usr/lib/libfoo.soúgood retrace!z$crash crashed with SIGSEGV in read()z)crash crashed with SIGSEGV in f() on exitz!good retrace with title amendmentz,crash crashed with SIGSEGV in read() on exitzcrash is crashyzgood retrace with custom titler–   u   "]Â¶"
rÀ  ÚStacktraceSourceÚtestsN)r8   rµ   rÎ  râ  rÇ  rã   ÚassertNotInrb   r™   r¤   r†   rÎ   r   )rH   rÌ  r¤   rÖ   r   r   r   Útest_2_update_traces  s€   ÿÿz_T.test_2_update_tracesc                 C   sŠ   d}t  d¡}||d< | j |d¡}|  | d¡¡ | d¡|d< | j |d¡}|  | d¡¡ d|d< | j |d¡}|  | d	¡¡ d
S )z&get_comment_url() for non-ASCII titless   1Ã¤â™¥2rŽ   r|   é*   z1/ubuntu/+filebug/42?field.title=1%C3%A4%E2%99%A52r}   r³  r   zC/ubuntu/+source/coreutils/+filebug/42?field.title=1%C3%A4%E2%99%A52N)r   r˜   r8   r‡   rÉ  r   r©   )rH   r†   rÌ  r[  r   r   r   Útest_get_comment_urlm  s   
z_T.test_get_comment_urlc                 C   s  | j jjdd}| j jjjd|dd}|j}|  |dk¡ tj	 
d| j|f ¡ t d¡}d	 d
¡|d< d|d< d|d< d|d< d|d< | j j||ddd | j  |¡}|  |d d	 d
¡¡ |  |d d¡ |  |d d¡ |  |d d¡ |  | j jj| jdg¡ dS )z"update() with changing descriptionÚbashrÞ   ztest description for test bug.Útestbug©r›   rå   r†   r   ú(https://%s/bugs/%i) rŽ   s   bogusâ†’r}   ÚOneLinerúf()
g()
h(1)rÜ  úlineone
linetwoÚShortGooúone
two
three
four
five
sixÚDpkgTerminalLogs   ÚVarLogDistupgradeBinGooÚNotMeT©rÓ   r¸   N)r8   rn   rç   rb   r™   rÖ  rˆ   rÉ  rS   rT   rU   ro   r   r˜   r©   rÜ   rµ   rÇ  r¤   ©rH   rÍ  rÖ   rˆ   rÌ  r   r   r   Útest_update_descriptionƒ  s0   
ý
ÿz_T.test_update_descriptionc                 C   s  | j jjdd}| j jjjd|dd}|j}|  |dk¡ tj	 
d| j|f ¡ t d¡}d	|d
< d|d< d|d< d|d< d|d< | j j||ddd | j  |¡}|  d
|¡ |  d|¡ |  |d d¡ |  |d d¡ |  |d d¡ |  | j jj| jdg¡ dS )zupdate() with appending commentrñ  rÞ   zPr0blem

--- 
ProblemType: Bugrò  ró  r   rô  rŽ   õ   bogusâ†’rõ  rö  rÜ  r÷  rø  rù  rú  á   rû  ÚmeowFrý  r   r¸   N©r8   rn   rç   rb   r™   rÖ  rˆ   rÉ  rS   rT   rU   ro   r   r˜   rÜ   rµ   rí  rÇ  r¤   rþ  r   r   r   Útest_update_comment£  s2   
ý
ÿz_T.test_update_commentc                 C   s  | j jjdd}| j jjjd|dd}|j}|  |dk¡ tj	 
d| j|f ¡ t d¡}d	|d
< d|d< d|d< d|d< d|d< | j j||ddg d¢d | j  |¡}|  d
|¡ |  |d d¡ |  |d d¡ |  |d d¡ |  d|¡ |  | j jj| jg ¡ dS )zupdate() with a key filterrñ  rÞ   ztest description for test bugrò  ró  r   rô  rŽ   r   rõ  rö  rÜ  r÷  rø  rù  rú  r  rû  rü  T)r   rø  rú  )rÓ   rÕ   r   Nr  rþ  r   r   r   Útest_update_filterÆ  s2   
ý
ÿz_T.test_update_filterc                 C   s"   |   | j |  ¡ ¡| jd ¡ dS )zget_distro_release()r]  N)rÇ  r8   rð   rÎ  r¶  rm   r   r   r   Útest_get_distro_releaseç  s   ÿz_T.test_get_distro_releasec                 C   s$   |   | j |  ¡ ¡| jd g¡ dS )zget_affected_packages()r   N)rÇ  r8   rù   rÎ  r¶  rm   r   r   r   Útest_get_affected_packagesí  s   
ÿz_T.test_get_affected_packagesc                 C   ó,   |   | j |  ¡ ¡¡ |  | j d¡¡ dS )zis_reporter()r+   N)rÉ  r8   rý   rÎ  ÚassertFalserm   r   r   r   Útest_is_reporteró  ó   z_T.test_is_reporterc                 C   r  )zcan_update()r+   N)rÉ  r8   r  rÎ  r	  rm   r   r   r   Útest_can_updateù  r  z_T.test_can_updatec                 C   sø  |   | j |  ¡ ¡d¡ |   | j |  ¡ ¡d¡ |  ¡ }|  ¡ }| jdd}| j |¡}| j |||¡ |   | j |¡|¡ | j |||¡ |   | j |¡|¡ |   | j |¡d¡ | j ||d¡ |   | j |¡d¡ |   | j |¡d¡ | j |  ¡ ¡}|  d|¡ |  d|¡ |  d|¡ |  d|¡ |  d	|¡ |  d
|¡ | j t	 
¡ ||¡ | j |||¡ |   | j |¡|¡ | j t	 
¡ |d¡ | j t	 
¡ |d¡ | j ||d¡ | j t	 
¡ |d¡ |   | j |¡d¡ | j ||¡ |  |¡ dS )zduplicate handlingNT©rË  r  rg  rÝ  r;  Ú
ProcStatusrÞ  r9  )rÇ  r8   rÿ   rÎ  r0  rØ  rµ   rN  rí  r   r˜   rP  Ú_verify_marked_regression)rH   Úsegv_idÚknown_test_idÚknown_test_id2rÌ  r   r   r   Útest_duplicatesÿ  sF   ÿÿz_T.test_duplicatesc                 C   s†  | j  ¡ }|  |  ¡ |¡ |  |  ¡ |¡ | j  |  ¡ ¡ | j  ¡ }|  |  ¡ |¡ |  || t	|  ¡ gƒ¡¡ |  | j  
|  ¡ ¡d¡ |  |  ¡ ¡ | j  |  ¡ ¡ | j  |  ¡ ¡ | j  ¡ }|  |  ¡ |¡ |  || t	|  ¡ gƒ¡¡ |  | j  
|  ¡ ¡d¡ |  |  ¡ ¡ | j  |  ¡ ¡ | j  |  ¡ d¡ | j  ¡ }|  |  ¡ |¡ |  || t	|  ¡ gƒ¡¡ |  | j  
|  ¡ ¡d¡ dS )z-processing status markings for signal crashesNzI don't like your  )r8   r
  râ  rÎ  rí  rÔ  rQ  rÇ  Úunionr$   r0  Ú_mark_needs_retracerS  )rH   Úunretraced_beforeÚunretraced_afterr   r   r   Útest_marking_segv5  s<   

ÿ
ÿ
ÿÿz_T.test_marking_segvc                 C   sÜ   | j jjjd| j j| j jdd}tj d¡pd}t	tj d¡d|dœƒ}|jjjd	|j|jd
d}| 
¡ }|  |j|¡ |  |j|¡ | |j¡ | 
¡ }|  |j|¡ |  || t|jgƒ¡¡ |  | j  |j¡d¡ dS )ú0processing status markings for a project CrashDBÚfoozubuntu distro retrace bug©r›   r¤   rå   r†   r(   rz   ÚLP_CREDENTIALSr´  ©r.   r)   Úbarzproject retrace bugN)r8   rb   r™   rÖ  r:   rn   r4   Úenvironr6   r'   r
  râ  rˆ   rí  rQ  rÇ  r  r$   r0  )rH   Ú
distro_bugr)   Ú
project_dbÚproject_bugr  r  r   r   r   Útest_marking_projectX  s6   
ü
þüÿz_T.test_marking_projectc                 C   s¤   t j d¡pd}tt j d¡d|ddœƒ}| ¡ }| j ¡ }| jjjjddg| jj	d	d
}t
d|j ƒ | ¡ }| j ¡ }|  ||¡ |  || t|jgƒ¡¡ dS )r  r(   rz   r  ÚubuntuÚfakearch)r-   r)   r/   r  zneed-fakearch-retracez&ubuntu distro retrace bug for fakearchr  z4fake arch bug: https://staging.launchpad.net/bugs/%iN)r4   r  r6   r'   r
  r8   rb   r™   rÖ  rn   Úprintrˆ   rÇ  r  r$   )rH   r)   Úfakearch_dbÚfakearch_unretraced_beforeÚsystemarch_unretraced_beforerÖ   Úfakearch_unretraced_afterÚsystemarch_unretraced_afterr   r   r   Útest_marking_foreign_arch{  s,   
ÿþ

ü
ÿz_T.test_marking_foreign_archc                 C   s   | j  ¡ }|  |  ¡ |¡ |  |  ¡ |¡ | j  |  ¡ | j¡ | j  ¡ }|  |  ¡ |¡ |  || 	t
|  ¡ gƒ¡¡ |  | j  |  ¡ ¡d¡ dS )z2processing status markings for interpreter crashesN)r8   r  râ  rÔ  rí  rÎ  rU  r¶  rÇ  r  r$   r0  )rH   Úunchecked_beforeÚunchecked_afterr   r   r   Útest_marking_python—  s   

ÿz_T.test_marking_pythonc                 C   sp   | j dd}| j |¡}| j |||   ¡ ¡ d|d< d|d< d|d< | j ||d	¡ | j |¡}|  d
|¡ dS )z¬updating an invalid crash

            This simulates a race condition where a crash being processed gets
            invalidated by marking it as a duplicate.
            Tr  ré  rÜ  rå  r8  ræ  r9  rê  rg  N)rÎ  r8   rµ   rN  rã   rí  )rH   rˆ   rÌ  r   r   r   Útest_update_traces_invalid¤  s   z_T.test_update_traces_invalidr  c                 G   s\   dt j_|  |  ¡ ¡ | j |  ¡ ¡}|  |d¡ |  |  ¡ ¡ |  | j |  ¡ ¡d¡ dS )zÃget_fixed_version() for fixed bugs

            Other cases are already checked in test_marking_segv() (invalid
            bugs) and test_duplicates (duplicate bugs) for efficiency.
            z3.14N)	r'   r  Úreturn_valueÚ_mark_report_fixedrÎ  r8   r0  rÇ  Ú_mark_report_new)rH   r…   Ú	fixed_verr   r   r   Útest_get_fixed_version¹  s   	z_T.test_get_fixed_versionc                 C   s(   t j d¡pd}tt j d¡d|dœƒS )zCreate a CrashDB instancer(   rz   r  r$  )r-   r)   )r4   r  r6   r'   )r~  r)   r   r   r   rµ  Í  s   ÿÿz_T._get_instancec                 C   s<   |j  d¡}d|v r|jj|d dS |r|jj| S | jS )z&Return the bug_target for this report.r.   r   rÞ   )r=   r6   rn   rç   rb   rk   )rH   Údbru   r.   r   r   r   rÈ  ×  s   z_T._get_bug_targetNc              	   C   sL  |du rd}| j  |¡}trt |¡}nt |¡}| ¡  | ¡ }tr'| ¡ p*| 	¡ }| 
¡ s1J ‚tr7| ¡ p:| 	¡ }| 
¡ rAJ ‚| ¡ dksIJ ‚|d|jdd dd¡ 7 }| j jjj||d	  ¡ || d
| ¡ ¡d}	|D ]}| 
¡ rwJ ‚|	jd| ¡ d|jdd| ¡ dd qo|d  ¡ D ]}
| j jj|
 }|r¢|	j|d q‘|	jS )zLFile a bug report for a report.

            Return the bug ID.
            Nzsome descriptionr·   z

Tr¹   r}   rŸ   r”   r|   r  r–   Fr¼   rm  r5  )r8   rq   rR   rÄ   rÅ   rÆ   rt   rÇ   rÈ   rÉ   rÊ   rË   rÏ   r©   rb   r™   rÖ  r   r6   r€   rÑ   rÒ   r@  rA  rˆ   )rH   rÍ  ru   r›   r×   ra   rØ   ÚheaderrÙ   rÖ   Ú
subscriberr  r   r   r   rÊ  â  sD   



ù

ü€z_T._file_bugc                 C   s>   | j jj| }| j j|jvr|j| j jg |_| ¡  dS dS )z$Mark a report ID as needing retrace.N)r8   rb   r™   r:   r¤   rÎ   rü   r   r   r   r    s
   þz_T._mark_needs_retracec                 C   s6   | j jj| }d|jvr|jdg |_| ¡  dS dS )z,Mark a report ID as needing duplicate check.r  N)r8   rb   r™   r¤   rÎ   rü   r   r   r   Ú_mark_needs_dupcheck$  s
   
þz_T._mark_needs_dupcheckc                 C   óB   | j jj| }t|jƒ}t|ƒdksJ ‚|d }d|_| ¡  dS )zClose a report ID as "fixed".r+   r   ró   N©r8   rb   r™   r'  rä   r)  rö   rÎ   ©rH   rˆ   rÖ   r%   rh   r   r   r   r2  ,  ó   
z_T._mark_report_fixedc                 C   r:  )zReopen a report ID as "new".r+   r   ÚNewNr;  r<  r   r   r   r3  6  r=  z_T._mark_report_newc                 C   s    | j jj| }|  d|j¡ dS )z.Verify that report ID is marked as regression.rO  N)r8   rb   r™   râ  r¤   rü   r   r   r   r  @  s   z_T._verify_marked_regressionc                 C   sv  t j d¡pd}tt j d¡d|dœƒ}|  |jd¡ t d¡}d|d	< d
|d< | ¡  | 	¡  |  | 
¡ d¡ |  ||¡}|  |jd¡ |  ||¡}|  |dk¡ tj d| j|f ¡ | |¡}d|d< d|d< d|d< | ||d¡ | |¡}|  | |¡d¡ | |||  ¡ ¡ |  | |¡|  ¡ ¡ |  | |¡d¡ | ||d¡ |  | |¡d¡ |  | |¡d¡ dS )z7reporting crashes against a project instead of a distror(   rz   r  r´  r  Nr   rÏ  rÐ  rÑ  rT  rÒ  r   rô  ré  rÜ  rå  r8  ræ  r9  rê  r  )r4   r  r6   r'   rÇ  r-   r   r˜   r·  r¸  r€   rÈ  r   rÊ  rÉ  rS   rT   rU   ro   rµ   rã   r0  rN  rØ  rÿ   )rH   r)   r8   rÌ  rÍ  rˆ   r   r   r   Útest_projectF  sB   ÿÿ

ÿ

z_T.test_projectc                 C   sH   | j  |  ¡ ¡}|  |d d¡ |  |d d¡ |  |d  d¡¡ dS )z*download() of uncommon description formatsr   r’   re  Úamd64r]  zUbuntu N)r8   rµ   rØ  rÇ  rÉ  rY  )rH   rÌ  r   r   r   Útest_download_robustnessv  s   z_T.test_download_robustnessc                    sx  t j d¡pd}tt j d¡d|dddœƒ}d}|jj|jd	  j‰ d
}zst||d ƒD ]N}|d7 }t	j
 d| ¡ | t ¡ ||  ¡ ¡ |jj|  ¡  }|jd |jv }t‡ fdd„|jD ƒƒ}|dkrp|  |¡ |  |¡ q,|  |¡ |  |¡ q,W t||| ƒD ]}t	j
 d| ¡ | t ¡ |d¡ qƒnt||| ƒD ]}t	j
 d| ¡ | t ¡ |d¡ qŸw t	j
 d¡ dS )z,Escalating bugs with more than 10 duplicatesr(   rz   r  r$  Ú
omgkittenszapport-hackers)r-   r)   r2  r4  r   r4  iV'  é   r+   z%i r2  c                    s   g | ]}|j ˆ k‘qS r   )rþ   )r!   Ús©rI  r   r   ra  ”  s    z&_T.test_escalation.<locals>.<listcomp>r1  zR%i NÚ
)r4   r  r6   r'   rb   r@  r=   r   ÚrangerS   rT   rU   rN  r   r˜   rÎ  r™   r¤   Úanyr   r	  rÉ  )rH   r)   r6  ÚcountÚ	first_dupr¯   Úhas_escalation_tagÚhas_escalation_subscriptionr   rE  r   Útest_escalation  sB   ýÿ

ôþþz_T.test_escalationc                 C   s„  |   |  ¡ ¡ | j ¡ }|  |  ¡ |¡ | jjj|  ¡  }|jr'd|_| ¡  |j	d }| jjj
d |_| ¡  |j| jjjd d | j |  ¡ ¡}| j |  ¡ |¡ | j ¡ }|  |  ¡ |¡ |  || t|  ¡ gƒ¡¡ | jjj|  ¡  }|  |j	d jd¡ |  |j	d jd¡ |  |j	d jd¡ |  |j	d jd	¡ |  |j	d jd¡ |  |j	d jd
¡ |  | j |  ¡ ¡d¡ dS )z9source package task fixup for marking interpreter crashesFr   r$  r³  )rå   r>  rà   r+   zcoreutils (Ubuntu)rá   N)r9  rÔ  r8   r  râ  rb   r™   r>  rÎ   rä   rj   rå   ÚaddTaskrk   rµ   rU  rí  rÇ  r  r$   rd   rö   rê   r0  )rH   r-  r¯   rh   rÌ  r.  r   r   r   Útest_marking_python_task_mangle¡  s4   


ÿz"_T.test_marking_python_task_manglerÛ  c              	   C   s0  d}t  ¡ }tj ¡ }z†t ¡ }t t	j
|¡ t  |¡ tddƒ}| d¡ W d  ƒ n1 s1w   Y  t g d¢¡dksAJ ‚t j d¡sIJ ‚tjg d¢tjd	 t j d
¡s[J ‚t dg¡ tjg d¢tjd	dksoJ ‚t j |d¡|d< t j |d
¡f|d< ||d< | ¡  W t  |¡ |S t  |¡ w )a  Create a test executable which will die with a SIGSEGV, generate a
            core dump for it, create a problem report with those two arguments
            (ExecutablePath and CoreDump) and call add_gdb_info().

            Return the apport.report.Report.
            Núcrash.cÚwzS
int f(x) {
    int* p = 0; *p = x;
    return x+1;
}
int main() { return f(42); }
)Úgccz-grP  z-oÚcrashr   rS  )Úgdbz--batchú--exÚrunrU  zgenerate-core-file corez./crash)ÚstdoutÚcoreÚsync)Úreadelfz-nrX  rÐ  rg  rÚ  )r4   Úgetcwdr   ru   r˜   rB   rC   rD   rE   rF   rG   Úchdirr   rU   Ú
subprocessÚcallrW   ÚexistsÚPIPEÚ
check_callr¦   rÆ  )r~  ÚsignalÚworkdirÚorig_cwdÚprÚfdr   r   r   rÃ  Ê  s:   

ÿ
ÿ
ÿÿ

þz_T._generate_sigsegv_report)Fr  )rÛ  ).r‚  rƒ  r„  rÅ  rá  rº  r¹  r†  ro   rÎ  rÔ  rØ  rä  rî  rð  rÿ  r  r  r  r  r
  r  r  r  r#  r,  r/  r0  r±  Úobjectr'   r5  r‡  rµ  rÈ  rÊ  r  r9  r2  r3  r  r?  rA  rM  rO  rÃ  r   r   r   r   r²  h  sV    

#
$"Q #!6##


	
:

0	")r²  )Nr{   );r…  rB   rD   Úos.pathr4   rf   r«   rS   rÄ   r‹  rF   Úhttplib2r   Úior   r¡  r¢  Úurllib2r   r   r   Úhttplibr   Úurllibr	   r
   rR   Úurllib.requestÚurllib.parseÚhttp.clientÚlaunchpadlib.errorsr   Úlaunchpadlib.launchpadr   ÚImportErrorÚapport.crashdbr   rW   Ú
expanduserr7   r   r&   r8   r'   r‰  rˆ  r‘  rr   r‚  Úunittestr]  Úunittest.mockr±  rÂ  rÓ  rÕ  ÚTestCaser²  Úmainr   r   r   r   Ú<module>   s|   Hþ       F
7              à