~ubuntu-core-dev/update-notifier/ubuntu

« back to all changes in this revision

Viewing changes to data/hooks.py

  • Committer: Andrea Azzarone
  • Date: 2019-03-13 10:05:20 UTC
  • mto: This revision was merged to the branch mainline in revision 956.
  • Revision ID: azzaronea@gmail.com-20190313100520-024zyxvlpm81vgrq
Add livepatch status icons.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python3
 
2
 
 
3
from __future__ import print_function
 
4
 
 
5
from optparse import OptionParser
 
6
 
 
7
import dircache
 
8
import os.path
 
9
import string
 
10
import sys
 
11
import user
 
12
 
 
13
 
 
14
class HookFiles(object):
 
15
    """ represents all hook files """
 
16
 
 
17
    # the hooks are stored here
 
18
    hookDir = "/var/lib/update-notifier/user.d/"
 
19
 
 
20
    class HookFile(object):
 
21
        """ represents a single hook file """
 
22
        __slots__ = ["filename", "mtime", "cmd_run", "seen"]
 
23
 
 
24
        def __init__(self, filename):
 
25
            self.filename = filename
 
26
            self.mtime = os.stat(HookFiles.hookDir + filename).st_mtime
 
27
            self.cmd_run = False
 
28
            self.seen = False
 
29
 
 
30
        def summary(self):
 
31
            """ show the summary for the notification """
 
32
            # FIXME: read rfc822 style file and get the i18n version of
 
33
            # "Name"
 
34
            pass
 
35
        summary = property(summary)
 
36
 
 
37
        def description(self):
 
38
            """ show a long description for the notification """
 
39
            # FIXME: read rfc822 style file and get the i18n version of
 
40
            # "Description", convert "\n" -> " " and strstrip it afterwards
 
41
            pass
 
42
        description = property(description)
 
43
 
 
44
    def __init__(self):
 
45
        self._hooks = {}
 
46
        self._readSeenFile()
 
47
        self._update()
 
48
 
 
49
    def _readSeenFile(self):
 
50
        """ read the users config file that stores what hook files are
 
51
            already seen
 
52
        """
 
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')
 
56
        if os.path.exists(hooks_seen):
 
57
            for line in open(hooks_seen):
 
58
                filename, mtime, cmd_run = string.split(line)
 
59
                if os.path.exists(self.hookDir + filename) and \
 
60
                   filename not in self._hooks:
 
61
                    h = self.HookFile(filename)
 
62
                    h.mtime = mtime
 
63
                    h.cmd_run = cmd_run
 
64
                    h.seen = True
 
65
                    # check if file was motified since we last saw it
 
66
                    if os.stat(self.hookDir + filename).st_mtime > int(mtime):
 
67
                        h.seen = False
 
68
                    self._hooks[filename] = h
 
69
 
 
70
    def _update(self):
 
71
        """ update hook dict with the current state on the fs """
 
72
        for hook in dircache.listdir(self.hookDir):
 
73
            if hook in self._hooks:
 
74
                # we have it already, check if it was motified since
 
75
                # we last saw it
 
76
                h = self._hooks[hook]
 
77
                if os.stat(self.hookDir + hook).st_mtime > int(h.mtime):
 
78
                        h.seen = False
 
79
            else:
 
80
                self._hooks[hook] = self.HookFile(hook)
 
81
 
 
82
    def checkNew(self):
 
83
        """ return the list of unseen hook files """
 
84
        new = []
 
85
        self._update()
 
86
        for hook in self._hooks:
 
87
            if not self._hooks[hook].seen:
 
88
                new.append(self._hooks[hook])
 
89
        return new
 
90
 
 
91
 
 
92
def check():
 
93
    hooks = HookFiles()
 
94
    new = hooks.checkNew()
 
95
    return len(new)
 
96
 
 
97
 
 
98
def test():
 
99
    hooks = HookFiles()
 
100
    new = hooks.checkNew()
 
101
    print("Hooks: %s" % len(new))
 
102
    for hook in hooks._hooks:
 
103
        print(hooks._hooks[hook].notification)
 
104
 
 
105
 
 
106
if __name__ == "__main__":
 
107
    parser = OptionParser()
 
108
    parser.add_option("-c", "--check", action="store_true", dest="check",
 
109
                      default=False, help="check for new hooks")
 
110
    parser.add_option("-r", "--run", action="store_true", dest="run",
 
111
                      default=False, help="run interactive hook view")
 
112
    parser.add_option("-t", "--test", action="store_true", dest="test",
 
113
                      default=False, help="run test")
 
114
    (options, args) = parser.parse_args()
 
115
 
 
116
    if options.check:
 
117
        sys.exit(check())
 
118
    elif options.test:
 
119
        test()