Last active
October 28, 2015 15:12
-
-
Save jxcl/1b1bb063253a7f7789db to your computer and use it in GitHub Desktop.
Time Since Last Pacman Sync/Upgrade
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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_last_sync(log_text): | |
last_sync = None | |
for line in log_text.split("\n"): | |
parsed_line = parse_pacman_line(line) | |
if parsed_line is None: | |
continue | |
else: | |
if parsed_line["message"] == "synchronizing package lists": | |
last_sync = parsed_line["date"] | |
return last_sync | |
def get_last_system_upgrade(log_text): | |
last_upgrade = None | |
for line in log_text.split("\n"): | |
parsed_line = parse_pacman_line(line) | |
if parsed_line is None: | |
continue | |
else: | |
if parsed_line["message"] == "starting full system upgrade": | |
last_upgrade = parsed_line["date"] | |
return last_upgrade | |
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() | |
last_sync = get_last_sync(text) | |
last_upgrade = get_last_system_upgrade(text) | |
last_sync_diff = get_diffs(last_sync) | |
last_upgrade_diff = get_diffs(last_upgrade) | |
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