
    9i5                        S r SSKJr  SSKrSSKrSSKrSSKrSSKJr  SSK	J
r
  SSKJr  SSKJr  SS	KJrJr  S
SKJrJrJr  \
" \5      r " S S\5      r " S S\5      rSS.S jrg)     )annotationsN)sha1)	getLogger)Path   )data)UnixLex
logged_run   )MenuMenuItemmenuitem_defaultsc                  T   ^  \ rS rSrSS jrSS jr\SU 4S jj5       rS	S jrSr	U =r
$ )
	MacOSMenu   c                "    U R                  5       $ N_pathsselfs    6lib/python3.13/site-packages/menuinst/platforms/osx.pycreateMacOSMenu.create       {{}    c                "    U R                  5       $ r   r   r   s    r   removeMacOSMenu.remove   r   r   c                   > [         TU ]  nUR                  [        U R	                  5       5      S[        U R
                  S-  S-  S-  S-  5      S.5        U$ )Nicnsz
python.appContentsMacOSpython)SP_DIRICON_EXT	PYTHONAPP)superplaceholdersupdatestr_site_packagesprefix)r   r*   	__class__s     r   r*   MacOSMenu.placeholders   s]    w+d1134" |!;j!H7!RU]!]^	
 r   c                    g)N r2   r   s    r   r   MacOSMenu._paths'   s    r   r2   returnztuple[os.PathLike])r5   zdict[str, str])r5   tuple)__name__
__module____qualname____firstlineno__r   r   propertyr*   r   __static_attributes____classcell__r/   s   @r   r   r      s+     	 	 r   r   c                  >  ^  \ rS rSr\SS j5       r\SS j5       r\SS j5       rSS jrU 4S jr	SS jr
SS jrSS	 jrS
 rS rS rSS jrSSS jjrSSS jjrSS S jjrSS S jjrS!S jrSS jrSS jrS"S#S jjrS"S#S jjrS$S jrS r\S%S j5       rSrU =r$ )&MacOSMenuItem+   c                B    U R                  5       S-  U R                  -  $ )z1Path to the .app directory defining the menu itemApplications)_base_location_bundle_namer   s    r   locationMacOSMenuItem.location,   s#     ""$~58I8IIIr   c                (    U R                  S0 S9 S3$ )Nname)extraz.app)
