
    kKjV                       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	Z	ddl
mZ ddlmZmZmZ erddlZddlmZmZ ddlmZmZmZ ddlmZ d	d
lmZ e	j8                  dk\  rddlmZ nddlmZ d	dl m!Z! d	dl"m#Z#  e$       Z% e& ee'      jQ                         jR                        Z* G d de+      Z,	 	 	 	 	 	 	 d 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d!dZ-	 	 	 	 	 	 d"	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d#dZ.	 	 	 	 	 	 	 d$	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d%dZ/	 	 	 	 	 d&	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d'dZ0dZ1d(dZ2d)dZ3d*dZ4d+d,dZ5d+d-dZ6	 	 	 	 d.	 	 	 	 	 	 	 	 	 d/dZ7d0dZ8d1dZ9d1dZ:d2dZ;y)3z@Utilities for installing Javascript extensions for the notebook.    )annotationsN)Path)TYPE_CHECKINGAnycast)PackageNotFoundErrorversion)ENV_JUPYTER_PATHSYSTEM_JUPYTER_PATHjupyter_data_dir)ensure_dir_exists   )get_federated_extensions)      )load)_test_overlap)get_core_metac                      e Zd ZdZy)ArgumentConflictzHRaised when conflicting arguments are passed to a labextension function.N)__name__
__module____qualname____doc__     u/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/jupyter_builder/federated_extensions.pyr   r   )   s    Rr   r   c                   d}t        |||      }	t        |	       t        | t        t        z        rd}
t        |
      |st        |       j                  }t        t        |	      |z        }|rt        j                  j                  |      ru|r|j                  d|       t        |      j                         r/t        |      j                         st        j                   |       nt        |      j#                          t        |      j$                  j'                  dd       |rt        t        |       j)                               } t        |      j+                         s2|r|j                  d||        	 t        |      j-                  |        |S t        |      j                         s| d
}
t5        |
      |S t        |       j                         rt        |       j)                         }t        j6                  |      D ]  \  }}}t        |      }t        |      |j9                  |      z  }|j+                         s&|r|j                  d|       |j'                  d       |D ],  }t        ||z        }t        ||z        }t;        |||       .  |S t        |       }t;        |||       |S # t.        $ r7}t1        j0                         j3                  d      rd	}
t/        |
      | d}~ww xY w)a  Install a prebuilt extension for JupyterLab.

    Stages files and/or directories into the labextensions directory.
    By default, this compares modification time, and only stages files that need updating.
    If `overwrite` is specified, matching files are purged before proceeding.

    Parameters
    ----------
    path : path to file, directory, zip or tarball archive, or URL to install
        By default, the file will be installed with its base name, so '/path/to/foo'
        will install to 'labextensions/foo'. See the destination argument below to change this.
        Archives (zip or tarballs) will be extracted into the labextensions directory.
    user : bool [default: False]
        Whether to install to the user's labextensions directory.
        Otherwise do a system-wide install (e.g. /usr/local/share/jupyter/labextensions).
    overwrite : bool [default: False]
        If True, always install the files, regardless of what may already be installed.
    symlink : bool [default: True]
        If True, create a symlink in labextensions, rather than copying files.
        Windows support for symlinks requires a permission bit which only admin users
        have by default, so don't rely on it.
    labextensions_dir : str [optional]
        Specify absolute path of labextensions directory explicitly.
    destination : str [optional]
        name the labextension is installed to.  For example, if destination is 'foo', then
        the source file will be installed to 'labextensions/foo', regardless of the source name.
    logger : Jupyter logger [optional]
        Logger instance to use
    sys_prefix : bool [default: False]
        If True, install to sys.prefix (i.e. the virtualenv/conda env prefix).

    N)user
sys_prefixlabextensions_dirzpath must be a string pointing to a single extension to install; call this function multiple times to install multiple extensionszRemoving: %sT)parentsexist_okzSymlinking: %s -> %sWindowszSymlinks can be activated on Windows 10 for Python version 3.8 or higher by activating the 'Developer Mode'. That may not be allowed by your administrators.
See https://docs.microsoft.com/en-us/windows/apps/get-started/enable-your-device-for-developmentz exists and is not a symlinkzMaking directory: %s)r"   logger)_get_labextension_dirr   
isinstancelisttuple	TypeErrorr   namestrospathlexistsinfois_dir
is_symlinkshutilrmtreeunlinkparentmkdirresolveexists
symlink_toOSErrorplatform
startswith
ValueErrorwalkrelative_to_maybe_copy)r/   symlink	overwriter   r!   destinationr&   r    	full_destlabextmsgesrc_root
parent_str_filesparent_pathdest_dir	file_namesrc	dest_files                        r   develop_labextensionrS   2   s   V I"+F f$u% Rn4jooDL;./IRWW__Y/KK	2	?!!#DO,F,F,HMM)$O""$ 	O   =4:%%'(I%%'2ItD
Y**40@ + i++-K;<CS/!& # 
d			:%%'$&GGH$5 J5z*KI)@)@)JJH??$KK 6At,"	+	129 45	C6: # %6  $iC62?  $$&11)<{ 
 "#,A-s   =K   	L 	2K;;L c                   t        |       \  }}t        t        |j                        j                        }	g }
|D ]  }t        t        |	      |d   z        }|d   }|r|j                  d||       t        |      j                         st        |	|       t        ||||||||      }|
j                  |        |
S )zDevelop a labextension bundled in a Python package.

    Returns a list of installed/updated directories.

    See develop_labextension for parameter information.
    rQ   destzInstalling %s -> %sr%   )rD   rC   r   r    r!   rE   r&   )
_get_labextension_metadatar-   r   __file__r7   r1   r:   build_labextensionrS   append)moduler   r    rD   rC   r!   r&   mlabexts	base_path
full_destsrG   rQ   rU   rF   s                  r   develop_labextension_pyr_      s     ,F3JAwD$++,IJ$y/F5M12f~KK-sD9Cy!y8(!/	
	 	)$' * r   c                p   t        t        |       j                               }|J|r|j                  d       t        |      j                         dz  }	|	j	                         rt        |	      }|xs t        |||      }t        t        |      j                               }|r|j                  d|        t        ||      \  }
}|dk(  rdt        |      g}nd|g}d	|
g||}||j                  d
|g       |r|j                  d       |r|j                  d       t        j                  ||       y)z'Build a labextension in the given path.Nz{[33m(Deprecated) `core_path` is deprecated and will be removed in a future release. Use `core_package_file` instead.
 [0mpackage.jsonext_pathr&   Building extension in %s@jupyterlab/builder--core-path--core-package-filenodez--static-url--development--source-mapcwd)r-   r   r9   warningr:   r   r1   _ensure_builder)_resolve_core_path_for_jupyterlab_builderextendrY   
subprocess
check_call)r/   r&   development
static_url
source_mapcore_versioncore_package_file	core_pathrc   core_path_packagebuilder
marker_pkg	core_flag	argumentss                 r   rX   rX      sG    4:%%'(HNNR !O335F##% #$5 6) ].
 D!23;;=>.5)(4EFGZ**"$MN_$`a	*,=>	797h7I.*56)()2r   c                   t        t        |       j                               }|xs t        |||      }t        t        |      j                               }|r|j	                  d|        t        |      }t        |      dz  j                         5 }	t        j                  |	      }
ddd       
d   |vrt        |d       nt        t        ||
d      d         |
d   z        }t        t        |      |
d	   j                  d
d      z        }t        |      j                         s/t        j                  |       t        |      j                  |       t        ||      \  }}|dk(  rdt!        |      g}nd|g}d|g|d|}|r|j#                  d       |r|j#                  d       t%        j&                  ||       y# 1 sw Y   "xY w)z%Watch a labextension in a given path.rb   rd   ra   Nr,   T)r    ext_dir
jupyterlab	outputDirstaticre   rf   rg   rh   z--watchri   rj   rk   )r-   r   r9   r   r1   r   openjsonr   r_   getr3   r4   r5   r;   rn   ro   rY   rq   rr   )r/   labextensions_pathr&   rs   ru   rv   rw   rc   federated_extensionsfidext_datarF   
output_dirrz   r{   r|   r}   s                    r   watch_labextensionr     s    4:%%'(H) ].
 D!23;;=>.5 44FG
