o
    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)default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}|r%||d7 }|D ]"}t|D ]\}}}|D ]}	| ||	}
t	|
rG|
|
 q5q.q'|D ]}t|}t|}|s\|ri|||d}|d 
| qLtj|ddd}| j|d	d
d d S )Nr   r   r	   :)pathsha1maven_id   ),z: )indent
separatorsz	jars.jsonT)plug_dir)listr   jar_locations
get_optionextra_jar_locationssplitoswalk	path_joinis_jarappendget_maven_id
get_jar_idjsondumpsadd_string_as_file)selfresults	jar_paths	locationsuser_locationslocationdirpath_	filenamesfilenamer   jar_pathr   jar_idrecordresults_str r5   9/usr/lib/python3/dist-packages/sos/report/plugins/jars.pysetup1   s:   






z
Jars.setupc              	   C   s   t j| rCt| rCz*t| }d| v r"	 W d   W dS W d   W dS 1 s.w   Y  W dS  ttjfyB   Y dS w 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.MFNTF)	r   r   isfilezipfile
is_zipfileZipFilenamelistIOError
BadZipfile)r   fr5   r5   r6   r    T   s    zJars.is_jarc                    sV  i }zt | }td  fdd| D }t|dkr)	 W d   W dS ||d L}| D ]?}| }|	dstz|
d\}}|d	 }|d	 }|||< W q5 tys   Y  W d   W d   W dS w q5W d   n1 sw   Y  W d   W |S W d   W |S 1 sw   Y  W |S  ty   Y |S w )
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 r5   )match).0xrr5   r6   
<listcomp>p   s    z%Jars.get_maven_id.<locals>.<listcomp>   Nr      #   =utf8)r9   r;   recompiler<   lenopen	readlinesstrip
startswithr   decode
ValueErrorr=   )r1   propsr?   resultprops_flinekeyvaluer5   rC   r6   r"   d   sH   

zJars.get_maven_idc                 C   s~   d}z2t | dd}t }tt|jddD ]}|| qW d   n1 s)w   Y  | }W |S  ty>   Y |S w )zHCompute JAR id.

        Returns sha1 hash of a given JAR file.
        r
   rb)modei       N)	rM   hashlibr   iterr   readupdate	hexdigestr=   )r1   r2   r?   mbufr5   r5   r6   r#      s   
zJars.get_jar_idN)__name__
__module____qualname__
short_descplugin_nameprofilesr   stroption_listr   r   r7   staticmethodr    r"   r#   r5   r5   r5   r6   r      s(    
#

r   )r\   r$   r   rJ   r9   	functoolsr   sos.report.pluginsr   r   r   r   r5   r5   r5   r6   <module>   s   
