#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:[file2 file3...] execute file(.py) -ft -filetime: [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()
用户登录
还没有账号?立即注册
用户注册
投稿取消
文章分类: |
|
还能输入300字
上传中....