1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
| import os import re import time import json import shutil from time import strftime,gmtime from optparse import OptionParser
CONST = { "LOG_FILE": ".log", "REGEX":{ "COMMIT": "^commit", "AUTHOR": "^Author", "DATE": "^Date", "Merge": "^Merge", "#": "#", } } gAfter = "" gBefore = ""
def getGitLog(): global gAfter global gBefore global gDays cwd = os.getcwd() configPath = os.path.join(cwd, "config.json") f = open(configPath, encoding='utf-8') res = f.read() author = json.loads(res)["author"] log_paths = json.loads(res)["log_paths"] source_path = json.loads(res)["source_path"] cmd = "" today_date = time.strftime("%Y-%m-%d", time.localtime()) my_file_name = today_date+CONST["LOG_FILE"] if gAfter == None and gBefore == None and gDays ==None: nowTime = strftime("%Y-%m-%d", gmtime()) cmd = "cd \"{0}\";git log --author=\"{1}\" --after=\"{2} 00:00:00\" --before=\"{3} 24:00:00\" > {4}/{5}".format(source_path, author, nowTime, nowTime, getRunPath(), my_file_name) elif gDays: ticks = time.time() timeStamp = ticks-int(gDays)*24*60*60 localtime = time.strftime("%Y-%m-%d", time.localtime(timeStamp)) my_file_name = localtime+CONST["LOG_FILE"] cmd = "cd \"{0}\";git log --author=\"{1}\" --after=\"{2} 00:00:00\" --before=\"{3} 24:00:00\" > {4}/{5}".format(source_path, author, localtime, localtime, getRunPath(), my_file_name) else: cmd = "cd \"{0}\";git log --author=\"{1}\"".format(source_path, author) if gBefore: cmd = "{0} --before=\"{1}\"".format(cmd, gBefore) if gAfter: cmd = "{0} --after=\"{1}\"".format(cmd, gAfter) my_file_name = gAfter+CONST["LOG_FILE"] cmd = "{0} > {1}/{2}".format(cmd, getRunPath(), my_file_name) print("执行命令:{}".format(cmd)) os.system(cmd) parseLog(my_file_name,log_paths)
def getRunPath(): return os.path.split(os.path.realpath(__file__))[0]
def parseLog(my_file_name,log_paths): print("================LOG====================") logPath = getRunPath() + "/" + my_file_name savePath = log_paths + "/" + my_file_name tempPath = getRunPath() + "/temp" if not os.path.exists(tempPath): os.makedirs(tempPath) if not os.path.exists(log_paths): os.makedirs(log_paths) saveFile = open(savePath,"w+") saveFile.writelines("今日工作内容:\n") idx = 1 logFile = open(logPath) for line in logFile: line = line.strip() if len(line) == 0: continue reCommit = re.match(CONST["REGEX"]["COMMIT"], line) reAuthor = re.match(CONST["REGEX"]["AUTHOR"], line) reDate = re.match(CONST["REGEX"]["DATE"], line) reMerge = re.match(CONST["REGEX"]["Merge"], line) reSpec = re.match(CONST["REGEX"]["#"], line)
if reCommit or reAuthor or reDate or reMerge or reSpec: continue else: print(line) saveFile.writelines(str(idx)+"."+line+"\n") idx = idx+1 saveFile.close() logFile.close() shutil.move(logPath,tempPath+"/"+my_file_name) print("=======================================") print("汪~赶快粘贴到每天的日报吧^_^")
if __name__ == '__main__': parser = OptionParser() parser.add_option( "--after", action="store", dest="after", help="开始时间(如果after和before都不填写,默认为当天时间)" ) parser.add_option( "--before", action="store", dest="before", help="结束时间(如果after和before都不填写,默认为当天时间)" ) parser.add_option( "--days", action="store", dest="days", help="距离当前日期的天数(比如1代表昨天,2代表前天)" ) (opts, args) = parser.parse_args() gAfter = opts.after gBefore = opts.before gDays = opts.days getGitLog()
|