
    !f<                        d Z ddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlZ	 ddlmZ ddlmZmZ ddlmZ ddlmZ ddlZddlZddlZddlm Z! dZ"ejF                  jI                  d	      Z%d
 Z&d Z' G d dejP                  jR                        Z)da* G d dejV                  jX                        Z- G d dej\                  j^                        Z0ddZ1y# e$ r dZY w xY w)z,Crash database implementation for Launchpad.    N)FailedToDecompressContent)	HTTPErrorRestfulError)	Launchpad)lookup_web_root)impl
productionz%~/.cache/apport/launchpad.credentialsc              #     K   | D ]P  }	 |j                   j                         }|j                  }|j                  d      s|j                  d      sM| R y # t        t        f$ r" t        j
                  j                  d       Y w xY ww)Nz"Broken attachment on bug, ignoring.txt.gz)	dataopenr   r   apportloggingerrorfilenameendswith)attachments
attachmentfnames       ?/usr/lib/python3/dist-packages/apport/crashdb_impl/launchpad.pyfilter_filenamer   1   s     ! 
	$$&A zz== DMM%$8G 45 	NN  !EF	s-   BA.BB.B
B	B

Bc                 &    t        d | D              S )Nc              3      K   | ]6  }t        |j                  j                  d       j                                8 yw)/N)int	self_linksplitpop).0is     r   	<genexpr>zid_set.<locals>.<genexpr>?   s-     @Qs1;;$$S)--/0@s   <>)set)taskss    r   id_setr&   =   s    @%@@@    c                   ,   e Zd ZdZd Zed        Zd Zed        Zd$dZ	de
fd	Zd
 Zdej                  j                  dede
fdZd Z	 	 	 d%dZd&dZd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Zd Z d Z!d'dZ"d Z#d  Z$d! Z%d" Z&e'd#        Z(y)(CrashDatabasez5Launchpad implementation of crash database interface.c                 h   t        j                  d      rt        j                  d      |d<   |sD|j                  d      }|r+dj                  t        |j                  dd      d   f      }nt        }t        j                  j                  j                  | ||       |j                  d      | _
        | j                  rd|vsJ d	       d|v sJ d
       d|v rd|d    d| _        ndt        j                          d| _        || _        || _        | j                  sJ d| _        d| _        t        j                  d|j                  d            | _        | j$                  sLt'        j(                  d      | _        t+        j,                  t.        j0                  | j$                  d       yy)aO  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 overridden or set by
          $APPORT_LAUNCHPAD_INSTANCE environment. For example: "qastaging" or
          "staging".
        - 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--retraceNAPPORT_LAUNCHPAD_CACHE	cache_dirzlaunchpadlib.cache.)prefixT)ignore_errors)osgetenvgetjoindefault_credentials_pathr   r   crashdbr)   __init__r0   arch_tag	packagingget_system_architectureoptionsauth_CrashDatabase__launchpad_CrashDatabase__lp_distro_CrashDatabase__lpcachetempfilemkdtempatexitregistershutilrmtree)selfrC   rB   lp_instances       r   r>   zCrashDatabase.__init__E   s   D 9901,.II6Q,RG()!++&:;Kxx-{/@/@/J2/NO 0$$--dD'Bkk(+;;(A@A( W$BAB$ W$#GN$;#<HEDM#I$E$E$G#HQDM	yyy#;W[[=UV~~%--5JKDNOOFMM4>>N r'   c                    | j                   r| j                   S t        It        j                  j	                  dt        j
                  d    d       t        j                  d       | j                  j                  d      r| j                  j                  d      }nt        }t        j                  j                  | j                        }|r4t        j                  j                  |      st        j                  |       	 t        j                   d|| j"                  dg| j                  d	
      | _         | j                   S # t$        t&        t(        f$ ri}t*        j,                  j/                  dt1        |dt3        |            | j                         t        j                  d       Y d}~| j                   S d}~ww xY w)zReturn Launchpad instance.NzERROR: The launchpadlib Python r   zK module is not installed. Please install the python3-launchpadlib package!
