a
    ø£Bet%  ã                   @   sÎ   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mZ ddlm	Z	 ddl
mZ ddlmZmZ ddlmZmZ ddlmZ d	d
lmZ dZdZe d¡du rªe dd¡ G dd„ deƒZG dd„ deƒZdS )z3Manager and Tornado handlers for license reporting.é    N)ÚThreadPoolExecutor)ÚPath)Ú
APIHandler)ÚgenÚweb)ÚListÚUnicode)ÚLoggingConfigurableé   )Úget_federated_extensionszthird-party-licenses.jsonÚUNKNOWNútext/markdownz.mdc                   @   sŒ   e Zd ZdZedd�Zeeƒ ede› �gdd�Z	e
dd„ ƒZejddd„ƒZdd„ Zdd„ Zdd„ Zddd„Zdd„ Zdd„ Zd dd„ZdS )!ÚLicensesManagerz€A manager for listing the licenses for all frontend end code distributed
    by an application and any federated extensions
    r
   )Úmax_workerszstatic/z=the license report data in built app and federated extensions)Údefault_valueÚhelpc                 C   s   t | jj| jjgg ƒ}t|ƒS )zÚLazily load the currrently-available federated extensions.

        This is expensive, but probably the only way to be sure to get
        up-to-date license information for extensions installed interactively.
        )ÚsumÚparentÚlabextensions_pathZextra_labextensions_pathr   )Úselfr   © r   úe/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jupyterlab_server/licenses_handler.pyÚfederated_extensions.   s    þûz$LicensesManager.federated_extensionsÚmarkdownú.*Fc                 c   s   | j j| j|||d�V }|S )zrAsynchronous wrapper around the potentially slow job of locating
        and encoding all of the licenses
        ©Úreport_formatÚbundles_patternÚ	full_text)ÚexecutorÚsubmitÚreport)r   r   r   r   r!   r   r   r   Úreport_async>   s    üzLicensesManager.report_asyncc                 C   sj   | j |d�}|dkr"|  |¡dfS |dkr8|  |¡dfS |dkrR| j||d�dfS d	|› d
�}t|ƒ‚dS )z*create a human- or machine-readable report)r   Újsonzapplication/jsonÚcsvztext/csvr   )r   r   zUnsupported report format Ú.N)ÚbundlesÚreport_jsonÚ
report_csvÚreport_markdownÚ
ValueError)r   r   r   r   r&   Úmsgr   r   r   r!   K   s    þzLicensesManager.reportc                 C   s   t jd|iddd�S )z0create a JSON report
        TODO: SPDX
        r&   é   T)ÚindentÚ	sort_keys)r#   Údumps)r   r&   r   r   r   r'   [   s    zLicensesManager.report_jsonc                    st   t  ¡ }g d¢}tj|dg|¢d�}| ¡  | ¡ D ]6\}}|d D ]$‰ | d|i‡ fdd„|D ƒ¥¡ qDq4| ¡ S )zcreate a CSV report)ÚnameÚversionInfoÚ	licenseIdÚextractedTextÚbundle)Ú
fieldnamesÚpackagesc                    s   i | ]}|ˆ   |d ¡“qS )Ú )Úget)Ú.0Úfield©Úpackager   r   Ú
<dictcomp>l   ó    z.LicensesManager.report_csv.<locals>.<dictcomp>)ÚioÚStringIOr$   Ú
DictWriterÚwriteheaderÚitemsÚwriterowÚgetvalue)r   r&   Úoutfiler5   ÚwriterÚbundle_namer4   r   r;   r   r(   a   s    ÿþÿzLicensesManager.report_csvTc              
   C   s  g }dd„ |  ¡ D ƒ}|r"t|ƒnd}|  ¡ D ]Þ\}}|d|› �dg7 }| dg ¡}|sf|ddg7 }q.|D ] }	|	 dt¡ ¡ }
