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

« back to all changes in this revision

Viewing changes to data/hooks.py

  • Committer: Michael Vogt
  • Date: 2012-01-18 08:35:33 UTC
  • Revision ID: michael.vogt@ubuntu.com-20120118083533-o80f4tfc13k9e587
add support to apply patches when running the release upgrader

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
        """
 
47
        data_home = os.getenv("XDG_CONFIG_HOME", os.path.join(user.home, '.config'))
 
48
        hooks_seen = os.path.join(data_home, 'update-notifier', 'hooks_seen')
 
49
        if os.path.exists(hooks_seen):
 
50
            for line in open(hooks_seen):
 
51
                filename, mtime, cmd_run = string.split(line)
 
52
                if os.path.exists(self.hookDir+filename) and \
 
53
                   not self._hooks.has_key(filename):
 
54
                    h = self.HookFile(filename)
 
55
                    h.mtime = mtime
 
56
                    h.cmd_run = cmd_run
 
57
                    h.seen = True
 
58
                    # check if file was motified since we last saw it
 
59
                    if os.stat(self.hookDir+filename).st_mtime > int(mtime):
 
60
                        h.seen = False
 
61
                    self._hooks[filename] = h
 
62
 
 
63
    def _update(self):
 
64
        """ update hook dict with the current state on the fs """
 
65
        for hook in dircache.listdir(self.hookDir):
 
66
            if self._hooks.has_key(hook):
 
67
                # we have it already, check if it was motified since
 
68
                # we last saw it
 
69
                h = self._hooks[hook]
 
70
                if os.stat(self.hookDir+hook).st_mtime > int(h.mtime):
 
71
                        h.seen = False
 
72
            else:
 
73
                self._hooks[hook] = self.HookFile(hook)
 
74
 
 
75
    def checkNew(self):
 
76
        """ return the list of unseen hook files """
 
77
        new = []
 
78
        self._update()
 
79
        for hook in self._hooks:
 
80
            if not self._hooks[hook].seen:
 
81
                new.append(self._hooks[hook])
 
82
        return new
 
83
    
 
84
 
 
85
def check():
 
86
    hooks = HookFiles()
 
87
    new = hooks.checkNew()
 
88
    return len(new)
 
89
 
 
90
def test():
 
91
    hooks = HookFiles()
 
92
    new = hooks.checkNew()
 
93
    print "Hooks: %s" % len(new)
 
94
    for hook in hooks._hooks:
 
95
        print hooks._hooks[hook].notification
 
96
 
 
97
 
 
98
if __name__ == "__main__":
 
99
    parser = OptionParser()
 
100
    parser.add_option("-c", "--check", action="store_true", dest="check",
 
101
                      default=False, help="check for new hooks")
 
102
    parser.add_option("-r", "--run", action="store_true", dest="run",
 
103
                      default=False, help="run interactive hook view")
 
104
    parser.add_option("-t", "--test", action="store_true", dest="test",
 
105
                      default=False, help="run test")
 
106
    (options, args) = parser.parse_args()
 
107
 
 
108
 
 
109
    if options.check:
 
110
        sys.exit(check())
 
111
    elif options.run:
 
112
        run()
 
113
    elif options.test:
 
114
        test()