Skip to content

Instantly share code, notes, and snippets.

@undu
Forked from jxcl/LICENSE
Created May 27, 2014 08:36
Show Gist options
  • Save undu/7ec81a04d7039e5a8e6f to your computer and use it in GitHub Desktop.
Save undu/7ec81a04d7039e5a8e6f to your computer and use it in GitHub Desktop.
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <http://unlicense.org/>
#!/usr/bin/env python3
import pathlib
import re
from datetime import datetime
LOG_PATH = pathlib.Path("/var/log/pacman.log")
def get_log():
with LOG_PATH.open() as fp:
log_text = fp.read()
return log_text
def parse_pacman_line(line):
if len(line) > 0:
date_str = line[:18]
pacman_indicator = line[19:27]
message = line[28:]
if pacman_indicator == "[PACMAN]":
parsed_date = datetime.strptime(date_str,
"[%Y-%m-%d %H:%M]")
return {"date": parsed_date,
"message": message}
return None
def get_date_of_last(log_text, messages):
last_messages = [None] * len(messages)
for line in log_text.split("\n"):
parsed_line = parse_pacman_line(line)
if parsed_line is None:
continue
else:
for index, message in enumerate(messages):
if parsed_line["message"] == message:
last_messages[index] = parsed_line["date"]
return last_messages
def get_diffs(t):
now = datetime.now()
diff = now - t
if diff.days != 0:
return "{}d".format(diff.days)
elif diff.seconds > 3600:
return "{}h".format(diff.seconds // 3600)
else:
return "{}m".format(diff.seconds // 60)
if __name__ == "__main__":
text = get_log()
messages_date = get_date_of_last(text, ["synchronizing package lists",
"starting full system upgrade"])
last_sync_diff = get_diffs(messages_date[0])
last_upgrade_diff = get_diffs(messages_date[1])
print("S: {} U: {}".format(last_sync_diff,
last_upgrade_diff))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment