1
# Copyright (C) 2005, 2006, 2008-2011 Canonical Ltd
3
# Copyright (C) 2005 Canonical Ltd
3
5
# This program is free software; you can redistribute it and/or modify
4
6
# it under the terms of the GNU General Public License as published by
5
7
# the Free Software Foundation; either version 2 of the License, or
6
8
# (at your option) any later version.
8
10
# This program is distributed in the hope that it will be useful,
9
11
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11
13
# GNU General Public License for more details.
13
15
# You should have received a copy of the GNU General Public License
14
16
# along with this program; if not, write to the Free Software
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17
"""bzr upgrade logic."""
19
from __future__ import absolute_import
27
from bzrlib.controldir import (
31
from bzrlib.i18n import gettext
32
from bzrlib.remote import RemoteBzrDir
35
class Convert(object):
37
def __init__(self, url=None, format=None, control_dir=None):
38
"""Convert a Bazaar control directory to a given format.
40
Either the url or control_dir parameter must be given.
42
:param url: the URL of the control directory or None if the
43
control_dir is explicitly given instead
44
:param format: the format to convert to or None for the default
45
:param control_dir: the control directory or None if it is
46
specified via the URL parameter instead
49
# XXX: Change to cleanup
50
warning_id = 'cross_format_fetch'
51
saved_warning = warning_id in ui.ui_factory.suppressed_warnings
52
if url is None and control_dir is None:
54
"either the url or control_dir parameter must be set.")
55
if control_dir is not None:
56
self.bzrdir = control_dir
58
self.bzrdir = ControlDir.open_unsupported(url)
59
if isinstance(self.bzrdir, RemoteBzrDir):
60
self.bzrdir._ensure_real()
61
self.bzrdir = self.bzrdir._real_bzrdir
62
if self.bzrdir.root_transport.is_readonly():
63
raise errors.UpgradeReadonly
64
self.transport = self.bzrdir.root_transport
65
ui.ui_factory.suppressed_warnings.add(warning_id)
70
ui.ui_factory.suppressed_warnings.remove(warning_id)
74
branch = self.bzrdir.open_branch()
75
if branch.user_url != self.bzrdir.user_url:
76
ui.ui_factory.note(gettext(
77
'This is a checkout. The branch (%s) needs to be upgraded'
78
' separately.') % (urlutils.unescape_for_display(
79
branch.user_url, 'utf-8')))
81
except (errors.NotBranchError, errors.IncompatibleRepositories):
82
# might not be a format we can open without upgrading; see e.g.
83
# https://bugs.launchpad.net/bzr/+bug/253891
85
if self.format is None:
87
rich_root = self.bzrdir.find_repository()._format.rich_root_data
88
except errors.NoRepositoryPresent:
89
rich_root = False # assume no rich roots
91
format_name = "default-rich-root"
17
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19
"""Experiment in converting existing bzr branches to weaves."""
31
from bzrlib.revfile import Revfile
32
from bzrlib.weave import Weave
33
from bzrlib.weavefile import read_weave, write_weave
34
from bzrlib.progress import ProgressBar
35
from bzrlib.atomicfile import AtomicFile
38
import hotshot, hotshot.stats
42
bzrlib.trace.enable_default_logging()
50
# holds in-memory weaves for all files
53
b = bzrlib.branch.find_branch('.')
56
rev_history = b.revision_history()
61
for rev_id in rev_history:
62
pb.update('converting revision', revno, len(rev_history))
64
inv_xml = b.get_inventory_xml(rev_id).readlines()
66
new_idx = inv_weave.add(rev_id, inv_parents, inv_xml)
67
inv_parents = [new_idx]
69
tree = b.revision_tree(rev_id)
72
# for each file in the inventory, put it into its own revfile
77
if last_text_sha.get(file_id) == ie.text_sha1:
80
last_text_sha[file_id] = ie.text_sha1
82
# new text (though possibly already stored); need to store it
83
text_lines = tree.get_file(file_id).readlines()
85
# if the file's created for the first time in this
86
# revision then make a new weave; else find the old one
87
if file_id not in text_weaves:
88
text_weaves[file_id] = Weave()
90
w = text_weaves[file_id]
92
# base the new text version off whatever was last
93
# (actually it'd be better to track this, to allow for
94
# files that are deleted and then reappear)
93
format_name = "default"
94
format = format_registry.make_bzrdir(format_name)
97
if not self.bzrdir.needs_format_conversion(format):
98
raise errors.UpToDateFormat(self.bzrdir._format)
99
if not self.bzrdir.can_convert_format():
100
raise errors.BzrError(gettext("cannot upgrade from bzrdir format %s") %
102
self.bzrdir.check_conversion_target(format)
103
ui.ui_factory.note(gettext('starting upgrade of %s') %
104
urlutils.unescape_for_display(self.transport.base, 'utf-8'))
106
self.backup_oldpath, self.backup_newpath = self.bzrdir.backup_bzrdir()
107
while self.bzrdir.needs_format_conversion(format):
108
converter = self.bzrdir._format.get_converter(format)
109
self.bzrdir = converter.convert(self.bzrdir, None)
110
ui.ui_factory.note(gettext('finished'))
113
"""Clean-up after a conversion.
115
This removes the backup.bzr directory.
117
transport = self.transport
118
backup_relpath = transport.relpath(self.backup_newpath)
119
child_pb = ui.ui_factory.nested_progress_bar()
120
child_pb.update(gettext('Deleting backup.bzr'))
122
transport.delete_tree(backup_relpath)
127
def upgrade(url, format=None, clean_up=False, dry_run=False):
128
"""Upgrade locations to format.
130
This routine wraps the smart_upgrade() routine with a nicer UI.
131
In particular, it ensures all URLs can be opened before starting
132
and reports a summary at the end if more than one upgrade was attempted.
133
This routine is useful for command line tools. Other bzrlib clients
134
probably ought to use smart_upgrade() instead.
136
:param url: a URL of the locations to upgrade.
137
:param format: the format to convert to or None for the best default
138
:param clean-up: if True, the backup.bzr directory is removed if the
139
upgrade succeeded for a given repo/branch/tree
140
:param dry_run: show what would happen but don't actually do any upgrades
141
:return: the list of exceptions encountered
143
control_dirs = [ControlDir.open_unsupported(url)]
144
attempted, succeeded, exceptions = smart_upgrade(control_dirs,
145
format, clean_up=clean_up, dry_run=dry_run)
146
if len(attempted) > 1:
147
attempted_count = len(attempted)
148
succeeded_count = len(succeeded)
149
failed_count = attempted_count - succeeded_count
151
gettext('\nSUMMARY: {0} upgrades attempted, {1} succeeded,'\
152
' {2} failed').format(
153
attempted_count, succeeded_count, failed_count))
157
def smart_upgrade(control_dirs, format, clean_up=False,
159
"""Convert control directories to a new format intelligently.
161
If the control directory is a shared repository, dependent branches
162
are also converted provided the repository converted successfully.
163
If the conversion of a branch fails, remaining branches are still tried.
165
:param control_dirs: the BzrDirs to upgrade
166
:param format: the format to convert to or None for the best default
167
:param clean_up: if True, the backup.bzr directory is removed if the
168
upgrade succeeded for a given repo/branch/tree
169
:param dry_run: show what would happen but don't actually do any upgrades
170
:return: attempted-control-dirs, succeeded-control-dirs, exceptions
175
for control_dir in control_dirs:
176
attempted, succeeded, exceptions = _smart_upgrade_one(control_dir,
177
format, clean_up=clean_up, dry_run=dry_run)
178
all_attempted.extend(attempted)
179
all_succeeded.extend(succeeded)
180
all_exceptions.extend(exceptions)
181
return all_attempted, all_succeeded, all_exceptions
184
def _smart_upgrade_one(control_dir, format, clean_up=False,
186
"""Convert a control directory to a new format intelligently.
188
See smart_upgrade for parameter details.
190
# If the URL is a shared repository, find the dependent branches
193
repo = control_dir.open_repository()
194
except errors.NoRepositoryPresent:
195
# A branch or checkout using a shared repository higher up
198
# The URL is a repository. If it successfully upgrades,
199
# then upgrade the dependent branches as well.
201
dependents = repo.find_branches(using=True)
204
attempted = [control_dir]
205
succeeded, exceptions = _convert_items([control_dir], format, clean_up,
207
if succeeded and dependents:
208
ui.ui_factory.note(gettext('Found %d dependent branches - upgrading ...')
209
% (len(dependents),))
210
# Convert dependent branches
211
branch_cdirs = [b.bzrdir for b in dependents]
212
successes, problems = _convert_items(branch_cdirs, format, clean_up,
213
dry_run, label="branch")
214
attempted.extend(branch_cdirs)
215
succeeded.extend(successes)
216
exceptions.extend(problems)
219
return attempted, succeeded, exceptions
221
# FIXME: There are several problems below:
222
# - RemoteRepository doesn't support _unsupported (really ?)
223
# - raising AssertionError is rude and may not be necessary
225
# - the only caller uses only the label
226
def _get_object_and_label(control_dir):
227
"""Return the primary object and type label for a control directory.
229
:return: object, label where:
230
* object is a Branch, Repository or WorkingTree and
233
* repository - a repository
234
* tree - a lightweight checkout
238
br = control_dir.open_branch(unsupported=True,
239
ignore_fallbacks=True)
240
except NotImplementedError:
241
# RemoteRepository doesn't support the unsupported parameter
242
br = control_dir.open_branch(ignore_fallbacks=True)
243
except errors.NotBranchError:
248
repo = control_dir.open_repository()
249
except errors.NoRepositoryPresent:
252
return repo, "repository"
254
wt = control_dir.open_workingtree()
255
except (errors.NoWorkingTree, errors.NotLocalUrl):
259
raise AssertionError("unknown type of control directory %s", control_dir)
262
def _convert_items(items, format, clean_up, dry_run, label=None):
263
"""Convert a sequence of control directories to the given format.
265
:param items: the control directories to upgrade
266
:param format: the format to convert to or None for the best default
267
:param clean-up: if True, the backup.bzr directory is removed if the
268
upgrade succeeded for a given repo/branch/tree
269
:param dry_run: show what would happen but don't actually do any upgrades
270
:param label: the label for these items or None to calculate one
271
:return: items successfully upgraded, exceptions
275
child_pb = ui.ui_factory.nested_progress_bar()
276
child_pb.update(gettext('Upgrading bzrdirs'), 0, len(items))
277
for i, control_dir in enumerate(items):
279
location = control_dir.root_transport.base
280
bzr_object, bzr_label = _get_object_and_label(control_dir)
281
type_label = label or bzr_label
282
child_pb.update(gettext("Upgrading %s") % (type_label), i+1, len(items))
283
ui.ui_factory.note(gettext('Upgrading {0} {1} ...').format(type_label,
284
urlutils.unescape_for_display(location, 'utf-8'),))
287
cv = Convert(control_dir=control_dir, format=format)
288
except errors.UpToDateFormat, ex:
289
ui.ui_factory.note(str(ex))
290
succeeded.append(control_dir)
292
except Exception, ex:
293
trace.warning('conversion error: %s' % ex)
294
exceptions.append(ex)
297
# Do any required post processing
298
succeeded.append(control_dir)
301
ui.ui_factory.note(gettext('Removing backup ...'))
304
except Exception, ex:
305
trace.warning(gettext('failed to clean-up {0}: {1}') % (location, ex))
306
exceptions.append(ex)
311
return succeeded, exceptions
101
w.add(rev_id, parents, text_lines)
107
print '%6d revisions and inventories' % revno
108
print '%6d texts' % text_count
111
# TODO: commit them all atomically at the end, not one by one
112
write_atomic_weave(inv_weave, 'weaves/inventory.weave')
113
for file_id, file_weave in text_weaves.items():
114
pb.update('writing weave', i, len(text_weaves))
115
write_atomic_weave(file_weave, 'weaves/%s.weave' % file_id)
121
def write_atomic_weave(weave, filename):
122
inv_wf = AtomicFile(filename)
124
write_weave(weave, inv_wf)
132
def profile_convert():
133
prof_f = tempfile.NamedTemporaryFile()
135
prof = hotshot.Profile(prof_f.name)
137
prof.runcall(convert)
140
stats = hotshot.stats.load(prof_f.name)
142
stats.sort_stats('time')
143
## XXX: Might like to write to stderr or the trace file instead but
144
## print_stats seems hardcoded to stdout
145
stats.print_stats(20)
148
if '-p' in sys.argv[1:]: