-
Notifications
You must be signed in to change notification settings - Fork 31
/
BzrParser.py
193 lines (155 loc) · 5.64 KB
/
BzrParser.py
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
# Copyright (C) 2007 LibreSoft
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# 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, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
# Authors :
# Carlos Garcia Campos <[email protected]>
import re
import time
import datetime
from Parser import Parser
from Repository import Commit, Action, Person
# TODO: Add debug messages
# Branches stuff
class BzrParser(Parser):
(
UNKNOWN,
COMMIT,
MESSAGE,
ADDED,
MODIFIED,
REMOVED,
RENAMED
) = range(7)
patterns = {}
patterns['commit'] = re.compile("^revno:[ \t]+(.*)$")
patterns['committer'] = re.compile("^committer:[ \t]+(.*)[ \t]+<(.*)>$")
patterns['author'] = re.compile("^author:[ \t]+(.*)[ \t]+<(.*)>$")
patterns['date'] = re.compile(
"^timestamp:.* ([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9] [0-9]+:[0-9]+:[0-9]+) ([+-][0-9][0-9][0-9][0-9])$")
patterns['message'] = re.compile("^message:$")
patterns['added'] = re.compile("^added:$")
patterns['modified'] = re.compile("^modified:$")
patterns['removed'] = re.compile("^removed:$")
patterns['renamed'] = re.compile("^renamed:$")
patterns['separator'] = re.compile("^-+$")
patterns['ignore'] = re.compile("^[ \t]+-+$")
def __init__(self):
Parser.__init__(self)
# Parser context
self.state = BzrParser.COMMIT
self.commit = None
def flush(self):
if self.commit is None:
return
self.handler.commit(self.commit)
self.commit = None
self.state = BzrParser.COMMIT
def _parse_line(self, line):
if line is None or line == '':
return
# Separator
match = self.patterns['separator'].match(line)
if match:
self.flush()
return
# Ignore details about merges
match = self.patterns['ignore'].match(line)
if match:
self.state = BzrParser.UNKNOWN
return
# Commit
match = self.patterns['commit'].match(line)
if match:
self.flush()
self.commit = Commit()
self.commit.revision = match.group(1)
self.state = BzrParser.COMMIT
return
# Committer
match = self.patterns['committer'].match(line)
if match:
self.commit.committer = Person()
self.commit.committer.name = match.group(1)
self.commit.committer.email = match.group(2)
self.handler.committer(self.commit.committer)
return
# Author
match = self.patterns['author'].match(line)
if match:
self.commit.author = Person()
self.commit.author.name = match.group(1)
self.commit.author.email = match.group(2)
self.handler.author(self.commit.author)
return
# Date
match = self.patterns['date'].match(line)
if match:
self.commit.date = datetime.datetime(*(time.strptime(match.group(1).strip(" "), "%Y-%m-%d %H:%M:%S")[0:6]))
# datetime.datetime.strptime not supported by Python2.4
#self.commit.date = datetime.datetime.strptime (match.group (1).strip (" "), "%a %b %d %H:%M:%S %Y")
return
# Message
match = self.patterns['message'].match(line)
if match:
self.state = BzrParser.MESSAGE
return
# Added files
match = self.patterns['added'].match(line)
if match:
self.state = BzrParser.ADDED
return
# Modified files
match = self.patterns['modified'].match(line)
if match:
self.state = BzrParser.MODIFIED
return
# Removed files
match = self.patterns['removed'].match(line)
if match:
self.state = BzrParser.REMOVED
return
# Renamed files
match = self.patterns['renamed'].match(line)
if match:
self.state = BzrParser.RENAMED
return
if self.state == BzrParser.MESSAGE:
self.commit.message += line.lstrip() + '\n'
elif self.state == BzrParser.ADDED or \
self.state == BzrParser.MODIFIED or \
self.state == BzrParser.REMOVED:
action = Action()
if self.state == BzrParser.ADDED:
action.type = 'A'
elif self.state == BzrParser.MODIFIED:
action.type = 'M'
elif self.state == BzrParser.REMOVED:
action.type = 'D'
action.f1 = line.strip()
self.commit.actions.append(action)
self.handler.file(action.f1)
elif self.state == BzrParser.RENAMED:
m = re.compile("^[ \t]+(.*) => (.*)$").match(line)
if not m:
return
action = Action()
action.type = 'V'
action.f1 = m.group(2)
action.f2 = m.group(1)
self.commit.actions.append(action)
self.handler.file(action.f1)
else:
self.state = BzrParser.UNKNOWN