import os
import subprocess
import sys
import time
from gspylib.common.ErrorCode import ErrorCode
from base_utils.os.cmd_util import CmdUtil
class CrontabUtil(object):
"""
os crontab utility
"""
@staticmethod
def getAllCrontab():
"""
function : Get the crontab
input : NA
output: status, output
"""
cmd_list = CmdUtil.getAllCrontabCmdList()
output, error, status = CmdUtil.execCmdList(cmd_list)
if output.find("no crontab for") >= 0:
output = ""
status = 0
if status != 0:
raise Exception(ErrorCode.GAUSS_502["GAUSS_50219"] %
"crontab list" + " Error:%s." % output +
"The cmd is %s" % ' '.join(cmd_list))
return status, output
@staticmethod
def execCrontab(path):
"""
function : Get the crontab
input : string
output: True or False
"""
if not os.path.exists(path):
raise Exception(ErrorCode.GAUSS_502["GAUSS_50201"] % path)
file_path = os.path.dirname(path)
file_name = os.path.basename(path)
cmd = CmdUtil.getCdCmd(file_path)
cmd += " && "
cmd += CmdUtil.getCrontabCmd()
cmd += (" ./%s" % file_name)
cmd += " && %s" % CmdUtil.getCdCmd("-")
(status, output) = subprocess.getstatusoutput(cmd)
if status != 0:
raise Exception(ErrorCode.GAUSS_514["GAUSS_51400"] % cmd +
" Error:\n%s" % output)
return True
@staticmethod
def check_user_crontab_permission():
"""
function : Check user crontab permission
input : NA
output: True or False
"""
cmd_list = CmdUtil.getAllCrontabCmdList()
output, error, status = CmdUtil.execCmdList(cmd_list)
error = bytes.decode(error).strip()
if output.find("not allowed") >= 0 or error.find("not allowed") >= 0:
return False
return True