
    Mh!)                    0   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j        d           ej        dd            G d de          Z G 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                      e Zd ZdZ ed          Z e e            ede gd          Z	e
d#d	            Z	 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 )-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                    t           rddlm} t          | j        |          sJ | j        j        | j        j        g}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                    g | ]	}|D ]}|
S  r   ).0
extensions	extensions      b/var/www/html/test/jupyter/venv/lib/python3.11/site-packages/jupyterlab_server/licenses_handler.py
<listcomp>z8LicensesManager.federated_extensions.<locals>.<listcomp>A   s&    ```JU_``	i````    )r   appr   
isinstanceparentlabextensions_pathextra_labextensions_pathr   )selfr   	per_pathsr&   s       r    federated_extensionsz$LicensesManager.federated_extensions1   su      	9))))))dk<88888 K*K0
	 a`)```'(:;;;r"   markdown.*Freport_formatstrbundles_pattern	full_textbooltuple[str, str]c                |   K   t          j        | j                            | j        |||                     d{V S )zrAsynchronous wrapper around the potentially slow job of locating
        and encoding all of the licenses
        r-   r/   r0   N)asynciowrap_futureexecutorsubmitreport)r(   r-   r/   r0   s       r    report_asynczLicensesManager.report_asyncD   sc       (M  + /#	 !  
 
 
 
 
 
 
 
 	
r"   c                
   |                      |          }|dk    r|                     |          dfS |dk    r|                     |          dfS |dk    r|                     ||          dfS d	| d
}t	          |          )z*create a human- or machine-readable report)r/   jsonzapplication/jsoncsvztext/csvr+   )r0   r   zUnsupported report format .)bundlesreport_json
report_csvreport_markdown
ValueError)r(   r-   r/   r0   r?   msgs         r    r9   zLicensesManager.reportS   s    ,,,??F""##G,,.@@@E!!??7++Z77J&&$$W	$BB 
 <=;;;oor"   r?   c                4    t          j        d|idd          S )z0create a JSON report
        TODO: SPDX
        r?      T)indent	sort_keys)r<   dumps)r(   r?   s     r    r@   zLicensesManager.report_jsonc   s      z9g.qDIIIIr"   c                H   t          j                    }g d}t          j        |dg|          }|                                 |                                D ]5\  }}|d         D ]'|                    d|ifd|D                        (6|                                S )zcreate a CSV report)nameversionInfo	licenseIdextractedTextbundle)
fieldnamespackagesc                >    i | ]}|                     |d           S ) )get)r   fieldpackages     r    
<dictcomp>z.LicensesManager.report_csv.<locals>.<dictcomp>t   s)    QQQU5'++eR"8"8QQQr"   )ioStringIOr=   
DictWriterwriteheaderitemswriterowgetvalue)r(   r?   outfilerP   writerbundle_namerO   rV   s          @r    rA   zLicensesManager.report_csvi   s    +--JJJ
X4K
4KLLL#*==?? 	 	K!*-   +QQQQjQQQ    !!!r"   Tc           
     .   g }d |                                 D             }|rt          |          nd}|                                 D ]8\  }}|d| dgz  }|                    dg           }|s|ddgz  }0|D ]}	|	                    dt                                                    }
|	                    dt                                                    }|	                    d	t                                                    }|	                    d
d          }|dd                    d|
 d                    |          d| d                    d          |g          z   gz  }|r|s|g dz  }|ddd|ddgz  }:d                    |          S )zcreate a markdown reportc           	         g | ]F\  }}|                     d g           D ]*}t          |                     dt                              +GS )rQ   rK   )rT   lenUNKNOWN_PACKAGE_NAME)r   ra   rO   rV   s       r    r!   z3LicensesManager.report_markdown.<locals>.<listcomp>|   sh     
 
 
#V!::j"55
 
  F$899::
 
 
 
r"   r   z# rS   rQ   z> No licenses foundrK   rL   rM   rN   z## 	z**`   )rS   z> No license text availablerS   z<pre/>z</pre>
)r\   maxrT   re   stripjoinljust)r(   r?   r0   lineslibrary_nameslongest_namera   rO   rQ   rV   rK   version_info
license_idextracted_texts                 r    rB   zLicensesManager.report_markdowny   s   
 
'.}}
 
 

 .;As=)))#*==??  	R  	RK(;(("--Ezz*b11H /44# R R{{6+?@@FFHH&{{=:NOOUUWW$[[6JKKQQSS
