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

858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
1
#!/usr/bin/python3
2
3
from __future__ import print_function
159 by mvo
* data/hooks.py:
4
5
from optparse import OptionParser
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
6
7
import dircache
8
import os.path
9
import string
10
import sys
11
import user
12
13
159 by mvo
* data/hooks.py:
14
class HookFiles(object):
15
    """ represents all hook files """
16
17
    # the hooks are stored here
18
    hookDir = "/var/lib/update-notifier/user.d/"
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
19
159 by mvo
* data/hooks.py:
20
    class HookFile(object):
21
        """ represents a single hook file """
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
22
        __slots__ = ["filename", "mtime", "cmd_run", "seen"]
23
159 by mvo
* data/hooks.py:
24
        def __init__(self, filename):
25
            self.filename = filename
888 by Brian Murray
releasing package update-notifier version 3.173
26
            self.mtime = os.stat(HookFiles.hookDir + filename).st_mtime
159 by mvo
* data/hooks.py:
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"
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
34
            pass
159 by mvo
* data/hooks.py:
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
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
41
            pass
159 by mvo
* data/hooks.py:
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
591 by Michael Vogt
apply xdg patches by Sebastian Geiger, many thanks
51
            already seen
52
        """
888 by Brian Murray
releasing package update-notifier version 3.173
53
        data_home = os.getenv("XDG_CONFIG_HOME",
54
                              os.path.join(user.home, '.config'))
591 by Michael Vogt
apply xdg patches by Sebastian Geiger, many thanks
55
        hooks_seen = os.path.join(data_home, 'update-notifier', 'hooks_seen')
159 by mvo
* data/hooks.py:
56
        if os.path.exists(hooks_seen):
57
            for line in open(hooks_seen):
58
                filename, mtime, cmd_run = string.split(line)
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
59
                if os.path.exists(self.hookDir + filename) and \
728 by Dmitrijs Ledkovs
replace dict.has_key with key in dict (LP: #1040380)
60
                   filename not in self._hooks:
159 by mvo
* data/hooks.py:
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
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
66
                    if os.stat(self.hookDir + filename).st_mtime > int(mtime):
159 by mvo
* data/hooks.py:
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):
728 by Dmitrijs Ledkovs
replace dict.has_key with key in dict (LP: #1040380)
73
            if hook in self._hooks:
159 by mvo
* data/hooks.py:
74
                # we have it already, check if it was motified since
75
                # we last saw it
76
                h = self._hooks[hook]
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
77
                if os.stat(self.hookDir + hook).st_mtime > int(h.mtime):
972 by Gianfranco Costamagna
Fix E117 over-indented pep issues.
78
                    h.seen = False
159 by mvo
* data/hooks.py:
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
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
90
159 by mvo
* data/hooks.py:
91
92
def check():
93
    hooks = HookFiles()
94
    new = hooks.checkNew()
95
    return len(new)
96
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
97
159 by mvo
* data/hooks.py:
98
def test():
99
    hooks = HookFiles()
100
    new = hooks.checkNew()
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
101
    print("Hooks: %s" % len(new))
159 by mvo
* data/hooks.py:
102
    for hook in hooks._hooks:
858 by Steve Langasek
Migrate fully from python2 to python3 now that python3-debian is
103
        print(hooks._hooks[hook].notification)
159 by mvo
* data/hooks.py:
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()