x.>
)	/	/	1S99S> 
2 33T:%hv&67	BChvFVV
	 NXl377XNN

 I))+MM)$O&&z2)(4EFGZ**"$MN_$`a	*,=>	B9BiBBI)()2; 
2	1s   GG)z@jupyter/builderre   c                    t        |       }|j                  }|j                  dk7  r+|dz  }|j                         st	        j
                  ||       t        |      S )a  Return the core path directory for @jupyterlab/builder.

    @jupyterlab/builder's downstream script expects a file named package.json
    inside the core path directory. If core_package_file is not named
    package.json, a copy named package.json is created in the same directory.
    ra   )r   r7   r,   r:   r4   copy2r-   )rw   	core_filecore_dirtargets       r   ro   ro   D  sQ     &'IH~~'N*}}LLF+x=r   c                    t         D ]P  }| j                  di       j                  |      }|xs! | j                  di       j                  |      }|L||fc S  y)zReturn (marker_pkg, dep_spec) for the builder marker the extension declares.

    Prefers `@jupyter/builder`. Returns (None, None) if neither marker is present.
    devDependenciesdependencies)NN)_BUILDER_MARKER_CANDIDATESr   )r   	candidatevs      r   _select_builder_markerr   V  s^    
 0	LL*B/33I>@nb155i@=a<	 0
 r   c                *   t        |      j                         5 }t        j                  |      }ddd       t        |       dz  j                         5 }t        j                  |      }ddd       t	              \  }}|#ddj                  t               }t        |      t        d|      }d|v rVt        |       |z  dz  j                         5 }t        t        j                  |      j                  dd            }ddd       t        |       d	z  j                         st        j                  d
g|        |j                  dd      }| }	 t        |	      j                  d	g| j                         szt        |	      j                   t        |	      k(  rd| }t        |      t        t        |	      j                         }	 t        |	      j                  d	g| j                         szj                  di       j                  |      }
|
t#        |
|dd      }|sr t        |	      j                  d	g|d j                         5 }t        t        j                  |      j                  dd            }ddd       t#        |
|dd      }|sd| d|
 d| }t        |      t         t        |	      j                  d	g|dd       }||fS # 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w# 1 sw Y   xY w)zKEnsure that we can build the extension and return ``(script, marker_pkg)``.Nra   z&Extensions require a devDependency on z or r-   /r	    node_modulesjlpmrk   r   zCould not find r   T)drop_prerelease1drop_prerelease2@z, you have a dependency on libzbuild-labextension.js)r   r   r   r   r   joinr   r?   r   r-   r   r:   rq   rr   splitjoinpathr7   r   )rc   rw   r   	core_datar   r{   dep_version2rH   marker_partsr   dep_version1overlapscripts                r   rn   rn   c  s   		 	%	%	'3IIcN	 
