~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/upgrade.py

  • Committer: Martin Pool
  • Date: 2005-09-22 11:57:15 UTC
  • Revision ID: mbp@sourcefrog.net-20050922115715-5c756ae94451c66f
- run conversion to weaves from the 'bzr upgrade' command

Show diffs side-by-side

added added

removed removed

Lines of Context:
67
67
# the moment saves us having to worry about when files need new
68
68
# versions.
69
69
 
70
 
# TODO: Check that the working directory is clean before converting
71
 
 
72
70
 
73
71
if False:
74
72
    try:
99
97
 
100
98
 
101
99
class Convert(object):
102
 
    def __init__(self):
 
100
    def __init__(self, base_dir):
 
101
        self.base = 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 = {}
108
107
        self.convert()
109
 
        
110
108
 
111
109
 
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')
 
126
            return
 
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()
154
157
 
155
158
    def _set_new_format(self):
156
159
        f = self.branch.controlfile('branch-format', 'wb')
157
 
        try:
 
160
        try:
158
161
            f.write(BZR_BRANCH_FORMAT_5)
159
162
        finally:
160
163
            f.close()
161
 
                
162
164
 
163
165
 
164
166
    def _cleanup_spare_files(self):
168
170
                continue
169
171
            assert os.path.getsize(p) == 0
170
172
            os.remove(p)
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')
174
176
 
175
177
 
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')
181
185
 
188
192
 
189
193
 
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')
193
197
        i = 0
194
198
        try:
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)
198
202
                i += 1
199
203
        finally:
200
204
            self.pb.clear()
202
206
 
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')
207
211
        try:
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')
211
215
                try:
212
216
                    serializer_v5.write_revision(self.revisions[rev_id], f)
213
217
                finally:
358
362
            ##mutter('import text {%s} of {%s}',
359
363
            ##       ie.text_id, file_id)
360
364
        else:
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]
363
367
        del ie.text_id
364
 
                   
 
368
 
365
369
 
366
370
 
367
371
    def _make_order(self):
385
389
                    todo.remove(rev_id)
386
390
                    done.add(rev_id)
387
391
        return o
388
 
                
 
392
 
389
393
 
390
394
def write_a_weave(weave, filename):
391
395
    inv_wf = file(filename, 'wb')
394
398
    finally:
395
399
        inv_wf.close()
396
400
 
397
 
    
398
 
 
399
 
 
400
 
def profile_convert(): 
401
 
    prof_f = tempfile.NamedTemporaryFile()
402
 
 
403
 
    prof = hotshot.Profile(prof_f.name)
404
 
 
405
 
    prof.runcall(Convert) 
406
 
    prof.close()
407
 
 
408
 
    stats = hotshot.stats.load(prof_f.name)
409
 
    ##stats.strip_dirs()
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)
414
 
 
415
 
 
416
 
if __name__ == '__main__':
417
 
    enable_default_logging()
418
 
 
419
 
    if '-p' in sys.argv[1:]:
420
 
        profile_convert()
421
 
    else:
422
 
        Convert()
 
401
 
 
402
def upgrade(base_dir):
 
403
    Convert(base_dir)