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()
44
50
""" read the users config file that stores what hook files are
47
data_home = os.getenv("XDG_CONFIG_HOME", os.path.join(user.home, '.config'))
53
data_home = os.getenv("XDG_CONFIG_HOME",
54
os.path.join(user.home, '.config'))
48
55
hooks_seen = os.path.join(data_home, 'update-notifier', 'hooks_seen')
49
56
if os.path.exists(hooks_seen):
50
57
for line in open(hooks_seen):
51
58
filename, mtime, cmd_run = string.split(line)
52
if os.path.exists(self.hookDir+filename) and \
53
not self._hooks.has_key(filename):
59
if os.path.exists(self.hookDir + filename) and \
60
filename not in self._hooks:
54
61
h = self.HookFile(filename)
56
63
h.cmd_run = cmd_run
58
65
# check if file was motified since we last saw it
59
if os.stat(self.hookDir+filename).st_mtime > int(mtime):
66
if os.stat(self.hookDir + filename).st_mtime > int(mtime):
61
68
self._hooks[filename] = h
64
71
""" update hook dict with the current state on the fs """
65
72
for hook in dircache.listdir(self.hookDir):
66
if self._hooks.has_key(hook):
73
if hook in self._hooks:
67
74
# we have it already, check if it was motified since
69
76
h = self._hooks[hook]
70
if os.stat(self.hookDir+hook).st_mtime > int(h.mtime):
77
if os.stat(self.hookDir + hook).st_mtime > int(h.mtime):
73
80
self._hooks[hook] = self.HookFile(hook)
80
87
if not self._hooks[hook].seen:
81
88
new.append(self._hooks[hook])
86
93
hooks = HookFiles()
87
94
new = hooks.checkNew()
91
99
hooks = HookFiles()
92
100
new = hooks.checkNew()
93
print "Hooks: %s" % len(new)
101
print("Hooks: %s" % len(new))
94
102
for hook in hooks._hooks:
95
print hooks._hooks[hook].notification
103
print(hooks._hooks[hook].notification)
98
106
if __name__ == "__main__":