#/usr/bin/env python
#-*- coding:utf-8 -*-
"""
1.解析 crontab 配置文件中的五个数间参数(分 时 日 月 周),获取他们对应的取值范围
2.将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
"""
#$Id $
import re, time, sys
from Core.FDateTime.FDateTime import FDateTime
def get_struct_time(time_stamp_int):
"""
按整型时间戳获取格式化时间 分 时 日 月 周
Args:
time_stamp_int 为传入的值为时间戳(整形),如:1332888820
经过localtime转换后变成
time.struct_time(tm_year=2012, tm_mon=3, tm_mday=28, tm_hour=6, tm_min=53, tm_sec=40, tm_wday=2, tm_yday=88, tm_isdst=0)
Return:
list____返回 分 时 日 月 周
"""
st_time = time.localtime(time_stamp_int)
return [st_time.tm_min, st_time.tm_hour, st_time.tm_mday, st_time.tm_mon, st_time.tm_wday]
def get_strptime(time_str, str_format):
"""从字符串获取 整型时间戳
Args:
time_str 字符串类型的时间戳 如 '31/Jul/2013:17:46:01'
str_format 指定 time_str 的格式 如 '%d/%b/%Y:%H:%M:%S'
Return:
返回10位整型(int)时间戳,如 1375146861
"""
return int(time.mktime(time.strptime(time_str, str_format)))
def get_str_time(time_stamp, str_format='%Y%m%d%H%M'):
"""
获取时间戳,
Args:
time_stamp 10位整型(int)时间戳,如 1375146861
str_format 指定返回格式,值类型为 字符串 str
Rturn:
返回格式 默认为 年月日时分,如2013年7月9日1时3分 :201207090103
"""
return time.strftime("%s" % str_format, time.localtime(time_stamp))
def match_cont(patten, cont):
"""
正则匹配(精确符合的匹配)
Args:
patten 正则表达式
cont____ 匹配内容
Return:
True or False
"""
res = re.match(patten, cont)
if res:
return True
else:
return False
def handle_num(val, ranges=(0, 100), res=list()):
"""处理纯数字"""
val = int(val)
if val >= ranges[0] and val = ranges[0] and tmp_val 1 or other_conf:
#纯数字多于1,或纯数字与其它参数共存,则数字作为时间列表
res = handle_nlist(val=','.join(number_conf), ranges=ranges, res=res)
else:
#只有一个纯数字存在,则数字为时间 间隔
res = handle_num(val=number_conf[0], ranges=ranges, res=res)
return res
def parse_crontab_time(conf_string):
"""
解析crontab时间配置参数
Args:
conf_string 配置内容(共五个值:分 时 日 月 周)
取值范围 分钟:0-59 小时:1-23 日期:1-31 月份:1-12 星期:0-6(0表示周日)
Return:
crontab_range list格式,分 时 日 月 周 五个传入参数分别对应的取值范围
"""
time_limit = ((0, 59), (1, 23), (1, 31), (1, 12), (0, 6))
crontab_range = []
clist = []
conf_length = 5
tmp_list = conf_string.split(' ')
for val in tmp_list:
if len(clist) == conf_length:
break
if val:
clist.append(val)
if len(clist) != conf_length:
return -1, 'config error whith [%s]' % conf_string
cindex = 0
for conf in clist:
res_conf = []
res_conf = parse_conf(conf, ranges=time_limit[cindex], res=res_conf)
if not res_conf:
return -1, 'config error whith [%s]' % conf_string
crontab_range.append(res_conf)
cindex = cindex + 1
return 0, crontab_range
def time_match_crontab(crontab_time, time_struct):
"""
将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
Args:
crontab_time____crontab配置中的五个时间(分 时 日 月 周)参数对应时间取值范围
time_struct____ 某个整型时间戳,如:1375027200 对应的 分 时 日 月 周
Return:
tuple 状态码, 状态描述
"""
cindex = 0
for val in time_struct:
if val not in crontab_time[cindex]:
return 0, False
cindex = cindex + 1
return 0, True
def close_to_cron(crontab_time, time_struct):
"""coron的指定范围(crontab_time)中 最接近 指定时间 time_struct 的值"""
close_time = time_struct
cindex = 0
for val_struct in time_struct:
offset_min = val_struct
val_close = val_struct
for val_cron in crontab_time[cindex]:
offset_tmp = val_struct - val_cron
if offset_tmp > 0 and offset_tmp 29:
continue
else:
#其它2月份有28天
if int(day_hm_s[:2]) > 28:
continue
if month in month_short:
if int(day_hm_s[:2]) > 30:
continue
month_dhm.append('%s%s' % (month, day_hm_s))
#按年 和 月组装
len_start = len(limit_start)
len_end = len(limit_end)
month_dhm_limit = []
for month_dhm_s in month_dhm:
time_ymdhm = '%s%s' % (str(year_num), month_dhm_s)
#开始时间\结束时间以外的排除
if (int(time_ymdhm[:len_start]) int(limit_end)):
continue
month_dhm_limit.append(time_ymdhm)
if len(cron_time[4]) >output_error.txt)'
conf_string = '*/10 * * * *'
#时间戳
time_stamp = int(time.time())
#解析crontab时间配置参数 分 时 日 月 周 各个取值范围
res, desc = parse_crontab_time(conf_string)
if res == 0:
cron_time = desc
else:
print desc
sys, exit(-1)
print "\nconfig:", conf_string
print "\nparse result(range for crontab):"
print " minute:", cron_time[0]
print " hour: ", cron_time[1]
print " day: ", cron_time[2]
print " month: ", cron_time[3]
print " week day:", cron_time[4]
#解析 时间戳对应的 分 时 日 月 周
time_struct = get_struct_time(time_stamp)
print "\nstruct time(minute hour day month week) for %d :" % \
time_stamp, time_struct
#将时间戳与crontab配置中一行时间参数对比,判断该时间戳是否在配置设定的时间范围内
match_res = time_match_crontab(cron_time, time_struct)
print "\nmatching result:", match_res
#crontab配置设定范围中最近接近时指定间戳的一组时间
most_close = close_to_cron(cron_time, time_struct)
print "\nin range of crontab time which is most colse to struct ", most_close
time_list = cron_time_list(cron_time)
print "\n\n %d times need to tart-up:\n" % len(time_list)
print time_list[:10], '...'
if __name__ == '__main__':
#请看 使用实例
strs = 'job_@-@-@-@-@_test02.txt.sh'
print isdo(strs)
#main()0")
查看更多关于python实现的解析crontab配置文件代码的详细内容...
声明:本文来自网络,不代表【好得很程序员自学网】立场,转载请注明出处:http://www.haodehen.cn/did87675