
    ͈2gqN                        d 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 ddlmZmZmZmZmZ dd	lmZmZ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 m!Z!m"Z"m#Z#  ee$      Z% G d de      Z& G d de      Z'y)
    N)	getLogger)Path)CompletedProcess)NamedTemporaryFile)AnyDictListOptionalTuple   )WinLex
logged_rununlink   )MenuMenuItem)folder_path)windows_terminal_settings_files)notify_shell_changesregister_file_extensionregister_url_protocolunregister_file_extensionunregister_url_protocolc            
       8    e Zd Zdeej
                     fdZdeej
                     fdZede	fd       Z
ede	fd       Zede	fd       Zedee	   fd       Zedeeef   f fd       Zd	 Z	 dd
ededeeeef      def fdZdde	fdZdee	   fdZ xZS )WindowsMenureturnc                 :   t         j                  d| j                         | j                  j                  dd       | j                  r| j                  j                  dd       | j
                  r| j
                  j                  dd       | j                  fS )NzCreating %sTparentsexist_ok)logdebugstart_menu_locationmkdirquick_launch_locationdesktop_locationselfs    6lib/python3.12/site-packages/menuinst/platforms/win.pycreatezWindowsMenu.create   s    		-!9!9:  &&td&C%%&&,,TD,I  !!''t'D((**    c                 l   t        | j                        }|j                         r'	 t        |j	                                | j                  fS | j                  fS # t
        $ rP t        j                  d| j                         t        j                  | j                  d       Y | j                  fS w xY w)NRemoving %sT)ignore_errors)
r   r$   existsnextiterdirStopIterationr"   r#   shutilrmtree)r)   menu_locations     r*   removezWindowsMenu.remove'   s    T556!L ]**,- ((**((** ! L		-)A)ABd66dK((**Ls   A A
B32B3c                 z    t        t        | j                  dd            | j                  | j                        z  S )a   
        On Windows we can create shortcuts in several places:

        - Start Menu
        - Desktop
        - Quick launch (only for user installs)

        In this property we only report the path to the Start menu.
        For other menus, check their respective properties.
        Fstart)r   windows_folder_pathmoderendernamer(   s    r*   r$   zWindowsMenu.start_menu_location4   s2     '		5'BCdkkRVR[R[F\\\r,   c                     | j                   dk(  rt        j                  d       y t        t	        | j                   dd            S )Nsystemz>Quick launch menus are not available for system level installsFquicklaunch)r;   warningswarnr   r:   r(   s    r*   r&   z!WindowsMenu.quick_launch_locationB   s7    99 MMZ['		5-HIIr,   c                 B    t        t        | j                  dd            S )NFdesktop)r   r:   r;   r(   s    r*   r'   zWindowsMenu.desktop_locationJ   s    '		5)DEEr,   c                 x    | j                   dk(  rt        j                  d       g S t        | j                         S )zLocation of the Windows terminal profiles.

        The parent directory is used to check if Terminal is installed
        because the settings file is generated when Terminal is opened,
        not when it is installed.
        r?   z=Terminal profiles are not available for system level installs)r;   r"   warningr   r(   s    r*   terminal_profile_locationsz&WindowsMenu.terminal_profile_locationsN   s1     99 KKWXI.tyy99r,   c                    t         |   }|j                  t        | j                  dz        t        | j                  dz        t        | j                  dz        t        | j
                  dz        t        | j
                  dz        t        | j                  dz  dz        t        | j                               dd       |S )NScriptsz
python.exezpythonw.exeLibrarybinico)SCRIPTS_DIRPYTHONPYTHONWBASE_PYTHONBASE_PYTHONWBIN_DIRSP_DIRICON_EXT)superplaceholdersupdatestrprefixbase_prefix_site_packages)r)   rV   	__class__s     r*   rV   zWindowsMenu.placeholders[   s    w+"4;;#:;dkkL89t{{]:;"4#3#3l#BC #D$4$4}$D Et{{Y6>?d1134!		
 r,   c           
      J   | j                   dz  t        t        j                  j	                  dd            | j                   dz  dz  | j                   dz  dz  t        t        j                  j	                  dd            | j                   dz  dz  | j                   dz  dz  fS )	Nz
