forked from cassianobrexbit/dio-live-aws-bigdata-2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogGenerator.py
64 lines (51 loc) · 1.66 KB
/
LogGenerator.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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import csv
import time
import sys
sourceData = "country_vaccinations.csv"
placeholder = "LastLine.txt"
def GetLineCount():
with open(sourceData, encoding='latin-1') as f:
for i, l in enumerate(f):
pass
return i
def MakeLog(startLine, numLines):
destData = time.strftime("/var/log/diolive/%Y%m%d-%H%M%S.log")
with open(sourceData, 'r') as csvfile:
with open(destData, 'w') as dstfile:
reader = csv.reader(csvfile)
writer = csv.writer(dstfile)
next (reader) #skip header
inputRow = 0
linesWritten = 0
for row in reader:
inputRow += 1
if (inputRow > startLine):
writer.writerow(row)
linesWritten += 1
if (linesWritten >= numLines):
break
return linesWritten
numLines = 100
startLine = 0
if (len(sys.argv) > 1):
numLines = int(sys.argv[1])
try:
with open(placeholder, 'r') as f:
for line in f:
startLine = int(line)
except IOError:
startLine = 0
print("Writing " + str(numLines) + " lines starting at line " + str(startLine) + "\n")
totalLinesWritten = 0
linesInFile = GetLineCount()
while (totalLinesWritten < numLines):
linesWritten = MakeLog(startLine, numLines - totalLinesWritten)
totalLinesWritten += linesWritten
startLine += linesWritten
if (startLine >= linesInFile):
startLine = 0
print("Wrote " + str(totalLinesWritten) + " lines.\n")
with open(placeholder, 'w') as f:
f.write(str(startLine))