-
Notifications
You must be signed in to change notification settings - Fork 0
/
cleanlifelog.py
executable file
·87 lines (71 loc) · 2.59 KB
/
cleanlifelog.py
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
#!/usr/bin/env python3
#
# cleanlifelog.py
#
# save life.log to life.log.timestamp
# read ~/rosbot-on-gopigo3/life.log file into list
# starting at next to last line
# loop until "boot logged line"
# if line is an execution log line: delete it
# else point to previous line
# write out modified file
import datetime
from shutil import copyfile
import argparse
dtNow = datetime.datetime.now()
inFileName = "/home/ubuntu/HumbleDave/life.log"
outFileName = "/home/ubuntu/HumbleDave/life.log"
# bkupFileName = "/home/pi/Carl/life.log.bkup_"+dtNow.strftime("%Y%m%d_%H%M%S")
bkupFileName = "/home/ubuntu/HumbleDave/tmp/life.log.bak"
# Uncomment these to test in ~/Carl/Projects/CleanLifeLog/
# inFileName = "life.log.test"
# outFileName = "life.log.new"
# bkupFileName = "life.log.bkup_"+dtNow.strftime("%Y%m%d_%H%M%S")
# ARGUMENT PARSER
ap = argparse.ArgumentParser()
# ap.add_argument("-f", "--file", required=True, help="path to input file")
# ap.add_argument("-n", "--num", type=int, default=5, help="number")
ap.add_argument("-p", "--previous", default=False, action='store_true', help="clean previous boot session")
args = vars(ap.parse_args())
# print("Started with args:",args)
clean_previous_session = args['previous']
changed = False
with open(inFileName) as fIn:
lineList = fIn.readlines()
print("Read in {}".format(inFileName))
lines = len(lineList)
lineIdx = lines - 1
last = -1
print("lines: {}".format(lines))
print("lastline: {}".format(lineList[last]))
bootlogline = "----- boot -----"
executionlogline = "dEmain execution:"
if (clean_previous_session == True):
# Find last boot log line
while (bootlogline not in lineList[lineIdx]):
lineIdx -=1
lineIdx -=1
# Find last execution log line before the last boot log line
while ((bootlogline not in lineList[lineIdx]) and (executionlogline not in lineList[lineIdx])):
lineIdx -= 1
# leave the last execution log line
if (executionlogline in lineList[lineIdx]):
lineIdx -= 1
while (bootlogline not in lineList[lineIdx]):
# print("Checking line {}".format(lineIdx+1))
if (executionlogline in lineList[lineIdx]):
print("removing: {}".format(lineList[lineIdx]))
del lineList[lineIdx]
changed = True
lineIdx -= 1
if changed == True:
# backup the original file before rewriting with changes
copyfile(inFileName, bkupFileName)
with open(outFileName,'w') as fOut:
fOut.writelines(lineList)
print("Wrote cleaned {}".format(outFileName))
lines = len(lineList)
print("lines: {}".format(lines))
print("lastline: {}".format(lineList[last]))
else:
print("File not changed")