|	 d	t¡ ¡ }|	 d
t¡ ¡ }|	 dd¡}|dd d|
› d� |¡d|› d� d¡|g¡ g7 }|rj|sö|g d¢7 }qj|ddd|ddg7 }qjq.d |¡S )zcreate a markdown reportc                 S   s2   g | ]*\}}|  d g ¡D ]}t|  dt¡ƒ‘qqS )r6   r0   )r8   ÚlenÚUNKNOWN_PACKAGE_NAME)r9   rH   r4   r<   r   r   r   Ú
<listcomp>t   s   þz3LicensesManager.report_markdown.<locals>.<listcomp>r
   z# r7   r6   z> No licenses foundr0   r1   r2   r3   z## ú	z**ú`é   )r7   z> No license text availabler7   z<pre/>z</pre>Ú
)rC   Úmaxr8   rJ   ÚstripÚjoinÚljust)r   r&   r   ÚlinesZlibrary_namesZlongest_namerH   r4   r6   r<   r0   Úversion_infoZ
license_idZextracted_textr   r   r   r)   q   s>    þýÿþÿzLicensesManager.report_markdownc           	      C   s  dg i}g }| j D ]â}|| }| j d|¡ | ¡ s@||g7 }qzt |jdd�¡}W nB ty˜ } z*| j d|||¡ W Y d}~qW Y d}~n
d}~0 0 z|d  	|d ¡ W q tyò } z*| j d|||¡ W Y d}~qW Y d}~qd}~0 0 q|d �s| j d||¡ |S )	z2Return the content of a packages's license bundlesr6   zLoading licenses from %súutf-8©Úencodingz1Failed to open third-party licenses for %s: %s
%sNz%Failed to find packages for %s: %s
%sz)Third-party licenses not found for %s: %s)
Úthird_party_licenses_filesÚlogÚdebugÚexistsr#   ÚloadsÚ	read_textÚ	ExceptionÚwarningÚextend)	r   Úpathr4   Zbundle_jsonZchecked_pathsZlicense_fileZlicenses_pathZ	file_jsonÚerrr   r   r   Úlicense_bundlež   s>    

ü"ü$
zLicensesManager.license_bundlec                 C   sV   t | jjƒ}|d }| ¡ s8|jd }| ¡ r4|}ndS t |jdd�¡d }||fS )z˜get the static directory for this app

        This will usually be in `static_dir`, but may also appear in the
        parent of `static_dir`.
        zpackage.json)NNrV   rW   r0   )r   r   Z
static_dirr\   r#   r]   r^   )r   rb   Zpackage_jsonZparent_package_jsonr0   r   r   r   Úapp_static_infoÅ   s    
zLicensesManager.app_static_infoc                    s^   ‡ ‡fdd„ˆj  ¡ D ƒ}ˆ ¡ \}}|durJt ˆ |¡rJˆ ||¡||< |sZˆj d¡ |S )z6Read all of the licenses
        TODO: schema
        c                    s2   i | ]*\}}t  ˆ |¡r|ˆ t|d  ƒ|¡“qS )Zext_path)ÚreÚmatchrd   r   )r9   r0   Úext©r   r   r   r   r=   Ú   s   þz+LicensesManager.bundles.<locals>.<dictcomp>NzNo license bundles found at all)r   rC   re   rf   rg   rd   rZ   r`   )r   r   r&   Zapp_pathÚapp_namer   ri   r   r&   Ö   s    þzLicensesManager.bundlesN)r   r   F)T)r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   r   r   r   Ú DEFAULT_THIRD_PARTY_LICENSE_FILErY   Úpropertyr   r   Ú	coroutiner"   r!   r'   r(   r)   rd   re   r&   r   r   r   r   r      s(   
þú	

-'r   c                       sB   e Zd ZdZeddœ‡ fdd„Zejdd„ ƒZ‡ fdd	„Z	‡  Z
S )
ÚLicensesHandlerz6A handler for serving licenses used by the applicationN)ÚmanagerÚreturnc                    s   t ƒ  ¡  || _dS )zInitialize the handler.N)ÚsuperÚ
initializers   )r   rs   ©Ú	__class__r   r   rv   í   s    
zLicensesHandler.initializec           	      Ã   sª   t t |  dd¡¡ƒ}|  dd¡}|  dd¡}t t |  dd¡¡ƒ}| jj|||d	�I d
H \}}|rŠd | jjj 	¡ t
 |¡¡}|  |¡ |  |¡ | j|d�I d
H  d
S )z Return all the frontend licensesr   ÚtrueÚformatr#   r&   r   ÚdownloadÚ0r   Nz{}-licenses{})Ú
_mime_type)Úboolr#   r]   Zget_argumentrs   r"   rz   r   rj   ÚlowerÚ	mimetypesÚguess_extensionZset_attachment_headerÚwriteÚfinish)	r   Ú_argsr   r   r   r{   r!   ÚmimeÚfilenamer   r   r   r8   ò   s     ýÿ

zLicensesHandler.getc                    s*   |   ¡  |  d|¡ tt| ƒj|i |¤ŽS )z>Overload the regular finish, which (sensibly) always sets JSONzContent-Type)Zupdate_api_activityZ
set_headerru   r   rƒ   )r   r}   ÚargsÚkwargsrw   r   r   rƒ     s    zLicensesHandler.finish)rk   rl   rm   rn   r   rv   r   Úauthenticatedr8   rƒ   Ú__classcell__r   r   rw   r   rr   ê   s
   
rr   )rn   r$   r?   r#   r€   rf   Úconcurrent.futuresr   Úpathlibr   Zjupyter_server.base.handlersr   Útornador   r   Z	traitletsr   r   Ztraitlets.configr	   Úconfigr   ro   rJ   r�   Úadd_typer   rr   r   r   r   r   Ú<module>   s&    M