(
x.>
)	/	/	1S99S> 
2  6h?J6v{{C]7^6_`o|,L l8n|+n<BBDtyy~11)R@AL EN^+335vhH5 ##C+LF#d6l##NB\BIIK<$v,.#J<0CS/!T&\(()	 $d6l##NB\BIIK ==!2B7;;JGL!!	
 &f&&~UUnUZZ\`c"499S>#5#5i#DE ]#!%!%	G 8Al^ T,,8>;  S/!Vn\|\U\D[\F :u 
(	'	1	1 ED2 ]\s/   K"K//K</L	"K,/K9<L	Lc                   t        |      j                         syt        |       j                         j                  t        |      j                         j                  z
  dkD  r|r|j	                  d|       y|r|j                  d|       y)a  Check whether a file should be copied because it is missing or the source is newer.

    Returns whether the file needs to be updated.

    Parameters
    ----------
    src : string
        A path that should exist from which to copy a file
    dest : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use

    Tgư>zOut of date: %szUp to date: %sF)r   r:   statst_mtimerm   r1   rQ   rU   r&   s      r   _should_copyr     sr     :Cy~~  4:??#4#=#==D NN,d3$d+r   c                x    t        | ||      r,|r|j                  d| |       t        j                  | |       yy)a"  Copy a file if it needs updating.

    Parameters
    ----------
    src : string
        A path that should exist from which to copy a file
    dest : string
        A path that might exist to which to copy a file
    logger : Jupyter logger [optional]
        Logger instance to use

    r%   zCopying: %s -> %sN)r   r1   r4   r   r   s      r   rB   rB     s7     Cf-KK+S$7S$ .r   c                   d| fd|fd|fd|fg}|D cg c]  \  }}|s	| d| }}}t        |      dkD  r!dj                  |      }d| }	t        |	      | r!t        t	        t                     d	z        }
|
S |r t        t	        t        d
         d	z        }
|
S |rt        t	        |      dz  dz  d	z        }
|
S |r|}
|
S t        t	        t        d
         d	z        }
|
S c c}}w )a  Return the labextension directory specified.

    Parameters
    ----------
    user : bool [default: False]
        Get the user's .jupyter/labextensions directory
    sys_prefix : bool [default: False]
        Get sys.prefix, i.e. ~/.envs/my-env/share/jupyter/labextensions
    prefix : str [optional]
        Get custom prefix
    labextensions_dir : str [optional]
        Get what you put in

    r   prefixr!   r    =r   z, zYcannot specify more than one of user, sys_prefix, prefix, or labextensions_dir, but got: labextensionsr   sharejupyter)lenr   r   r-   r   r   r
   r   )r   r    r   r!   conflictingnr   conflicting_setconflictrH   rG   s              r   r'   r'     s.   * 
	6	/0	z"	K 1<A1q!AaU|OA
?a99_-ijrists##T*,-?@ M 
T*1-.@A M 
T&\G+i7/IJ
 M	 
" M T-a01OCDM Bs
   
C&	C&c                B    | D cg c]  }d|vs|dk7  s| c}S c c}w )z<Filter out dirs that can never be Python package components..__pycache__r   )dirsds     r   _valid_package_dirsr     s'    Ct!s!|]0BAtCCCs   	c                :   t        |       }g }t        j                  t        |      d      D ]j  \  }}}t	        |      |dd d|v st        |      j                  |      }|j                  sA|j                  dj                  |j                               l |S )zSFind Python packages (dirs with __init__.py), pruning __pycache__ and dotted names.TfollowlinksNz__init__.pyr   )	r   r.   r@   r-   r   rA   partsrY   r   )r/   path_objpackagesrootr   rM   rels          r   _find_packagesr     s    DzHHWWS]EdE%d+QE!t*((2Cyy 34 F Or   c           
        t        |       }t               }t        j                  t	        |      d      D ]  \  }}}t        |      |dd t        d |D              s(t        |      j                  |      }|j                  sOt        t        |j                              D ]2  }|j                  dj                  |j                  d|dz                 4  t        |      S )zvFind namespace packages (dirs with .py files, no __init__.py required).

    Prunes __pycache__ and dotted names.
    Tr   Nc              3  >   K   | ]  }|j                  d         yw)z.pyN)endswith).0fs     r   	<genexpr>z+_find_namespace_packages.<locals>.<genexpr>  s     0%Qqzz% %s   r   r   )r   setr.   r@   r-   r   anyrA   r   ranger   addr   r)   )r/   r   foundr   r   rM   r   is           r   _find_namespace_packagesr     s    
 DzHeEWWS]EdE%d+Q0%00t*((2Cyys399~.AIIchhsyy1q5'9:; / F ;r   c           	     |   t        t        |       j                               }t        |      j                         sd| d}t	        |      g }	 t        j                  |       }t        |d      r||j                         fS 	 d}t        |      dz  j                         rRt        |      dz  j                  d      5 }t        |      }ddd       j                  di       j                  d      }|sF	 t        j                  t         j"                  d	d
g|      j%                  d      j'                         }	 t+        |       |j5                  dd      g}	|	j7                  t9        |             |	j7                  t;        |             |	D ]8  }	 t        j                  |      }t        |d      r||j                         fc S : d|  d| }t=        |      # t        $ r}|j                  |       Y d}~{d}~ww xY w# 1 sw Y   >xY w# t        j(                  $ r d|  d}t	        |      dw xY w# t,        $ rM t        j.                  t         j"                  dddd|g       t         j0                  j3                  d|       Y Jw xY w# t        $ r}|j                  |       Y d}~%d}~ww xY w)af  Get the list of labextension paths associated with a Python module.

    Returns a tuple of (the module path,             [{
        'src': 'mockextension',
        'dest': '_mockdestination'
    }])

    Parameters
    ----------
    module : str
        Importable Python module exposing the
        magic-named `_jupyter_labextension_paths` function

    z
The path `z` does not exist._jupyter_labextension_pathsNzpyproject.tomlrbprojectr,   zsetup.pyz--namerk   utf8zThe Python package `zl` is not a valid package, it does not specify a `name` in `pyproject.toml` nor has a legacy `setup.py` file.z-mpipinstallz-er   -rL   zThere is no labextension at z. Errors encountered: )r-   r   r9   r:   FileNotFoundError	importlibimport_modulehasattrr   	ExceptionrY   r   r   r   rq   check_outputsys
executabledecodestripCalledProcessErrorr	   r   rr   r/   insertreplacerp   r   r   ModuleNotFoundError)
rZ   mod_pathrH   errorsr[   excpackager   datapackage_candidatess
             r   rV   rV      s    4<'')*H>  "8*$56$$F##F+134a33555 5 G 	X))1138n//55d;s9D <((9b)--f5 	3''^^Z:    % 	S! nX67 * &	''0Aq78!77999 9 & )0Fvh
OC
c
""m  c <; ,, 	3&vh /e e  $C(d2	3   %s~~tUItXVW8$%"  	MM#	s\   2G ?H7AH =H= 2J	H(G??HH&H:=AJJ	J;J66J;)TFFNNNF)r/   str | os.PathLike[str]rC   boolrD   r   r   r   r!   
str | NonerE   r   r&   logging.Logger | Noner    r   returnr-   )FFTTNN)rZ   r-   r   r   r    r   rD   r   rC   r   r!   r   r&   r   r   	list[str])NFNFNNN)r/   r   r&   r   rs   r   rt   r   ru   r   rv   r   rw   r   rx   r   r   None)NFFNN)r/   r   r   r   r&   r   rs   r   ru   r   rv   r   rw   r   r   r   )rw   r-   r   r-   )r   zdict[str, Any]r   ztuple[str | None, str | None])rc   r-   rw   r-   r   ztuple[str, str])N)rQ   r-   rU   r-   r&   r   r   r   )rQ   r-   rU   r-   r&   r   r   r   )FFNN)
r   r   r    r   r   r   r!   r   r   r-   )r   r   r   r   )r/   r-   r   r   )rZ   r-   r   z tuple[Any, list[dict[str, str]]])<r   
__future__r   r   r   r.   r=   r4   rq   r   pathlibr   typingr   r   r   loggingimportlib.metadatar   r	   jupyter_core.pathsr
   r   r   jupyter_core.utilsr   !federated_extensions_requirementsr   version_infotomllibr   tomlicommandsr   rx   r   objectDEPRECATED_ARGUMENTr-   rW   r9   r7   HEREr?   r   rS   r_   rX   r   r   ro   r   rn   r   rB   r'   r   r   r   rV   r   r   r   <module>r	     s   F
 #   	    
  + + < V V 0 Gw # $h 
4>!!#**+Sz S $("$(n
 nn n 	n
 "n n "n n 	nf $($())
) ) 	)
 ) ") ") )\ %)!#$( 03
 03!03 03 	03
 03 03 "03 03 
03l %)#$(53
 53!53 "53 	53
 53 53 "53 
53~ I $
<~8 ( $(	)
)) ) "	)
 	)XD

"Q#r   