r.   r,   zapport-collectWRITE_PRIVATEz1.0)launchpadlib_dirallow_access_levelscredentials_fileversionzZconnecting to Launchpad failed: %s
You can reset the credentials by removing the file "%s"contentc   )rD   r   sysstderrwriterT   exitrB   r:   DEFAULT_LAUNCHPAD_INSTANCEr8   pathdirnamerC   isdirmakedirs
login_withrF   r   OSError
ValueErrorr   r   r   getattrstr)rM   r,   auth_dirr   s       r   	launchpadzCrashDatabase.launchpad   sd    ###JJ1#++a.1A BF G
 HHQK<<01!%!1!12F!G!;77??499-BGGMM(3KK!	(33 "!%%4$5!% D"  gz2 	NN  Jy#e*5			 HHRLL	s   5E GAF>>Gc              #     K   | j                   sy |D ]j  }|j                  j                         | j                   k(  s=t        j                  d| j                    d|j                  j                               sg| l y w)Nz^.+\(z.*\)$)r0   bug_target_namelowerrematch)rM   r%   ts      r   _get_distro_taskszCrashDatabase._get_distro_tasks   sm     {{ 	A  &&(DKK7288U+Q->->-D-D-F< 		s   A6B 9B c                 R   | j                   | j                  r4| j                  j                  | j                     | _         | j                   S d| j                  v r7| j                  j
                  | j                  d      | _         | j                   S t        d      | j                   S )z,Return Launchpad distribution (e.g. ubuntu).r1   z:distro or project needs to be specified in crashdb options)rE   r0   rf   distributionsrB   projectsSystemError)rM   s    r   	lp_distrozCrashDatabase.lp_distro   s     #{{#'>>#?#?#L   dll*#'>>#:#:4<<	;R#S  	 "P  r'   Nc                     | j                  |      sJ | j                  |      }t        ||| j                               }|j	                          |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)rM   reportprogress_callbackuser_message_callback	blob_filetickets         r   uploadzCrashDatabase.upload   sT     ||F###..v6	Y(9DDUDUDWXvr'   returnc                     | j                   j                  dt              }t        j                  j                  t        |            }|j                  S )z/Return the hostname for the Launchpad instance.r,   )rB   r:   r[   urllibparseurlparser   netloc)rM   r,   urls      r   rx   zCrashDatabase.get_hostname   sC    !\\-- "<
 ll##O4F$GHzzr'   c                     i }|j                  d|j                               }|r||d<   | j                         }d|v r|d   }n| j                  j                  d      }|r+d| d| d| dt        j
                  j                  |       S d	|v r;d| d| j                   d
|d	    d| dt        j
                  j                  |       
S d| d| j                   d| dt        j
                  j                  |       S )a8  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.
        Titlezfield.title
SnapSourcer1   https://bugs.r   z
/+filebug/?SourcePackagez	/+source/)r:   standard_titlerx   rB   r   r   	urlencoder0   )rM   rz   handleargstitlert   r1   s          r   get_comment_urlzCrashDatabase.get_comment_url   s&    

7F$9$9$;<"'D$$&6!\*Gll&&y1Gz7):fXFLL**4013
 f$z4;;- 8?+,JvhFLL**4013 H:Qt{{m:fX&&t,-/	
r'   rz   crash_idc                 .    d| j                          d| 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   z/bugs/)rx   )rM   rz   r   s      r   
get_id_urlzCrashDatabase.get_id_url  s!     t00236(DDr'   c                 Z   t         j                  j                         }| j                  j                  |   }t        j                  d|j                  t
        j                        }|s@t        j                  d|j                  t
        j                  t
        j                  z        }|sJ d       |j                  d      j                  d      j                  dd      j                  dd	      }d
|v rZd|v rA|j                  dd      \  }}|j                  d
d	      dz   |j                  d
d      d   z   }n|j                  d
d      d   }|j                  t        j                   |             d|vr	 |j"                  j%                         |d<   d|vrqd|j(                  v rd|d<   n]d|j(                  v rd|d<   nId|j(                  v rd|d<   n5d|j(                  v rd|d<   n!t+        dt-        |j(                               |j/                  |j(                         d|v r|d   |d<   |j0                  |d<   t3        |j4                        D ]  }t6        j8                  j;                  |j<                        \  }	}
	 d||	<   |
