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

« back to all changes in this revision

Viewing changes to data/hooks.py

  • Committer: dann frazier
  • Date: 2020-02-04 02:27:07 UTC
  • Revision ID: dannf@ubuntu.com-20200204022707-37vu1ltkpx2m2t5x
tests/test_motd.py: Fix cut & paste error in comment.

Show diffs side-by-side

added added

removed removed

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