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

« back to all changes in this revision

Viewing changes to data/hooks.py

  • Committer: Julian Andres Klode
  • Date: 2020-01-28 09:54:52 UTC
  • Revision ID: juliank@ubuntu.com-20200128095452-g0q3a1xg75a7k56j
Replace pep8 with pycodestyle

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