
    =	f&                        d dl Z d dlZd dlmZmZ d dlZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlZd dlZd dlZd dlZd dlZd dlZd dl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 ddl ddlmZmZ ddlmZ  ej:                  dej:                  j<                  ej:                  j>                        Z e jC                  d	       d
 Z"d Z#d Z$ddZ%d Z&d Z'd Z(d Z)d Z*d Z+y)    N)PopenPIPE   )html_entities)*)utilstui)logger infoc                  0    t        j                         dk(  S )Nr   )osgeteuid     !/usr/share/hplip/base/services.pyrunning_as_rootr   <   s    ::<1r   c                      t         j                  j                  d      ryt         j                  j                  d      ryy)Nz/etc/init.d/cupsz/etc/init.d/cups restartz/etc/init.d/cupsysz/etc/init.d/cupsys restartzkillall -HUP cupsd)r   pathexistsr   r   r   restart_cupsr   ?   s/    	ww~~())	,	-+ $r   c                     d}t        j                  d      }|rW| rUdt        j                  j	                  |d      z  }| j                         |z  }t        j                  || d      \  }}|dk(  }|S )NFshutdownz	%s -r nowz%Need authentication to restart systemr   )r   whichr   r   join
getAuthCmdrun)passwordObjokr   cmdstatusoutputs         r   restartr#   I   sk    	B{{:&HKRWW\\(J?@$$&,35\]kIr   c                 X   | j                  d      }t        j                  |       |r|rd}|D ]w  }|j                         |z  }t	        j
                  ||d|z        \  }}|dk7  r-t        j                  d|z         t        j                  |       | ||d|z         |dz  }y y y y )Nopen_mdns_portr   z*Need authentication to open mdns port [%s]r   zAn error occurred running '%s'zOpen mDNS/Bonjour step %d)get_distro_ver_datalogdebugr   r   r   warn)corer   callbackopen_mdns_port_cmdxr    r!   r"   s           r   run_open_mdns_portr.   U   s    112BCII !k% 	C((*S0C"YYsK9efi9ijNFF{9C?@ #9A=>FA	 *r   c                     | Lt        j                  | d      }|s| }t        j                  |       t        j                  |      \  }}|dk(  S t        j                  d       y)NTr   zCommand not foundF)r   r   r'   r(   r   error)r    	hpCommandr!   r"   s       r   run_hp_toolsr2   h   sW    
KKT*	I		)9-{		%&r   c                     | f|dt        j                  | d      }|s| }|j                         |z  }t        j	                  |       t        j
                  ||d| z        \  }}|dk(  S t        j                  d       y)NTz%Need authentication to run %s commandr   z1Command not found or password object is not validF)r   r   r   r'   r(   r   r0   )r    r   r1   r!   r"   s        r   run_hp_tools_with_authr4   w   sz    
;3KKD)	I**,y8			)9k;bcf;fg{		EFr   c                 t   d}| r|s|S t        j                  d      r|j                         d| z  z  }t        j	                  |       t        j
                  ||d| z        \  }}|dk(  r`d|v sd|v rT|j                         d| z  z  }t        j	                  d	|z         t        j
                  ||d
| z        \  }}|dk(  r$d}|S d}|S t        j                  d| d| d       |S t        j                  d      r|j                         d| z  z  }t        j	                  |       t        j
                  ||d| z        \  }}|dk(  r~d|v sd|v rT|j                         d| z  z  }t        j	                  d	|z         t        j
                  ||d
| z        \  }}|dk(  rBd}|S d|v rt        j                  d| z         |S d}|S t        j                  d| d| d       |S t        j                  j                  d| z        r|j                         d| z  z  }t        j	                  |       t        j
                  ||d| z        \  }}|dk(  r`d|v sd|v rT|j                         d| z  z  }t        j	                  d	|z         t        j
                  ||d
| z        \  }}|dk(  r$d}|S d}|S t        j                  d| d| d       |S | dk(  rCd}t        j
                  ||d| z        \  }}|dk(  rd|v rd}|S t        j                  d       |S t        j                  d| d| d       |S )NF	systemctlzsystemctl status %s.servicez,Need authentication to get %s service statusr   stopinactivezsystemctl start %s.servicezcmd_start=%sz/Need authentication to start/restart %s serviceTzFail to start z service, please start z service manually.servicezservice %s statuszservice %s startzunrecognized servicez0Failed to Start since %s is unrecognized servicez/etc/init.d/%sz/etc/init.d/%s statusz/etc/init.d/%s startcupsz	lpstat -rz
