| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
''' |
| 6 |
A simple wrapper to show desktop notifications while running nvchecker. |
| 7 |
''' |
| 8 |
|
| 9 |
import os |
| 10 |
import subprocess |
| 11 |
import json |
| 12 |
|
| 13 |
import gi |
| 14 |
try: |
| 15 |
gi.require_version('Notify', '0.8') |
| 16 |
except ValueError: |
| 17 |
gi.require_version('Notify', '0.7') |
| 18 |
from gi.repository import Notify |
| 19 |
|
| 20 |
def get_args(): |
| 21 |
import argparse |
| 22 |
parser = argparse.ArgumentParser(description='show desktop notifications while running nvchecker') |
| 23 |
parser.add_argument('-c', '--file', |
| 24 |
metavar='FILE', type=str, |
| 25 |
help='software version configuration file if not default') |
| 26 |
parser.add_argument('-k', '--keyfile', |
| 27 |
metavar='FILE', type=str, |
| 28 |
help='use specified keyfile (override the one in configuration file)') |
| 29 |
parser.add_argument('-t', '--tries', default=1, type=int, metavar='N', |
| 30 |
help='try N times when network errors occur') |
| 31 |
parser.add_argument('--failures', action='store_true', |
| 32 |
help='exit with code 3 if failures / errors happen during checking') |
| 33 |
|
| 34 |
return parser.parse_args() |
| 35 |
|
| 36 |
def main(): |
| 37 |
args = get_args() |
| 38 |
|
| 39 |
Notify.init('nvchecker') |
| 40 |
notif = Notify.Notification() |
| 41 |
updates = [] |
| 42 |
|
| 43 |
rfd, wfd = os.pipe() |
| 44 |
cmd = [ |
| 45 |
'nvchecker', '--logger', 'both', '--json-log-fd', str(wfd), |
| 46 |
] |
| 47 |
if args.file: |
| 48 |
cmd.extend(['-c', args.file]) |
| 49 |
if args.keyfile: |
| 50 |
cmd.extend(['-k', args.keyfile]) |
| 51 |
if args.tries: |
| 52 |
cmd.extend(['-t', str(args.tries)]) |
| 53 |
if args.failures: |
| 54 |
cmd.append('--failures') |
| 55 |
|
| 56 |
process = subprocess.Popen(cmd, pass_fds=(wfd,)) |
| 57 |
os.close(wfd) |
| 58 |
|
| 59 |
output = os.fdopen(rfd) |
| 60 |
for l in output: |
| 61 |
j = json.loads(l) |
| 62 |
event = j['event'] |
| 63 |
if event == 'updated': |
| 64 |
updates.append('%(name)s updated to version %(version)s' % j) |
| 65 |
notif.update('nvchecker', '\n'.join(updates)) |
| 66 |
notif.show() |
| 67 |
|
| 68 |
ret = process.wait() |
| 69 |
if ret != 0: |
| 70 |
raise subprocess.CalledProcessError(ret, cmd) |
| 71 |
|
| 72 |
if __name__ == '__main__': |
| 73 |
main() |