render_keyr   s    r   rE   MacOSMenuItem._bundle_name1   s    //&/34D99r   c                @    U R                   S-  S-  U R                  -  $ )zAPath to the nested .app directory defining the menu item main appr#   	Resources)rF   rE   r   s    r   _nested_locationMacOSMenuItem._nested_location5   s#     }}z)K7$:K:KKKr   c                ~    U R                   R                  S:X  a  [        S5      R                  5       $ [        S5      $ )Nuser~/)menumoder   
expanduserr   s    r   rD   MacOSMenuItem._base_location:   s/    99>>V#9''))Cyr   c                  > [         TU ]  5         U R                  S   =(       d    0 R                  5        H  u  pU R                  U R                  U5      -  R                  5       n[        U5      R                  [        U R                  5      5      (       d  [        SU R                   SU S35      eUR                  R                  SSS9  [        R                  " U R                  U5      U5        M     g )Nlink_in_bundlezF'link_in_bundle' destinations MUST be created inside the .app bundle (z), but it points to '.Tparentsexist_ok)r)   
_precreatemetadataitemsrF   renderresolver,   
startswith
ValueErrorparentmkdirossymlink)r   srcdestrendered_destr/   s       r   r_   MacOSMenuItem._precreate?   s    --(89?RFFHIC#'==4;;t3D#D"M"M"OM}%00T]]1CDD //3}}o=RS`Raabd    &&td&CJJt{{3'7 Ir   c                0   U R                   R                  5       (       a  SU R                    S3n[        U5      e[        R	                  SU R                   5        U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                  5         U R                   4$ )NzApp already exists at z. Please remove the existing shortcut before installing. If you used conda to install this package, reinstall the package with --force-reinstall to create the shortcut once the location is cleared.zCreating %s)rF   existsRuntimeErrorlogdebug_create_application_treer_   
_copy_icon_write_pkginfo_write_plistinfo_write_appkit_launcher_write_launcher_write_script_write_event_handler#_maybe_register_with_launchservices_sign_with_entitlements)r   messages     r   r   MacOSMenuItem.createL   s    ==!!( 8D D  w''		-/%%'##%!!#002$$&r   c                
   [         R                  SU R                  5        U R                  SS9  U R                  R	                  5       (       a,  [
        R                  " U R                  SS9  U R                  4$ [        5       $ )NzRemoving %sF)registerT)ignore_errors)rq   rr   rF   r{   ro   shutilrmtreer6   r   s    r   r   MacOSMenuItem.removed   s^    		-/00%0@==!!MM$--t<MM##wr   c                   U R                   S-  S-  U R                   S-  S-  /nU R                  (       a'  UU R                  S-  S-  U R                  S-  S-  /-  nU H  nUR                  SSS9  M     [	        U5      $ )Nr#   rN   r$   TFr\   )rF   _needs_appkit_launcherrO   rg   r6   )r   pathspaths      r   rs   &MacOSMenuItem._create_application_treel   s    MMJ&4MMJ&0
 &&%%
2[@%%
2W< E DJJteJ4 U|r   c                    U R                  S5      nU(       a_  [        R                  " XR                  S-  S-  5        U R                  (       a'  [        R                  " XR
                  S-  S-  5        g g g )Niconr#   rN   )rK   r   copyrF   r   rO   )r   r   s     r   rt   MacOSMenuItem._copy_iconz   s\    v&KKmmj8;FG**D"7"7*"D{"RS + r   c           
     ,   U R                   /nU R                  (       a  UR                  U R                  5        U HC  n[	        US-  S-  S5       nUR                  SU R                  SSS9S S  35        S S S 5        ME     g ! , (       d  f       MW  = f)	Nr#   PkgInfowAPPLrI   Tslug   )rF   r   appendrO   openwriterK   )r   app_bundlesappfs       r   ru   MacOSMenuItem._write_pkginfo   s    }}o&&t445CcJ&2C8A$tvDA"1EFGH 98 88s   &B
B	c           	     2   U R                  S5      nU R                  SSS9n[        U5      S:  a1  US S [        UR                  5       5      R	                  5       S S -   nOUnUUUU S3SU 3S	S
S
S.nU R                  S5      nU(       a  [        U5      R                  US'   U R                  (       aI  [        U R                  S-  S-  S5       n[        R                  " XF5        S S S 5        SUS'   SU S3US'   / [        QSPSPSP7nU R                  R                  S5      =(       d    0 n[        S   S    HL  n	X;   a  M
  X;   a  [        SU	 S35      eU R                  U	5      n
U
c  M4  U	S:X  a  XS'   U SU
 3US'   XU	'   MN     UR!                  5        H  u  pU R#                  U
5      XI'   M     [        U R$                  S-  S-  S5       n[        R                  " XF5        S S S 5        g ! , (       d  f       GN= f! , (       d  f       g = f)NrI   Tr      
      z-1.0.0com.r   z1.0.0)CFBundleNameCFBundleDisplayNameCFBundleExecutableCFBundleGetInfoStringCFBundleIdentifierCFBundlePackageTypeCFBundleVersionCFBundleShortVersionStringr   CFBundleIconFiler#   z
Info.plistwbLSBackgroundOnlyz-appkit-launcherr   entitlementsrZ   info_plist_extra	platformsosxz%Duplicate Info.plist property found: z.. Remove the property from `info_plist_extra`.r   r   -r   )rK   lenr   encode	hexdigestr   rI   r   r   rO   plistlibdumpr   r`   getre   ra   rb   rF   )r   rI   slugname	shortnameplr   r   ignore_keysr   keyvalues              r   rv   MacOSMenuItem._write_plistinfo   s;   v&??6?5x=2 "X__->(?(I(I(KBQ(OOI I%#'"*(0z%8$(
"3#)&*1	
 v&%)$Z__B!"&&d++j8<GNRSb$ O &*B!")-hZ7G'HB#$ a)`>`;K`M_`==,,-?@FB$[1%8C!& ;C5 AC C  OOC(E}''38/019
!E7.C*+sG 9  +002JCkk%(BG 3$--*,|;TBaMM"  CB7 ON6 CBs   G6H6
H
Hc           	        S/nU R                  S5      (       a  UR                  / SQ5        U R                  S5      nU(       aK  [        [        R                  R                  U5      5      R                  SSS9  UR                  SU S35        U R                  S	5      nU(       a  UR                  U5        U R                  S
   (       aj  U R                  R                  nU R                  R                  U5      (       a  SnOSnUR                  SU SU SU R                  R                   S35        UR                  SR                  [        R                  " U R                  S5      5      5      5        SR                  U5      $ )Nz	#!/bin/shterminal)z@if [ "${__CFBundleIdentifier:-}" != "com.apple.Terminal" ]; thenz#    open -b com.apple.terminal "$0"z    exit $?fiworking_dirTr\   zcd ""
precommandactivatezshell activatezshell.bash activatez	eval "$("z" z "z")" command
)rK   extendr   rh   r   
expandvarsrg   r   r`   rU   	conda_exe_is_micromambar.   joinr
   
quote_args)r   linesr   r   r   r   s         r   _commandMacOSMenuItem._command   s5   ??:&& LL oom4##K0177t7TLL4}A./__\2
LL$==$		++Iyy''	22+0LL9YKr(2dii>N>N=OsSTSXXg001KLMNyyr   c                    Uc  U R                  5       n[        R                  " U R                  5       U5        [        R
                  " US5        U$ N  )_default_appkit_launcher_pathr   r   _find_appkit_launcherrh   chmodr   launcher_paths     r   rw   $MacOSMenuItem._write_appkit_launcher   sA      >>@MD..0-@
&r   c                    Uc  U R                  5       n[        R                  " U R                  5       U5        [        R
                  " US5        U$ r   )_default_launcher_pathr   r   _find_launcherrh   r   r   s     r   rx   MacOSMenuItem._write_launcher   sA      779MD'')=9
&r   c                    Uc  U R                  SS9n[        US5       nUR                  U R                  5       5        S S S 5        [        R
                  " US5        U$ ! , (       d  f       N'= f)Nz-script)suffixr   r   )r   r   r   r   rh   r   )r   script_pathr   s      r   ry   MacOSMenuItem._write_script   s[    55Y5GK+s#qGGDMMO$ $
e$ $#s    A  
A.c                (   U R                   (       d  g U R                  S5      nUc  g Uc  U R                  S-  S-  S-  n[        US5       nUR	                  SU S35        S S S 5        [
        R                  " US5        U$ ! , (       d  f       N'= f)	Nevent_handlerr#   rN   zhandle-eventr   z#!/bin/bash
r   r   )r   rK   rF   r   r   rh   r   )r   r   event_handler_logicr   s       r   rz   "MacOSMenuItem._write_event_handler   s    **"ooo>&--*4{B^SK+s#qGGm$7#8;< $
e$ $#s   B
Bc                    U R                   4$ r   )rF   r   s    r   r   MacOSMenuItem._paths  s    r   c                H   S[         R                  " 5        3n[        R                   HV  n[	        U5      U-  nUR                  5       (       d  M(  [        R                  " U[        R                  5      (       d  MT  Us  $    [        S[         R                  " 5        35      e)Nappkit_launcher_'Could not find executable launcher for 
platformmachine_menuinst_data__path__r   is_filerh   accessX_OKre   r   launcher_namedatapathr   s       r   r   #MacOSMenuItem._find_appkit_launcher  s~    *8+;+;+=*>?&//H N]:M$$&&299]BGG+L+L$$ 0 B8CSCSCUBVWXXr   c                H   S[         R                  " 5        3n[        R                   HV  n[	        U5      U-  nUR                  5       (       d  M(  [        R                  " U[        R                  5      (       d  MT  Us  $    [        S[         R                  " 5        35      e)Nosx_launcher_r   r   r   s       r   r   MacOSMenuItem._find_launcher  s~    '(8(8(:';<&//H N]:M$$&&299]BGG+L+L$$ 0 B8CSCSCUBVWXXr   c                T    U R                  SSS9nU R                  S-  S-  U U 3-  $ NrI   Tr   r#   r$   )rK   rF   r   r   rI   s      r   r   +MacOSMenuItem._default_appkit_launcher_path  s6    vD1}}z)G3vh6GGGr   c                    U R                  SSS9nU R                  (       a  U R                  S-  S-  U U 3-  $ U R                  S-  S-  U U 3-  $ r   )rK   r   rO   rF   r   s      r   r   $MacOSMenuItem._default_launcher_path  sc    vD1&&((:5?TF6(BSSS}}z)G3vh6GGGr   c                    U R                   (       d  g U(       a   [        S[        U R                  5      5        g [        SSS[        U R                  5      5        g )Nz-Rz-uz-all)r   _lsregisterr,   rF   )r   r   s     r   r{   1MacOSMenuItem._maybe_register_with_launchservices"  s;    **c$--01dFC,>?r   c                h   U R                  S5      nU(       d  gU R                  SSS9nU Vs0 s H  o3S_M     nnU R                  S-  S-  n[        US5       n[        R                  " XF5        SSS5        [        S	S
SSSSU 3SSSSSUU R                  /SS9  gs  snf ! , (       d  f       N8= f)z1Self-sign shortcut to apply required entitlementsr   NrI   Tr   r#   zEntitlements.plistr   z/usr/bin/codesignz	--verbosez--signr   z--prefixr   z	--optionsruntimez--forcez--deepz--entitlementscheck)rK   rF   r   r   r   r   )r   entitlement_keysr   r   plistentitlements_pathr   s          r   r|   %MacOSMenuItem._sign_with_entitlements+  s    ??>:??6?5&67&6sd&67 MMJ69MM#T*aMM%# + $xj! !  #	
	 8**s   BB##
B1c                J    [        U R                  R                  S5      5      $ )a  
In macOS, file type and URL protocol associations are handled by the
Apple Events system. When the user opens on a file or URL, the system
will send an Apple Event to the application that was registered as a handler.
Some apps might not have the needed listener to process the event. In that case,
we provide a generic one. This is decided by the presence of "event_handler".
If that key is absent or null, we assume the app has its own listener.

See:
- https://developer.apple.com/library/archive/documentation/Carbon/Conceptual/LaunchServicesConcepts/LSCConcepts/LSCConcepts.html
- The source code at /src/appkit-launcher in this repository
r   )boolr`   r   r   s    r   r   $MacOSMenuItem._needs_appkit_launcherI  s     DMM%%o677r   r2   )r5   r   )r5   r,   )r5   ztuple[Path]r   )r   os.PathLike | Noner5   os.PathLike)r   r  r5   r  r4   )r   )r   r,   r5   r   )T)r5   r  )r7   r8   r9   r:   r;   rF   rE   rO   rD   r_   r   r   rs   rt   ru   rv   r   rw   rx   ry   rz   r   r   r   r   r   r{   r|   r   r<   r=   r>   s   @r   r@   r@   +   s    J J : : L L
8 0TI4!l! F YYHH@
< 8 8r   r@   Tr  c                (    Sn[        U/UQ4SU 0UD6$ )Nzh/System/Library/Frameworks/CoreServices.framework/Frameworks/LaunchServices.framework/Support/lsregisterr  )r   )r  argskwargsexes       r   r  r  Z  s*    	B  slTl:%:6::r   )__doc__
__future__r   rh   r   r   r   hashlibr   loggingr   pathlibr   r   r	   r   utilsr
   r   baser   r   r   r7   rq   r   r@   r  r2   r   r   <module>r     s[     " 	       % ' 3 3 .l8H l8^	 " ;r   