101
99
class Convert(object):
100
def __init__(self, base_dir):
103
102
self.converted_revs = set()
104
103
self.absent_revisions = set()
105
104
self.text_count = 0
106
105
self.revisions = {}
107
106
self.inventories = {}
112
110
def convert(self):
113
if not os.path.exists('.bzr/allow-upgrade'):
114
raise Exception, "please create .bzr/allow-upgrade to indicate consent"
111
note('starting upgrade of %s', self.base)
115
112
self._backup_control_dir()
116
113
note('starting upgrade')
117
114
note('note: upgrade will be faster if all store files are ungzipped first')
118
115
self.pb = ProgressBar()
119
if not os.path.isdir('.bzr/weaves'):
120
os.mkdir('.bzr/weaves')
116
if not os.path.isdir(self.base + '/.bzr/weaves'):
117
os.mkdir(self.base + '/.bzr/weaves')
121
118
self.inv_weave = Weave('__inventory')
122
119
self.anc_weave = Weave('__ancestry')
123
120
self.ancestries = {}
124
121
# holds in-memory weaves for all files
125
122
self.text_weaves = {}
126
self.branch = Branch('.', relax_version_check=True)
123
self.branch = Branch(self.base, relax_version_check=True)
124
if self.branch._branch_format == 5:
125
note('this branch is already in the most current format')
127
if self.branch._branch_format != 4:
128
raise BzrError("cannot upgrade from branch format %r" %
129
self.branch._branch_format)
127
130
os.remove(self.branch.controlfilename('branch-format'))
128
131
self._convert_working_inv()
129
132
rev_history = self.branch.revision_history()
155
158
def _set_new_format(self):
156
159
f = self.branch.controlfile('branch-format', 'wb')
158
161
f.write(BZR_BRANCH_FORMAT_5)
164
166
def _cleanup_spare_files(self):
169
171
assert os.path.getsize(p) == 0
171
os.remove('.bzr/allow-upgrade')
172
shutil.rmtree('.bzr/inventory-store')
173
shutil.rmtree('.bzr/text-store')
173
os.remove(self.base + '/.bzr/allow-upgrade')
174
shutil.rmtree(self.base + '/.bzr/inventory-store')
175
shutil.rmtree(self.base + '/.bzr/text-store')
176
178
def _backup_control_dir(self):
177
shutil.copytree('.bzr', '.bzr.backup')
178
note('.bzr has been backed up to .bzr.backup')
179
orig = self.base + '/.bzr'
180
backup = orig + '.backup'
181
shutil.copytree(orig, backup)
182
note('%s has been backed up to %s', orig, backup)
179
183
note('if conversion fails, you can move this directory back to .bzr')
180
184
note('if it succeeds, you can remove this directory if you wish')
190
194
def _write_all_weaves(self):
191
write_a_weave(self.inv_weave, '.bzr/inventory.weave')
192
write_a_weave(self.anc_weave, '.bzr/ancestry.weave')
195
write_a_weave(self.inv_weave, self.base + '/.bzr/inventory.weave')
196
write_a_weave(self.anc_weave, self.base + '/.bzr/ancestry.weave')
195
199
for file_id, file_weave in self.text_weaves.items():
196
200
self.pb.update('writing weave', i, len(self.text_weaves))
197
write_a_weave(file_weave, '.bzr/weaves/%s.weave' % file_id)
201
write_a_weave(file_weave, self.base + '/.bzr/weaves/%s.weave' % file_id)
203
207
def _write_all_revs(self):
204
208
"""Write all revisions out in new form."""
205
shutil.rmtree('.bzr/revision-store')
206
os.mkdir('.bzr/revision-store')
209
shutil.rmtree(self.base + '/.bzr/revision-store')
210
os.mkdir(self.base + '/.bzr/revision-store')
208
212
for i, rev_id in enumerate(self.converted_revs):
209
213
self.pb.update('write revision', i, len(self.converted_revs))
210
f = file('.bzr/revision-store/%s' % rev_id, 'wb')
214
f = file(self.base + '/.bzr/revision-store/%s' % rev_id, 'wb')
212
216
serializer_v5.write_revision(self.revisions[rev_id], f)
358
362
##mutter('import text {%s} of {%s}',
359
363
## ie.text_id, file_id)
361
##mutter('text of {%s} unchanged from parent', file_id)
365
##mutter('text of {%s} unchanged from parent', file_id)
362
366
ie.text_version = file_parents[0]
367
371
def _make_order(self):
400
def profile_convert():
401
prof_f = tempfile.NamedTemporaryFile()
403
prof = hotshot.Profile(prof_f.name)
405
prof.runcall(Convert)
408
stats = hotshot.stats.load(prof_f.name)
410
stats.sort_stats('time')
411
# XXX: Might like to write to stderr or the trace file instead but
412
# print_stats seems hardcoded to stdout
413
stats.print_stats(100)
416
if __name__ == '__main__':
417
enable_default_logging()
419
if '-p' in sys.argv[1:]:
402
def upgrade(base_dir):