HEX
Server: Apache
System: Linux scp1.abinfocom.com 5.4.0-216-generic #236-Ubuntu SMP Fri Apr 11 19:53:21 UTC 2025 x86_64
User: confeduphaar (1010)
PHP: 8.1.33
Disabled: exec,passthru,shell_exec,system
Upload Files
File: //lib/python3/dist-packages/apport/crashdb_impl/__pycache__/launchpad.cpython-38.pyc
U

��8d�N�@s�dZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlmZddl
mZejjdkr�ddlmZmZmZddlmZddlmZmZeeeeeefd	Zn4dd
lmZmZmZmZddlmZddlmZdZz dd
lmZddl m!Z!e!Wne"k
�r$dZ!YnXddl#Z$ddl$Z$ej%�&d�Z'dd�Z(dd�Z)Gdd�de$j*j+�Z+da,Gdd�de�Z-Gdd�de�Z.d!dd�Z/e0dk�r�ddl1Z1ddl2Z2ddl3m4Z4da*da5da6da7Gdd �d e1j8�Z9e1�:�dS)"z,Crash database implementation for Launchpad.�N)�FailedToDecompressContent)�BytesIO�)�HTTPSHandler�Request�build_opener)�HTTPSConnection)�	urlencode�urlopenT)rrrr
)r	F)�	HTTPError)�	Launchpadz%~/.cache/apport/launchpad.credentialsc
csd|D]Z}z|j��}Wn&ttfk
r<t�d�YqYnX|j}|�d�sX|�d�r|VqdS)Nz"Broken attachment on bug, ignoring�.txt�.gz)�data�openrr�apport�error�filename�endswith)�attachments�
attachment�f�name�r�?/usr/lib/python3/dist-packages/apport/crashdb_impl/launchpad.py�filter_filename,s

rcCstdd�|D��S)Ncss"|]}t|j�d����VqdS)�/N)�int�	self_link�split�pop)�.0�irrr�	<genexpr>:szid_set.<locals>.<genexpr>)�set)�tasksrrr�id_set8sr&c@seZdZdZdd�Zedd��Zdd�Zedd	��ZdAdd�Z	d
d�Z
dd�Zdd�Zdd�Z
dBdd�ZdCdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Zd/d0�Zd1d2�Zd3d4�ZdDd5d6�Zd7d8�Zd9d:�Zd;d<�Z d=d>�Z!e"d?d@��Z#d
S)E�
CrashDatabasez5Launchpad implementation of crash database interface.cCst�d�rt�d�|d<|sH|�d�}|rDtd|�dd�d}nt}tjj�|||�|�d�|_	|j	r~d|ks�t
d	��nd|ks�t
d
��d|kr�d|d|_ndtj�
�|_||_||_|js�t
�d
|_d
|_t�d|�d��|_|j�stjdd�|_tjtj|jdd�d
S)aInitialize 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�architecturezneed-%s-retraceNZAPPORT_LAUNCHPAD_CACHEZ	cache_dirzlaunchpadlib.cache.)�prefixT)�
ignore_errors)�os�getenv�get�default_credentials_pathrr�crashdbr'�__init__r-�AssertionError�arch_tag�	packaging�get_system_architecture�options�auth�_CrashDatabase__launchpad�_CrashDatabase__lp_distro�_CrashDatabase__lpcache�tempfile�mkdtemp�atexit�register�shutil�rmtree)�selfr=r<Zlp_instancerrrr7@s4!
�

zCrashDatabase.__init__c
Cs|jr|jStdkr<tr&tj�d�ntj�d�t�d�|j�d�rV|j�d�}nd}t	j
�|j�}|r�t	j
�
|�s�t	�|�z"tjd||jdg|jd	d
�|_WnVtk
r�}z8t|d�r�|j}nt|�}t�d||j�t�d
�W5d}~XYnX|jS)zReturn 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)Z
productionzapport-collectZ
WRITE_PRIVATEz1.0)Zlaunchpadlib_dirZallow_access_levelsZcredentials_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<r4r2�path�dirnamer=�isdir�makedirsZ
login_withr@�	Exception�hasattrrI�strrr)rGr)Zauth_dir�e�msgrrr�	launchpad�s8

�
zCrashDatabase.launchpadccsF|js
t�|D]2}|j��|jks:t�d|j|j���r|VqdS)Nz^.+\(%s.*\)$)r-�
StopIteration�bug_target_name�lower�re�match)rGr%�trrr�_get_distro_tasks�s�zCrashDatabase._get_distro_taskscCsP|jdkrJ|jr"|jj|j|_n(d|jkrB|jj|jd|_ntd��|jS)Nr.z:distro or project needs to be specified in crashdb options)r?r-rY�
distributionsr<�projects�SystemError�rGrrr�	lp_distro�s

zCrashDatabase.lp_distroNcCs>|�|�st�|�|�}t|||��d�}|��|s:t�|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)Zacceptsr8�_generate_upload_blob�upload_blob�get_hostname�close)rG�report�progress_callbackZ	blob_file�ticketrrr�upload�s

