前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python 定时运行脚本

Python 定时运行脚本

原创
作者头像
用户7365393
修改2021-11-01 14:07:55
8870
修改2021-11-01 14:07:55
举报
代码语言:javascript
复制
#coding=utf-8
import sys
import os
import datetime
import time
class ArgsDealwith:

def arg_environment(self, args):
    filepath = ('PYTHON_PATH', 'path')
    for i in filepath:
        filename = os.environ.get(i,"%s is null." % i)
        print '%s:' % i,' ', filename

def arg_en(self, args):
    filepath = ('PYTHON_PATH', 'path')
    for i in filepath:
        filename = os.environ.get(i,"%s is null." % i)
        print '%s:' % i, ' ', ' '.join(filename.split(';'))

def arg_file(self, args):
    if not args:
        print 'Error: file name is null.'
    else:
        if len(args) == 1 and args[0].lower() == 'python_path':
            filename = os.environ.get(args[0], None)
            if filename:
                args = filename.split(';')
            else:
                print 'Error: "%s" is null' % args[0]
                exit()

        for i in args:
            if os.path.isfile(i):
                try:
                    execfile(i)
                except:
                    print 'Error: "%s" run failed.' % i
            else:
                print 'Error: Not found "%s" file.' % i

def arg_filetime(self, args):
    if not args:
        print 'Error: file name is null.'
    elif len(args) % 2 != 0:
        print 'Error: args isn't validate'
    else:
        fileTimes = [i for i in args if args.index(i) % 2 != 0 ]
        for i in fileTimes:
            try:
                fileTimes[fileTimes.index(i)] = int(i)
            except Exception:
                print 'Error: time args isn't validate'
                exit()
            if i <= 0:
                print 'Error: time<=0'
                exit()                 

        minTime = fileTimes[0]
        for i in fileTimes:
            if i < minTime:
                minTime = i

        strTime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
        tupleTime = tuple([int(i) for i in strTime.split('-')])
        begin = datetime.datetime(tupleTime[0], tupleTime[1], tupleTime[2], tupleTime[3], tupleTime[4], tupleTime[5])

        fileTimes = [[i, 0] for i in fileTimes]
        seconds = 0
        while True:
            strTime = time.strftime('%Y-%m-%d-%H-%M-%S', time.localtime())
            tupleTime = tuple([int(i) for i in strTime.split('-')])
            end = datetime.datetime(tupleTime[0], tupleTime[1],tupleTime[2], tupleTime[3], tupleTime[4], tupleTime[5])

            time_sub = end - begin
            seconds = time_sub.seconds
            begin = end
            for i in fileTimes:
                i[1] += seconds

            for i in fileTimes:
                if i[1] == 0:
                    self.arg_file( [args[args.index(str(i[0])) - 1] ] )
                elif i[1] >= i[0]:
                    i[1] %= i[0]
                    self.arg_file( [args[args.index(str(i[0])) - 1] ] )
                    if i[0] - i[1] < minTime:
                        minTime = i[0] - i[1]

            time.sleep(minTime)

def arg_help(self, args):
    strHelp = "Usage: ps [-options] [args...] where option include:"
    strHelp += """
    -? -help            print this help message
    -e -environment     print environment path
    -en                 print envrionment path per row
    -f -file:<file> [file2 file3...]
                        execute file(.py)      
    -ft -filetime:<file time> [file2 time2 file3 time3...]
                        execute file(.py) per time,
                        this run not stop,
                        but this command hasn't validate.  
                        time(seconds) must is interger and
                        not less than zero"""
    print strHelp


def arg_args():
    args_dic = {'arg_help' : ['-?', '-help'], 'arg_environment' : ['-e', '-environment'],
                'arg_en' : ['-en'], 'arg_file' : ['-f', '-file'], 'arg_filetime' : ['-ft', 'filetime']}
    argsCls = ArgsDealwith()
    if len(sys.argv) <= 1:
        argsCls.arg_help(sys.argv)
    else:
        argsFun = ''
        for i in sys.argv[1:]:
            bMath = False
            for j in args_dic.items():
                if i in j[1]:
                    argsFun = j[0]
                    bMath = True
                    break
            if bMath:
                break
        if argsFun:
            try:
                getattr(argsCls, argsFun)(sys.argv[2:])

            except Exception, error:
                print error
                exit()
        else:
            print 'Error: "%s" isn't validate arg.' % ' '.join(sys.argv[1:])
            del argsCls
if name == 'main':
    arg_args()</pre> 

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档