_conda.exe	CONDA_EXEz"C:\oops\a_file_that_does_not_existcondabinz	conda.batrK   	MAMBA_EXEzmicromamba.exe)rZ   r   osenvirongetr(   s    r*   _conda_exe_path_candidatesz&WindowsMenu._conda_exe_path_candidatesl   s    |+-RSTz)K7u${2-RSTz),<<u$'77
 	
r,   valueslugextrac                     t         |   |||      }t        |d      rd|v r|d   dk7  r|j                  dd      }|S )a?  
        We extend the render method here to replace forward slashes with backslashes.
        We ONLY do it if the string does not start with /, because it might
        be just a windows-style command-line flag (e.g. cmd /K something).

        This will not escape strings such as `/flag:something`, common
        in compiler stuff but we can assume such windows specific
        constructs will have their platform override, which is always an option.

        This is just a convenience for things like icon paths or Unix-like paths
        in the command key.
        )rf   rg   replace/r   \)rU   r<   hasattrri   )r)   re   rf   rg   r\   s       r*   r<   zWindowsMenu.renderw   sH     u4u=5)$%(c/MM#t,Er,   c                 B    || j                   }| j                   dz  dz  S )NLibzsite-packages)rY   )r)   rY   s     r*   r[   zWindowsMenu._site_packages   s$    >[[F{{U"_44r,   c                     | j                   fS N)r$   r(   s    r*   _pathszWindowsMenu._paths   s    ((**r,   )FNrp   )__name__
__module____qualname__r   ra   PathLiker+   r7   propertyr   r$   r&   r'   r
   rG   r	   rX   rV   rd   r   boolr   r<   r[   rq   __classcell__)r\   s   @r*   r   r      s#   +bkk* ++bkk* + ]T ] ] Jt J J F$ F F 
:DJ 
: 
: d38n   	
 QU $5=d38n5M	(5T 5
+d +r,   r   c                   Z   e Zd Zedefd       Zdeedf   fdZdeedf   fdZdeedf   fdZ	dde
fdZdefd	Zd
 Zde
fdZddeej"                     defdZddee
   fdZddedefdZd Zd Zd Zed defd       Zed defd       ZdefdZdefdZdefdZdefdZ d Z!y)!WindowsMenuItemr   c                 P    | j                   j                  | j                         z  S )z
        Path to the .lnk file placed in the Start Menu
        On Windows, menuinst can create up to three shortcuts (start menu, desktop, quick launch)
        This property only lists the one for start menu
        )menur$   _shortcut_filenamer(   s    r*   locationzWindowsMenuItem.location   s"     yy,,t/F/F/HHHr,   .c                 :   ddl m} | j                          | j                         }|D ]  }|j                  dk(  s| j                         ^}}| j                  d      }|r:t        t        j                  j                  |            j                  dd       ndt        j                  v rd}nd	}| j                  d
      xs d}t        |      j                         rt        j                  d|        ||| j!                  d      t#        |      dj%                  |      ||d| j'                                 | j(                  j*                  D ]  }| j-                  |d        | j/                         }	| j1                         }
|	s|
r
t3                |S )Nr   )create_shortcutz.lnkworking_dirTr   USERPROFILEz%USERPROFILE%z%HOMEDRIVE%%HOMEPATH%icon z Overwriting existing link at %s.)ext r   Fr7   )win_utils.winshortcutr   
_precreaterq   suffix_process_command
render_keyr   ra   path
expandvarsr%   rb   r0   r"   rF   r}   rX   join_app_user_model_idr|   rG   $_add_remove_windows_terminal_profile_register_file_extensions_register_url_protocolsr   )r)   r   pathsr   target_path	argumentsr   r   r~   changed_extensionschanged_protocolss              r*   r+   zWindowsMenuItem.create   sh   :D;;&(&*&;&;&=#K)//-8KRWW''45;;DSW;X
 "**,-5??6*0bD Dz  ">E''B'/D	#'')	3 H 		<<H55hu5M =!;;= 88:!2 "r,   c                 2   | j                         }| j                         }|s|r
