
    `gj ,                         d 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efdZdefdZdefd	Zd
edefdZdefdZd
eddfdZddZd
edefdZd
edefdZdd
ddefdZdedefdZdddd
ddeddfdZy)uK  
Hermes Desktop (Chat GUI) uninstaller.

The desktop GUI ships in two shapes and this module knows how to find and
remove the artifacts of both, on Linux, macOS, and Windows, WITHOUT touching
the Python agent or the user's config/data:

  1. Source-built GUI (``hermes desktop`` / ``hermes gui``)
     Built inside the agent checkout under ``$HERMES_HOME/hermes-agent/``:
       - ``apps/desktop/dist``      (compiled renderer)
       - ``apps/desktop/release``   (electron-builder unpacked app + installers)
       - ``apps/desktop/node_modules`` and the workspace-root ``node_modules``
         (Electron itself, ~200MB) — only removed on a GUI uninstall because
         the agent does not need them.
       - ``$HERMES_HOME/desktop-build-stamp.json`` (the build freshness stamp)

  2. Packaged distributable (DMG / NSIS / AppImage / deb / rpm)
     Installed by the OS to a standard application location and carrying its
     own bundled Electron + a per-user Electron ``userData`` directory:
       - macOS:   ``/Applications/Hermes.app`` or ``~/Applications/Hermes.app``
       - Windows: ``%LOCALAPPDATA%\Programs\Hermes`` (NSIS per-user)
       - Linux:   ``~/.local/share/applications`` .desktop entry + AppImage

In both shapes the Electron runtime keeps a ``userData`` directory keyed on
the app name ("Hermes"), separate from ``$HERMES_HOME``:
  - macOS:   ``~/Library/Application Support/Hermes``
  - Windows: ``%APPDATA%\Hermes``
  - Linux:   ``$XDG_CONFIG_HOME/Hermes`` (default ``~/.config/Hermes``)

This holds the desktop's own ``connection.json`` / ``updates.json`` and
Chromium cache — pure GUI state, safe to remove on a GUI uninstall.

The functions here are deliberately import-light and side-effect-free at
import time so the Electron main process can shell out to
``hermes uninstall --gui`` (and friends) without paying for the full CLI.
    N)Path)get_hermes_home)Colorscolormsgc                 T    t        t        dt        j                         d|         y )Nu   → )printr   r   CYANr   s    K/root/.hermes/venv/lib/python3.12/site-packages/hermes_cli/gui_uninstall.pylog_infor   0   s!    	U5&++&'q
./    c                 T    t        t        dt        j                         d|         y )Nu   ✓r	   )r
   r   r   GREENr   s    r   log_successr   4   s!    	U5&,,'(#
/0r   c                 T    t        t        dt        j                         d|         y )Nu   ⚠r	   )r
   r   r   YELLOWr   s    r   log_warnr   8   s!    	U5&--()3%
01r   hermes_homereturnc                     | dz  S )uE   The agent checkout root — same layout install.sh / install.ps1 use.zhermes-agent )r   s    r   _agent_rootr   A   s    ''r   c                  j   t        j                         } t        j                  dk(  r| dz  dz  dz  S t        j                  dk(  r9t        j
                  j                  d      }|rt        |      n| dz  dz  }|dz  S t        j
                  j                  d	      }|rt        |      n| d
z  }|dz  S )a  Return the Electron ``userData`` directory for the desktop app.

    Mirrors Electron's ``app.getPath('userData')`` for an app named "Hermes"
    on each platform. This is GUI-only state (connection.json, updates.json,
    Chromium cache) and never holds agent config or sessions.
    darwinLibraryzApplication SupportHermeswin32APPDATAAppDataRoamingXDG_CONFIG_HOMEz.config)r   homesysplatformosenvironget)r$   appdatabasexdgs       r   desktop_userdata_dirr-   F   s     99;D
||xi"77(BB
||w**..+ 'tG}dY.>.Jh
**..*
+C49$"2D(?r   
list[Path]c                 T    t        |       }|dz  dz  }|dz  |dz  |dz  |dz  | dz  gS )a  GUI build artifacts produced by ``hermes desktop`` inside the checkout.

    These are removable on a GUI uninstall without harming the agent: the
    Python agent runs from ``hermes-agent/`` source + ``venv/`` and never
    needs the Electron build output or node_modules.
    appsdesktopdistreleasenode_moduleszdesktop-build-stamp.json)r   )r   
agent_rootdesktop_dirs      r   source_built_gui_artifactsr7   Z   sR     [)Jv%	1Kfin$ 	^#00	 	r   c                  D   t        j                         } g }t        j                  dk(  r|t        d      | dz  dz  gz  }|S t        j                  dk(  rt        j
                  j                  d      }|rt        |      n| dz  dz  }||d	z  d
z  |dz  gz  }t        j
                  j                  d      }|r|j                  t        |      d
z         |S t        j
                  j                  d      }|rt        |      n| dz  dz  }||dz  dz  |dz  dz  gz  }|S )u  Standard install locations of the packaged desktop distributable.

    Returns every candidate for the current OS; the caller filters to those
    that actually exist. We never glob system-wide — only the well-known
    electron-builder output locations for the "Hermes" product.
    r   z/Applications/Hermes.appApplicationsz
Hermes.appr   LOCALAPPDATAr!   LocalProgramsr   zhermes-desktopProgramFilesXDG_DATA_HOMEz.localshareapplicationszhermes.desktopzHermes.desktop)r   r$   r%   r&   r'   r(   r)   append)r$   pathslocal
local_baseprogram_filesdata	data_bases          r   packaged_gui_app_pathsrH   o   s8    99;DE
||x+,>!L0
 	
< L5 
	 

~.$)T%[y0@70J
#h.))	
 	
 

~6LLm,x78 L zz~~o."&DJTH_w-F	&)99&)99
 	
 Lr   c                     t        |       }|dz  j                         ry|dz  j                         s|dz  j                         ryy)a  Return True when a usable Python agent install exists under HERMES_HOME.

    Used by the desktop UI to decide which uninstall options to offer: if the
    agent isn't present (a future "lite" GUI-only client), the "remove agent"
    options are hidden.
    
hermes_cliTvenvz.venvF)r   is_dir)r   r5   s     r   agent_is_installedrM      sK     [)J 	\!))+V##%*w*>)F)F)Hr   c                     t        |       D ]  }|j                         s y t               D ]  }|j                         s y t               j                         ryy)zEReturn True when any desktop GUI artifact exists (built or packaged).TF)r7   existsrH   r-   )r   ps     r   gui_is_installedrQ      sY    '4 88: $% 88: $$&r   zPath | Nonec           
         | | n	t               }t        |      D cg c]  }|j                         s| }}t               D cg c]  }|j                         s| }}t	               }t        |      t        |      t        |      |D cg c]  }t        |       c}|D cg c]  }t        |       c}t        |      |j                         t        j                  dS c c}w c c}w c c}w c c}w )a  Structured snapshot of what's installed, for the desktop UI to render.

    Returns JSON-serializable primitives so the Electron main process can
    forward it to the renderer via IPC (paths as strings, booleans for the
    high-level questions the UI gates options on).
    )r   agent_installedgui_installedsource_built_artifactspackaged_app_pathsuserdata_diruserdata_existsr&   )
r   r7   rO   rH   r-   strrM   rQ   r%   r&   )r   r$   rP   source_artifactspackageduserdatas         r   gui_install_summaryr]      s     !, 7_=ND#=d#CRaqxxzRR13BaqxxzBHB#%H 4y-d3)$/3C"Da3q6"D/78!s1v8H#??,LL	 		 SB #E8s"   C#C#C(C(C-
$C2pathc                    	 | j                         s| j                         r| j                          y| j                         rt	        j
                  |        y	 y# t        $ r}t        d|  d|        Y d}~yd}~ww xY w)zIRemove a file or directory tree. Returns True when something was removed.TzCould not remove z: NF)
is_symlinkis_fileunlinkrL   shutilrmtree	Exceptionr   )r^   es     r   _remove_pathrg      su    2??KKM;;=MM$ 
   2$TF"QC0112s   0A %A 	A?$A::A?T)remove_userdatarh   c                   | | n	t               }g }t        d       t        |      D ]>  }|j                         st	        |      s t        d|        |j                  |       @ t        d       d}t               D ]@  }|j                         sd}t	        |      s"t        d|        |j                  |       B |st        d       |rOt               }|j                         r5t        d       t	        |      rt        d|        |j                  |       |st        d       t        j                  j                  d	      rt        d
       |S )a>  Remove the desktop GUI's artifacts, leaving the agent + user data intact.

    Removes:
      - source-built GUI artifacts (dist/release/node_modules/build-stamp)
      - the packaged app bundle / install dir (best-effort; deb/rpm need the
        system package manager and are reported, not force-removed)
      - the Electron ``userData`` directory (unless ``remove_userdata=False``)

    Never touches ``hermes-agent/hermes_cli`` (agent source), ``venv/``, or any
    config / sessions / .env under ``$HERMES_HOME``.

    Returns the list of paths actually removed.
    zARemoving built GUI artifacts (renderer, release, node_modules)...zRemoved z!Removing installed desktop app...FTz3No packaged desktop app found in standard locationsz0Removing desktop app data (Electron userData)...z(No desktop GUI artifacts found to removelinuxzIf you installed the desktop via a .deb / .rpm package, remove it with your package manager (e.g. 'sudo apt remove hermes' or 'sudo dnf remove hermes'). AppImage builds are a single file you can delete from wherever you saved it.)r   r   r7   rO   rg   r   rA   rH   r-   r%   r&   
startswith)r   rh   r$   removedr^   found_packagedr\   s          r   uninstall_guirn      s6    !, 7_=NDGPQ*40 !;;=\$/(4&)*NN4 !
 01N&( %;;=!ND!htf-.t$% FG')??GHH%hxj12x(;<
 ||w'5	
 Nr   )r   r.   )N)__doc__r'   rc   r%   pathlibr   hermes_constantsr   hermes_cli.colorsr   r   rY   r   r   r   r   r-   r7   rH   boolrM   rQ   dictr]   rg   rn   r   r   r   <module>ru      s   #J 
  
  , +0# 01S 12# 2(T (d (
d (D \ *(VD T "
$ 
4 
] d <t  9PT 9} 9 9Ye 9r   