o
    ^i!)                     @  s   d Z ddlm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mZ ddlmZmZ ddlmZ ddl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 reedd G dd deZG dd deZ dS )z3Manager and Tornado handlers for license reporting.    )annotationsN)ThreadPoolExecutor)Path)TYPE_CHECKINGAny)
APIHandler)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	d
Z	d-d.ddZd.ddZd/ddZd/ddZd0d1ddZd2d$d%Zd3d'd(Zd4d5d)d*Zd+S )6LicensesManagerzA 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helpreturndict[str, Any]c                 C  sF   t rddlm} t| j|sJ | jj| jjg}dd |D }t|S )zLazily 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.
        r   LabServerAppc                 S  s   g | ]	}|D ]}|qqS  r   ).0
extensions	extensionr   r   ]/home/ubuntu/hpml_nyu/venv/lib/python3.10/site-packages/jupyterlab_server/licenses_handler.py
<listcomp>A       z8LicensesManager.federated_extensions.<locals>.<listcomp>)r   appr   
isinstanceparentlabextensions_pathextra_labextensions_pathr   )selfr   	per_pathsr"   r   r   r   federated_extensions1   s   z$LicensesManager.federated_extensionsmarkdown.*Freport_formatstrbundles_pattern	full_textbooltuple[str, str]c                   s$   t | jj| j|||dI dH S )zrAsynchronous wrapper around the potentially slow job of locating
        and encoding all of the licenses
        r)   r+   r,   N)asynciowrap_futureexecutorsubmitreport)r$   r)   r+   r,   r   r   r   report_asyncD   s   
zLicensesManager.report_asyncc                 C  sf   | j |d}|dkr| |dfS |dkr| |dfS |dkr)| j||ddfS d	| d
}t|)z*create a human- or machine-readable report)r+   jsonzapplication/jsoncsvztext/csvr'   )r,   r   zUnsupported report format .)bundlesreport_json
report_csvreport_markdown
ValueError)r$   r)   r+   r,   r9   msgr   r   r   r4   S   s   zLicensesManager.reportr9   c                 C  s   t jd|idddS )z0create a JSON report
        TODO: SPDX
        r9      T)indent	sort_keys)r6   dumps)r$   r9   r   r   r   r:   c   s   zLicensesManager.report_jsonc                   st   t  }g d}tj|dg|d}|  | D ]\}}|d D ] |d|i fdd|D  q"q| S )zcreate a CSV report)nameversionInfo	licenseIdextractedTextbundle)
fieldnamespackagesc                   s   i | ]	}|  |d qS ) )get)r   fieldpackager   r   
<dictcomp>t   r   z.LicensesManager.report_csv.<locals>.<dictcomp>)ioStringIOr7   
DictWriterwriteheaderitemswriterowgetvalue)r$   r9   outfilerH   writerbundle_namerG   r   rM   r   r;   i   s   zLicensesManager.report_csvTc              
   C  s  g }dd |  D }|rt|nd}|  D ]o\}}|d| dg7 }|dg }|s3|ddg7 }q|D ]P}	|	dt }
|	d	t }|	d
t }|	dd}|ddd|
 d|d| dd|g g7 }|r|s{|g d7 }q5|ddd|ddg7 }q5qd|S )zcreate a markdown reportc                 S  s2   g | ]\}}| d g D ]
}t| dtqqS )rI   rC   )rK   lenUNKNOWN_PACKAGE_NAME)r   rY   rG   rN   r   r   r   r   |   s    
z3LicensesManager.report_markdown.<locals>.<listcomp>r   z# rJ   rI   z> No licenses foundrC   rD   rE   rF   z## 	z**`   )rJ   z> No license text availablerJ   z<pre/>z</pre>
)rT   maxrK   r[   stripjoinljust)r$   r9   r,   lineslibrary_nameslongest_namerY   rG   rI   rN   rC   version_info
license_idextracted_textr   r   r   r<   y   sB   
zLicensesManager.report_markdownpathr   rG   
str | Nonec           	      C  s   dg i}g }| j D ]d}|| }| jd| | s ||g7 }q	zt|jdd}W n tyF } z| jd||| W Y d}~q	d}~ww z|d 	|d  W q	 tym } z| jd||| W Y d}~q	d}~ww |d sz| jd|| |S )	z2Return the content of a packages's license bundlesrI   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existsr6   loads	read_text	Exceptionwarningextend)	r$   rj   rG   bundle_jsonchecked_pathslicense_filelicenses_path	file_jsonerrr   r   r   license_bundle   sF   

		zLicensesManager.license_bundletuple[Path | None, str | None]c                 C  sv   t rddlm} t| j|sJ t| jj}|d }| s,|jd }| r*|}ndS t	|j
