a
    ip“h+  ã                   @   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                    sx   t  ¡ }zVt| dƒ2‰ t‡ fdd„dƒD ]}| |¡ q(W d   ƒ n1 sL0    Y  | ¡ W S  tyr   Y d S 0 d S )NÚrbc                      s
   ˆ   d¡S )Ni   )Úread© ©Úfr   ú1/home/pi/dev/triomed/launcher/command_executor.pyÚ<lambda>
   ó    zcompute_md5.<locals>.<lambda>r	   )ÚhashlibZmd5ÚopenÚiterÚupdateZ	hexdigestÚFileNotFoundError)Z	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 ]f}t  d|› ¡ ztj|ddd W q tjy€ } z"t  d|› d|› ¡ W Y d }~qd }~0 0 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 rÊt  d¡ | d  ¡ D ]N\}‰ ˆ rŒ|| n|| }|rª‡ fdd„|D ƒ}t|ˆ r¶d 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'ƒ qðd 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   r	   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*   r/   r1   r3   r   r   r5   U   r	   z (default state))r   r   Úitemsr    )ÚconfigÚ
persistentZservice_enable_commandsZservice_disable_commandsÚservicer   Zdefault_servicesr   r3   r   Úmanage_services)   sJ    ù
ù

 
ù
r;   c               
   C   sZ   zt jdddd t d¡ W n6 t jyT }  zt d| › ¡ W Y d } ~ n
d } ~ 0 0 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    )r8   r   r   r   Úexecute_pre_launch_   s    r>   c              
   C   s|   | g}|r.t  ¡ dkr | d¡ | g d¢¡ ztj|dd W n6 tjyv } zt d|› ¡ W Y d }~n
d }~0 0 d S )Nr   z--no-sandbox)z--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    
rC   )r   )F)r   r
   r   r?   r   r   r    r;   r<   r>   rC   r   r   r   r   Ú<module>   s   


/