dk(  r,|jC                         ||	<   	 ||	   jE                  d      ||	<   f|
dk(  r4	 tI        jJ                  |      5 }|jC                         ||	<   ddd       tQ        d |j<                          |S # t&        $ r |j"                  |d<   Y w xY w# t>        t@        t*        f$ r Y w xY w# tF        $ r Y w xY w# 1 sw Y   mxY w# tL        $ r=}dt-        |      vr |jO                  d       |jC                         ||	<   Y d}~Od}~ww xY w)!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.   UTF-8s        s   
   
s   

s   Uname:r   DateProblemTypez
apport-bugBugapport-crashCrashzapport-kernelcrashKernelCrashzapport-packagePackagez(cannot determine ProblemType from tags: r   OriginalTitle r   r   )fileobjNz
Not a gzipzUnknown attachment type: ))r   rz   Reportrf   bugsrj   searchdescriptionSMgroupencodereplacer   loadioBytesIOdate_createdctimeAttributeErrortagsrb   rd   add_tagsr   r   r   r8   r\   splitextr   AssertionError	TypeErrorreaddecodeUnicodeDecodeErrorgzipGzipFilera   seekNotImplementedError)rM   r   rz   bmr   part1part2r   keyextgzr   s                r   downloadzCrashDatabase.download  s~    %%'NN) II*AMM244@		11=="$$+NALLLq GGAJVG_W[$'WWe$	 	 k! K' "-!2!29a!@MM'51 kk'1-a01  *//;A>BJJ{+,
0!"!5!5!7v &qvv%(-}%166)(/}%%/(5}%!QVV+(1}% >s166{mL  	f&,WoF?#''w)!--8 	Jww''
(;(;<HC s f}(oo/s"(+"4"4W"=F3K 	4z: 0b&(ggis0 */
0C0C/DE 3	8 i " 0!"v0: #Iz:  * 0 0 4 $3u:5OOA&",//"3F3K4sf   9L L/$M	M$M,M$L,+L,/MM		MMM!	M$$	N*-2N%%N*c           
         | j                   j                  |   }|r&t        |j                               t        |      z
  }nd}t	        j
                         5 }	|j                  |	|       |	j                          |	j                  d       t        j                  |	      }
|
j                         }t        |      }|j                         sJ t        |      }|j                         rJ |j                         dk(  sJ |sy|j                  dd }|j!                  d       d|v r%|| j#                  |d         j%                         z  }||_        |j'                          | j                   j                  |   }|j)                  d      j+                  d	d
      }|rJ|r'|j,                   d| |_        |j'                          n!|s|j.                  }|j1                  ||       |D ]I  }|j3                  |xs d|j5                         d|j)                  d      |j5                         d       K 	 ddd       y# 1 sw Y   yxY w)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   z
text/plainzapport-collectedTagsT)r   r   r   z
--- 
rU   subjectr   F)commentr   content_typer   r   is_patch)rf   r   r$   keysrG   TemporaryFile
write_mimeflushr   emailmessage_from_binary_filewalknextis_multipartget_content_typer   append_filter_tag_namesr   lp_saveget_payloadr   r   r   
newMessageaddAttachmentget_filename)rM   r   rz   r   change_descriptionattachment_comment
key_filterbugr   mimemsgmsg_iterpartxtexts                  r   updatezCrashDatabase.update~  s    0 nn!!(+ FKKM*S_<II
 ##% 3	di8JJLIIaL006CxxzH >D$$&&& >D((***((*l::: HHQK+,V#//v?EEGGAnn))(3 ##4#077KD%),(9$&HCOKKM""%))NN4NA ! !!.4" $ 1 1 3!%)))6!..0" " W3	 3	 3	s   G IIc                 z   t         j                  j                  j                  | |||       | j                  j
                  |   }d|v r|j                  D ]v  }|j                  j                  j                  d      s)| j                  j                  |d         |_        |j                          | j                  j
                  |   } n |j                         r|j                  D ]#  }|j                  dk(  s	 |j!                          % 	 | j%                  |j                        }t'        |      }|j(                  dk(  rd|_        |j                          |j-                         }|rt/        j0                  d|j                        }|ri|j3                  d      |k7  rU|j3                  d	      |z   |j3                  d
      z   |_        	 |j                          | j                  j
                  |   }| j5                  ||       y# t"        $ r Y /w xY w# t*        $ r Y w xY w# t"        $ r Y Vw xY w)zUpdate 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.      N)r   r=   r)   update_tracesrf   r   	bug_taskstargetresource_type_linkr   rr   getSourcePackager   has_useful_stacktracer   r   removeFromBugr   rm   r   
importanceStopIterationstacktrace_top_functionrj   rk   r   _subscribe_triaging_team)	rM   r   rz   r   r   taskafnr   s	            r   r   zCrashDatabase.update_traces  s    	$$22467Snn!!(+f$ ;;11::?K"&.."A"A#O4 #B #DK LLN..--h7C '')__ 77m+)--cmm<Dz??k1&.DOLLN
 //1BHHLciiXr) !
R!''!* <CI ..--h7C%%c62/ %  !  % s7    HAH H. 	HH	H+*H+.	H:9H:c                     | j                   j                  |   }t        j                  d|j                        }|r|j                  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)rf   r   rj   r   r   r   rb   )rM   r   r   r   s       r   get_distro_releasez CrashDatabase.get_distro_release	  sJ     nn!!(+II:COOL771:DEEr'   c                 R   t        j                  d| j                   d      }| j                  j                  |   }g }|j
                  D ]Y  }|j                  |j                  j                        }|s+|j                  dv r:|j                  |j                  d             [ |S )z5Return list of affected source packages for given ID.r   z2/(?:(?P<suite>[^/]+)/)?\+source/(?P<source>[^/]+)$>   Invalid	Won't FixFix Releasedsource)rj   compiler0   rf   r   r   r   r   r   statusr   r   )rM   r   bug_target_rer   resultr   rk   s          r   get_affected_packagesz#CrashDatabase.get_affected_packages  s    

TU
 nn!!(+MM 	1D!(()>)>?E{{FFMM%++h/0	1 r'   c                     | j                   j                  |   }|j                  j                  | j                   j                  j                  k(  S )z3Check whether the user is the reporter of given ID.)rf   r   ownerr   me)rM   r   r   s      r   is_reporterzCrashDatabase.is_reporter$  s9    nn!!(+yy~~!2!2!7!777r'   c                 J   | j                   j                  |   }|j                  ry|j                  j                  | j                   j
                  j                  k(  ry| j                   j
                  j                  }|j                  j                  D ]  }|d   |k(  s y y)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)	rf   r   duplicate_ofr  r   r  r   subscriptionsentries)rM   r   r   r  subs        r   
can_updatezCrashDatabase.can_update)  s     nn!!(+99>>T^^..333 ^^(($$,, 	C=!R'	 r'   c                    	 | j                   j                  | j                  d      }t        |      S # t        $ rH}t
        j                  j                  dt        |             t        j                  d       Y d}~yd}~ww xY 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: %srV   N)rr   searchTasksr?   r&   r   r   r   r   rd   rW   rZ   rM   r   r   s      r   get_unretracedzCrashDatabase.get_unretraced@  sk    	>>--]], . D $< 	NN  !Es5zRHHRLL	s   14 	B>B  Bc                     	 | j                   j                  dd      }t        |      S # t        $ rH}t        j
                  j                  dt        |             t        j                  d       Y d}~yd}~ww xY 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  rV   N)
rr   r  r&   r   r   r   r   rd   rW   rZ   r  s      r   get_dup_uncheckedzCrashDatabase.get_dup_uncheckedL  sg    	>>--+< . D $< 	NN  !Es5zRHHRLL	s   '* 	A;>A66A;c                 P    | j                   j                  d      }t        |      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   )rr   r  r&   )rM   r   s     r   get_unfixedzCrashDatabase.get_unfixed]  s%     ~~))~)>d|r'   c                     | j                   j                  j                  d|| j                   j                        }|d   j                  S )zReturn 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   )rr   main_archivegetPublishedSourcescurrent_seriessource_package_version)rM   packagesourcess      r   _get_source_versionz!CrashDatabase._get_source_versioni  sI     ..--AA..77 B 
 qz000r'   c                 :   	 	  j                   j                  |   }|j                  ryt	        |j
                        } j                  rd j                  j                          d	t	        t        	fd|            }|st	        t         fd|            }|ryt        |      dkD  r,t        j                  j                  d j                  |       y|r=|j                         }	  j                  |j                  j!                         d	         S t	        t        	fd
|            }|rt	        t        	fd|            }|syyt	        t        d |            }|ryt	        t        d |            ryy# t        $ r Y yw xY w# 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).
        invalid()c                 ^    | j                   dk(  xr | j                  j                         v S Nr  r	  bug_target_display_nameri   r   distro_identifiers    r   <lambda>z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s0    !> "R)T-I-I-O-O-QQ r'   c                     | j                   dk(  xr5 | j                  j                         j                  j                         k(  S r5  )r	  rh   ri   r0   )r   rM   s    r   r:  z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s=    T[[N%B &P 00668DKK<M<M<OO r'   r   r.   zVThere is more than one task fixed in %s %s, using first one to determine fixed versionr   c                 \    | j                   dv xr | j                  j                         v S )N>   Expiredr  r  r6  r8  s    r   r:  z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s1    T[[4W%W &V-1M1M1S1S1UU r'   c                 \    | j                   dvxr | j                  j                         v S )N)r  r  r=  r6  r8  s    r   r:  z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s8    #F*G *D 1#;;AAC!D r'   c                      | j                   dk(  S r5  r	  r   s    r   r:  z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s    DKK>$A r'   c                      | j                   dk(  S )Nr  r@  rA  s    r   r:  z1CrashDatabase.get_fixed_version.<locals>.<lambda>  s    y(@ r'   N)rf   r   KeyErrorr  listr   r0   ri   filterlenr   r   warningr    r/  r7  r   
IndexError)
rM   r   r   r%   fixed_tasksfixed_distror   invalid_tasksnon_invalid_tasksr9  s
   `        @r   get_fixed_versionzCrashDatabase.get_fixed_versionw  s   $	##H-A >>Q[[!;;"#DKK$5$5$7#8 :RK #P   ;!#&&BKK	 "(%3344::<Q?  !%V! !(,D ")% -(  A5IK F@%HI a  		Z " %$%s#   E? ,+F ?	F
F	FFc                 f    | j                   j                  |   j                  }|r|j                  S y)zcReturn master ID for a duplicate bug.

        If the bug is not a duplicate, return None.
        N)rf   r   r  id)rM   r   r   s      r   r  zCrashDatabase.duplicate_of  s-    
 NN)6644Kr'   c                    | j                   j                  |   }|rY||k7  sJ dt        |       d       | j                   j                  |   }|j                  rI|j                  }|j                  }|j                  |k(  r"t
        j                  j                  d||       y|j                  D ]"  }|j                  dv s	 |j                          $ | j                   j                  |   }|j                  d| dd	       | j                   j                  |   }|j                  rd
|_        |j                  s||_        |j                  }t        |j                         dk(  rd| j"                  v rW| j"                  d   |vrF| j"                  j%                  dd      |vr(|| j"                  d   gz   |_        |j'                          d| j"                  v rV| j"                  j%                  dd      |vr8| j                   j(                  | j"                  d      }|j+                  |       |j-                         rd|v sd|v r~| j/                  ||d| g d       | j                   j                  |   }|j                  dd }		 |	j1                  d       	 |	j1                  d       |	|_        	 |j'                          dg}
| j4                  j6                  D ],  }|j8                  dvr|
j;                  |j<                         . t?        |j                        }|j                  }|jA                  |      }|D ]  }||
v s|j;                  |        ||_        |j'                          n|j                  rd|_        |jB                  r|j'                          yy# t        $ r Y w xY w# t2        $ r Y &w xY w# t2        $ r Y $w xY w# t        $ r Y w xY w)zlMark a crash id as duplicate of given master ID.

        If master is None, id gets un-duplicated.
        zcannot mark bug z as a duplicate of itselfzNBug %i was manually marked as a dupe of newer bug %i, not closing as duplicateN>   ProcMaps.txtRegisters.txtProcStatus.txtStacktrace.txtDisassembly.txtThreadStacktrace.txtr   zThank 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 #aE  , 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 )
StacktraceThreadStacktracer   DependenciesProcMapsProcCmdline)r   zbugpattern-needed)zActive DevelopmentzCurrent Stable Release	SupportedzPre-release Freeze)"rf   r   rd   r  rO  r   r   rG  r   r   r   r   r   privater   rF  
duplicatesrB   r:   r   people	subscriber   r   removerb   rr   seriesr	  r   r   r$   
difference_dirty_attributes)rM   rz   r   	master_idr   masterr   master_tagspr   tags_to_copyri  	dupe_tagsmissing_tagstags                  r   close_duplicatezCrashDatabase.close_duplicate  s    nn!!(+I%K!#h-0IJK% ^^((3F"",,"II	99(NN**4 !	 __ 77  )  ..%%h/CNN K  	 2   ..%%h/C{{# ###)  !++K6$$%+$4%56kI((+F&' #.%561 #FK NN$ .=((+F&' --dll;T.UVA$$A$. ++-(K7*k9=hZH 	   ,,Y7KKNHH45HH56  NN$
 00L..// 1== )  ##FKK01 CHHI ++K$//<L# ,,&&&s+, &FKNN#C   KKM !g % V "  " 
 ! sH   N'N7 O 7O '	N43N47	OO	OO	O$#O$c                     | j                   j                  |   }|j                  d| dd       | j                   j                  |   }|j                  dgz   |_        |j	                          y)zyMark 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 #z. 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   zregression-retracerN)rf   r   r   r   r   )rM   r   rm  r   s       r   mark_regressionzCrashDatabase.mark_regression  su     nn!!(+H 
 3 	 	
 nn!!(+88455r'   c                 
   | j                   j                  |   }| j                  |j                  v rC|j                  dd }|j	                  | j                         ||_        	 |j                          yy# t        $ r Y yw xY w)zMark crash id as retraced.N)rf   r   r?   r   rh  r   r   )rM   r   r   r   s       r   mark_retracedzCrashDatabase.mark_retraced  sq    nn!!(+==CHH$AHHT]]#CH %  s   $A6 6	BBc                    | j                   j                  |   }|r	 | j                  |j                        }t	        |      }d|_        |j                          |j                  |d       |j                  D ]#  }|j                  dk(  s	 |j                          % yd|j                  vr&|j                  dgz   |_        |j                          yy# t
        $ r |j                  d   }Y w xY w# t        $ r Y w xY w)z%Mark crash id as 'failed to retrace'.r   r  z Crash report cannot be processedr   r   r]  N)rf   r   rm   r   r   r   r	  r   r   r   r   r   r   r   )rM   r   invalid_msgr   r   r   s         r   mark_retrace_failedz!CrashDatabase.mark_retrace_failed  s    nn!!(+(--cmm<Dz $DKLLNNN#-O   __ 77m+) %CHH4xx#:";;CHKKM 5 ! (}}Q'( % s#   &C C3C0/C03	C?>C?c                    | j                   j                  |   }d|v r|j                  D ]w  }|j                  j                  j                  d      s)| j                  j                  |d         |_        	 |j                          | j                   j                  |   } n d|j                  v r|j                  dd }|j                  d       ||_
        |j                          d|v rQ|j                  D ]B  }d|j                  j                  v s|j                  dk(  s,d|_        |j                          D | j                  ||       y# t        $ r Y w xY w)	z/Mark crash id as checked for being a duplicate.r   r   r   r!  N	Tracebackr   r   )rf   r   r   r   r   r   rr   r   r   r   r   rh  r   r   )rM   r   rz   r   r   r   s         r   _mark_dup_checkedzCrashDatabase._mark_dup_checked  s?   nn!!(+ f$ ;;11::?K"&.."A"A#O4 #B #DK"nn11(;
  "SXX-AHH+,CHKKMf$MM +D&$++*H*HH??k9.6DO LLN	+
 	%%c62# %  s   8)E	EEc                    t         j                  j                  j                  | |      }|s|S ||d<   	 t        j
                  j                  | d      5 }|j                         }|j                  d      s
	 ddd       y|D ]9  }|j                  d      rd|v sd|v r
 ddd       y n|j                         r9 n ddd       |S # 1 sw Y   |S xY w# t        $ r |cY S w xY w)	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:NTs   tags:s   apport-failed-retraces   apport-request-retrace)r   r=   r)   knownr   requesturlopenreadline
startswithstripra   )rM   rz   r   r   lines        r   r  zCrashDatabase.known  s    * nn**00v>J !$}	''3%v7 1zz|w/     Dx04<8D@#'    ::<2 
32 
  	 J	sM   "C #C:C C"C +C>CC CC C C&%C&c                    d|v r|d   j                         d   dk7  ry| j                  j                  dd      }| j                  j                   d| }|j                  t        | j                  j                        d      j                  d      |j                  D cg c]!  }t        |      j                  d	d
      d   # c}vr|j                  |       yyc c}w )z-Subscribe the right triaging team to the bug.DistroReleaser   UbuntuNtriaging_teamzubuntu-crashes-universe~r   r   r.   )maxsplitr/   r[  )
r   rB   r:   rf   	_root_urir   rd   r  r  rg  )rM   r   rz   teamr\  r  s         r   r   z&CrashDatabase._subscribe_triaging_team  s    
 f$)@)F)F)H)Kx)W ||1JKNN,,-Qtf5~~c$..":":;R@FFsK;>;L;LP
47CHNN3N+B/P
 
 MMM(
 P
s   !&Cc                    dd|d   j                          i}|j                  d      }|r|dk(  r|j                  d      }|r|dxx   d| z  cc<   d|v r"|dxx   d| j                  |d          z  cc<   d|v ro|rRd	|v sd
|v sd|v rFd|d<   |j                  d| j                  j                  dd            |d<   |dxx   d| dz  cc<   nd|v rd|d<   d|d<   |dxx   dz  cc<   d|v rd|d   vr|dxx   dz  cc<   d|v r|d   |d<   g d}t	        j
                         }|j                  ||g d|       |j                          |j                  d       |S )zGenerate a multipart/MIME temporary file for uploading.

        You have to close the returned file object after you are done with it.
        r   zapport-r   PackageArchitectureallArchitecture r  VmCoreCoreDumpLaunchpadPrivateyesPrivateLaunchpadSubscribeinitial_subscriberr   Subscribersz need-r3   r}  z need-duplicate-checkDuplicateSignaturer!  CheckboxSubmissionzHWDB-Submission)	r   r  r   
RegressionReproducibleTestedUpstreamProcVersionSignatureUnameNonfreeKernelModules)r   r  r  )extra_headersr   priority_fieldsr   )	ri   r:   r   rB   rG   r   r   r   r   )rM   rz   hdrr   orderr   s         r   rv   z#CrashDatabase._generate_upload_blob*  s    !6!<!<!> ?@AJJ,-AJ

>*AKQqc7"KVKQt55fVnEFGGK f$F"'%/!&I%+ZZ(LL$$%98D&M" Fs(33&!&I%-M"F666).DCPVK.WK22K  6)%+,@%AC!"

 %%'H!	 	 	
 	

		!r'   c                     d}| j                         j                  dd      D ],  }|dv st        |      dkD  r|dv r|t        |      z  }(|dz  }. |S )	zJReplace characters from tags which are not palatable to
        Launchpad.r   ASCIIignore)errorss%   abcdefghijklmnopqrstuvwxyz0123456789 r   s   +-.r-   )ri   r   rF  chr)r   reschs      r   r   zCrashDatabase._filter_tag_namesn  se     **,%%gh%? 	B==C1vs2ws
	 
r'   )NN)FNN)r   N))__name__
__module____qualname____doc__r>   propertyrf   rm   rr   r   rd   rx   r   r   rz   r   r   r   r   r   r   r  r  r  r  r  r"  r$  r/  rM  r  rt  rv  rx  r{  r~  r  r   rv   staticmethodr    r'   r   r)   r)   B   s   ?FOP (  ( T    $c $
LE!5!5 E E EaP !Vp13fF$8
.
"
1dL]~$
63B7r)"BH  r'   r)   c                       e Zd ZdZd Zy)HTTPSProgressConnectionzWImplement a HTTPSConnection with an optional callback function for
    upload progress.c                    t         s+t        j                  j                  j	                  | |       y d}t        |      }d}||k  rt        ||       t        j                         }t        j                  j                  j	                  | ||||z           ||z  }t        j                         }|dkD  r||z
  dk  r|dz  }n||z
  dkD  r|dz  }||k  ry y )Nr   i   g      ?r.   r   )_https_upload_callbackhttpclientHTTPSConnectionsendrF  time)rM   r   senttotal	chunksizet1t2s          r   r  zHTTPSProgressConnection.send  s    %KK'',,T48D		Ul"4/BKK'',,T4y@P3RSIDB 47S=!OI"Wq[!OI Ulr'   N)r  r  r  r  r  r  r'   r   r  r    s    $r'   r  c                       e Zd ZdZd Zy)HTTPSProgressHandlerzTImplement a HTTPSHandler with an optional callback function for
    upload progress.c                 .    | j                  t        |      S r  )do_openr  )rM   reqs     r   
https_openzHTTPSProgressHandler.https_open  s    ||3S99r'   N)r  r  r  r  r  r  r'   r   r  r    s    :r'   r  c                    d}d| d}|a t        j                  j                  j	                         }t        j                  j
                  j                  d      }|j                  dd       |j                  |       t        j                  j                  j                  dd      }|j                  dd	       |j                  | j                         j                  d
             |j                  |       t        j                         }t        j                   j#                  |d      }	|	j%                  |       t&        j(                  j+                  ||j-                               }
|
j                  dd|j/                                 t&        j(                  j1                  t2              }|j5                  |
      }|j7                         j9                  d      }|sJ |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', 'qastaging.launchpad.net', or
    'staging.launchpad.net' to use another instance for testing.
    Nzhttps://z/+storeblob1zContent-Dispositionzform-data; name="FORM_SUBMIT"applicationzoctet-streamz*form-data; name="field.blob"; filename="x"asciiF)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   r   	generatorBytesGeneratorflattenr   r  Requestgetvalueget_boundarybuild_openerr  r   infor:   )blobr{   rt   r~   r   r   submit	form_blob	data_flatgenr  openerr  s                r   rw   rw     s    FXJk
*C / ::--/DZZ__%%c*F
+-LMKK

((GIK $))+,,W56KK	

I
//
(
(
(
GCKK ..
 
 i&8&8&:
;CNN89J9J9L8MN ^^(()=>F[[F[[]78FM6Mr'   )Nzlaunchpad.net)2r  rI   r   r   http.clientr  r   os.pathr8   rj   rK   rW   rG   r  urllib.parser   urllib.requesthttplib2r   launchpadlib.errorsr   r   launchpadlib.launchpadr   launchpadlib.urisr   ImportErrorapport.crashdbr   apport.loggingapport.reportapport.packaging_implr   r@   r[   r\   
expanduserr<   r   r&   r=   r)   r  r  r  r  r  HTTPSHandlerr  rw   r  r'   r   <module>r     s    2      	  	  
    2;01
    3) 77--.UV 	A
yFNN00 y@"  $dkk99 $::6>>66 :1U$  Is   C+ +C54C5