!(_b!A!A		 1t 1 1 1 7 7 E E 7l 7 7 7 = =b A A * 	   R) R!H!H!HH"b(NHb!QQ/R0 yyr"   pathr   rO   
str | Nonec                L   dg i}g }| j         D ]}||z  }| j                            d|           |                                s||gz  }=	 t	          j        |                    d                    }n4# t          $ r'}| j                            d|||           Y d}~d}~ww xY w	 |d         	                    |d                    # t          $ r'}| j                            d|||           Y d}~d}~ww xY w|d         s| j                            d||           |S )	z2Return the content of a packages's license bundlesrQ   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(   rt   rO   bundle_jsonchecked_pathslicense_filelicenses_path	file_jsonerrs	            r    license_bundlezLicensesManager.license_bundle   s   ', ; 	 	L </MHNN5}EEE '')) -0	 J}'>'>'>'P'PQQ		     H!	   	J'..y/DEEEE     <!	    :& 	aHH&R_```s0   (A55
B&?B!!B&*!C
C=C88C=tuple[Path | None, str | None]c                X   t           rddlm} t          | j        |          sJ t          | j        j                  }|dz  }|                                s#|j        dz  }|                                r|}ndS t          j	        |
                    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)NNrw   rx   rK   )r   r#   r   r$   r%   r   
static_dirr}   r<   r~   r   )r(   r   rt   package_jsonparent_package_jsonrK   s         r    app_static_infozLicensesManager.app_static_info   s      	9))))))dk<88888DK*++n,""$$ 	""&+">"))++ "2!zz,00'0BBCCFKTzr"   c                $     fd j                                         D             }                                 \  }}|2|J t          j        |          r                     ||          ||<   |s j                            d           |S )z6Read all of the licenses
        TODO: schema
        c           	         i | ]D\  }}t          j        |          |                    t          |d                    |          ES )ext_path)rematchr   r   )r   rK   extr/   r(   s      r    rW   z+LicensesManager.bundles.<locals>.<dictcomp>   s]     
 
 
cx..
$%%d3z?&;&;TBB
 
 
r"   NzNo license bundles found at all)r*   r\   r   r   r   r   r{   r   )r(   r/   r?   app_pathapp_names   ``   r    r?   zLicensesManager.bundles   s    
 
 
 
 
!6<<>>
 
 
 "1133('''x22 L$($7$7($K$K! 	@H>???r"   N)r   r   )r+   r,   F)r-   r.   r/   r.   r0   r1   r   r2   )r?   r   r   r.   )T)r?   r   r0   r1   r   r.   )rt   r   rO   ru   r   r   )r   r   )r,   )r/   r.   r   r   )__name__
__module____qualname____doc__r   r7   r
   r    DEFAULT_THIRD_PARTY_LICENSE_FILErz   propertyr*   r:   r9   r@   rA   rB   r   r   r?   r   r"   r    r   r   !   sI         "!a000H!%		,8688
 M" " " < < < X<& _d
 
 
 
 
    J J J J" " " " +  +  +  +  + Z% % % %N   *      r"   r   c                  R     e Zd ZdZd fdZej        dd	            Zd fdZ xZ	S )LicensesHandlerz6A handler for serving licenses used by the applicationmanagerr   r   Nonec                V    t                                                       || _        dS )zInitialize the handler.N)super
initializer   )r(   r   	__class__s     r    r   zLicensesHandler.initialize   s$    r"   _argsr   c                  K   t          t          j        |                     dd                              }|                     dd          }|                     dd          }t          t          j        |                     dd                              }| j                            |||	           d
{V \  }}t          r"ddlm} t          | j        j
        |          sJ |r^d                    | j        j
        j                                        t          j        |                    }	|                     |	           |                     |           |                     |           d
{V  d
S )z Return all the frontend licensesr0   trueformatr<   r?   r,   download0r4   Nr   r   z{}-licenses{})
_mime_type)r1   r<   r~   get_argumentr   r:   r   r#   r   r$   r%   r   r   lower	mimetypesguess_extensionset_attachment_headerwritefinish)
r(   r   r0   r-   r/   r   r9   mimer   filenames
             r    rT   zLicensesHandler.get   s      D$5$5k6$J$JKKLL	))(F;;++It<<
4#4#4Z#E#EFFGG!\66'+ 7 
 
 
 
 
 
 
 
  	A))))))dl1<@@@@@ 	1&--#,2244i6OPT6U6U H &&x000

6kkTk***********r"   r   r.   argskwargsc                   K   |                                   |                     d|            t          t          |           j        |i | d{V 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   s       r    r   zLicensesHandler.finish  sc       	  """
3333U:t,,3TDVDDDDDDDDDr"   )r   r   r   r   )r   r   r   r   )r   r.   r   r   r   r   r   r   )
r   r   r   r   r   r	   authenticatedrT   r   __classcell__)r   s   @r    r   r      s        @@     
 	+ + + +4E E E E E E E E E Er"   r   )!r   
__future__r   r5   r=   rX   r<   r   r   concurrent.futuresr   pathlibr   typingr   r   jupyter_server.base.handlersr   tornador	   	traitletsr
   r   traitlets.configr   configr   r   re   r   add_typer   r   r   r"   r    <module>r      s   9 9 # " " " " "  



 				      				 1 1 1 1 1 1       % % % % % % % % 3 3 3 3 3 3       # # # # # # # # 0 0 0 0 0 0 , , , , , , $?    9_--5I...T T T T T) T T Tn)E )E )E )E )Ej )E )E )E )E )Er"   