U
    ch+                     @   s`   d dl Z d dlZd dlZd dlZdd Zdd ZdddZdd
dZdd Zdd Z	dd Z
dS )    Nc              	      sf   t  }zBt| d( t fdddD ]}|| q(W 5 Q R X | W S  tk
r`   Y d S X d S )Nrbc                      s
     dS )Ni   )read fr   [/Users/vladyslavskrypnychenko/dev/triomed/trio-medical-desktop/launcher/command_executor.py<lambda>
       zcompute_md5.<locals>.<lambda>r	   )hashlibZmd5openiterupdate	hexdigestFileNotFoundError)	file_pathZhash_md5chunkr   r   r   compute_md5   s    
r   c                 C   sd   |st d dS t| }|s4t d|  d dS ||krVt d| d|  dS t d dS )	NzCWarning: No MD5 checksum provided in config. Skipping verification.TzError: AppImage 'z' not found.Fz-Error: AppImage checksum mismatch. Expected: z, Got: z(AppImage checksum verified successfully.)clickechor   )appimage_pathZexpected_md5Zcurrent_md5r   r   r   verify_appimage   s    

r   Executing commandsc                 C   s   | sd S t | d | D ]d}t d|  ztj|ddd W q tjk
r~ } zt d| d|  W 5 d }~X Y qX qd S )Nz...z	Running: TshellcheckzError running command 'z': )r   r   
subprocessrunCalledProcessError)commandsdescriptioncmder   r   r   execute_commands   s    r"   Fc                    sL  ddgddgdgddgdd	gd
dgddgd}ddgddgdgddgdgddgddgd}| d rt d | d  D ]N\}  r|| n|| }|r fdd|D }t| rd nd! d"|  qxn~t d# d$d%d$d%d$d%d%d}| D ]V\}  r|| n|| }|r& fd&d|D }t| r4d nd! d"| d' qd S )(Nz sudo systemctl enable networkingzsudo systemctl start networkingz#sudo systemctl enable getty@ttyAMA0z"sudo systemctl start getty@ttyAMA0zsudo modprobe i2c-devz,sudo systemctl enable vncserver-x11-servicedz+sudo systemctl start vncserver-x11-servicedzsudo modprobe i2c-rtcz&sudo hwclock --systohc --rtc=/dev/rtc0zsudo systemctl enable bluetoothzsudo systemctl start bluetoothz$sudo systemctl enable wpa_supplicantz#sudo systemctl start wpa_supplicant)ethernetseriali2cvncrtc	bluetoothwifiz!sudo systemctl disable networkingzsudo systemctl stop networkingz$sudo systemctl disable getty@ttyAMA0z!sudo systemctl stop getty@ttyAMA0zsudo modprobe -r i2c-devz-sudo systemctl disable vncserver-x11-servicedz*sudo systemctl stop vncserver-x11-servicedzsudo modprobe -r i2c-rtcz sudo systemctl disable bluetoothzsudo systemctl stop bluetoothz%sudo systemctl disable wpa_supplicantz"sudo systemctl stop wpa_supplicant
kiosk_modez&Configuring services for kiosk mode...servicesc                    s0   g | ](}| d  rdnd d r&dndqS startenabledisablestopreplace.0r    enabledr   r   
<listcomp>B   s     z#manage_services.<locals>.<listcomp>ZEnablingZ	Disabling z&Restoring services to default state...TFc                    s0   g | ](}| d  rdnd d r&dndqS r,   r1   r3   r5   r   r   r7   U   s     z (default state))r   r   itemsr"   )config
persistentZservice_enable_commandsZservice_disable_commandsservicer   Zdefault_servicesr   r5   r   manage_services)   sJ    


 

r=   c               
   C   sX   zt jdddd td W n4 t jk
rR }  ztd|   W 5 d } ~ X Y nX d S )Nzunclutter -idle 0 &Tr   zMouse pointer hidden.zError hiding mouse pointer: )r   r   r   r   r   )r!   r   r   r   
hide_mouseX   s
    r>   c                 C   s   t | d d d S )Npre_launch_commandszRunning pre-launch commands)r"   )r:   r   r   r   execute_pre_launch_   s    r@   c              
   C   s~   | g}|r2t  dkr |d |ddddg ztj|dd W n4 tjk
rx } ztd	|  W 5 d }~X Y nX d S )
Nr   z--no-sandboxz--kioskz--fullscreenz	-geometryz100%T)r   zError launching AppImage: )	osgeteuidappendextendr   r   r   r   r   )r   r*   r    r!   r   r   r   launch_appimageb   s    
rE   )r   )F)r   r
   r   rA   r   r   r"   r=   r>   r@   rE   r   r   r   r   <module>   s   


/