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

« back to all changes in this revision

Viewing changes to data/hooks.py

  • Committer: mvo
  • Date: 2005-02-03 12:45:41 UTC
  • Revision ID: gustavo@niemeyer.net-20050203124541-66b00f59dd8cee24
* src/rfc822.h: added a comment explaining about the memory use
* src/hooks.c: basic i18n support added, needs more love

Show diffs side-by-side

added added

removed removed

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