U
    id{                     @   s^   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	m
Z
 G dd dee	ZdS )    N)partial)PluginRedHatPlugin	PluginOptc                   @   sj   e Zd ZdZdZdZeddeddedd	d
dgZdZ	dZ
dd Zedd Zedd Zedd ZdS )Jarsz1Collect information about available Java archivesjars)javaappend_locations z,colon-delimited list of additional JAR paths)defaultZval_typedescall_known_locationsFzscan all known paths)r   r   )z/usr/share/javaz/usr/lib/java)z/optz
/usr/localz/var/libc                 C   s   dg i}g }t tj}| dr.|t tj7 }| d}|rJ||d7 }|D ]D}t|D ]4\}}}|D ]$}	| ||	}
t	|
rj|
|
 qjq\qN|D ]:}t|}t|}|s|r|||d}|d 
| qtj|ddd}| j|d	d
d d S )Nr   r   r	   :)pathsha1maven_id   ),z: )indentZ
separatorsz	jars.jsonT)Zplug_dir)listr   jar_locationsZ
get_optionextra_jar_locationssplitoswalkZ	path_joinis_jarappendget_maven_id
get_jar_idjsondumpsZadd_string_as_file)selfZresultsZ	jar_pathsZ	locationsZuser_locationslocationdirpath_	filenamesfilenamer   jar_pathr   jar_idrecordZresults_str r*   9/usr/lib/python3/dist-packages/sos/report/plugins/jars.pysetup1   s2    





z
Jars.setupc              	   C   sl   t j| rht| rhz6t| "}d| kr@W 5 Q R  W dS W 5 Q R X W n ttjfk
rf   Y nX dS )zCheck whether given file is a JAR file.

        JARs are ZIP files which usually include a manifest
        at the canonical location 'META-INF/MANIFEST.MF'.
        zMETA-INF/MANIFEST.MFTF)	r   r   isfilezipfileZ
is_zipfileZipFilenamelistIOErrorZ
BadZipfile)r   fr*   r*   r+   r   T   s    zJars.is_jarc                    s  i }zt | }td  fdd| D }t|dkrNW 5 Q R  W dS ||d }| D ]|}| }|	dsfz6|
d\}}|d	 }|d	 }|||< W qf tk
r   Y  W 5 Q R  W 5 Q R  W dS X qfW 5 Q R X W 5 Q R X W n tk
r   Y nX |S )
zExtract Maven coordinates from a given JAR file, if possible.

        JARs build by Maven (most popular Java build system) contain
        'pom.properties' file. We can extract Maven coordinates
         from there.
        z*META-INF/maven/[^/]+/[^/]+/pom.properties$c                    s   g | ]}  |r|qS r*   )match).0xrr*   r+   
<listcomp>p   s     
 z%Jars.get_maven_id.<locals>.<listcomp>   Nr      #   =utf8)r.   r/   recompiler0   lenopen	readlinesstrip
startswithr   decode
ValueErrorr1   )r'   Zpropsr2   resultZprops_flinekeyvaluer*   r6   r+   r   d   s*    

:zJars.get_maven_idc              	   C   sl   d}zNt | dd0}t }tt|jddD ]}|| q.W 5 Q R X | }W n tk
rf   Y nX |S )zHCompute JAR id.

        Returns sha1 hash of a given JAR file.
        r
   rb)modei       )	r@   hashlibr   iterr   readupdateZ	hexdigestr1   )r'   r(   r2   mZbufr*   r*   r+   r      s    zJars.get_jar_idN)__name__
__module____qualname__Z
short_descZplugin_nameZprofilesr   strZoption_listr   r   r,   staticmethodr   r   r   r*   r*   r*   r+   r      s&   
#

r   )rM   r   r   r=   r.   	functoolsr   Zsos.report.pluginsr   r   r   r   r*   r*   r*   r+   <module>   s   