Mini Shell
# -*- coding: utf-8 -*-
# Copyright © Cloud Linux GmbH & Cloud Linux Software, Inc 2010-2019 All Rights Reserved
#
# Licensed under CLOUD LINUX LICENSE AGREEMENT
# http://cloudlinux.com/docs/LICENSE.TXT
from __future__ import print_function
from __future__ import division
from __future__ import absolute_import
import os
import sys
import random
__ALL__ = ["add_cron", "erase_cron", "remove_cron", "add_cron_task"]
def add_cron(file_name, minute, hour, day, month, day_of_week, user, command,
check_command=True):
"""
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.
"""
if minute == 'r':
minute = int(round(random.uniform(0, 59))) # pylint: disable=round-builtin
if hour == 'r':
hour = int(round(random.uniform(0, 23))) # pylint: disable=round-builtin
try:
cron_task = format_cron_task(minute, hour, day, month, day_of_week, user, command)
except TypeError:
sys.stderr.write("Can not add task with wrong syntax")
add_cron_task(file_name, cron_task, check_command)
def add_cron_task(file_name, task, check_command=False):
"""
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.
"""
f = None
try:
f = open('/etc/cron.d/' + file_name, 'a+')
if not is_in_cron(task, f, check_command):
f.write("%s\n" % task)
except (IOError, OSError):
if f is not None:
f.close()
return False
f.close()
return True
def remove_cron(file_name):
"""
Remove cron-file from fs
:param str file_name: Name of cron-file in /etc/cron.d
"""
try:
os.remove('/etc/cron.d/' + file_name)
except (OSError, IOError):
pass
def erase_cron(file_name):
"""
Make cron-file empty
:param str file_name: Name of cron-file in /etc/cron.d
"""
f = None
try:
f = open("/etc/cron.d/" + file_name, "w")
except (IOError, OSError) as err:
sys.stderr.write("Can not erase crontab file %s because %s\n" % (
file_name, str(err)))
if f is not None:
f.close()
def format_cron_task(minute, hour, day, month, day_of_week, user, command):
"""
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
"""
arguments = (minute, hour, day, month, day_of_week, user, command)
for arg in arguments:
if arg is None:
raise TypeError("Wrong schedule for cron task")
return "%2s %2s %2s %2s %2s %10s %s" % arguments
def parse_cron_task(task):
"""
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
"""
return task.split(None, 6)
def get_task_in_cron(crontab, get_parsed=False):
"""
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
"""
for cron_t in (s.strip() for s in crontab):
try:
if get_parsed:
t = parse_cron_task(cron_t)
else:
t = format_cron_task(*parse_cron_task(cron_t))
except TypeError:
sys.stderr.write("Wrong crontab task syntax: %s\n" % cron_t)
else:
yield t
def is_task_in_cron(task, fd):
"""
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
"""
for t in get_task_in_cron(fd.readlines()):
if t == task:
return True
return False
def is_command_in_cron(task, fd):
"""
: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
"""
command = parse_cron_task(task)[-1]
for t in get_task_in_cron(fd.readlines(), get_parsed=True):
if t[-1] == command:
return True
return False
def is_in_cron(task, fd, check_command=False):
"""
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
"""
if check_command:
return is_command_in_cron(task, fd)
return is_task_in_cron(task, fd)
Zerion Mini Shell 1.0