-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlog_concat.py
59 lines (51 loc) · 1.91 KB
/
log_concat.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
# pip install pandas
"""
# This script is meant to be run after cloning the relevant folder
# from the private repo that holds the log files
# Run following bash commands before calling this module:
git clone \
--depth 1 \
--filter=blob:none \
--sparse \
https://$WORKFLOW_TOKEN@github.com/schmwong/login-log \
;
cd login-log
git sparse-checkout init --cone
git sparse-checkout set <folder name>
cd ..
"""
import sys
sys.dont_write_bytecode = True
import os
import shutil
import pandas as pd
import csv
def update_logs(instance):
instance.logger.removeHandler(instance.DuoHandler)
filename = instance.filename
filepath = f"./login-log/{filename.split()[1].split('_')[0]}/{filename}" # ./<folder name>/<filename>
if os.path.exists(filename):
# Move csv file to temp folder
os.makedirs("./temp", exist_ok=True)
temp = f"./temp/{filename}"
shutil.move(filename, temp)
# ----------------------------------------------------------------
# For debugging new file
print(f"\n\nReading update file: {filename}...\n\n")
with open(temp, "r", newline="", encoding="utf-8") as file:
reader = csv.reader(file)
for row in reader:
print(row)
print(f"\n-------------- End of file '{filename}' --------------\n\n")
# ----------------------------------------------------------------
if os.path.exists(filepath):
df_old = pd.read_csv(filepath, index_col=False, encoding="utf-8")
df_update = pd.read_csv(temp, index_col=False, encoding="utf-8")
df_new = pd.concat([df_old, df_update])
df_new.drop_duplicates(inplace=True)
df_new.to_csv(filepath, index=False, encoding="utf-8")
shutil.rmtree("./temp") # Delete the update file after successful concat
else:
shutil.move(temp, filepath)
else:
print("New logs do not exist.")