3
NZ0                 @   s  d dl mZ dZdZdZdZd dlZd dlZd dlZd dl	Z	d dl
Z
d dlT d dlmZmZmZmZ d d	lmZ d d
lmZmZ d dlT dZdZdZdZee ZdZdZi Zdd Zdd Z dd Z!d}ddZ"dd Z#edddfde dddfd e dddfd~dej$ej%ej&ej'ej(g
Z)yJej*eeeee)e+fddd'Z,e,j-d(d)d*d+d,d-d.d/gd#d0\Z.Z/Z0Z1Z2Z3W n8 ej4k
r Z5 ze6j7e5j8 e"  W Y ddZ5[5X nX ej9d1re6j:d. xe.D ]\Z;Z<e;dkre"  ne;d4kre"d5 ne;d6kre"d7 npe;d8kre=ed9d: e>d d# nNe;dkrFe<j? j@ ZAe6j:eAsje"  n$e;dkr\e6j:d. ne;d?kre<ZqW ejB d kre6j7d@ ejC  esedAgedBZDeeDjE d  ZFeFreFjGd9d  Zese6j7dC ejCdD ejHjIesdEZdFe ZJe6jKdGeJ  ejLeJ\ZMZNeMd kr:e6j7dHe  ejCdD ejLdIe \ZMZNeMd krde6j7dJe  dKe dL ee< dMedN< xxeD ]pZOejLdOeeOf \ZMZNeMd kre6j7dHe  ejCdD ejLdPeeOf \ZMZNeMd kre6j7dQeeOf  qW e   e spejPdRdSdTdUgdS\ZQZReQ s.eRdUkrHe!  e6jSdV ejCdD eQrpeRdSkrpe#dW e#dX e#dY xe
j
jTe
j
jU dZZVe6jWe6jXd[ ejPd\dSdUgdS\ZQZReQreRdSkrP n,eQ seRdUkrre!  e6jSdV ejCdD qrW ejLd]\ZMZNeMd kre6j7d^ e6jKd_e  e sejHjIdWrbejLd`e \ZMZNeMd krbe6j7dadW  ejHjIdXrejLdbe \ZMZNeMd kre6j7dadX  ejHjIdYrDejLdce \ZMZNeMd krDe6j7dadY  npe6jKdd deeV ZJejYeJdfe  ZMeMd kre6j7dg e6jKdh dieV ZJejYeJdje  ZMeMd krDe6j7dk x&eD ]ZOejYdleeO eeOf ZMqJW ejLdme \ZMZNeMd kre6j7dndo  dpe ZJejZeJZMeMd kre6j7dJe  dqeef ZJe6jKdreJ  ejLeJ\Z[ZNe[d kre6j7dse  nle6jKdte  ejLdue \ZMZNeMd kr:e6j7dve  e6jKdw ejLdxe \ZMZNeMd krne6j7dye  e!  e6jWd e6jWd e[d kre6jWe6jXdzej\ ef  e6jWe6jXd{ ne6jWe6jXd|ej\ ef  e6jWd dS )    )print_functionz1.0zHPLIP logs capture Utilityzhp-logcapturezCaptures the HPLIP log files.N)*)utilstuimoduleos_utils)to_string_utf8)PopenPIPEz/etc/cups/cupsd.confz/etc/cups/cupsd.conf_orginalz./Zhplip_troubleshoot_logsz/var/spool/cups/tmp c              C   s<   t tt} | j  | j}| j}|dkr4|dkr4d}nd}|S )NZfedoraz 21TF)ZCoreInstallZMODE_INSTALLERINTERACTIVE_MODEZ
get_distrodistro_nameZdistro_version)Zcorer   Z
distro_verZjournal r   /usr/share/hplip/logcapture.py
is_journal3   s    
r   c              C   sH  d} dt tf }tjd|  tj|\}}|dkrBtjdt   dt  }tjd|  tj|\}}|dkrDdt  }tjd	|  tj|}|dkrtjd
t   d }tj	drtj
jtj	ddd }nFtj	drtj
jtj	dddt  }ntj
jdr
d}n
tjd |rDtjd|  tj|\}}|dkrDd} | S )NFzcp -f %s %szBackup CUPS conf file. cmd =%sr   z Failed to take back cups file=%szgrep 'LogLevel warn' %szcmd= %sz;sed -i 's/LogLevel.*warn/LogLevel debughpLogLevel 15/' %s z%Changing 'Log level' to debug. cmd=%sz-Failed to update Loglevel to Debug in cups=%sservicez cups restart	systemctlz restart %s.servicez/etc/init.d/cupsz/etc/init.d/cups restartz:service command not found.. Please restart cups manually..zCUPS restart cmd = %sT)	CUPS_FILECUPS_BACKUP_FILElogdebugr   runerrorossystemwhichpathjoinservice_nameexists)resultcmdstsoutr   r   r   
enable_logA   s:    





r$   c              C   s(  d} dt tf }tjd|  tj|\}}|dkrpdt  }tjd|  tj|\}}|dkr~tjdt   ntjdt  tjd	 d }tjd
rt	j
jtjd
d
d }nDtjdrt	j
jtjdddt  }nt	j
jdrd}n
tjd |r$tjd|  tj|\}}|dkr$d} | S )NFzcp -f %s %sz Restoring CUPS conf file. cmd=%sr   zrm -f %sz Removing Temporary file.. cmd=%sz-Failed to remove the Temporary backup file=%sz'Failed to restore cups config file = %szRestarting CUPS servicer   z cups restartr   z restart %s.servicez/etc/init.d/cupsz/etc/init.d/cups restartz:service command not found.. Please restart cups manually..zCUPS restart cmd = %sT)r   r   r   r   r   r   warnr   r   r   r   r   r   r   )r    r!   r"   r#   r   r   r   restore_loglevelsi   s4    




r&   textc             C   s4   | dkrt jtt t jt| ttt tjd d S )Nr'   r   )	r   Z	log_title	__title____version__Zformat_textUSAGE__mod__sysexit)typr   r   r   usage   s    r/   c             C   s  t jj| rd}x4t jjd| |f s<t jjd| |f rF|d7 }qW tjd| | |f \}}|dkrxtjd|   nd|  }tj|}|dkrtj	d|   tj
d	rtjd
| |f \}}|dkrtjd| | |f  ntjd| | |f  ntjd| | |f  d S )N   z%s.%dz%s.%d.gzzcp %s %s.%dr   zFailed to archive %s log filezcat /dev/null > %szFailed to clear the %s log fileZgzipz
gzip %s.%dz$Existing %s log file copied to %s.%dz'Existing %s log file copied to %s.%d.gz)r   r   r   r   r   r   r   r   executer%   r   info)ZstrLogZiArchr"   r#   r!   r   r   r   backup_clearLog   s"    *

r3   nameTz-Usage: [su -c /sudo] %s [USER INFO] [OPTIONS]Zsummaryze.g. su -c '%s'[USER INFO]headingF)User name for which logs to be collected:--user=<username> option)Zrun_as_root_okquietzhl:g:rhelpz	help-restzhelp-manz	help-desczlogging=r   zuser=)Zhandle_device_printerZHPLIP_DEBUG-h--helpz--help-restrestz
--help-manZmanz--help-desc )end-l	--logging-g--debugz--userzIlogCapture needs root permissions since cups service restart requires....Zwho)stdoutzAFailed to get the user name. Try again by passing '--user' optionr0   z/tmpzmkdir -p %sz"Creating temporary logs folder =%sz'Failed to create directory =%s. Exitingzchmod 755  %sz$Failed to change permissions for %s.z/home/z/.hplipz/root/.hpliprootzmkdir -p %s/%szchmod 755  %s/%sz'Failed to change permissions for %s/%s.zVArchiving system logs (i.e. syslog, message, error_log). Press (y=yes*, n=no, q=quit):ynqz	User exitz/var/log/syslogz/var/log/messagesz/var/log/cups/error_logz%Y-%m-%d %H:%M:%SzT
Please perform the tasks (Print, scan, fax) for which you need to collect the logs.z1Are you done with tasks?. Press (y=yes*, q=quit):zhp-checkzFailed to run hp-check commandz$Copying logs to Temporary folder =%sz#cp -f /var/log/syslog %s/syslog.logzFailed to capture %s log file.z'cp -f /var/log/messages %s/messages.logz3cp -f /var/log/cups/error_log %s/cups_error_log.logz(Collecting cups logs from system journalz+journalctl -u cups.service -e --since '%s' z> %s/cups_error.logz#Failed to capture logs from journalz'Collecting messages from system journalzjournalctl --since '%s' z> %s/messages.logz'Failed to capture messages from journalz!cp -f %s/*.log  %s/%s 2>/devnull zmv -f ./hp-check.log %szFailed to capture %s log files.z./hp-check.logzchmod 666  %s/*.logztar -zcf %s.tar.gz %szCompressing logs. cmd =%szFailed to compress %s folder.z$Changing Permissions of ./%s.tar.gz zchmod 666 -R ./%s.tar.gzz+Failed to change permissions for %s.tar.gz.zRemoving Temporary log files..z	rm -rf %sz2Failed to remove temporary files. Remove manually.zLogs are saved as %s/%s.tar.gzzXPlease create a bug @https://bugs.launchpad.net/hplip/+filebug and upload this log file.zLogs are saved as %s/%s)r'   )r5   r   r6   F)r7   r8   r9   F)r<   r=   )rA   rB   )rC   rD   )]Z
__future__r   r)   r(   r+   __doc__r   r,   ZgetoptglobZdatetimeZbase.gbaser   r   r   r   Zbase.sixextr   
subprocessr	   r
   Zinstaller.core_installr   r   ZLOG_FOLDER_PATHZLOG_FOLDER_NAMEZ	LOG_FILESZTMP_DIRZ	USER_NAMEZUSERSr   r$   r&   r/   r3   ZUSAGE_OPTIONSZ
USAGE_HELPZUSAGE_LOGGING1ZUSAGE_LOGGING2ZUSAGE_LOGGING3r*   ZModuler   modZparseStdOptsZoptsZ
device_uriZprinter_namemodeZ
ui_toolkitlocZGetoptErrorer   r   msggetenvZ	set_leveloaprintZ
clean_exitlowerstripZ	log_levelgetuidr-   ZpoutZcommunicateoutputsplitr   r   r!   r   r   r"   r#   uZenter_choiceokZ
user_inputr%   ZstrftimeZnowZlog_timer2   Zboldr   r1   Zsts_compressgetcwdr   r   r   r   <module>   sX  (!

.













