ddd }||fS )zget the static directory for this app

        This will usually be in `static_dir`, but may also appear in the
        parent of `static_dir`.
        r   r   zpackage.json)NNrl   rm   rC   )r   r   r   r    r!   r   
static_dirrr   r6   rs   rt   )r$   r   rj   package_jsonparent_package_jsonrC   r   r   r   app_static_info   s   
zLicensesManager.app_static_infoc                   sj    fddj  D } \}}|dur+|dusJ t |r+||||< |s3jd |S )z6Read all of the licenses
        TODO: schema
        c                   s2   i | ]\}}t  |r|t|d  |qS )ext_path)rematchr~   r   )r   rC   extr+   r$   r   r   rO      s    
z+LicensesManager.bundles.<locals>.<dictcomp>NzNo license bundles found at all)r&   rT   r   r   r   r~   rp   rv   )r$   r+   r9   app_pathapp_namer   r   r   r9      s   zLicensesManager.bundlesN)r   r   )r'   r(   F)r)   r*   r+   r*   r,   r-   r   r.   )r9   r   r   r*   )T)r9   r   r,   r-   r   r*   )rj   r   rG   rk   r   r   )r   r   )r(   )r+   r*   r   r   )__name__
__module____qualname____doc__r   r2   r	   r
    DEFAULT_THIRD_PARTY_LICENSE_FILEro   propertyr&   r5   r4   r:   r;   r<   r~   r   r9   r   r   r   r   r   !   s*    
	



-
'r   c                      s@   e Zd ZdZd fddZejdd
dZd fddZ  Z	S )LicensesHandlerz6A handler for serving licenses used by the applicationmanagerr   r   Nonec                   s   t    || _dS )zInitialize the handler.N)super
initializer   )r$   r   	__class__r   r   r      s   

zLicensesHandler.initialize_argsr   c           
        s   t t| dd}| dd}| dd}t t| dd}| jj|||d	I d
H \}}trBddlm} t	| jj
|sBJ |rWd| jj
j t|}	| |	 | | | j|dI d
H  d
S )z Return all the frontend licensesr,   trueformatr6   r9   r(   download0r/   Nr   r   z{}-licenses{})
_mime_type)r-   r6   rs   get_argumentr   r5   r   r   r   r    r!   r   r   lower	mimetypesguess_extensionset_attachment_headerwritefinish)
r$   r   r,   r)   r+   r   r4   mimer   filenamer   r   r   rK      s(   

zLicensesHandler.getr   r*   argskwargsc                   s2   |    | d| tt| j|i |I dH S )z>Overload the regular finish, which (sensibly) always sets JSONzContent-TypeN)update_api_activity
set_headerr   r   r   )r$   r   r   r   r   r   r   r     s   zLicensesHandler.finish)r   r   r   r   )r   r   r   r   )r   r*   r   r   r   r   r   r   )
r   r   r   r   r   r   authenticatedrK   r   __classcell__r   r   r   r   r      s    r   )!r   
__future__r   r0   r7   rP   r6   r   r   concurrent.futuresr   pathlibr   typingr   r   jupyter_server.base.handlersr   tornador   	traitletsr	   r
   traitlets.configr   configr   r   r[   r   add_typer   r   r   r   r   r   <module>   s.     X