U
    8dw                     @   sl  d Z ddlZddlZddlZddlZddlZddlZddlZddlZddl	Z	ddl
Z
ddlZddlZddlZddlZddlmZ ddlZddlZedZdadd Zdkd	d
ZdlddZdmd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ej$ddfd!d"Z%d#d$ Z&d%d& Z'd'd( Z(dej$dfd)d*Z)d+d, Z*d-d. Z+dod/d0Z,dpd1d2Z-d3Z.d4Z/d5Z0d6Z1d7Z2d8Z3d9Z4d:Z5d;Z6d<Z7d=Z8d>Z9d?d@ Z:dAdB Z;dqdCdDZ<dEdF Z=dGdH Z>dIdJ Z?drddKdLdMZ@dNdO ZAdPdQ ZBdRdS ZCdsdTdUZDdVdW ZEdXdY ZFdZd[ ZGd\d] ZHdtd_d`ZIdadb ZJdcdd ZKdedf ZLdudgdhZMdidj ZNejOjPZPejOjQZQdS )vz/Convenience functions for use in package hooks.    N)implz[^0-9a-zA-Z_.-]c                 C   sL   t jd dkr$t| tr8| d} nt| ts8| d} td| ddS )z{Generate a valid report key name from a file path.

    This will replace invalid punctuation symbols with valid ones.
    r   3UTF-8. _)	sysversion
isinstancebytesdecodeencode_invalid_key_chars_resubreplace)path r   2/usr/lib/python3/dist-packages/apport/hookutils.pypath_to_key'   s    


r   TFc                 C   s8   d|krdS |st |}tj|r4t| |||| dS )a  Attach file contents if file exists.

    If key is not specified, the key name will be derived from the file
    name with path_to_key().

    If overwrite is True, an existing key will be updated. If it is False, a
    new key with '_' appended will be added instead.

    If the contents is valid UTF-8, or force_unicode is True, then the value
    will be a string, otherwise it will be bytes.
    ../N)r   osr   existsattach_filereportr   keyZ	overwriteforce_unicoder   r   r   attach_file_if_exists5   s    r   c              
   C   s&  zd| krW dS t | t jt jB t jB }t |}t j| }|jt 	|jksZ| |krjt 
| W dS t	|jrt |d}|  }W 5 Q R X nt 
| W dS |r|jdddW S z|dW W S  tk
r   | Y W S X W n4 tk
r  } zd	t|  W Y S d
}~X Y nX d
S )a  Return the contents of the specified path.

    If the contents is valid UTF-8, or force_unicode is True, then the value
    will a string, otherwise it will be bytes.

    Upon error, this will deliver a text representation of the error,
    instead of failing.
    r   zError: invalid path.zError: path contained symlinks.rbz#Error: path was not a regular file.r   r   errorsError: N)r   open
O_NOFOLLOWO_RDONLY
O_NONBLOCKfstatr   realpathst_inostatcloseS_ISREGst_modefdopenreadstripr   UnicodeDecodeError	Exceptionstr)r   r   fdstZ	real_pathfcontentser   r   r   	read_fileM   s,    	


r8   c                 C   s6   |st |}|s"|| kr"|d7 }qt||d| |< dS )a  Attach a file to the report.

    If key is not specified, the key name will be derived from the file
    name with path_to_key().

    If overwrite is True, an existing key will be updated. If it is False, a
    new key with '_' appended will be added instead.

    If the contents is valid UTF-8, or force_unicode is True, then the value
    will a string, otherwise it will be bytes.
    r   )r   N)r   r8   r   r   r   r   r   r   s    
r   c           
      C   s   t |}| D ]\}}|r(||kr(qdt| }t|tkr\|dksR|dr\|| |< q|r|d| }|r||| |< qd| |< nd| |< tj	t
|j}	|	 | dt| < qdS )a(  Attach information about any modified or deleted conffiles.

    If conffiles is given, only this subset will be attached. If ui is given,
    ask whether the contents of the file may be added to the report; if this is
    denied, or there is no UI, just mark it as "modified" in the report.
    zmodified.conffile.z	[deleted]z[inaccessibleznIt seems you have modified the contents of "%s".  Would you like to add the contents of it to your bug report?z
[modified]zmtime.conffile.N)	packagingZget_modified_conffilesitemsr   typer2   
startswithZyesnodatetimeZfromtimestampr   r)   st_mtimeZ	isoformat)
r   packageZ	conffilesZuiZmodifiedr   r6   r   Zresponsemtimer   r   r   attach_conffiles   s     


rA   c                 C   st   zt j|}W n tk
r&   Y dS X |D ]B}tj|r,|dr,|dd}d|dd }t	| || q,dS )z3Attach information about any Upstart override filesNz
/etc/init/.confz	.overrideupstart. )
apportr9   	get_files
ValueErrorr   r   r   r<   r   r   )r   r?   filesfileoverrider   r   r   r   attach_upstart_overrides   s    rK   c                 C   s  zt j|}W n tk
r&   Y dS X |D ]`}tj|s@q,|drtj|	dd}d| }ztj
tjd d|}W nL tk
r   ztj
tjd d	d|}W n tk
r   Y Y q,Y nX Y nX t| || |d
r,|dr,tjtj|d }d| }d| }ztj
tjd d|}W nP tk
r   ztj
tjd d	d|}W n tk
rz   Y Y q,Y nX Y nX t| || q,dS )z9Attach information about a package's session upstart logsNz/usr/share/upstart/sessions/rB   z.logrC   ZXDG_CACHE_HOMEZupstartHOMEz.cachez/usr/share/applications/z.desktopr   zupstart.application.zapplication-%s.log)rE   r9   rF   rG   r   r   r   r<   basenamer   joinenvironKeyErrorr   endswithsplitext)r   r?   rH   r5   logr   Zdesktopnamer   r   r   attach_upstart_logs   s<    

rT   c                 C   s"   |  dd stdg| d< dS )zwAttach information from the kernel ring buffer (dmesg).

    This will not overwrite already existing information.
    ZCurrentDmesgrD   dmesgN)getr/   command_outputr   r   r   r   attach_dmesg   s    rY   c              
   C   s   d}t j|rt |D ]}t jd||f }t |}t|jr|jd@ dkrXq|dkrbqzt|}W n t	t
fk
r   Y qY nX |r|| d|dd < qd S )	Nz/sys/class/dmi/idz%s/%s   r   )Z	subsystemueventzdmi.r   r   )r   r   isdirlistdirr'   r)   r+   r,   r8   OSErrorIOErrorr   )r   Zdmi_dirr5   pr4   valuer   r   r   
attach_dmi   s    

rb   c                 C   s  t |  t| dd t| dd t| dd tjdrXtdd	g| d
< tddg| d< tdg| d< tddg| d< tddg| d< tddg| d< tdddg| d< tdg| d< | d }tdd|}tdd|}td d!|}tdd|}tdd|}td d!|}|| d< t	|  d"| krNd#| krNd$| d" | d# f | d%< t
d&rftd&g| d'< t
d(rtd(d)g }|r|| d*< td(d+g }|r|| d,< d-S ).a~  Attach a standard set of hardware-related data to the report, including:

    - kernel dmesg (boot and current)
    - /proc/interrupts
    - /proc/cpuinfo
    - /proc/cmdline
    - /proc/modules
    - lspci -vvnn
    - lscpi -vt
    - lsusb
    - lsusb -v
    - lsusb -t
    - devices from udev
    - DMI information from /sys
    - prtconf (sparc)
    - pccardctl status/ident
    z/proc/interruptsZProcInterrupts/proc/cpuinfoProcCpuinfo/proc/cmdlineZProcKernelCmdLinez/sys/bus/pcilspci-vvnnZLspciz-vtzLspci-vtlsusbZLsusb-vzLsusb-vz-tzLsusb-tsort/proc/modulesZProcModulesZudevadminfoz--export-dbZUdevDbz%/usr/share/apport/dump_acpi_tables.pyZacpidumpzID_FS_LABEL=(.*)zID_FS_LABEL=<hidden>zID_FS_LABEL_ENC=(.*)zID_FS_LABEL_ENC=<hidden>zby-label/(.*)zby-label/<hidden>zdmi.sys.vendorzdmi.product.namez%s %sZMachineTypeZprtconfZPrtconfZ	pccardctlZstatusZPccardctlStatusZidentZPccardctlIdentN)rY   r   r   r   r   rW   root_command_outputrer   rb   command_availabler/   )r   labelsoutr   r   r   attach_hardware   sH    


rr   c              	   C   s  t | tjdd t | tjdd t | d t | dd t| dd	 td
ddg| d< tddg| d< tddg| d< tt| d< g }tjdrt	d6}|D ]*}d|kr|
  }|t|d  qW 5 Q R X |D ]}d| }tddt|dg| |< d| }tddt|g| |< td| D ]}tj|rntj|}d|t|f }t| ||d n\tj|r0tj|}t|D ]6}	tj||	}
d|t|t|	f }t| |
| qq0qdS ) z (loosely based on http://www.alsa-project.org/alsa-info.sh)
    for systems where alsa-info is not installed (i e, *buntu 12.04 and earlier)
    z~/.asoundrcZUserAsoundrcz~/.asoundrc.asoundconfZUserAsoundrcAsoundconfz/etc/asound.confz/proc/asound/versionZAlsaVersionrc   rd   Zlsz-lz	/dev/snd/ZAlsaDevicesZaplayZAplayDevicesZarecordZArecordDevicesZPciMultimediaz/proc/asound/cardsz]:r   zCard%d.Amixer.infoZamixerz-crl   zCard%d.Amixer.valuesz/proc/asound/card%d/codec*zCard%d.Codecs.%sr   zCard%d.Codecs.%s.%sN)r   r   r   
expanduserr   rW   pci_devicesPCI_MULTIMEDIAr   r"   lstripsplitappendintr2   globisfilerM   r   r\   r]   rN   )r   Zcardsr3   linefieldsZcardr   Z	codecpathcodecnamer   r   r   r   attach_alsa_old:  sH    

r   c                 C   s   t jdr tdddg| d< nt|  tddgtd td td	 | d
< t jdrrtddg| d< t|  t|  dS )z5Attach ALSA subsystem information to the report.
    z!/usr/share/alsa-base/alsa-info.shz--stdoutz--no-uploadZAlsaInfoZfuserri   z	/dev/dsp*z
/dev/snd/*z	/dev/seq*ZAudioDevicesInUsez/usr/bin/pacmdZpacmdlistZ	PulseListN)r   r   r   rW   r   r{   rb   rY   rX   r   r   r   attach_alsag  s    $r   c                 C   sb   dt jkrdS t jd }|t jD ]8}|s.q$t j|| }t j|r$t |t jr$ dS q$dS )z/Is given command on the executable search path?PATHFT)	r   rO   rx   pathsepr   rN   r|   accessX_OK)commandr   elementfilenamer   r   r   ro   y  s    

ro   c           
   
   C   s   t j }|sd|d< z$tj| tj||r.tjp0d|d}W n2 tk
rl } zdt|  W Y S d}~X Y nX ||d }|j	dkr|
 }	n*dt|   d t|j	  d	 | }	|r|	jd
dd}	|	S )a;  Try to execute given command (list) and return its stdout.

    In case of failure, a textual error gets returned. This function forces
    LC_MESSAGES to C, to avoid translated output in bug reports.

    If decode_utf8 is True (default), the output will be converted to a string,
    otherwise left as bytes.
    CLC_MESSAGESN)stdoutstderrstdinenvr!   r   s   Error: command s    failed with exit code s   : r   r   r   )r   rO   copy
subprocessPopenPIPEr^   r2   communicate
returncoder/   r   r   )
r   inputr   keep_localedecode_utf8r   spr7   rq   resr   r   r   rW     s0    



"

rW   c               	   C   s   t d k	rd S t dkrd S tj s*d S tjds:d S ztd\} }W n t	k
rb   Y d S X t
jddt|dgdt
jt
jda t| t P}|| tj | }|D ],\}}|tj@ rt|   W 5 Q R  d S qqW 5 Q R X d S )Nr   z/usr/bin/pkttyagentZ
pkttyagentz--notify-fdz
--fallbackF)Z	close_fdsr   r   )_AGENTr   geteuidr   r   isattyr   r   pipe2r^   r   r   r2   r   r*   selectepollregisterZEPOLLINpollZEPOLLHUP)rwr   Zeventsr3   Z
event_typer   r   r   _spawn_pkttyagent  s4    




r   c                   C   s$   t d krd S t   t   d a d S )N)r   Z	terminatewaitr   r   r   r   kill_pkttyagent  s
    r   c                   C   s0   t  dkrg S t jdr(t  dgS g S d S )Nr   z/usr/bin/pkexecZpkexec)r   getuidr   r   r   r   r   r   r   _root_command_prefix  s    r   c                 C   s.   t | tstdtt |  ||d|d}|S )a=  Try to execute given command (list) as root and return its stdout.

    This passes the command through pkexec, unless the caller is already root.

    In case of failure, a textual error gets returned.

    If decode_utf8 is True (default), the output will be converted to a string,
    otherwise left as bytes.
    zcommand must be a listT)r   r   )r
   r   AssertionErrorrW   r   )r   r   r   r   outputr   r   r   rm     s    
 rm   c              
   C   sL  t jt jt jddd}t }zt j|d}t
|d}| D ]6\}}t|dsftd|d|t j||f  qL|  tt ||g }|  |D ]}z0t
t j||d	}	|	  }
W 5 Q R X W n tk
r   Y qY nX z|
d
}
W n tk
r   Y nX |
r.|
| |< |	  qW 5 t	| X dS )a*  Execute multiple commands as root and put their outputs into report.

    command_map is a keyname -> 'shell command' dictionary with the commands to
    run. They are all run through /bin/sh, so you need to take care of shell
    escaping yourself. To include stderr output of a command, end it with
    "2>&1".

    Just like root_command_output, this passes the command through pkexec,
    unless the caller is already root.

    This is preferrable to using root_command_output() multiple times, as that
    will ask for the password every time.
    ZAPPORT_DATA_DIRz/usr/share/apportZroot_info_wrapperz:script:r   r/   z(command must be a string (shell command)z%s | cat > %s
r   r   N)r   r   rN   abspathrO   rV   tempfileZmkdtempshutilZrmtreer"   r:   hasattrr   writer*   r   r   r   r   r.   r/   r_   r   r0   )r   Zcommand_mapZwrapper_pathZworkdirZscript_pathZscriptZkeynamer   r   r5   Zbufr   r   r   attach_root_command_outputs  s:    

r   c                 C   sb   d}|  d kr>|jD ]$}|jddd}| |r||7 }qq|j  |  |jdkr^|S dS )NrD   r   r   r   r   )r   r   r   searchr*   r   r   )patternZprocesslinesr}   r   r   r   __filter_re_process&  s    



r   c                 C   sv   |rt jddd|gt jd}nNtjdrFt jdddd	d
gt jd}n&tdtjrlt jddddgt jd}t| |S )zExtract recent system messages which match a regex.

    pattern should be a "re" object. By default, messages are read from
    the systemd journal, or /var/log/syslog; but when giving "path", messages
    are read from there instead.
    tailz-nZ10000r   /run/systemd/system
journalctlz--systemz--quiet-bz-az/var/log/syslog)	r   r   r   r   r   r   r   R_OKr   )r   r   r`   r   r   r   recent_syslog4  s    r   c              	   C   s   t jd}t j|r&t |t js*dS | s8td} d}t|d0}|D ]$}|j	ddd}| 
|rL||7 }qLW 5 Q R X |S )al  Extract messages from ~/.xsession-errors.

    By default this parses out glib-style warnings, errors, criticals etc. and
    X window errors.  You can specify a "re" object as pattern to customize the
    filtering.

    Please note that you should avoid attaching the whole file to reports, as
    it can, and often does, contain sensitive and private data.
    z~/.xsession-errorsrD   z^(\(.*:\d+\): \w+-(WARNING|CRITICAL|ERROR))|(Error: .*No Symbols named)|([^ ]+\[\d+\]: ([A-Z]+):)|([^ ]-[A-Z]+ \*\*:)|(received an X Window System error)|(^The error was \')|(^  \(Details: serial \d+ error_code)r   r   r   r   )r   r   rt   r   r   r   rn   compiler"   r   r   )r   r   r   r5   r}   r   r   r   xsession_errorsG  s    


r            rZ               	   
         c            
   
   G   s   | st ddgS d}t ddg}|dD ]}d}d}|dD ]v}z|dd	\}}W n tk
rp   Y q@Y nX | }| }|d
krt|dd d}	|	d@ d? }q@|dkr@|}q@|r*|r*|| kr*|r|d7 }|t dd|g 7 }q*|S )z9Return a text dump of PCI devices attached to the system.rf   rg   rD   z-vvmmnnz

N
:r   ZClass   i   r   ZSlotz-vvnns)rW   rx   rG   r/   rz   )
Zpci_classesresultr   Z	paragraphZ	pci_classZslotr}   r   ra   nr   r   r   ru   p  s0    
ru   c                   C   s   t ddgS )z9Return a text dump of USB devices attached to the system.rh   ri   )rW   r   r   r   r   usb_devices  s    r   c                    s*   t | } r" fdd|D }n|}|S )zFRetrieve a list of files owned by package, optionally matching globpatc                    s   g | ]}t j| r|qS r   )r{   Zfnmatch).0r5   globpatr   r   
<listcomp>  s      z$files_in_package.<locals>.<listcomp>)r9   rF   )r?   r   rH   r   r   r   r   files_in_package  s
    
r   c                 C   s   dS )ZObsoleteNr   )r   r?   r   r   r   attach_gconf  s    r   c           
   	   C   s6  |  dd}i }tj }d|d< tjdd|g|tjd}|jD ]L}z|dd	\}}}	|		 }	W n t
k
rz   Y q@Y nX |	||i |< q@tjdd|gtjd
}|jD ]~}z|dd	\}}}	|		 }	W n t
k
r   Y qY nX |	| |i  |dkr|dkr|dkrd}	|d|||	f 7 }q|| d< dS )z0Attach user-modified gsettings keys of a schema.ZGsettingsChangesrD   z/nonexistingZXDG_CONFIG_HOME	gsettingszlist-recursively)r   r   Nr   r   s   org.gnome.shell)s   command-historys   favorite-appszredacted by apportz	%s %s %s
)rV   r   rO   r   r   r   r   r   rx   rstriprG   
setdefault)
r   schemaZ	cur_valuedefaultsr   r   r}   Zschema_namer   ra   r   r   r   attach_gsettings_schema  s>    
 




r   c                 C   s2   t |dD ]"}tj|dd }t| | q
dS )z@Attach user-modified gsettings keys of all schemas in a package.z)/usr/share/glib-2.0/schemas/*.gschema.xmlNi)r   r   r   rM   r   )r   r?   Zschema_filer   r   r   r   attach_gsettings_package  s    r   )returnc                 C   sb   t jdsdS |  }|rB|| }|| }d| d| g}nddg}tddg| | d	< dS )
zAttach journal warnings and errors.

    If the report contains a date, get the journal logs around that
    date (plus/minus the time_window in seconds). Otherwise attach the
    latest 1000 journal logs since the last boot.
    r   Nz	--since=@z	--until=@r   z--lines=1000r   z--priority=warningZJournalErrors)r   r   r   Zget_timestamprW   )r   Ztime_windowZcrash_timestampZbefore_crashZafter_crashargsr   r   r   attach_journal_errors  s    
r   c                 C   s`   t ddg| d< t ddg| d< tt| d< t| ddd	 d
D ]}|tjkr>tj| | |< q>dS )z5Attach generic network-related information to report.ZipZrouteZIpRouteZaddrZIpAddrZ
PciNetworkz/etc/network/interfacesZIfupdownConfigrs   )Z
http_proxyZ	ftp_proxyZno_proxyN)rW   ru   PCI_NETWORKr   r   rO   )r   varr   r   r   attach_network  s    
r   c                 C   s   t td| d< tddtddtddtd	g| d
< tddg| d< tjdrjtdddg}nd}|| d< t| ddd dS )z5Attach wireless (WiFi) network information to report.zH(NetworkManager|modem-manager|dhclient|kernel|wpa_supplicant)(\[\d+\])?:Z
WifiSyslogz
ESSID:(.*)zESSID:<hidden>zEncryption key:(.*)zEncryption key: <hidden>zAccess Point: (.*)zAccess Point: <hidden>ZiwconfigZIwConfigZrfkillr   ZRfKillz/sbin/iwZiwZregrV   N/AZCRDAz/var/log/wpa_supplicant.logZWpaSupplicantLogrs   N)	r   rn   r   r   rW   r   r   r   r   )r   Z	iw_outputr   r   r   attach_wifi  s      r   c              !   C   s   t | dd t | dd tdg| d< tddg| d	< td
}|rhtdddg| }tdd|| d< tdddddddddddddddd d!d"d#d$d%d&d'd(dd)d*d+d,d-d.d/ | d0< d1S )2zkAttach printing information to the report.

    Based on http://wiki.ubuntu.com/PrintingBugInfoScript.
    z/etc/papersizeZ	Papersizez/var/log/cups/error_logZCupsErrorLoglocaleZLocaleZlpstatri   ZLpstatz/etc/cups/ppd/*.ppdZfgrepz-Hz	*NickNamez*/etc/cups/ppd/(.*).ppd:\*NickName: *"(.*)"z\g<1>: \g<2>ZPpdFilesZfoo2zjszfoomatic-dbzfoomatic-db-enginezfoomatic-db-gutenprintzfoomatic-db-hpijszfoomatic-filterszfoomatic-guiZhpijsZhplipZm2300wZmin12xxwZc2050ZhpojZpxljrZpnm2ppaZsplixzhp-ppdz
hpijs-ppdszlinuxprinting.org-ppdszopenprinting-ppdszopenprinting-ppds-extraZghostscriptZcupszcups-driver-gutenprintZijsgutenprintzcupsys-driver-gutenprintzgimp-gutenprintzgutenprint-doczgutenprint-localeszsystem-config-printer-commonZkdeprintZPrintingPackagesN)r   rW   r{   rn   r   package_versions)r   ZppdsZ	nicknamesr   r   r   attach_printing  sT    
                       r   c           	   	   C   sv  t |tr|g}d}t|tj}d}t|tj}d| krXt|tjdgtjd| d< d| krt	j
drt| dd| d	 i t| d
d t| dd t|| dd| dd D ]}|st| d  qrzR|d dkr|dd }n4tjd dkrt|jddd}n|jddd}W n tk
r>   Y qY nX |D ]*}td| d |rDt| d  qqDqdS )z0Attach MAC information and events to the report.z!audit\(|apparmor|selinux|securityz'apparmor="DENIED".+?profile=([^ ]+?)[ ]ZKernLogrU   r   ZAuditLogz/var/run/auditd.pidzegrep "z" /var/log/audit/audit.logz/proc/version_signatureZProcVersionSignaturere   ZProcCmdlinerD   Zapparmorr   "r   r   r   r   r   r   hex^$N)r
   r2   rn   r   
IGNORECASEr   r   r   r   r   r   r   r   r   r   findallrV   _add_tagr   r	   r   fromhexr   r1   match)	r   ZprofilesZ	mac_regexZmac_reZaa_regexZaa_rer   ZprofileZsearch_profiler   r   r   attach_mac_events(  s>    
 $


r   c                 C   s(   |  dd}|r|d7 }|| | d< dS )z#Adds or appends a tag to the reportZTagsrD   r   N)rV   )r   tagZcurrent_tagsr   r   r   r   S  s    r   c                 C   s   t | | d< dS )ziAttach version information for related packages

    In the future, this might also run their hooks.
    ZRelatedPackageVersionsN)r   )r   packagesr   r   r   attach_related_packages[  s    r   c               
      s   | sdS g }| D ]v}|sqt |}|s6||df t|D ]F}zt |}W n tk
rh   d}Y nX |dkrvd}|||f q>qttdd t| D \}}d|  d	 fdd|D S )	ztReturn a text listing of package names and versions.

    Arguments may be package names or globs, e. g. "foo*"
    rD   r   Nc                 S   s   g | ]}t t|qS r   )maplen)r   tr   r   r   r   ~  s     z$package_versions.<locals>.<listcomp>z
%%-%ds %%sr   c                    s   g | ]} | qS r   r   )r   vZfmtr   r   r     s     )
r9   Zpackage_name_globry   sortedZget_versionrG   r   maxziprN   )r   ZversionsZpackage_patternZmatching_packagesr?   r	   Zpackage_widthZversion_widthr   r   r   r   c  s*    

r   c                 C   s   z>t jd| gt jt jd}| d d}|jdkr<W dS W n tk
rT   Y dS X | D ]:}|dd}t	|d	k r|q^|d d
kr^|d 
   S q^dS )z-Return the license for a given kernel module.z/sbin/modinfo)r   r   r   r   ZinvalidNr   r   r   license)r   r   r   r   r   r   r^   
splitlinesrx   r   r/   )moduleZmodinforq   r}   r~   r   r   r   _get_module_license  s"    
 

r  rk   c              	   C   s   z&t | }dd |D }W 5 Q R X W n tk
r>   g  Y S X g }|D ]:}t|}|rHd|ksHd|ksHd|ksHd|ksH|| qH|S )zCCheck loaded modules and return a list of those which are not free.c                 S   s   g | ]}|  d  qS )r   )rx   r   r}   r   r   r   r     s     z*nonfree_kernel_modules.<locals>.<listcomp>ZGPLZBSDZMPLZMIT)r"   r_   r  ry   )Zmodule_listr5   ZmodsZnonfreemsr   r   r   nonfree_kernel_modules  s    

$r  c                 C   s   d}t | D ]}t j| |}|dkst j|s6qt|d  }|dkr\|dd}|dkrvt	
|}|d7 }|d	||jd
ddf 7 }q|S )NrD   r[   r   Zmodes   
    Zedidz-base64z%s: %s
r   r   r   )r   r]   r   rN   r|   r"   r.   r/   r   base64Z	b64encoder   )conrl   r5   r   valr   r   r   __drm_con_info  s    
r  c                 C   s`   d}t j|sdS t |D ]<}t j||}t jt j|drt|| dt| < qdS )zWAdd information about DRM hardware.

    Collect information from /sys/class/drm/.
    z/sys/class/drmNenabledzDRM.)r   r   r\   r]   rN   r   r  r   )r   Zdrm_dirr5   r  r   r   r   attach_drm_info  s    r  c              
   C   s:  t jd}|sztd\}|D ]B}| }d|kr|drd|kr|ddd dd } qpqW 5 Q R  dS W 5 Q R X ttj	}z^zHz&t
tj	d	 tt| d
 }W n tk
r   Y W W dS X W 5 t
tj	| X W n tjk
 r   Y dS X zt d| j}W n ttfk
r0   Y dS X ||kS )zCheck if the problem happened in the currently running XDG session.

    This can be used to determine if e. g. ~/.xsession-errors is relevant and
    should be attached.

    Return None if this cannot be determined.
    ZXDG_SESSION_IDz/proc/self/cgroupzname=systemd:z.scopez	/session-r   Nir   ZDatez/run/systemd/sessions/)r   rO   rV   r"   r/   rQ   rx   r   Z	getlocaleLC_TIME	setlocaletimemktimestrptimerP   Errorr)   r>   r_   r^   )r   Z
session_idr5   r}   Z
orig_ctimeZreport_timeZsession_start_timer   r   r   in_session_of_problem  s0    
r  c              	   C   sV   d}|st |}tj|rRt|d&}dd | D }d|| |< W 5 Q R X dS )z;attach /etc/default/grub after filtering out password linesz/etc/default/grubr   c                 S   s   g | ]}| d s|ndqS )Zpasswordz### PASSWORD LINE REMOVED ###)r<   r  r   r   r   r     s   z'attach_default_grub.<locals>.<listcomp>rD   N)r   r   r   r   r"   	readlinesrN   )r   r   r   r5   Zfilteredr   r   r   attach_default_grub  s    r  c                 C   sv   d}g }t j|rLt| dd| i d| krLt| d }|d }|d }|| d< |rfd|| d< | dd	 d	S )
z:attach the results of the casper md5check of install mediaskipZCasperMD5jsonzcat '%s'r   Zchecksum_missmatchZCasperMD5CheckResultr   ZCasperMD5CheckMismatchesN)r   r   r   r   jsonloadsrN   pop)r   locationr   Z
mismatchesZcheckr   r   r   attach_casper_md5check  s    r  )NTF)F)NTF)NN)N)N)N)r   )N)rk   )N)R__doc__r   r   r   r  r=   r{   rn   r)   r
  r   r   r   r   r  Zapport.packaging_implr   r9   rE   apport.fileutilsr   r   r   r   r   r8   r   rA   rK   rT   rY   rb   rr   r   r   ro   ZSTDOUTrW   r   r   r   rm   r   r   r   r   ZPCI_MASS_STORAGEr   ZPCI_DISPLAYrv   Z
PCI_MEMORYZ
PCI_BRIDGEZPCI_SIMPLE_COMMUNICATIONSZPCI_BASE_SYSTEM_PERIPHERALSZPCI_INPUT_DEVICESZPCI_DOCKING_STATIONSZPCI_PROCESSORSZPCI_SERIAL_BUSru   r   r   r   r   r   r   r   r   r   r   r   r   r   r  r  r  r  r  r  r  	fileutilsZshared_librariesZlinks_with_shared_libraryr   r   r   r   <module>   s   


%

'	@- 
!#5

!
$
+!
*
