From be6a862b7c169f1c4c3c88ec296d4ede5160e518 Mon Sep 17 00:00:00 2001 From: vitrinekast Date: Tue, 26 Mar 2024 23:48:06 +0100 Subject: [PATCH] add day param --- script.py | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/script.py b/script.py index ee12d21..7e975b5 100644 --- a/script.py +++ b/script.py @@ -2,7 +2,12 @@ from jinja2 import Environment, FileSystemLoader import subprocess from datetime import datetime, timedelta import os -import fnmatch +import sys + +if len(sys.argv) > 1: + time_ag_arg = int(sys.argv[1]) +else: + time_ag_arg = False #create the log directory path = "/home/xpub/www/html/tl-dr/log-books" @@ -13,6 +18,12 @@ if not os.path.exists(path): now = datetime.today() book_name = "TL;DR_" + now.strftime("%B %dth %Y") +def get_journalctl_on(): + if time_ag_arg: + return ["-S", f"{time_ag_arg + 1} days ago", "-U", f"{time_ag_arg} days ago"] + else: + return ["-S","today"] + def create_all_logs_file(): list_logs = subprocess.run(["ls", "/home/xpub/www/html/tl-dr/log-books"], capture_output=True) @@ -34,9 +45,11 @@ def run_command(command, based = False): # Run all the commands for getting the logs, and assign to variables print("start running the log commands"); + +# journalctl --utc last_user_added = subprocess.run(["sudo", "journalctl","_COMM=useradd","-r","-n", "1" , "--output-fields=MESSAGE"], capture_output=True) last_user_added_name = run_command(['grep', '-Po', "(?<=name)\W*\K[^ ]*"], based=last_user_added.stdout) -users_created_today = run_command(["sudo", "journalctl", "-S","today","_COMM=useradd","-r","--output-fields=MESSAGE"]).splitlines() +users_created_today = run_command(["sudo", "journalctl","_COMM=useradd","-r"] + get_journalctl_on()).splitlines() since_last_boot = run_command(["uptime","-s"]) slb_date = datetime.strptime(since_last_boot, "%Y-%m-%d %H:%M:%S") @@ -105,9 +118,9 @@ output = template.render( print("Output the files"); # Export the html as book.html, which is used as an input for pandoc -with open("/home/xpub/www/html/tl-dr/book.html", "w") as f: +with open(f"/home/xpub/www/html/tl-dr/book_${time_ag_arg}.html", "w") as f: print(output, file=f) - subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name]) + # subprocess.run(['sh', '/home/xpub/www/html/tl-dr/create_book.sh', book_name]) print("Finished"); # At the moment, we are not storing logs in the log folder anymore