is runningz>service command not found, please start cups service manually.)
r   r   r   r'   r(   r   r0   r   r   r   )service_namer   ret_Val
cmd_statusstsout	cmd_startr    s           r   start_servicerA      sw   G{{{; ++-/L\/YZ
		*))J5cdp5pqC7}
c 1'2246RS_6_`			.23))I{<mnz<z{C7"Gd Na ` N] IIUabnop\ NY 
Y	 ++-/B</OP
		*))J5cdp5pqC7}
c 1'2246H6UV			.23))I{<mnz<z{C7"GD NC (3.		L\YZ@ N= < N9 IIUabnop8 N5 
(5	6 ++-/F|/ST
		*))J5cdp5pqC7}
c 1'2246L\6YZ			.23))I{<mnz<z{C7"G  N  N IIUabnop N 6!Cii[2`am2mnGCQw<3. N	 		Z[ N IIUabnopNr   c                  
   t        j                  d      } | r!t        j                  j	                  | d      } n.t        j                  j	                  t
        j                  d      } t        j                  j                  |       st        j                  d       t        j                  d| z         t        j                  t        j                  | ddd       t        j                  d       t        j                  d       y )	N
hp-systrayz
systray.pyzUnable to start hp-systrayz&Running hp-systray: %s --force-startupz--force-startupz--ignore-update-firsttimez"Waiting for hp-systray to start...r   )r   r   r   r   r   prophome_dirr   r'   r)   r(   spawnlpP_NOWAITtimesleep)r   s    r   run_systrayrJ      s    ;;|$Dww||D,/ww||DMM<877>>$-.II6=>JJr{{D,0AC^_II23JJqMr   c                     t        j                  dd      } | r@d}| d|}t        |      rt        j	                  d       y t        j                  d       y 	 ddlm} t        }t        }|j                  |t        k(  |      \  }}|r|st        j                  d	       y y # t        $ r t        j                  d
       Y y w xY w)Nzhp-SIDisableT- zSmart Install is disabled
z$Smart Install could not be disabled
r   )pkitzFailed to install plug-in.zImport error
)r   r   r2   r'   r(   r0   r   rN   PLUGIN_REQUIREDPLUGIN_REASON_NONErun_plugin_commandImportErrorr)   )r   paramsicmdrN   pluginplugin_reasonr   sudo_oks           r   disable_SmartInstallrX      s    ;;~d+D&II34II=>	'$F.M11&O2K][KBW		67 % 	'HH%&	's   AB# #CCc                     t        j                  d      \  } }| du rt        j                  dddgd      \  }}|r|dk(  rt        j                  d       y	 dd	lm}m} 	 d
d
t        t        j                  dd
d
g}|j                  ddd      } |j                  |ddi t        j                  d        |       j                  |       t!        j"                  d       t        j                  d      \  }}|du rt        j%                  d       yy#  t        j%                  d       Y LxY w# t&        $ r t        j%                  d       Y ow xY w)NrC   Tzq
Some HPLIP applications are running. Press 'y' to close applications or press 'n' to quit upgrade(y=yes*, n=no):ynz;Manually close HPLIP applications and run hp-upgrade again.Fr   )	SystemBuslowlevelr   /zcom.hplip.StatusServiceEvent	signaturessisissz'Sending close message to hp-systray ...g      ?z5Failed to send DBus message to hp-systray/hp-toolbox.zUnable to load DBus.z
hp-toolboxzVFailed to close either HP-Toolbox/HP-Systray. Manually close and run hp-upgrade again.)r   Is_Process_Runningr	   enter_choicer'   r   dbusr\   r]   EVENT_SYSTEMTRAY_EXITrD   usernameSignalMessageappendr(   send_messagerH   rI   r0   rR   )	r!   r"   r   choicer\   r]   argsmsgtoolbox_statuss	            r   close_running_hp_processesrn      sL   ,,\:MF6~$$  &Z  \_  `c  [d  eh  i	6Vc\HHRS	0
	B 5t}}aRP,,S2KWU

6Y6		CD((-

3
 "44\BN6		jk		QR  	II,-	s   D/  B D D,/EE)N),sysr   
subprocessr   r   grpfnmatchtempfilesocketstructselectrH   fcntlerrnostatstringglobioregetpasslocalesixext.movesr   gcodesr   r   r	   r
   LoggerLOG_LEVEL_INFOLOG_TO_CONSOLEr'   	set_levelr   r   r#   r.   r2   r4   rA   rJ   rX   rn   r   r   r   <module>r      s   6  	 " 
             	 	   '     fmmB44fmm6R6RS f $	&0AH'*!r   