File: zypp-plugin.py

package info (click to toggle)
snapper 0.2.4-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 3,576 kB
  • ctags: 2,049
  • sloc: cpp: 15,605; sh: 11,424; ansic: 1,401; makefile: 287; python: 159
file content (252 lines) | stat: -rwxr-xr-x 7,340 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/python
#
# Copyright (c) [2011-2014] Novell, Inc.
#
# All Rights Reserved.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of version 2 of the GNU General Public License as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
# more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, contact Novell, Inc.
#
# To contact Novell about this file by physical or electronic mail, you may
# find current contact information at www.novell.com.
#
# Author: Arvin Schnell <aschnell@suse.de>
#


from os import readlink, getppid
from os.path import basename
import sys
import fnmatch
import re
import logging
from dbus import SystemBus, Interface, DBusException
import xml.dom.minidom as minidom
import xml.parsers.expat as expat
import json
from zypp_plugin import Plugin



class Solvable:

    def __init__(self, pattern, important):
        self.pattern = re.compile(pattern)
        self.important = important

    def __repr__(self):
        return "pattern:%s important:%s" % (self.pattern, self.important)

    def match(self, name):
        return self.pattern.match(name)



class Config:

    def __init__(self):
        self.solvables = []
        self.load_file("/etc/snapper/zypp-plugin.conf")


    def load_file(self, filename):
        try:
            self.load_dom(minidom.parse(filename))
        except IOError:
            logging.error("failed to open %s" % filename)
        except expat.ExpatError:
            logging.error("failed to parse %s" % filename)
        except:
            logging.error("failed to load %s" % filename)


    def load_dom(self, dom):
        try:
            for tmp1 in dom.getElementsByTagName("solvables"):
                for tmp2 in tmp1.getElementsByTagName("solvable"):

                    pattern = tmp2.childNodes[0].data
                    match = tmp2.getAttribute("match")
                    important = tmp2.getAttribute("important") == "true"

                    if not match in [ "w", "re" ]:
                        logging.error("unknown match attribute %s" % match)
                        continue

                    if match == "w":
                        pattern = fnmatch.translate(pattern)

                    self.solvables.append(Solvable(pattern, important))

        except:
            pass



class MyPlugin(Plugin):

    def __init__(self):
        Plugin.__init__(self)
        self.num1 = self.num2 = None
        self.description = ""
        self.cleanup = "number"
        self.userdata = {}


    def parse_userdata(self, s):
        userdata = {}
        for kv in s.split(","):
            k, v = kv.split("=", 1)
            k = k.strip()
            if not k:
                raise ValueError
            userdata[k] = v.strip()
        return userdata


    def get_userdata(self, headers):
        try:
            return self.parse_userdata(headers['userdata'])
        except KeyError:
            pass
        except ValueError:
            logging.error("invalid userdata")
        return {}


    def get_solvables(self, body, todo):
        tmp = json.loads(body)
        tsl = tmp["TransactionStepList"]
        solvables = set()
        for ts in tsl:
            if "type" in ts:
                if todo or "stage" in ts:
                    solvables.add(ts["solvable"]["n"])
        return solvables


    def match_solvables(self, names):
        found = important = False
        for name in names:
            for solvable in config.solvables:
                if solvable.match(name):
                    found = True
                    important = important or solvable.important
                    if found and important:
                        return True, True
        return found, important


    def PLUGINBEGIN(self, headers, body):

        logging.info("PLUGINBEGIN")

        logging.debug("headers: %s" % headers)

        self.description = "zypp(%s)" % basename(readlink("/proc/%d/exe" % getppid()))
        self.userdata = self.get_userdata(headers)

        self.ack()


    def COMMITBEGIN(self, headers, body):

        logging.info("COMMITBEGIN")

        solvables = self.get_solvables(body, True)
        logging.debug("solvables: %s" % solvables)

        found, important = self.match_solvables(solvables)
        logging.info("found: %s, important: %s" % (found, important))

        if found or important:

            self.userdata["important"] = "yes" if important else "no"

            try:
                logging.info("creating pre snapshot")
                self.num1 = snapper.CreatePreSnapshot("root", self.description, self.cleanup,
                                                      self.userdata)
                logging.debug("created pre snapshot %d" % self.num1)
            except DBusException as e:
                logging.error("creating snapshot failed:")
                logging.error("  %s", e)

        self.ack()


    def COMMITEND(self, headers, body):

        logging.info("COMMITEND")

        if self.num1:

            solvables = self.get_solvables(body, False)
            logging.debug("solvables: %s" % solvables)

            found, important = self.match_solvables(solvables)
            logging.info("found: %s, important: %s" % (found, important))

            if found or important:

                self.userdata["important"] = "yes" if important else "no"

                try:
                    snapper.SetSnapshot("root", self.num1, self.description, self.cleanup,
                                        self.userdata)
                except DBusException as e:
                    logging.error("setting snapshot data failed:")
                    logging.error("  %s", e)

                try:
                    logging.info("creating post snapshot")
                    self.num2 = snapper.CreatePostSnapshot("root", self.num1, "", self.cleanup,
                                                           self.userdata)
                    logging.debug("created post snapshot %d" % self.num2)
                except DBusException as e:
                    logging.error("creating snapshot failed:")
                    logging.error("  %s", e)

            else:

                try:
                    logging.info("deleting pre snapshot")
                    snapper.DeleteSnapshots("root", [ self.num1 ])
                    logging.debug("deleted pre snapshot %d" % self.num1)
                except DBusException as e:
                    logging.error("deleting snapshot failed:")
                    logging.error("  %s", e)

        self.ack()


    def PLUGINEND(self, headers, body):

        logging.info("PLUGINEND")

        self.ack()



config = Config()

try:
    bus = SystemBus()
    snapper = Interface(bus.get_object('org.opensuse.Snapper', '/org/opensuse/Snapper'),
                        dbus_interface='org.opensuse.Snapper')
except DBusException as e:
    logging.error("connect to snapperd failed:")
    logging.error("  %s", e)
    sys.exit(1)

plugin = MyPlugin()
plugin.main()