t                | j                  j                  D ]  }| j                  |d        | j                         }|D ]%  }t        j                  d|       t        |d       ' |S )NTr   r.   )
missing_ok)
_unregister_file_extensions_unregister_url_protocolsr   r|   rG   r   rq   r"   r#   r   )r)   r   r   r~   r   r   s         r*   r7   zWindowsMenuItem.remove   s    !==? ::<!2 "		<<H55ht5L = DIImT*4D)  r,   c                    | j                   g}g }| j                  d   r%|j                  | j                  j                         | j                  d   rP| j                  j
                  r:t        j                  d       |j                  | j                  j
                         |r$||D cg c]  }|| j                         z   c}z  }| j                  d   r|j                  | j                                t        |      S c c}w )NrD   r@   z"Quick launch menus are deprecated.activate)r~   metadataappendr|   r'   r&   rA   rB   r}   _path_for_scripttuple)r)   r   
extra_dirs	directorys       r*   rq   zWindowsMenuItem._paths   s    
==#dii889=='DII,K,KMM>?dii==>ZXZ	i$"9"9";;ZXXE==$LL..01U| Ys   *Dr   c                 B    |rd| nd}| j                  di        | S )N.r   r=   )rg   r   )r)   r   s     r*   r}   z"WindowsMenuItem._shortcut_filename   s.    #iB//&/34SE::r,   c                 j    t        | j                  j                  d         | j                  d      z  S )NMENU_DIRbat)r   r|   rV   r}   r(   s    r*   r   z WindowsMenuItem._path_for_script   s-    DII**:67$:Q:QRW:XXXr,   c                    | j                  d      }|sy t        dd      5 }|j                  |       d d d        t        t        j
                  j                  dd            dz  }t        |dz  d	z  d
z        dj                   dg}t        |d       t	        j                  |j                         y # 1 sw Y   xY w)N	precreateFw)deleter;   
SystemRoot
C:\Windowssystem32WindowsPowerShellv1.0powershell.exe"start 'z' -WindowStyle hidden"Tcheck)r   r   writer   ra   rb   rc   rX   r=   r   r   )r)   precreate_codetmpr   cmds        r*   r   zWindowsMenuItem._precreate   s    5u373IIn% 8

|]CDzQ..7:JJKz!89
 	3d#
		#(( 87s   B99Cc           	         ddg}| j                  d      }|r|j                  |       | j                  d   rv| j                  j                  }| j                  j                  |      rd}nd}| j                  j                   d| d| j                  j                   d	}|d
d| dddgz  }|j                  dj                  t        j                  | j                  d                         dj                  |      S )Nz	@ECHO OFFz%:: Script generated by conda/menuinst
precommandr   zshell activatezshell.cmd.exe activater   z ""z @SETLOCAL ENABLEDELAYEDEXPANSIONz%@FOR /F "usebackq tokens=*" %%i IN (`z`) do set "ACTIVATOR=%%i"z@CALL %ACTIVATOR%z!:: This below is the user commandcommandz
)
r   r   r   r|   	conda_exe_is_micromambarY   r   r   
quote_args)r)   linesr   r   r   	activators         r*   _commandzWindowsMenuItem._command  s    3
 __\2
LL$==$		++Iyy''	2+399../q
"TYY=M=M<NaPI27	{B[\#3	 E 	SXXf//	0JKLM{{5!!r,   Nscript_pathc                     || j                         }nt        |      }|j                  j                  dd       t	        |d      5 }|j                  | j                                ddd       |S # 1 sw Y   |S xY w)z\
        This method generates the batch script that will be called by the shortcut
        NTr   r   )r   r   parentr%   openr   r   )r)   r   fs      r*   _write_scriptzWindowsMenuItem._write_script"  st     //1K{+K   =+s#qGGDMMO$ $  $ s    A22A<c                    | j                   d   r| j                         }| j                   d   rdddd| dg}|r|j                  d       |S t        t        j
                  j                  dd	            d
z  }|rdnd}d|dz   ddddddd|dz  dz  dz   dddd| d| dg
}|S | j                  d      }|r#t        d |D              r|j                  d       t        j                  |      S )Nr   terminalr   /Dz/Kr   %1r   r   r   z%1 r   zcmd.exe/CSTARTz/MINz""r   r   r   z-WindowStylehiddenr   z' z-WindowStyle hidden"r   c              3   $   K   | ]  }d |v 
 yw)r   N ).0args     r*   	<genexpr>z3WindowsMenuItem._process_command.<locals>.<genexpr>M  s     @T_s   )r   r   r   r   ra   rb   rc   r   allr   r   )r)   	with_arg1scriptr   r   arg1s         r*   r   z WindowsMenuItem._process_command1  s!   ==$'')F}}Z( $&m<NN4(& N#  

|] KLzY )ur
 9,-Q/#66?BRRSSTU"xr$/DE N//),@@@NN4   ))r,   r~   r7   c                    | j                   j                  d      r|j                  j                         sy| j	                  d      }|j                         r#t        j                  |j                               ni }d}t        |j                  di       j                  dg             D ]  \  }}|j                  d      |k(  s|} n |r|dk  ry|d   d   |= ndj                  t        j                  | j	                  d	                  |d
}| j                   j                  d      r| j	                  d      |d<   | j                   j                  d      r| j	                  d      |d<   |dk  r0d|vri |d<   d|d   vrg |d   d<   |d   d   j                  |       n$t        j                  d| d       ||d   d   |<   |j                  t        j                   |d             y)a  Add/remove the Windows Terminal profile.

        Windows Terminal is using the name of the profile to create a GUID,
        so the name will be used as the unique identifier to find existing profiles.

        If the Terminal app has never been opened, the settings file may not exist yet.
        Writing a minimal profile file will not break the application - Terminal will
        automatically generate the missing options and profiles without overwriting
        the profiles menuinst has created.
        terminal_profileNprofileslistr=   r   r   r   )commandliner=   r   r   startingDirectoryz!Overwriting terminal profile for r      )indent)r   rc   r   r0   r   jsonloads	read_text	enumerater   r   r   r   r"   rF   
write_textdumps)	r)   r~   r7   r=   settingsindexpprofileprofile_datas	            r*   r   z4WindowsMenuItem._add_remove_windows_terminal_profileQ  s    }}  !34HOO<R<R<T127?7H4::h0023b#HLLR$@$D$DVR$PQJAw{{6"d* R
 qy$V,U3  #xx(9(9$//):T(UVL }}  ('+v'>V$}}  /48OOM4R01qyX-+-HZ(*!5535HZ(0$V,33LA?vQGH6B$V,U3DJJx:;r,   c                 6    | j                  dd      }| d| S )Nr=   Trf   z
.AssocFiler   )r)   	extension
identifiers      r*   _ftype_identifierz!WindowsMenuItem._ftype_identifier  s%    __V$_7
Z	{33r,   c           	      ^   | j                   d   }|sydj                  | j                               }t        t        j                  |D cg c]  }|j                          c}            }|D ]8  }| j                  |      }| j                  ||       | j                  ||       : yc c}w )H
        This function uses CMD's `assoc` and `ftype` commands.
        file_extensionsNr   )associate_to)
r   r   r   r   dictfromkeyslowerr   
_cmd_ftype
_cmd_assoc)r)   
extensionsr   r   extsr   s         r*   _register_file_extensions_cmdz-WindowsMenuItem._register_file_extensions_cmd  s     ]]#45
((40023DMM*"E*3399;*"EFGC//4JOOJ0OOCjO9  #Fs   
B*c           	          | j                   d   }|syt        t        j                  |D cg c]  }|j	                          c}            }|D ]$  }| j                  |      }| j                  |       & yc c}w )r   r   N)r   r   r   r   r   r   r   r)   r   r   r   r   s        r*   _unregister_file_extensions_cmdz/WindowsMenuItem._unregister_file_extensions_cmd  sm     ]]#45
DMM*"E*3399;*"EFGC//4JOOJ'  #Fs   A7c                     t        |||g      dk7  rt        d      | j                  d      st        d      |r|  d| }n|r| }n|r|  d}t        dddd	 gd
      S )zVhttps://learn.microsoft.com/en-us/windows-server/administration/windows-commands/assocr   z6Only one of {associate_to, query, remove} must be set.r   zextension must startwith '.'=r   r   r   assoc Tr   )sum
