3
from __future__ import print_function
3
5
from optparse import OptionParser
4
import user, string, dircache, sys, os.path
7
14
class HookFiles(object):
8
15
""" represents all hook files """
10
17
# the hooks are stored here
11
18
hookDir = "/var/lib/update-notifier/user.d/"
13
20
class HookFile(object):
14
21
""" represents a single hook file """
15
__slots__ = [ "filename", "mtime", "cmd_run", "seen" ]
22
__slots__ = ["filename", "mtime", "cmd_run", "seen"]
17
24
def __init__(self, filename):
18
25
self.filename = filename
19
self.mtime = os.stat(HookFiles.hookDir+filename).st_mtime
26
self.mtime = os.stat(HookFiles.hookDir + filename).st_mtime
20
27
self.cmd_run = False
24
31
""" show the summary for the notification """
25
32
# FIXME: read rfc822 style file and get the i18n version of
28
35
summary = property(summary)
30
37
def description(self):
31
38
""" show a long description for the notification """
32
39
# FIXME: read rfc822 style file and get the i18n version of
33
40
# "Description", convert "\n" -> " " and strstrip it afterwards
35
42
description = property(description)
38
44
def __init__(self):
40
46
self._readSeenFile()
43
49
def _readSeenFile(self):
44
50
""" read the users config file that stores what hook files are
46
hooks_seen = user.home+"/.update-notifier/hooks_seen"
53
data_home = os.getenv("XDG_CONFIG_HOME",
54
os.path.join(user.home, '.config'))
55
hooks_seen = os.path.join(data_home, 'update-notifier', 'hooks_seen')
47
56
if os.path.exists(hooks_seen):
48
57
for line in open(hooks_seen):
49
58
filename, mtime, cmd_run = string.split(line)
50
if os.path.exists(self.hookDir+filename) and \
51
not self._hooks.has_key(filename):
59
if os.path.exists(self.hookDir + filename) and \
60
filename not in self._hooks:
52
61
h = self.HookFile(filename)
54
63
h.cmd_run = cmd_run
56
65
# check if file was motified since we last saw it
57
if os.stat(self.hookDir+filename).st_mtime > int(mtime):
66
if os.stat(self.hookDir + filename).st_mtime > int(mtime):
59
68
self._hooks[filename] = h
62
71
""" update hook dict with the current state on the fs """
63
72
for hook in dircache.listdir(self.hookDir):
64
if self._hooks.has_key(hook):
73
if hook in self._hooks:
65
74
# we have it already, check if it was motified since
67
76
h = self._hooks[hook]
68
if os.stat(self.hookDir+hook).st_mtime > int(h.mtime):
77
if os.stat(self.hookDir + hook).st_mtime > int(h.mtime):
71
80
self._hooks[hook] = self.HookFile(hook)
78
87
if not self._hooks[hook].seen:
79
88
new.append(self._hooks[hook])
84
93
hooks = HookFiles()
85
94
new = hooks.checkNew()
89
99
hooks = HookFiles()
90
100
new = hooks.checkNew()
91
print "Hooks: %s" % len(new)
101
print("Hooks: %s" % len(new))
92
102
for hook in hooks._hooks:
93
print hooks._hooks[hook].notification
103
print(hooks._hooks[hook].notification)
96
106
if __name__ == "__main__":