B
    `                 @   s   d dl mZ d dl mZ d dl mZ d dlZd dlZd dlZddddgZdd
dZdddZ	dd Z
dd Zdd Zdd ZdddZdd Zdd ZdddZdS )    )print_function)division)absolute_importNadd_cron
erase_cronremove_cronadd_cron_taskTc	       
      C   s   |dkrt ttdd}|dkr8t ttdd}yt|||||||}	W n  tk
rp   tjd Y nX t	| |	| dS )a  
    Add new cron task into crontab schedule iff this task or command wasn't already existed in the cron-file.

    :param str file_name: Name of cron-file in /etc/cron.d
    :param minute: Integer or char 'r' if to set random minute
    :param hour: Integer or char 'r' if to set random hour
    :param int, str day: Day number
    :param int, str month: Month number
    :param int, str day_of_week: Day of week number
    :param str user: Under what user do run command
    :param str command: What command do run
    :param bool check_command: If it is False, check that whole cron-task line already exists in crontab,
        check that command string exists instead. Default is True, check a command string.
    rr   ;      z"Can not add task with wrong syntaxN)
introundrandomZuniformformat_cron_task	TypeErrorsysstderrwriter   )
	file_nameminutehourdaymonthday_of_weekusercommandcheck_commandZ	cron_task r   
crontab.pyr      s    Fc          	   C   sf   d}y,t d|  d}t|||s.|d|  W n( ttfk
rX   |dk	rT|  dS X |  dS )a  
    Add new cron task in cron format iff this task or command in this task wasn't already existed in the cron-file.

    :param str file_name: Name of cron-file in /etc/cron.d
    :param str task: Cron task in format "min hour day mon d_of_w user command"
    :param bool check_command: If it is False, check that whole cron-task line already exists in crontab,
        check that command string exists instead. Default is False, check a whole cron-task string.
    Nz/etc/cron.d/za+z%s
FT)open
is_in_cronr   IOErrorOSErrorclose)r   taskr   fr   r   r   r   -   s    	c          	   C   s0   yt d|   W n ttfk
r*   Y nX dS )z^
    Remove cron-file from fs

    :param str file_name: Name of cron-file in /etc/cron.d
    z/etc/cron.d/N)osremover"   r!   )r   r   r   r   r   C   s    c          
   C   sj   d}yt d|  d}W n> ttfk
rT } ztjd| t|f  W dd}~X Y nX |dk	rf|  dS )zZ
    Make cron-file empty

    :param str file_name: Name of cron-file in /etc/cron.d
    Nz/etc/cron.d/wz)Can not erase crontab file %s because %s
)r   r!   r"   r   r   r   strr#   )r   r%   errr   r   r   r   O   s    "c       	      C   s8   | ||||||f}x|D ]}|dkrt dqW d| S )a  
    Build cron-task string in the cron format

    :param minute: Integer or char 'r' if to set random minute
    :param hour: Integer or char 'r' if to set random hour
    :param int day: Day number
    :param int month: Month number
    :param int day_of_week: Day of week number
    :param str user: Under what user do run command
    :param str command: What command do run
    :return: Cron-task in the cron format
    :rtype: str
    NzWrong schedule for cron taskz%2s %2s %2s %2s %2s %10s %s)r   )	r   r   r   r   r   r   r   Z	argumentsargr   r   r   r   _   s
    
r   c             C   s   |  ddS )z
    Split cron task string into cron task parts

    :param str task: Cron-task string in the cron format
    :return: List of cron-task parts
    :rtype: list of str
    N   )split)r$   r   r   r   parse_cron_taskt   s    r.   c          	   c   sf   x`dd | D D ]N}y|r$t |}ntt | }W n$ tk
rV   tjd|  Y qX |V  qW dS )a<  
    Returns iterator through crontab tasks

    :param iterable crontab: Iterator with crontab tasks' strings
    :param bool get_parsed: If it is True, return crontab task as list of task's parts
        return crontab task as a string instead
    :return: Crontab task
    :rtype: str
    :rtype: list of str
    c             s   s   | ]}|  V  qd S )N)strip).0sr   r   r   	<genexpr>   s    z#get_task_in_cron.<locals>.<genexpr>zWrong crontab task syntax: %s
N)r.   r   r   r   r   r   )Zcrontab
get_parsedZcron_ttr   r   r   get_task_in_cron   s    
r5   c             C   s&   x t | D ]}|| krdS qW dS )a  
    Find first occurence of task in cron-file if it has

    :param str task: Cron-task in cront format to compare with
    :param file fd: File descriptor of opened cron file
    :return: True if such a task is already existed in cron-file, False instead
    :rtype: bool
    TF)r5   	readlines)r$   fdr4   r   r   r   is_task_in_cron   s    	r8   c             C   s:   t | d }x(t| ddD ]}|d |krdS qW dS )a  
    :param str task: Task with command to looking for
    :param file fd: File descriptor of opened cron file
    :return: True if such a command is already existed in cron-file, False instead
    :rtype: bool

    Find first occurence of command in cron-file if it has
    T)r3   F)r.   r5   r6   )r$   r7   r   r4   r   r   r   is_command_in_cron   s
    	r:   c             C   s   |rt | |S t| |S )a  
    Find first occurence of command or task in cron-file if it has

    :param str task: Task or command to looking for
    :param file, BinaryIO fd: File descriptor of opened cron file
    :param bool check_command: If it is True, check command occurence, check task occurence instead
    :return: True if such a command or task is already existed in cron-file, False instead
    :rtype: bool
    )r:   r8   )r$   r7   r   r   r   r   r       s    

r    )T)F)F)F)Z
__future__r   r   r   r&   r   r   Z__ALL__r   r   r   r   r   r.   r5   r8   r:   r    r   r   r   r   <module>   s    