ValueError
startswithr   )r   r   queryr7   r   s        r*   r   zWindowsMenuItem._cmd_assoc  s     eV,-2UVV##C(;<<Kq/CCKq/C5$sen=TJJr,   c                     t        |||g      dk7  rt        d      |r|  d| }n|r| }n|r|  d}t        dddd gd	      S )
zVhttps://learn.microsoft.com/en-us/windows-server/administration/windows-commands/ftyper   z1Only one of {command, query, remove} must be set.r  r   r   r   r  Tr   )r  r  r   )r   r   r
  r7   r   s        r*   r   zWindowsMenuItem._cmd_ftype  sj     '(A-PQQL'+CCL"C5$sen=TJJr,   c           
         | j                   d   }|sydj                  | j                  d            }| j                  d      }t	        t
        j                  |D cg c]  }|j                          c}            }|D ]V  }| j                  |      }t        ||||| j                  d      | j                         | j                  j                         X yc c}w )	WIPr   Fr   Tr   r   r=   r   app_nameapp_user_model_idr;   )r   r   r   r   r   r   r   r   r   r   r   r|   r;   )r)   r   r   r   r   r   r   s          r*   r   z)WindowsMenuItem._register_file_extensions  s    ]]#45
((40040@Av&DMM*"E*3399;*"EFGC//4J#0"&"9"9";YY^^   #Fs   Cc           	         | j                   d   }|syt        t        j                  |D cg c]  }|j	                          c}            }|D ]5  }| j                  |      }t        ||| j                  j                         7 yc c}w )Nr   Fr;   T)	r   r   r   r   r   r   r   r|   r;   r  s        r*   r   z+WindowsMenuItem._unregister_file_extensions  sx    ]]#45
DMM*"E*3399;*"EFGC//4J%c:DIINNK  	 #Fs   Bc           
      B   | j                   d   }|sydj                  | j                  d            }| j                  d      }|D ]V  }| j	                  |      }t        ||||| j                  d      | j                         | j                  j                         X y)	z~See https://learn.microsoft.com/en-us/previous-versions/windows/internet-explorer/ie-developer/platform-apis/aa767914(v=vs.85)url_protocolsFr   Tr  r   r=   r  )	r   r   r   r   r   r   r   r|   r;   )r)   	protocolsr   r   protocolr   s         r*   r   z'WindowsMenuItem._register_url_protocols  s    MM/2	((40040@Av&!H//9J!0"&"9"9";YY^^ " r,   c                     | j                   d   }|sy|D ]5  }| j                  |      }t        ||| j                  j                         7 y)Nr  Fr  T)r   r   r   r|   r;   )r)   r  r  r   s       r*   r   z)WindowsMenuItem._unregister_url_protocols  sI    MM/2	!H//9J#Hjtyy~~N " r,   c                 ~    | j                  d      }|s)d| j                  dd      j                  dd       d d S |S )	Nr  z	Menuinst.r=   Tr   r   r      )r   ri   )r)   aumis     r*   r   z"WindowsMenuItem._app_user_model_id  sJ    23tvDAII#rRSTUYVYZZr,   )lnkrp   )F)NFF)"rr   rs   rt   rv   r   r~   r   r+   r7   rq   rX   r}   r   r   r   r   ra   ru   r   r   rw   r   r   r   r  staticmethodr   r   r   r   r   r   r   r   r   r,   r*   rz   rz      sL   I$ I I1dCi( 1fdCi(  dCi( $;c ;Y$ Y"# "4"++)> $ *5: *@-<T -<4 -<^4:
( KO_ K K 
KK[ 
K 
K4 ,	T 	 (4 r,   rz   )(__doc__r   ra   r4   rA   loggingr   pathlibr   
subprocessr   tempfiler   typingr   r	   r
   r   r   utilsr   r   r   baser   r   win_utils.knownfoldersr   r:   r   win_utils.registryr   r   r   r   r   rr   r"   r   rz   r   r,   r*   <module>r(     sl     	     ' ' 3 3 . .   F C  t+$ t+nih ir,   