U
    -_g                     @   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                   @   s^   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dd Zdd Z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 ]$}	| ||	}
| 	|
rj|
|
 qjq\qN|D ]:}| |}| |}|s|r|||d}|d 
| qtj|ddd}| j|d	d
d d S )Nr   r   r	   :)pathsha1maven_id   ),: )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   s   t j|rt|rz6t|"}d| kr@W 5 Q R  W dS W 5 Q R X W n> ttjfk
r } z| 	d| d|  W 5 d}~X 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.MFTzCould not determine if z is a JAR: NF)
r   r   isfilezipfileZ
is_zipfileZipFilenamelistIOErrorZ
BadZipfile	_log_info)r"   r   fileerrr+   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
r4 }	 z| d
| d|	  W 5 d}	~	X 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Zrgxr+   r,   
<listcomp>p   s     
 z%Jars.get_maven_id.<locals>.<listcomp>   Nr      #   =utf8z)Could not extract Maven coordinates from r   )r/   r0   recompiler1   lenopen	readlinesstrip
startswithr   decode
ValueErrorr2   r3   )
r"   r(   Zpropsr4   resultZprops_flinekeyvaluer5   r+   r9   r,   r   e   s.    

:zJars.get_maven_idc              
   C   s   d}zNt |dd0}t }tt|jddD ]}|| q.W 5 Q R X | }W n8 tk
r } z| 	d| d|  W 5 d}~X Y nX |S )	zHCompute JAR id.

        Returns sha1 hash of a given JAR file.
        r
   rb)modei       zCould not compute JAR id for r   N)
rB   hashlibr   iterr   readupdateZ	hexdigestr2   r3   )r"   r(   r)   r4   ZdigestZbufr5   r+   r+   r,   r      s    (zJars.get_jar_idN)__name__
__module____qualname__Z
short_descZplugin_nameZprofilesr   strZoption_listr   r   r-   r   r   r   r+   r+   r+   r,   r      s    
#r   )rO   r    r   r?   r/   	functoolsr   Zsos.report.pluginsr   r   r   r   r+   r+   r+   r,   <module>   s   