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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
|
# Copyright (C) 2005 Canonical Ltd
# 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
"""\
Routines for extracting all version information from a bzr branch.
"""
import time
import pprint
from StringIO import StringIO
from bzrlib.errors import NoWorkingTree
from bzrlib.log import show_log, log_formatter
from bzrlib.rio import RioReader, RioWriter, Stanza
from bzrlib.osutils import local_time_offset, format_date
def get_file_revisions(branch, check=False):
"""Get the last changed revision for all files.
:param branch: The branch we are checking.
:param check: See if there are uncommitted changes.
:return: ({file_path => last changed revision}, Tree_is_clean)
"""
clean = True
file_revisions = {}
basis_tree = branch.basis_tree()
for path, ie in basis_tree.inventory.iter_entries():
file_revisions[path] = ie.revision
if not check:
# Without checking, the tree looks clean
return file_revisions, clean
try:
new_tree = branch.working_tree()
except NoWorkingTree:
# Without a working tree, everything is clean
return file_revisions, clean
from bzrlib.diff import compare_trees
delta = compare_trees(basis_tree, new_tree, want_unchanged=False)
# Using a 2-pass algorithm for renames. This is because you might have
# renamed something out of the way, and then created a new file
# in which case we would rather see the new marker
# Or you might have removed the target, and then renamed
# in which case we would rather see the renamed marker
for old_path, new_path, file_id, kind, text_mod, meta_mod in delta.renamed:
clean = False
file_revisions[old_path] = u'renamed to %s' % (new_path,)
for path, file_id, kind in delta.removed:
clean = False
file_revisions[path] = 'removed'
for path, file_id, kind in delta.added:
clean = False
file_revisions[path] = 'new'
for old_path, new_path, file_id, kind, text_mod, meta_mod in delta.renamed:
clean = False
file_revisions[new_path] = u'renamed from %s' % (old_path,)
for path, file_id, kind, text_mod, meta_mod in delta.modified:
clean = False
file_revisions[path] = 'modified'
for info in new_tree.list_files():
path, status = info[0:2]
if status == '?':
file_revisions[path] = 'unversioned'
clean = False
return file_revisions, clean
# This contains a map of format id => formatter
# None is considered the default formatter
version_formats = {}
def create_date_str(timestamp=None, offset=None):
"""Just a wrapper around format_date to provide the right format.
We don't want to use '%a' in the time string, because it is locale
dependant. We also want to force timezone original, and show_offset
Without parameters this function yields the current date in the local
time zone.
"""
if timestamp is None and offset is None:
timestamp = time.time()
offset = local_time_offset()
return format_date(timestamp, offset, date_fmt='%Y-%m-%d %H:%M:%S',
timezone='original', show_offset=True)
def generate_rio_version(branch, to_file,
check_for_clean=False,
include_revision_history=False,
include_file_revisions=False):
"""Create the version file for this project.
:param branch: The branch to write information about
:param to_file: The file to write the information
:param check_for_clean: If true, check if the branch is clean.
This can be expensive for large trees. This is also only
valid for branches with working trees.
:param include_revision_history: Write out the list of revisions, and
the commit message associated with each
:param include_file_revisions: Write out the set of last changed revision
for each file.
"""
info = Stanza()
info.add('build-date', create_date_str())
info.add('revno', str(branch.revno()))
last_rev_id = branch.last_revision()
if last_rev_id is not None:
info.add('revision_id', last_rev_id)
rev = branch.get_revision(last_rev_id)
info.add('date', create_date_str(rev.timestamp, rev.timezone))
if branch.nick is not None:
info.add('branch_nick', branch.nick)
file_revisions = {}
clean = True
if check_for_clean or include_file_revisions:
file_revisions, clean = get_file_revisions(branch, check=check_for_clean)
if check_for_clean:
if clean:
info.add('clean', 'True')
else:
info.add('clean', 'False')
if include_revision_history:
revs = branch.revision_history()
log = Stanza()
for rev_id in revs:
rev = branch.get_revision(rev_id)
log.add('id', rev_id)
log.add('message', rev.message)
sio = StringIO()
log_writer = RioWriter(to_file=sio)
log_writer.write_stanza(log)
info.add('revisions', sio.getvalue())
if include_file_revisions:
files = Stanza()
for path in sorted(file_revisions.keys()):
files.add('path', path)
files.add('revision', file_revisions[path])
sio = StringIO()
file_writer = RioWriter(to_file=sio)
file_writer.write_stanza(files)
info.add('file-revisions', sio.getvalue())
writer = RioWriter(to_file=to_file)
writer.write_stanza(info)
version_formats['rio'] = generate_rio_version
# Default format is rio
version_formats[None] = generate_rio_version
# Header and footer for the python format
_py_version_header = '''#!/usr/bin/env python
"""\\
This file is automatically generated by generate_version_info
It uses the current working tree to determine the revision.
So don't edit it. :)
"""
'''
_py_version_footer = '''
if __name__ == '__main__':
print 'revision: %(revno)d' % version_info
print 'nick: %(branch_nick)s' % version_info
print 'revision id: %(revision_id)s' % version_info
'''
def generate_python_version(branch, to_file,
check_for_clean=False,
include_revision_history=False,
include_file_revisions=False):
"""Create a python version file for this project.
:param branch: The branch to write information about
:param to_file: The file to write the information
:param check_for_clean: If true, check if the branch is clean.
This can be expensive for large trees. This is also only
valid for branches with working trees.
:param include_revision_history: Write out the list of revisions, and
the commit message associated with each
:param include_file_revisions: Write out the set of last changed revision
for each file.
"""
# TODO: jam 20051228 The python output doesn't actually need to be
# encoded, because it should only generate ascii safe output.
info = {'build-date':create_date_str()
, 'revno':branch.revno()
, 'revision_id':None
, 'branch_nick':branch.nick
, 'clean':None
, 'date':None
}
revisions = []
last_rev_id = branch.last_revision()
if last_rev_id:
rev = branch.get_revision(last_rev_id)
info['revision_id'] = last_rev_id
info['date'] = create_date_str(rev.timestamp, rev.timezone)
file_revisions = {}
clean = True
if check_for_clean or include_file_revisions:
file_revisions, clean = get_file_revisions(branch, check=check_for_clean)
if check_for_clean:
if clean:
info['clean'] = True
else:
info['clean'] = False
info_str = pprint.pformat(info)
to_file.write(_py_version_header)
to_file.write('version_info =')
to_file.write(info_str)
to_file.write('\n')
if include_revision_history:
revs = branch.revision_history()
for rev_id in revs:
rev = branch.get_revision(rev_id)
revisions.append((rev_id, rev.message))
revision_str = pprint.pformat(revisions)
to_file.write('revisions = ')
to_file.write(revision_str)
to_file.write('\n')
else:
to_file.write('revisions = {}\n\n')
if include_file_revisions:
file_rev_str = pprint.pformat(file_revisions)
to_file.write('file_revisions = ')
to_file.write(file_rev_str)
to_file.write('\n')
else:
to_file.write('file_revisions = {}\n\n')
to_file.write(_py_version_footer)
version_formats['python'] = generate_python_version
|