zCrashDatabase.uploadcCs,|j�d�}|r$|dkrd}q(d}nd}|S)z/Return the hostname for the Launchpad instance.r)�stagingzstaging.launchpad.netz
launchpad.dev�
launchpad.net)r<r4)rGr)rfrrrri�szCrashDatabase.get_hostnamecCs�i}|�d|���}|r4t|t�s,|�d�}||d<|��}d|krN|d}n|j�d�}|s�d|kr�d||j|d|t|�fSd||j|t|�fSnd|||t|�fSd	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-8zfield.titleZ
SnapSourcer.�
SourcePackagez,https://bugs.%s/%s/+source/%s/+filebug/%s?%sz!https://bugs.%s/%s/+filebug/%s?%sN)	r4�standard_title�
isinstance�bytes�encoderir<r-r	)rGrkZhandle�args�titlerfr.rrr�get_comment_url�s>


���zCrashDatabase.get_comment_urlcCsdt|�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/)rV)rGrk�idrrr�
get_id_urlszCrashDatabase.get_id_urlcCs�t��}|jj|}t�d|jtj�}|sBt�d|jtjtjB�}|sNt	d��|�
d��d��dd��dd	�}d
|kr�d|kr�|�
dd�\}}|�d
d	�d|�
d
d�d}n|�
d
d�d}|�t|��d
|k�rz|j��|d
<Wn tk
�r
|j|d
<YnXd|k�r�d|jk�r,d|d<nTd|jk�rBd|d<n>d|jk�rXd|d<n(d|jk�rnd|d<ntdt|j���d�|j�|d<d|k�r�|d|d<|j|d<t|j�D�]}tj�|j�\}	}
zd||	<Wntk
�r�Y�q�YnX|
dk�rB|��||	<z||	� d�||	<Wnt!k
�r>YnXn~|
dk�r�zt"j#|d���||	<WnJt$k
�r�}z*d t|�k�r��|�%d�|��||	<W5d!}~XYnXntd"|j���q�|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+rrs � s
�
s

sUname:rZDate�ProblemTypez
apport-bug�Bug�apport-crash�Crashzapport-kernelcrashZKernelCrashzapport-package�Packagez(cannot determine ProblemType from tags: � �TagsrqZ
OriginalTitle�r
r)Zfileobjz
Not a gzipNzUnknown attachment type: )&r�ReportrY�bugsr]�search�description�S�Mr8�grouprw�replacer�loadrZdate_created�ctime�AttributeError�tags�
ValueErrorrV�joinryrrr2rP�splitextrrT�read�decode�UnicodeDecodeError�gzipZGzipFile�IOError�seek)rGr{rk�b�mr�Zpart1Zpart2r�keyZextrWrrr�downloadsp �










 zCrashDatabase.downloadFc	Cs�|jj|}|r&t|���t|�}nd}t��}	|j|	|d�|	��|	�d�t	rbt
�|	�}
n
t
�|	�}
|
�
�}t	r�|��p�|��}|��s�t�t	r�|��p�|��}|��r�t�|��dks�t�|�s|jdd�}
|
�d�d|k�r|
|�|d���7}
|
|_|��|jj|}|jdd��d	d
�}|�rp|�rV|jd||_|��n|�sb|j}|j||d�|D]2}|j|�p�d
|��d|jdd�|��dd��qt|	��dS)ahUpdate 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�rrr�z
--- 
�rIZsubjectr�F��commentr�Zcontent_typerrZis_patch) rYr�r$�keysrA�
TemporaryFile�
write_mime�flushr�rK�email�message_from_file�message_from_binary_file�walk�next�__next__�is_multipartr8�get_content_typer��append�_filter_tag_namesr�lp_save�get_payloadr�r�ry�
newMessage�
addAttachment�get_filenamerj)rGr{rkr��change_descriptionZattachment_comment�
key_filter�bugr��mimerX�msg_iter�part�x�textrrr�updateasT





�
zCrashDatabase.updater�c		Cs�tjj�||||�|jj|}d|krn|jD]>}|jj�	d�r.|j
j|dd�|_|��|jj|}qnq.|�
��r�|jD]0}|jdkr~z|��Wq~tk
r�Yq~Xq~z>|�|j�}tr�|��}n|��}|jdkr�d|_|��Wntk
�rYnX|��}|�r�t�d|j�}|�r�|�d�|k�r�|�d	�||�d
�|_z|��Wntk
�rtYnX|jj|}|�||�dS)z�Update the given report ID for retracing results.

        This updates Stacktrace, ThreadStacktrace, StacktraceTop,
        and StacktraceSource. You can also supply an additional comment.
        rs�
#distribution�r�CoreDump.gz�	Undecided�Mediumz,^(.*crashed with SIG.* in )([^( ]+)(\(\).*$)rr+�N)rr6r'�
update_tracesrYr��	bug_tasks�target�resource_type_linkrre�getSourcePackager��has_useful_stacktracerry�
removeFromBugrr`rKr�r��
importancerZZstacktrace_top_functionr]r^r��_subscribe_triaging_team)	rGr{rkr�r��task�a�fnr�rrrr��sJ





zCrashDatabase.update_tracescCs4|jj|}t�d|j�}|r(|�d�Std��dS)zNGet 'DistroRelease: <release>' from the given report ID and return
        it.z"DistroRelease: ([-a-zA-Z0-9.+/ ]+)r+z)URL does not contain DistroRelease: fieldN)rYr�r]r�r�r�r�)rGr{r�r�rrr�get_distro_release�s

z CrashDatabase.get_distro_releasecCs`t�d|j�}|jj|}g}|jD]4}|�|jj�}|s>q&|j	dkrJq&|�
|�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)r]�compiler-rYr�r�r�r�r�statusr�r�)rGr{Z
bug_target_rer��resultr�r^rrr�get_affected_packages�s�

z#CrashDatabase.get_affected_packagescCs|jj|}|jj|jjjkS)z3Check whether the user is the reporter of given ID.)rYr��ownerr�me�rGr{r�rrr�is_reporterszCrashDatabase.is_reportercCsZ|jj|}|jrdS|jj|jjjkr,dS|jjj}|jjD]}|d|kr>dSq>dS)aACheck 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)	rYr��duplicate_ofr�rr�r�
subscriptions�entries)rGr{r�r��subrrr�
can_update	s
zCrashDatabase.can_updatec
Cs^z|jj|jdd�}t|�WStk
rX}zt�dt|��t�	d�W5d}~XYnXdS)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�Z
created_since�"connecting to Launchpad failed: %srJN)
re�searchTasksr9r&rTrrrVrLrO�rGr�rWrrr�get_unretraced s
zCrashDatabase.get_unretracedc
Cs\z|jjddd�}t|�WStk
rV}zt�dt|��t�d�W5d}~XYnXdS)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�rJN)	rer�r&rTrrrVrLrOr�rrr�get_dup_unchecked*s
zCrashDatabase.get_dup_uncheckedcCs|jjdd�}t|�S)aOReturn 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�)rer�r&)rGr�rrr�get_unfixed9s	zCrashDatabase.get_unfixedcCs"|jjjd||jjd�}|djS)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)Zexact_matchZsource_nameZ
distro_seriesr)reZmain_archiveZgetPublishedSourcesZcurrent_seriesZsource_package_version)rG�packageZsourcesrrr�_get_source_versionEs�z!CrashDatabase._get_source_versionc	s\z�jj|}Wntk
r&YdSX|jr2dSt|j�}�j�r$d�j���tt�fdd�|��}|s�tt�fdd�|��}|r�dSt	|�dkr�t
�d�j|�dS|r�|��}z��
|j��d	�WStk
r�YdSXn<tt�fd
d�|��}|�rXtt�fdd�|��}|�sXdSn4ttdd�|��}|�r@dSttd
d�|���rXdSdS)aReturn 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)cs|jdko�|j��kS�Nr��r��bug_target_display_namer\�r���distro_identifierrr�<lambda>os
z1CrashDatabase.get_fixed_version.<locals>.<lambda>cs|jdko|j���j��kSr�)r�r[r\r-r�rdrrr�ss
r�r+zVThere is more than one task fixed in %s %s, using first one to determine fixed versionrcs|jdko�|j��kS�N)r�r�ZExpiredr�r�r�rrr��s
cs|jdko�|j��kSr�r�r�r�rrr��s
cSs
|jdkSr��r�r�rrrr���cSs
|jdkS)Nr�r�r�rrrr��r�N)rYr��KeyErrorr��listr�r-r\�filter�lenr�warningr r�r�r�
IndexError)	rGr{r�r%Zfixed_tasksZfixed_distror�Z
invalid_tasksZnon_invalid_tasksr)r�rGr�get_fixed_versionSsV
��
�
�zCrashDatabase.get_fixed_versioncCs |jj|j}|r|jSdSdS)zcReturn master ID for a duplicate bug.

        If the bug is not a duplicate, return None.
        N)rYr�r�r{)rGr{r�rrrr��szCrashDatabase.duplicate_ofc
Cs�|jj|}|�r�||ks*tdt|���|jj|}|jrd|j}|j}|j|krdt�d||�dS|jD]0}|j	dkrjz|�
�Wqjtk
r�YqjXqj|jj|}|jd|dd�|jj|}|j
r�d|_
|js�||_|j}t|j�d	k�r|d
|jk�r>|jd
|k�r>|j�dd�|k�r>||jd
g|_|��d
|jk�r||j�dd�|k�r||jj|jd
}|j|d�|���rLd|k�s�d|k�rL|j||d|ddddddgd�|jj|}|jdd�}	z|	�d�Wntk
�r�YnXz|	�d�Wntk
�r YnX|	|_z|��Wntk
�rJYnXdg}
|jjD]"}|jdk�rn�qZ|
�|j��qZt|j�}|j}|� |�}
|
D]}||
k�r�|�|��q�||_|��n|j�r�d|_|j!�r�|��dS)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�zStacktrace.txtzThreadStacktrace.txtzProcMaps.txtzProcStatus.txtz
Registers.txtzDisassembly.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.zThis bug is a duplicater�F�
�escalation_tagZ
escalated_tagz	 invalid �escalation_subscription��personzapport-request-retrace�apport-failed-retracez)Updated stack trace from duplicate bug %i�
Stacktrace�ThreadStacktracer��Dependencies�ProcMapsZProcCmdline)r�zbugpattern-needed)zActive DevelopmentzCurrent Stable ReleaseZ	SupportedzPre-release Freeze)"rYr�r8rVr�r{rrrryr�rr��privater�r�
duplicatesr<r4r��people�	subscriber�r��remover�re�seriesr�r�rr$�
differenceZ_dirty_attributes)rGrkr{Z	master_idr��masterr�Zmaster_tags�pr�Ztags_to_copyrZ	dupe_tagsZmissing_tags�tagrrr�close_duplicate�s�
�

��	0 ���



zCrashDatabase.close_duplicatecCsD|jj|}|jd|dd�|jj|}|jdg|_|��dS)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.zPossible regression detectedr��regression-retracerN)rYr�r�r�r�)rGr{rr�rrr�mark_regressions��zCrashDatabase.mark_regressioncCs^|jj|}|j|jkrZ|jdd�}|�|j�||_z|��Wntk
rXYnXdS)zMark crash id as retraced.N)rYr�r9r�rr�r)rGr{r�r�rrr�
mark_retraced#szCrashDatabase.mark_retracedc	Cs�|jj|}|r�z&|�|j�}tr,|��}n|��}Wntk
rT|jd}YnXd|_|�	�|j
|dd�|jD]0}|jdkrxz|�
�Wqxtk
r�YqxXqxn d|jkr�|jdg|_|�	�dS)z%Mark crash id as 'failed to retrace'.rr�z Crash report cannot be processedr�r�r
N)rYr�r`r�rKr�r�rZr�r�r�rryr�rr�)rGr{Zinvalid_msgr�r�r�rrr�mark_retrace_failed0s.
�



z!CrashDatabase.mark_retrace_failedc	Cs�|jj|}d|krt|jD]X}|jj�d�r|jj|dd�|_z|��|jj|}Wnt	k
rlYnXqtqd|j
kr�|j
dd�}|�d�||_
|��d|kr�|jD](}d|jjkr�|jdkr�d|_|��q�|�
||�dS)	z/Mark crash id as checked for being a duplicate.rsr�r�r�N�	Tracebackr�r�)rYr�r�r�r�rrer�r�rr�rr�r�)rGr{rkr�r�r�rrr�_mark_dup_checkedNs2
�




zCrashDatabase._mark_dup_checkedcCs�tjj�||�}|s|S||d<zt|d�}Wntk
rH|YSX|��}|�d�s`dS|D]4}|�d�r�d|ks�d|kr�dSq�|��sdq�qd|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.
        ZDuplicateOfz/+textsbug:Tstags:sapport-failed-retracesapport-request-retraceN)	rr6r'�knownr
r��readline�
startswith�strip)rGrk�urlr�linerrrr ns&


zCrashDatabase.knowncCstd|kr |d��ddkr dSd|jj|j�dd�f}|�t|jj�d��d	�d
d�|jD�krp|j	|d�dS)
z-Subscribe the right triaging team to the bug.�
DistroReleaserZUbuntuNz%s~%sZ
triaging_teamzubuntu-crashes-universer��~cSsg|]}t|��d�d�qS)rr,)rVr)r!r�rrr�
<listcomp>�sz:CrashDatabase._subscribe_triaging_team.<locals>.<listcomp>r)
rrYZ	_root_urir<r4r�rVr#r�r)rGr�rkr	rrrr��s��z&CrashDatabase._subscribe_triaging_teamc	Cs�i}d|d��|d<|�d�}|r.|dkr8|�d�}|rP|dd|7<d|krv|dd|�|d�7<d|kr�|r�d	|ks�d
|ks�d|kr�d|d
<|�d|j�dd��|d<|dd|7<n(d|kr�d|d
<d|d<|dd7<d|k�r"d|dk�r"|dd7<d|k�r8|d|d<dddddddddg	}t��}|j||dddg|d �|��|�d!�|S)"z�Generate a multipart/MIME temporary file for uploading.

        You have to close the returned file object after you are done with it.
        z	apport-%srr�ZPackageArchitecture�all�Architecturer�r&ZVmCore�CoreDumpZLaunchpadPrivateZyesZPrivateZLaunchpadSubscribeZinitial_subscriberr�Subscribersz need-%s-retracerz need-duplicate-checkZDuplicateSignaturer�ZCheckboxSubmissionzHWDB-Submissionr�Z
RegressionZReproducibleZTestedUpstreamZProcVersionSignature�Uname�NonfreeKernelModules)Z
extra_headersr�Zpriority_fieldsr)	r\r4r�r<rAr�r�r�r�)rGrkZhdrr��orderr�rrrrg�sL

�

��
z#CrashDatabase._generate_upload_blobcCs`d}|��jddd�D]D}|dks6t|�dkrR|dkrRtrD||7}qZ|t|�7}q|d7}q|S)	zAReplace characters from tags which are not palatable to Launchpadr��ASCII�ignore)�errorss%abcdefghijklmnopqrstuvwxyz0123456789 rs+-.r*)r\rwrrK�chr)�klassr��resZchrrrr��s

zCrashDatabase._filter_tag_names)N)FNN)r�)N)$�__name__�
__module__�__qualname__�__doc__r7�propertyrYr`rernrirzr|r�r�r�r�r�r�r�r�r�r�r�rr�rrrrrr r�rg�classmethodr�rrrrr'=sJB
(	


#
U�
S
4	
Fq
 72r'c@seZdZdZdd�ZdS)�HTTPSProgressConnectionzWImplement a HTTPSConnection with an optional callback function for
    upload progress.cCs�tst�||�dSd}t|�}d}||kr�t||�t��}t�|||||��||7}t��}|dkr$||dkr�|dK}q$||dkr$|dL}q$dS)Nrig�?r+r)�_https_upload_callbackr�sendr�time)rGrZsentZtotalZ	chunksize�t1�t2rrrr>s"

zHTTPSProgressConnection.sendN)r6r7r8r9r>rrrrr<sr<c@seZdZdd�ZdS)�HTTPSProgressHandlercCs|�t|�S)N)Zdo_openr<)rG�reqrrr�
https_open$szHTTPSProgressHandler.https_openN)r6r7r8rDrrrrrB"srBrpc
Csd}d|}|atjj��}tjj�d�}|�dd�|�|�tjj	�
dd�}|�dd�|�|���
d	��|�|�t�}tjjd
kr�tjj|dd�}	ntjj|dd�}	|	�|�t||���}
|
�d
d|���tt�}|�|
�}|���d�}|�st�|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.
    Nzhttps://%s/+storeblob�1zContent-Dispositionzform-data; name="FORM_SUBMIT"Zapplicationzoctet-streamz*form-data; name="field.blob"; filename="x"�asciirF)Zmangle_from_zContent-Typezmultipart/form-data; boundary=zX-Launchpad-Blob-Token)r=r�r�Z	multipartZ
MIMEMultipartr�ZMIMETextZ
add_headerZattach�baseZMIMEBaseZset_payloadr�r�rrL�version_info�major�	generator�	GeneratorZBytesGeneratorZflattenr�getvalueZget_boundaryrrBr�infor4r8)
Zblobrlrfrmr$rZsubmitZ	form_blobZ	data_flat�genrCZopenerr�rrrrh(s.




rh�__main__)�patchc@sTeZdZdZdZdd�Zdd�Zedd��ZdNd	d
�Z	dd�Z
dOd
d�Zdd�Zdd�Z
dd�Zdd�Zdd�Zdd�Zdd�Zdd�Zdd �Zd!d"�Zd#d$�Zd%d&�Zd'd(�Zd)d*�Zd+d,�Zd-d.�Ze�ed/�d0d1��Ze d2d3��Z!d4d5�Z"dPd7d8�Z#d9d:�Z$d;d<�Z%d=d>�Z&d?d@�Z'dAdB�Z(dCdD�Z)dEdF�Z*dGdH�Z+dIdJ�Z,e dQdLdM��Z-d6S)R�_T�	coreutilscCsHts|��at|_t��|_|j��|j��d|jd<|�d�dS)NrRrs�langpack-o-matic)r6�
_get_instancerr��
ref_report�add_os_info�
add_user_info�_create_projectrdrrr�setUprs



z_T.setUpcCs<|jjj|}|s8|jjjj|d|||d|dd�dS)z8Create a project using launchpadlib to be used by tests.r��summaryry)r�Zdisplay_namerrZryN)r6rYrbZnew_project)rGrr.rrrrX�s
�z_T._create_projectcCs
|j��S)z1Get the Launchpad hostname for the given crashdb.)r6rirdrrrrf�sz_T.hostnameFcCs�|stdk	rtS|��}|�|j�|��|��|��|�|��d�d|d<d|d<|�	|j
|�}|�|�|�||�}|�|dk�t
j�d|j|f�|s�|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 "]¶"
ZShortGibberish�a
b
c
d
e
ÿÿÿ
Z
LongGibberishrz*(Created SEGV report: https://%s/bugs/%i) )�_segv_report�_generate_sigsegv_report�add_package_info�test_packagerV�add_gdb_inforW�assertEqualrt�_get_bug_targetr6�
assertTrue�	_file_bugrLrMrNrf)rG�force_fresh�r�
bug_targetr{rrr�get_segv_report�s$	
z_T.get_segv_reportcCs�tdk	rtSt�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 definedrzboogus pybogusr��Hfoo crashed with NameError in fuzz(): global name 'weird' is not definedrz,(Created Python report: https://%s/bugs/%i) )�_python_reportrr�r_r`rVrWrbrtrcr6rdrerLrMrNrf)rGrgrhr{rrr�get_python_report�s&

�
z_T.get_python_reportcCsV|stdk	rtSd}|jjjjd||jjd�}tj�d|j	|j
f�|sP|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
zmixed description bug)ryr�r�z3(Created uncommon description: https://%s/bugs/%i) )�_uncommon_description_reportr6rYr��	createBugrerLrMrNrfr{)rGrfZdescr�rrr�get_uncommon_description_report�s


�z"_T.get_uncommon_description_reportc
Cs�|j�|���}|�|dd�|�|dd�|�|d|jd�|�|d|jd�|�|d|jd�|�|�d�|j�d��|�|�d	�|j�d	��t|d
���}|�|t|jjdt	j
��g��|�|dd
�|�|d�
d��|�|d|j�|�|d�|jd��|�d|d�|�d|d�|�d|d�|�t|d�d�|�d|�|�d|�|�d|�|j�|���}t|d
���}|�|tddddt	j
��g��dS) z
download()rr�rqr[r&r*r-r.Z
UserGroupsr�r��Signal�11rkz/crashrsr�r�zf (x=42)r�
StacktraceToprr+i�r
�Disassembly�	RegistersZboogusZpybogusr�N)r6r�rirbrUr4r$rr9rr:r;rdr�test_srcpackager"r`�assertInZ
assertGreaterrro)rGrgr�rrr�test_1_download�s>
���z_T.test_1_downloadcCs�|j�|���}|�d|�|�d|�|�d|�|�d|�|�d|�|�d|�|�|dd�d	|d
<d|d<d|d<d
|d<|j�|��|d�|j�|���}|�d|�|�d|�|�d|�|�d|�|�d|�|�d|�|�d|�|�|dd�|jjj|��j	}|�d|�|�d|�d|d
<d|d<d|d<|j�|��|d�|j�|���}|�d|�|�d|�|�d|�|�d|�|�d|�|�d|�|�d|�|�|dd�|jjj|��}d|_
z|��Wntk
�rYnXd|d
<|j�|��|d�|j�|���}|�|dd�|jjj|��}d|_
z|��Wntk
�r�YnXd|d
<|j�|��|d�|j�|���}|�|dd�d|d<d|d<d|d<|j�|��|d�dS) zupdate_traces()r+r
rvrwrrrqr[z?? ()ru�
long
trace�thread
even longer
traceZbogusZFooBarzI 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 exitzcrash is crashyzgood retrace with custom titler�u"]¶"
r\ZStacktraceSourceZtestsN)
r6r�riryrbr��assertNotInrYr�r�ryr�r)rGrgr�r�rrr�test_2_update_tracess|z_T.test_2_update_tracescCs�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 titless1ä♥2r�rq�*z1/ubuntu/+filebug/42?field.title=1%C3%A4%E2%99%A52rrrRrszC/ubuntu/+source/coreutils/+filebug/42?field.title=1%C3%A4%E2%99%A52N)rr�r6rzrdrr�)rGryrgr$rrr�test_get_comment_urlms
z_T.test_get_comment_urlcCs|jjjdd�}|jjjjd|dd�}|j}|�|dk�tj	�
d|j|f�t�
d�}d	�d
�|d<d|d
<d|d<d|d<d|d<|jj||ddd�|j�|�}|�|dd	�d
��|�|dd�|�|dd�|�|dd�|�|jjj|jdg�dS)z"update() with changing description�bashr�ztest description for test bug.�testbug�r�r�ryr�(https://%s/bugs/%i) r�sbogus→rr�OneLiner�f()
g()
h(1)ru�lineone
linetwo�ShortGoo�one
two
three
four
five
six�DpkgTerminalLogs�VarLogDistupgradeBinGoo�NotMeT�r�r�N)r6rer�rYr�rqr{rdrLrMrNrfrr�r�r�r�rbr��rGrhr�r{rgrrr�test_update_description�s0
�
�z_T.test_update_descriptioncCs|jjjdd�}|jjjjd|dd�}|j}|�|dk�tj	�
d|j|f�t�
d�}d	|d
<d|d<d
|d<d|d<d|d<|jj||ddd�|j�|�}|�d
|�|�d|�|�|dd�|�|dd�|�|dd�|�|jjj|jdg�dS)zupdate() with appending commentr�r�zPr0blem

--- 
ProblemType: Bugr�r�rr�r��bogus→r�r�rur�r�r�r��r�ZmeowFr�rr�N�r6rer�rYr�rqr{rdrLrMrNrfrr�r�r�rrbr�r�rrr�test_update_comment�s2
�
�z_T.test_update_commentcCs|jjjdd�}|jjjjd|dd�}|j}|�|dk�tj	�
d|j|f�t�
d�}d	|d
<d|d<d
|d<d|d<d|d<|jj||dddddgd�|j�|�}|�d
|�|�|dd
�|�|dd�|�|dd�|�d|�|�|jjj|jg�dS)zupdate() with a key filterr�r�ztest description for test bugr�r�rr�r�r�r�r�rur�r�r�r�r�r�r�Tr)r�r�Nr�r�rrr�test_update_filter�s2
�
�z_T.test_update_filtercCs"|�|j�|���|jd�dS)zget_distro_release()r&N)rbr6r�rirUrdrrr�test_get_distro_release�s�z_T.test_get_distro_releasecCs$|�|j�|���|jdg�dS)zget_affected_packages()rsN)rbr6r�rirUrdrrr�test_get_affected_packages�s
�z_T.test_get_affected_packagescCs,|�|j�|����|�|j�d��dS)z
is_reporter()r+N)rdr6r�ri�assertFalserdrrr�test_is_reporter�sz_T.test_is_reportercCs,|�|j�|����|�|j�d��dS)zcan_update()r+N)rdr6r�rir�rdrrr�test_can_update�sz_T.test_can_updatecCs�|�|j�|���d�|�|j�|���d�|��}|��}|jdd�}|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�||�|�|�dS)zduplicate handlingNT�rfr�r+rvrZ
ProcStatusrwr)
rbr6r�rirrrr�rrrr�r�_verify_marked_regression)rGZsegv_idZ
known_test_idZknown_test_id2rgrrr�test_duplicates�sF��z_T.test_duplicatescCs�|j��}|�|��|�|�|��|�|j�|���|j��}|�|��|�|�||�t	|��g���|�|j�
|���d�|�|���|j�|���|j�|���|j��}|�|��|�|�||�t	|��g���|�|j�
|���d�|�|���|j�|���|j�|��d�|j��}|�|��|�|�||�t	|��g���|�|j�
|���d�dS)z-processing status markings for signal crashesNzI don't like your�)
r6r�ryrirrorrb�unionr$r�_mark_needs_retracer)rG�unretraced_before�unretraced_afterrrr�test_marking_segv5s<

�
�
��z_T.test_marking_segvcCs�|jjjjd|jj|jjdd�}tj�d�p.d}t	tj�d�d|d��}|jjjd	|j|jd
d�}|�
�}|�|j|�|�
|j|�|�|j�|�
�}|�
|j|�|�||�t|jg���|�|j�|j�d�dS)�0processing status markings for a project CrashDB�foozubuntu distro retrace bug�r�r�r�ryr(ro�LP_CREDENTIALSrS�r.r)Zbarzproject retrace bugN)r6rYr�rqr9rer2�environr4r'r�ryr{rrrbr�r$r)rGZ
distro_bugr)Z
project_dbZproject_bugr�r�rrr�test_marking_projectXs6
�
���z_T.test_marking_projectcCs�tj�d�pd}ttj�d�d|dd��}|��}|j��}|jjjjddg|jj	d	d
�}t
d|j�|��}|j��}|�||�|�||�
t|jg���dS)
r�r(ror��ubuntuZfakearch)r-r)r/r�zneed-fakearch-retracez&ubuntu distro retrace bug for fakearchr�z4fake arch bug: https://staging.launchpad.net/bugs/%iN)r2r�r4r'r�r6rYr�rqre�printr{rbr�r$)rGr)Zfakearch_dbZfakearch_unretraced_beforeZsystemarch_unretraced_beforer�Zfakearch_unretraced_afterZsystemarch_unretraced_afterrrr�test_marking_foreign_arch{s,
��

�
�z_T.test_marking_foreign_archcCs�|j��}|�|��|�|�|��|�|j�|��|j�|j��}|�|��|�|�||�	t
|��g���|�|j�|���d�dS)z2processing status markings for interpreter crashesN)r6r�ryrorrirrUrbr�r$r)rG�unchecked_before�unchecked_afterrrr�test_marking_python�s

�z_T.test_marking_pythoncCsp|jdd�}|j�|�}|j�|||���d|d<d|d<d|d<|j�||d	�|j�|�}|�d
|�dS)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}rur{rr|rr~r+N)rir6r�rr�r)rGr{rgrrr�test_update_traces_invalid�sz_T.test_update_traces_invalidr�cGs\dtj_|�|���|j�|���}|�|d�|�|���|�|j�|���d�dS)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.
            z3.14N)	r'r�Zreturn_value�_mark_report_fixedrir6rrb�_mark_report_new)rGrxZ	fixed_verrrr�test_get_fixed_version�s	z_T.test_get_fixed_versioncCs(tj�d�pd}ttj�d�d|d��S)zCreate a CrashDB instancer(ror�r�)r-r))r2r�r4r')r4r)rrrrT�s��z_T._get_instancecCs@|j�d�}d|kr&|jj|dd�S|r6|jj|S|jSdS)z&Return the bug_target for this report.r.rsr�N)r<r4rer�rYrb)rG�dbrkr.rrrrc�sz_T._get_bug_targetNc	CsP|dkrd}|j�|�}tr(t�|�}n
t�|�}|��|��}trN|��pT|�	�}|�
�sbt�trn|��pt|�	�}|�
�r�t�|��dks�t�|d|j
dd��dd�7}|jjjj||d	��||�d
|���d�}	|D]6}|�
�r�t�|	jd|��d|j
dd�|��d
d�q�|d��D]&}
|jjj|
}|�r"|	j|d��q"|	jS)zLFile a bug report for a report.

            Return the bug ID.
            Nzsome descriptionr�z

Tr�rrr�r�rqr�r�Fr�r,r)r6rgrKr�r�r�rjr�r�r�r�r8r�r�r�rYr�rqrr4rtr�r�rrr{)rGrhrkr�r�rXr��headerr�r�Z
subscriberr�rrrre�sD



�

�z_T._file_bugcCs:|jjj|}|jj|jkr6|j|jjg|_|��dS)z$Mark a report ID as needing retrace.N)r6rYr�r9r�r�r�rrrr�sz_T._mark_needs_retracecCs2|jjj|}d|jkr.|jdg|_|��dS)z,Mark a report ID as needing duplicate check.r�N)r6rYr�r�r�r�rrr�_mark_needs_dupcheck$s
z_T._mark_needs_dupcheckcCsB|jjj|}t|j�}t|�dks(t�|d}d|_|��dS)zClose a report ID as "fixed".r+rr�N�	r6rYr�r�r�rr8r�r��rGr{r�r%r_rrrr�,s
z_T._mark_report_fixedcCsB|jjj|}t|j�}t|�dks(t�|d}d|_|��dS)zReopen a report ID as "new".r+r�NewNr�r�rrrr�6s
z_T._mark_report_newcCs |jjj|}|�d|j�dS)z.Verify that report ID is marked as regression.rN)r6rYr�ryr�r�rrrr�@sz_T._verify_marked_regressioncCsvtj�d�pd}ttj�d�d|d��}|�|jd�t�d�}d|d	<d
|d<|��|�	�|�|�
�d�|�||�}|�|jd�|�
||�}|�|d
k�tj�d|j|f�|�|�}d|d<d|d<d|d<|�||d�|�|�}|�|�|�d�|�|||���|�|�|�|���|�|�|�d�|�||d�|�|�|�d�|�|�|�d�dS)z7reporting crashes against a project instead of a distror(ror�rSr�Nr�rjrkrlrrmrr�r}rur{rr|rr~r�)r2r�r4r'rbr-rr�rVrWrtrcrrerdrLrMrNrfr�r�rrrrr�)rGr)r6rgrhr{rrr�test_projectFsB��

�

z_T.test_projectcCsH|j�|���}|�|dd�|�|dd�|�|d�d��dS)z*download() of uncommon description formatsrr�r*Zamd64r&zUbuntu N)r6r�rrrbrdr")rGrgrrr�test_download_robustnessvsz_T.test_download_robustnesscsDtj�d�pd}ttj�d�d|ddd��}d}|jj|jd	j�d
}z�t||d
�D]�}|d7}t	j
�d|�|�t
��||���|jj|��}|jd|jk}t�fdd�|jD��}|dkr�|�|�|�|�qX|�|�|�|�qXW5t|||�D](}t	j
�d|�|�t
��|d��qXt	j
�d�dS)z,Escalating bugs with more than 10 duplicatesr(ror�r�Z
omgkittenszapport-hackers)r-r)rrrriV'zR%i N�
r+z%i rcsg|]}|j�k�qSr)r�)r!�s�rrrr(�sz&_T.test_escalation.<locals>.<listcomp>r�
)r2r�r4r'rYrr<r�rangerLrMrNrrr�rir�r��anyr�r�rd)rGr)r��countZ	first_dupr�Zhas_escalation_tagZhas_escalation_subscriptionrr�r�test_escalations8��

z_T.test_escalationcCs�|�|���|j��}|�|��|�|jjj|��}|jrNd|_|��|j	d}|jjj
d|_|��|j|jjj
dd�|j�|���}|j�|��|�|j��}|�|��|�|�||�t|��g���|jjj|��}|�|j	djd�|�|j	djd�|�|j	djd�|�|j	djd	�|�|j	djd�|�|j	djd
�|�|j�|���d�dS)z9source package task fixup for marking interpreter crashesFrr�rR)r�r�r�r+zcoreutils (Ubuntu)r�N)r�ror6r�ryrYr�rr�r�rar�ZaddTaskrbr�rrrbr�r$r[r�r�r)rGr�r�r_rgr�rrr�test_marking_python_task_mangle�s4


�z"_T.test_marking_python_task_manglertc	Cs&d}t��}tj��}z�t��}t�	t
j|�t�|�tdd��}|�
d�W5QRXt�dddddg�d	kstt�tj�d�s�t�tjd
ddd
dddgtjd�tj�d�s�t�t�dg�tjdddgtjd�d	ks�t�tj�|d�|d<tj�|d�f|d<||d<|��W5t�|�X|S)aCreate 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.
            Nzcrash.c�wzS
int f(x) {
    int* p = 0; *p = x;
    return x+1;
}
int main() { return f(42); }
Zgccz-gz-oZcrashrZgdbz--batchz--ex�runzgenerate-core-file corez./crash)�stdoutZcore�syncZreadelfz-nrkr+rs)r2�getcwdrrkr��chdirrArBrCrDrErFrrN�
subprocess�callr8rP�exists�PIPEZ
check_callr�ra)r4�signalZworkdirZorig_cwd�pr�fdrrrr^�s<

����z_T._generate_sigsegv_report)F)F)N)rt).r6r7r8r`rxrYrXr:rfrirorrrzr�r�r�r�r�r�r�r�r�r�r�r�r�r�r�rP�objectr'r�r;rTrcrer�r�r�r�r�r�r�r�r�r^rrrrrQhsT

#
$"Q #!6##



	
:

0	")rQ)Nrp);r9rArC�os.pathr2r]r�rLr�r?rEZhttplib2r�iorrHrIZurllib2rrrZhttplibrZurllibr	r
rKZurllib.requestZurllib.parseZhttp.clientZlaunchpadlib.errorsrZlaunchpadlib.launchpadr�ImportErrorZapport.crashdbrrP�
expanduserr5rr&r6r'r=r<rBrhr6Zunittestr�Z
unittest.mockrPr]rnrpZTestCaserQ�mainrrrr�<module>shH
F
7