2020-10-06 03:54:30 +00:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# MIT licensed
|
2022-07-16 07:48:38 +00:00
|
|
|
# Copyright (c) 2020,2022 lilydjwg <lilydjwg@gmail.com>, et al.
|
2020-10-06 03:54:30 +00:00
|
|
|
|
|
|
|
'''
|
|
|
|
A simple wrapper to show desktop notifications while running nvchecker.
|
|
|
|
'''
|
|
|
|
|
|
|
|
import os
|
|
|
|
import subprocess
|
|
|
|
import json
|
|
|
|
|
|
|
|
import gi
|
2022-07-16 07:48:38 +00:00
|
|
|
try:
|
|
|
|
gi.require_version('Notify', '0.8')
|
|
|
|
except ValueError:
|
|
|
|
gi.require_version('Notify', '0.7')
|
2020-10-06 03:54:30 +00:00
|
|
|
from gi.repository import Notify
|
|
|
|
|
|
|
|
def get_args():
|
|
|
|
import argparse
|
|
|
|
parser = argparse.ArgumentParser(description='show desktop notifications while running nvchecker')
|
|
|
|
parser.add_argument('-c', '--file',
|
|
|
|
metavar='FILE', type=str,
|
|
|
|
help='software version configuration file if not default')
|
2020-10-09 11:11:12 +00:00
|
|
|
parser.add_argument('-k', '--keyfile',
|
|
|
|
metavar='FILE', type=str,
|
|
|
|
help='use specified keyfile (override the one in configuration file)')
|
2023-10-03 15:20:32 +00:00
|
|
|
parser.add_argument('-t', '--tries', default=1, type=int, metavar='N',
|
|
|
|
help='try N times when network errors occur')
|
|
|
|
parser.add_argument('--failures', action='store_true',
|
|
|
|
help='exit with code 3 if failures / errors happen during checking')
|
|
|
|
|
2020-10-06 03:54:30 +00:00
|
|
|
return parser.parse_args()
|
|
|
|
|
|
|
|
def main():
|
|
|
|
args = get_args()
|
|
|
|
|
|
|
|
Notify.init('nvchecker')
|
|
|
|
notif = Notify.Notification()
|
|
|
|
updates = []
|
|
|
|
|
|
|
|
rfd, wfd = os.pipe()
|
|
|
|
cmd = [
|
|
|
|
'nvchecker', '--logger', 'both', '--json-log-fd', str(wfd),
|
|
|
|
]
|
|
|
|
if args.file:
|
|
|
|
cmd.extend(['-c', args.file])
|
2020-10-09 11:11:12 +00:00
|
|
|
if args.keyfile:
|
|
|
|
cmd.extend(['-k', args.keyfile])
|
2023-10-03 15:20:32 +00:00
|
|
|
if args.tries:
|
|
|
|
cmd.extend(['-t', str(args.tries)])
|
|
|
|
if args.failures:
|
|
|
|
cmd.append('--failures')
|
2020-10-06 03:54:30 +00:00
|
|
|
|
|
|
|
process = subprocess.Popen(cmd, pass_fds=(wfd,))
|
|
|
|
os.close(wfd)
|
|
|
|
|
|
|
|
output = os.fdopen(rfd)
|
|
|
|
for l in output:
|
|
|
|
j = json.loads(l)
|
|
|
|
event = j['event']
|
|
|
|
if event == 'updated':
|
|
|
|
updates.append('%(name)s updated to version %(version)s' % j)
|
|
|
|
notif.update('nvchecker', '\n'.join(updates))
|
|
|
|
notif.show()
|
|
|
|
|
|
|
|
ret = process.wait()
|
|
|
|
if ret != 0:
|
|
|
|
raise subprocess.CalledProcessError(ret, cmd)
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|