Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make arch_updates module more responsive to system updates #2215

Merged
merged 1 commit into from
Oct 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions py3status/modules/arch_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
Display number of pending updates for Arch Linux.

Configuration parameters:
cache_timeout: refresh interval for this module (default 600)
format: display format for this module, otherwise auto (default None)
hide_if_zero: don't show on bar if True (default False)
pacman_log_location: location of the pacman log (default '/var/log/pacman.log')
refresh_interval: interval (in seconds) between refreshing data from package
database or AUR. Note that this module may refresh sooner than the
specified interval, if pacman log is modified since the last refresh
time. (default 3600)
Comment on lines +8 to +11
Copy link
Contributor

@lasers lasers Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't be renamed just because it gets refreshed sooner. Modules get refreshed sooner all times via clicks, py3-cmd, i3, et cetera. This now ignore people's current cache_timeout setting too.


Format placeholders:
{aur} Number of pending aur updates
Expand All @@ -28,20 +32,34 @@
aur
{'full_text': 'UPD: 15/4'}
"""
import os
import time

STRING_NOT_INSTALLED = "{} not installed"
CACHE_KEY_TEXT = "arch_updates_text"
CACHE_KEY_TIMESTAMP = "arch_updates_timestamp"


class Py3status:
""" """

# available configuration parameters
cache_timeout = 600
format = None
hide_if_zero = False
pacman_log_location = "/var/log/pacman.log"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Opinion) I'd like the module to just parse /etc/pacman.conf and grab the log location from there instead.

refresh_interval = 3600

class Meta:
deprecated = {"remove": [{"param": "include_aur", "msg": "obsolete"}]}
deprecated = {
"remove": [{"param": "include_aur", "msg": "obsolete"}],
"rename": [
{
"param": "cache_timeout",
"new": "refresh_interval",
"msg": "cache_timeout has been renamed to refresh_interval",
}
],
}

def post_config_hook(self):
helper = {
Expand Down Expand Up @@ -117,6 +135,36 @@ def _get_pikaur_updates(self):
return None if ce.error else 0

def arch_updates(self):
cached = self._get_cached_value()
if cached is not None:
return {"full_text": cached}

full_text = self._get_display_text()

self.py3.storage_set(CACHE_KEY_TEXT, full_text)
self.py3.storage_set(CACHE_KEY_TIMESTAMP, time.time())

return {"full_text": full_text}
Copy link
Contributor

@lasers lasers Oct 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not using Arch right now, but this looks like arch_updates module may be constantly updated all times now... in order to avoid running updater command. Unconfirmed, but something to think about.

The better way just may be to document users to use this hook instead with py3-cmd refresh arch_updates along with either disabled cache_timeout = -1 or longer delay cache_timeout = 3600. https://archlinux.org/pacman/alpm-hooks.5.html


def _get_cached_value(self):
text = self.py3.storage_get(CACHE_KEY_TEXT)
generated_at = self.py3.storage_get(CACHE_KEY_TIMESTAMP)

if text is None or generated_at is None:
return None

# If the log file has been updated since last refresh, the update number
# is likely no longer valid. We skip the cache here.
log_mtime = os.path.getmtime(self.pacman_log_location)
if generated_at < log_mtime:
return None

if generated_at + self.refresh_interval > time.time():
return text

return None

def _get_display_text(self):
pacman, aur, total, full_text = None, None, None, ""

if self._get_pacman_updates:
Expand All @@ -130,10 +178,7 @@ def arch_updates(self):
arch_data = {"aur": aur, "pacman": pacman, "total": total}
full_text = self.py3.safe_format(self.format, arch_data)

return {
"cached_until": self.py3.time_in(self.cache_timeout),
"full_text": full_text,
}
return full_text


if __name__ == "__main__":
Expand Down
Loading