summaryrefslogtreecommitdiffstats
path: root/tests/utils/changelogparser.py
blob: 3b8f81d1bad387f3751361bbdd3833c7f5c45225 (plain)
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
# -*- coding: utf-8 -*-
"""
Why?

Converts this

GlusterFS Changelog | version: v1.1 | encoding : 2
E0b99ef11-4b79-4cd0-9730-b5a0e8c4a8c0^@4^@16877^@0^@0^@00000000-0000-0000-0000-
000000000001/dir1^@Ec5250af6-720e-4bfe-b938-827614304f39^@23^@33188^@0^@0^@0b99
ef11-4b79-4cd0-9730-b5a0e8c4a8c0/hello.txt^@Dc5250af6-720e-4bfe-b938-827614304f
39^@Dc5250af6-720e-4bfe-b938-827614304f39^@


to human readable :)

E 0b99ef11-4b79-4cd0-9730-b5a0e8c4a8c0 MKDIR 16877 0 000000000-0000-0000-0000
  -000000000001/dir1
E c5250af6-720e-4bfe-b938-827614304f39 CREATE 33188 0 0 0b99ef11-4b79-4cd0-9730
  -b5a0e8c4a8c0/hello.txt
D c5250af6-720e-4bfe-b938-827614304f39
D c5250af6-720e-4bfe-b938-827614304f39


"""
import sys
import codecs

ENTRY = 'E'
META = 'M'
DATA = 'D'
SEP = "\x00"

GF_FOP = [
    "NULL", "STAT", "READLINK", "MKNOD", "MKDIR", "UNLINK",
    "RMDIR", "SYMLINK", "RENAME", "LINK", "TRUNCATE", "OPEN",
    "READ", "WRITE", "STATFS", "FLUSH", "FSYNC", "SETXATTR",
    "GETXATTR", "REMOVEXATTR", "OPENDIR", "FSYNCDIR", "ACCESS",
    "CREATE", "FTRUNCATE", "FSTAT", "LK", "LOOKUP", "READDIR",
    "INODELK", "FINODELK", "ENTRYLK", "FENTRYLK", "XATTROP",
    "FXATTROP", "FSETXATTR", "FGETXATTR", "RCHECKSUM", "SETATTR",
    "FSETATTR", "READDIRP", "GETSPEC", "FORGET", "RELEASE",
    "RELEASEDIR", "FREMOVEXATTR", "FALLOCATE", "DISCARD", "ZEROFILL"]


class NumTokens_V11(object):
    E = 7
    M = 3
    D = 2
    NULL = 3
    MKNOD = 7
    MKDIR = 7
    UNLINK = 4
    RMDIR = 4
    SYMLINK = 4
    RENAME = 5
    LINK = 4
    SETXATTR = 3
    REMOVEXATTR = 3
    CREATE = 7
    SETATTR = 3
    FTRUNCATE = 3
    FXATTROP = 3


class NumTokens_V12(NumTokens_V11):
    UNLINK = 5
    RMDIR = 5


class Version:
    V11 = "v1.1"
    V12 = "v1.2"


class Record(object):
    def __init__(self, **kwargs):
        self.ts = kwargs.get("ts", None)
        self.fop_type = kwargs.get("fop_type", None)
        self.gfid = kwargs.get("gfid", None)
        self.path = kwargs.get("path", None)
        self.fop = kwargs.get("fop", None)
        self.path1 = kwargs.get("path1", None)
        self.path2 = kwargs.get("path2", None)
        self.mode = kwargs.get("mode", None)
        self.uid = kwargs.get("uid", None)
        self.gid = kwargs.get("gid", None)

    def create_mknod_mkdir(self, **kwargs):
        self.path = kwargs.get("path", None)
        self.fop = kwargs.get("fop", None)
        self.mode = kwargs.get("mode", None)
        self.uid = kwargs.get("uid", None)
        self.gid = kwargs.get("gid", None)

    def metadata(self, **kwargs):
        self.fop = kwargs.get("fop", None)

    def rename(self, **kwargs):
        self.fop = kwargs.get("fop", None)
        self.path1 = kwargs.get("path1", None)
        self.path2 = kwargs.get("path2", None)

    def link_symlink_unlink_rmdir(self, **kwargs):
        self.path = kwargs.get("path", None)
        self.fop = kwargs.get("fop", None)

    def __unicode__(self):
        if self.fop_type == "D":
            return u"{ts} {fop_type} {gfid}".format(**self.__dict__)
        elif self.fop_type == "M":
            return u"{ts} {fop_type} {gfid} {fop}".format(**self.__dict__)
        elif self.fop_type == "E":
            if self.fop in ["CREATE", "MKNOD", "MKDIR"]:
                return (u"{ts} {fop_type} {gfid} {fop} "
                        u"{path} {mode} {uid} {gid}".format(**self.__dict__))
            elif self.fop == "RENAME":
                return (u"{ts} {fop_type} {gfid} {fop} "
                        u"{path1} {path2}".format(**self.__dict__))
            elif self.fop in ["LINK", "SYMLINK", "UNLINK", "RMDIR"]:
                return (u"{ts} {fop_type} {gfid} {fop} "
                        u"{path}".format(**self.__dict__))
            else:
                return repr(self.__dict__)
        else:
            return repr(self.__dict__)

    def __str__(self):
        if sys.version_info >= (3,):
            return self.__unicode__()
        else:
            return unicode(self).encode('utf-8')


def get_num_tokens(data, tokens, version=Version.V11):
    if version == Version.V11:
        cls_numtokens = NumTokens_V11
    elif version == Version.V12:
        cls_numtokens = NumTokens_V12
    else:
        sys.stderr.write("Unknown Changelog Version\n")
        sys.exit(1)

    if data[tokens[0]] in [ENTRY, META]:
        if len(tokens) >= 3:
            return getattr(cls_numtokens, GF_FOP[int(data[tokens[2]])])
        else:
            return None
    else:
        return getattr(cls_numtokens, data[tokens[0]])


def process_record(data, tokens, changelog_ts, callback):
    if data[tokens[0]] in [ENTRY, META]:
        try:
            tokens[2] = GF_FOP[int(data[tokens[2]])]
        except ValueError:
            tokens[2] = "NULL"

    if not changelog_ts:
        ts1 = int(changelog_ts)
    else:
        ts1=""
    record = Record(ts=ts1, fop_type=data[tokens[0]],
                    gfid=data[tokens[1]])
    if data[tokens[0]] == META:
        record.metadata(fop=tokens[2])
    elif data[tokens[0]] == ENTRY:
        if tokens[2] in ["CREATE", "MKNOD", "MKDIR"]:
            record.create_mknod_mkdir(fop=tokens[2],
                                      path=data[tokens[6]],
                                      mode=int(data[tokens[3]]),
                                      uid=int(data[tokens[4]]),
                                      gid=int(data[tokens[5]]))
        elif tokens[2] == "RENAME":
            record.rename(fop=tokens[2],
                          path1=data[tokens[3]],
                          path2=data[tokens[4]])
        if tokens[2] in ["LINK", "SYMLINK", "UNLINK", "RMDIR"]:
            record.link_symlink_unlink_rmdir(fop=tokens[2],
                                             path=data[tokens[3]])
    callback(record)


def default_callback(record):
    sys.stdout.write(u"{0}\n".format(record))


def parse(filename, callback=default_callback):
    data = None
    tokens = []
    changelog_ts = filename.rsplit(".")[-1]
    with codecs.open(filename, mode="rb", encoding="utf-8") as f:
        # GlusterFS Changelog | version: v1.1 | encoding : 2
        header = f.readline()
        version = header.split()[4]

        data = f.readline()

        slice_start = 0
        in_record = False

        prev_char = ""
        next_char = ""
        for i, c in enumerate(data):
            next_char = ""
            if len(data) >= (i + 2):
                next_char = data[i+1]

            if not in_record and c in [ENTRY, META, DATA]:
                tokens.append(slice(slice_start, i+1))
                slice_start = i+1
                in_record = True
                continue

            if c == SEP and ((prev_char != SEP and next_char == SEP) or
                             (prev_char == SEP and next_char != SEP) or
                             (prev_char != SEP and next_char != SEP)):
                tokens.append(slice(slice_start, i))
                slice_start = i+1

                num_tokens = get_num_tokens(data, tokens, version)

                if num_tokens == len(tokens):
                    process_record(data, tokens, changelog_ts, callback)
                    in_record = False
                    tokens = []

            prev_char = c

        # process last record
        if slice_start < (len(data) - 1):
            tokens.append(slice(slice_start, len(data)))
            process_record(data, tokens, changelog_ts, callback)
            tokens = []

parse(sys.argv[1])