89
# Note: RemoteBzrDirFormat is in bzrdir.py
91
class RemoteBzrDir(BzrDir, _RpcHelper):
97
# Note that RemoteBzrDirProber lives in bzrlib.bzrdir so bzrlib.remote
98
# does not have to be imported unless a remote format is involved.
100
class RemoteBzrDirFormat(_mod_bzrdir.BzrDirMetaFormat1):
101
"""Format representing bzrdirs accessed via a smart server"""
103
supports_workingtrees = False
106
_mod_bzrdir.BzrDirMetaFormat1.__init__(self)
107
# XXX: It's a bit ugly that the network name is here, because we'd
108
# like to believe that format objects are stateless or at least
109
# immutable, However, we do at least avoid mutating the name after
110
# it's returned. See <https://bugs.launchpad.net/bzr/+bug/504102>
111
self._network_name = None
114
return "%s(_network_name=%r)" % (self.__class__.__name__,
117
def get_format_description(self):
118
if self._network_name:
119
real_format = controldir.network_format_registry.get(self._network_name)
120
return 'Remote: ' + real_format.get_format_description()
121
return 'bzr remote bzrdir'
123
def get_format_string(self):
124
raise NotImplementedError(self.get_format_string)
126
def network_name(self):
127
if self._network_name:
128
return self._network_name
130
raise AssertionError("No network name set.")
132
def initialize_on_transport(self, transport):
134
# hand off the request to the smart server
135
client_medium = transport.get_smart_medium()
136
except errors.NoSmartMedium:
137
# TODO: lookup the local format from a server hint.
138
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
139
return local_dir_format.initialize_on_transport(transport)
140
client = _SmartClient(client_medium)
141
path = client.remote_path_from_transport(transport)
143
response = client.call('BzrDirFormat.initialize', path)
144
except errors.ErrorFromSmartServer, err:
145
_translate_error(err, path=path)
146
if response[0] != 'ok':
147
raise errors.SmartProtocolError('unexpected response code %s' % (response,))
148
format = RemoteBzrDirFormat()
149
self._supply_sub_formats_to(format)
150
return RemoteBzrDir(transport, format)
152
def parse_NoneTrueFalse(self, arg):
159
raise AssertionError("invalid arg %r" % arg)
161
def _serialize_NoneTrueFalse(self, arg):
168
def _serialize_NoneString(self, arg):
171
def initialize_on_transport_ex(self, transport, use_existing_dir=False,
172
create_prefix=False, force_new_repo=False, stacked_on=None,
173
stack_on_pwd=None, repo_format_name=None, make_working_trees=None,
176
# hand off the request to the smart server
177
client_medium = transport.get_smart_medium()
178
except errors.NoSmartMedium:
181
# Decline to open it if the server doesn't support our required
182
# version (3) so that the VFS-based transport will do it.
183
if client_medium.should_probe():
185
server_version = client_medium.protocol_version()
186
if server_version != '2':
190
except errors.SmartProtocolError:
191
# Apparently there's no usable smart server there, even though
192
# the medium supports the smart protocol.
197
client = _SmartClient(client_medium)
198
path = client.remote_path_from_transport(transport)
199
if client_medium._is_remote_before((1, 16)):
202
# TODO: lookup the local format from a server hint.
203
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
204
self._supply_sub_formats_to(local_dir_format)
205
return local_dir_format.initialize_on_transport_ex(transport,
206
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
207
force_new_repo=force_new_repo, stacked_on=stacked_on,
208
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
209
make_working_trees=make_working_trees, shared_repo=shared_repo,
211
return self._initialize_on_transport_ex_rpc(client, path, transport,
212
use_existing_dir, create_prefix, force_new_repo, stacked_on,
213
stack_on_pwd, repo_format_name, make_working_trees, shared_repo)
215
def _initialize_on_transport_ex_rpc(self, client, path, transport,
216
use_existing_dir, create_prefix, force_new_repo, stacked_on,
217
stack_on_pwd, repo_format_name, make_working_trees, shared_repo):
219
args.append(self._serialize_NoneTrueFalse(use_existing_dir))
220
args.append(self._serialize_NoneTrueFalse(create_prefix))
221
args.append(self._serialize_NoneTrueFalse(force_new_repo))
222
args.append(self._serialize_NoneString(stacked_on))
223
# stack_on_pwd is often/usually our transport
226
stack_on_pwd = transport.relpath(stack_on_pwd)
229
except errors.PathNotChild:
231
args.append(self._serialize_NoneString(stack_on_pwd))
232
args.append(self._serialize_NoneString(repo_format_name))
233
args.append(self._serialize_NoneTrueFalse(make_working_trees))
234
args.append(self._serialize_NoneTrueFalse(shared_repo))
235
request_network_name = self._network_name or \
236
_mod_bzrdir.BzrDirFormat.get_default_format().network_name()
238
response = client.call('BzrDirFormat.initialize_ex_1.16',
239
request_network_name, path, *args)
240
except errors.UnknownSmartMethod:
241
client._medium._remember_remote_is_before((1,16))
242
local_dir_format = _mod_bzrdir.BzrDirMetaFormat1()
243
self._supply_sub_formats_to(local_dir_format)
244
return local_dir_format.initialize_on_transport_ex(transport,
245
use_existing_dir=use_existing_dir, create_prefix=create_prefix,
246
force_new_repo=force_new_repo, stacked_on=stacked_on,
247
stack_on_pwd=stack_on_pwd, repo_format_name=repo_format_name,
248
make_working_trees=make_working_trees, shared_repo=shared_repo,
250
except errors.ErrorFromSmartServer, err:
251
_translate_error(err, path=path)
252
repo_path = response[0]
253
bzrdir_name = response[6]
254
require_stacking = response[7]
255
require_stacking = self.parse_NoneTrueFalse(require_stacking)
256
format = RemoteBzrDirFormat()
257
format._network_name = bzrdir_name
258
self._supply_sub_formats_to(format)
259
bzrdir = RemoteBzrDir(transport, format, _client=client)
261
repo_format = response_tuple_to_repo_format(response[1:])
265
repo_bzrdir_format = RemoteBzrDirFormat()
266
repo_bzrdir_format._network_name = response[5]
267
repo_bzr = RemoteBzrDir(transport.clone(repo_path),
271
final_stack = response[8] or None
272
final_stack_pwd = response[9] or None
274
final_stack_pwd = urlutils.join(
275
transport.base, final_stack_pwd)
276
remote_repo = RemoteRepository(repo_bzr, repo_format)
277
if len(response) > 10:
278
# Updated server verb that locks remotely.
279
repo_lock_token = response[10] or None
280
remote_repo.lock_write(repo_lock_token, _skip_rpc=True)
282
remote_repo.dont_leave_lock_in_place()
284
remote_repo.lock_write()
285
policy = _mod_bzrdir.UseExistingRepository(remote_repo, final_stack,
286
final_stack_pwd, require_stacking)
287
policy.acquire_repository()
291
bzrdir._format.set_branch_format(self.get_branch_format())
293
# The repo has already been created, but we need to make sure that
294
# we'll make a stackable branch.
295
bzrdir._format.require_stacking(_skip_repo=True)
296
return remote_repo, bzrdir, require_stacking, policy
298
def _open(self, transport):
299
return RemoteBzrDir(transport, self)
301
def __eq__(self, other):
302
if not isinstance(other, RemoteBzrDirFormat):
304
return self.get_format_description() == other.get_format_description()
306
def __return_repository_format(self):
307
# Always return a RemoteRepositoryFormat object, but if a specific bzr
308
# repository format has been asked for, tell the RemoteRepositoryFormat
309
# that it should use that for init() etc.
310
result = RemoteRepositoryFormat()
311
custom_format = getattr(self, '_repository_format', None)
313
if isinstance(custom_format, RemoteRepositoryFormat):
316
# We will use the custom format to create repositories over the
317
# wire; expose its details like rich_root_data for code to
319
result._custom_format = custom_format
322
def get_branch_format(self):
323
result = _mod_bzrdir.BzrDirMetaFormat1.get_branch_format(self)
324
if not isinstance(result, RemoteBranchFormat):
325
new_result = RemoteBranchFormat()
326
new_result._custom_format = result
328
self.set_branch_format(new_result)
332
repository_format = property(__return_repository_format,
333
_mod_bzrdir.BzrDirMetaFormat1._set_repository_format) #.im_func)
336
class RemoteControlStore(config.IniFileStore):
337
"""Control store which attempts to use HPSS calls to retrieve control store.
339
Note that this is specific to bzr-based formats.
342
def __init__(self, bzrdir):
343
super(RemoteControlStore, self).__init__()
345
self._real_store = None
347
def lock_write(self, token=None):
349
return self._real_store.lock_write(token)
353
return self._real_store.unlock()
357
# We need to be able to override the undecorated implementation
358
self.save_without_locking()
360
def save_without_locking(self):
361
super(RemoteControlStore, self).save()
363
def _ensure_real(self):
364
self.bzrdir._ensure_real()
365
if self._real_store is None:
366
self._real_store = config.ControlStore(self.bzrdir)
368
def external_url(self):
369
return self.bzrdir.user_url
371
def _load_content(self):
372
medium = self.bzrdir._client._medium
373
path = self.bzrdir._path_for_remote_call(self.bzrdir._client)
375
response, handler = self.bzrdir._call_expecting_body(
376
'BzrDir.get_config_file', path)
377
except errors.UnknownSmartMethod:
379
return self._real_store._load_content()
380
if len(response) and response[0] != 'ok':
381
raise errors.UnexpectedSmartServerResponse(response)
382
return handler.read_body_bytes()
384
def _save_content(self, content):
385
# FIXME JRV 2011-11-22: Ideally this should use a
386
# HPSS call too, but at the moment it is not possible
387
# to write lock control directories.
389
return self._real_store._save_content(content)
392
class RemoteControlStack(config._CompatibleStack):
393
"""Remote control-only options stack."""
395
def __init__(self, bzrdir):
396
cstore = RemoteControlStore(bzrdir)
397
super(RemoteControlStack, self).__init__(
398
[cstore.get_sections],
403
class RemoteBzrDir(_mod_bzrdir.BzrDir, _RpcHelper):
92
404
"""Control directory on a remote server, accessed via bzr:// or similar."""
94
406
def __init__(self, transport, format, _client=None, _force_probe=False):
2071
2505
if isinstance(a_bzrdir, RemoteBzrDir):
2072
2506
a_bzrdir._ensure_real()
2073
2507
result = self._custom_format.initialize(a_bzrdir._real_bzrdir,
2508
name, append_revisions_only=append_revisions_only)
2076
2510
# We assume the bzrdir is parameterised; it may not be.
2077
result = self._custom_format.initialize(a_bzrdir, name)
2511
result = self._custom_format.initialize(a_bzrdir, name,
2512
append_revisions_only=append_revisions_only)
2078
2513
if (isinstance(a_bzrdir, RemoteBzrDir) and
2079
2514
not isinstance(result, RemoteBranch)):
2080
2515
result = RemoteBranch(a_bzrdir, a_bzrdir.find_repository(), result,
2084
def initialize(self, a_bzrdir, name=None):
2519
def initialize(self, a_bzrdir, name=None, repository=None,
2520
append_revisions_only=None):
2085
2521
# 1) get the network name to use.
2086
2522
if self._custom_format:
2087
2523
network_name = self._custom_format.network_name()
2089
2525
# Select the current bzrlib default and ask for that.
2090
reference_bzrdir_format = bzrdir.format_registry.get('default')()
2526
reference_bzrdir_format = _mod_bzrdir.format_registry.get('default')()
2091
2527
reference_format = reference_bzrdir_format.get_branch_format()
2092
2528
self._custom_format = reference_format
2093
2529
network_name = reference_format.network_name()
2094
2530
# Being asked to create on a non RemoteBzrDir:
2095
2531
if not isinstance(a_bzrdir, RemoteBzrDir):
2096
return self._vfs_initialize(a_bzrdir, name=name)
2532
return self._vfs_initialize(a_bzrdir, name=name,
2533
append_revisions_only=append_revisions_only)
2097
2534
medium = a_bzrdir._client._medium
2098
2535
if medium._is_remote_before((1, 13)):
2099
return self._vfs_initialize(a_bzrdir, name=name)
2536
return self._vfs_initialize(a_bzrdir, name=name,
2537
append_revisions_only=append_revisions_only)
2100
2538
# Creating on a remote bzr dir.
2101
2539
# 2) try direct creation via RPC
2102
2540
path = a_bzrdir._path_for_remote_call(a_bzrdir._client)
2148
2601
self._ensure_real()
2149
2602
return self._custom_format.supports_set_append_revisions_only()
2604
def _use_default_local_heads_to_fetch(self):
2605
# If the branch format is a metadir format *and* its heads_to_fetch
2606
# implementation is not overridden vs the base class, we can use the
2607
# base class logic rather than use the heads_to_fetch RPC. This is
2608
# usually cheaper in terms of net round trips, as the last-revision and
2609
# tags info fetched is cached and would be fetched anyway.
2611
if isinstance(self._custom_format, branch.BranchFormatMetadir):
2612
branch_class = self._custom_format._branch_class()
2613
heads_to_fetch_impl = branch_class.heads_to_fetch.im_func
2614
if heads_to_fetch_impl is branch.Branch.heads_to_fetch.im_func:
2619
class RemoteBranchStore(config.IniFileStore):
2620
"""Branch store which attempts to use HPSS calls to retrieve branch store.
2622
Note that this is specific to bzr-based formats.
2625
def __init__(self, branch):
2626
super(RemoteBranchStore, self).__init__()
2627
self.branch = branch
2629
self._real_store = None
2631
def lock_write(self, token=None):
2632
return self.branch.lock_write(token)
2635
return self.branch.unlock()
2639
# We need to be able to override the undecorated implementation
2640
self.save_without_locking()
2642
def save_without_locking(self):
2643
super(RemoteBranchStore, self).save()
2645
def external_url(self):
2646
return self.branch.user_url
2648
def _load_content(self):
2649
path = self.branch._remote_path()
2651
response, handler = self.branch._call_expecting_body(
2652
'Branch.get_config_file', path)
2653
except errors.UnknownSmartMethod:
2655
return self._real_store._load_content()
2656
if len(response) and response[0] != 'ok':
2657
raise errors.UnexpectedSmartServerResponse(response)
2658
return handler.read_body_bytes()
2660
def _save_content(self, content):
2661
path = self.branch._remote_path()
2663
response = self.branch._call_with_body_bytes(
2664
'Branch.put_config_file', (path,
2665
self.branch._lock_token, self.branch._repo_lock_token),
2667
except errors.UnknownSmartMethod:
2669
return self._real_store._save_content(content)
2670
if response != ('ok', ):
2671
raise errors.UnexpectedSmartServerResponse(response)
2673
def _ensure_real(self):
2674
self.branch._ensure_real()
2675
if self._real_store is None:
2676
self._real_store = config.BranchStore(self.branch)
2679
class RemoteBranchStack(config._CompatibleStack):
2680
"""Remote branch-only options stack."""
2682
def __init__(self, branch):
2683
bstore = RemoteBranchStore(branch)
2684
super(RemoteBranchStack, self).__init__(
2685
[bstore.get_sections],
2687
self.branch = branch
2152
2690
class RemoteBranch(branch.Branch, _RpcHelper, lock._RelockDebugMixin):
2153
2691
"""Branch stored on a server accessed by HPSS RPC.
2774
3393
medium = self._branch._client._medium
2775
3394
if medium._is_remote_before((1, 14)):
2776
3395
return self._vfs_set_option(value, name, section)
3396
if isinstance(value, dict):
3397
if medium._is_remote_before((2, 2)):
3398
return self._vfs_set_option(value, name, section)
3399
return self._set_config_option_dict(value, name, section)
3401
return self._set_config_option(value, name, section)
3403
def _set_config_option(self, value, name, section):
2778
3405
path = self._branch._remote_path()
2779
3406
response = self._branch._client.call('Branch.set_config_option',
2780
3407
path, self._branch._lock_token, self._branch._repo_lock_token,
2781
3408
value.encode('utf8'), name, section or '')
2782
3409
except errors.UnknownSmartMethod:
3410
medium = self._branch._client._medium
2783
3411
medium._remember_remote_is_before((1, 14))
2784
3412
return self._vfs_set_option(value, name, section)
2785
3413
if response != ():
2786
3414
raise errors.UnexpectedSmartServerResponse(response)
3416
def _serialize_option_dict(self, option_dict):
3418
for key, value in option_dict.items():
3419
if isinstance(key, unicode):
3420
key = key.encode('utf8')
3421
if isinstance(value, unicode):
3422
value = value.encode('utf8')
3423
utf8_dict[key] = value
3424
return bencode.bencode(utf8_dict)
3426
def _set_config_option_dict(self, value, name, section):
3428
path = self._branch._remote_path()
3429
serialised_dict = self._serialize_option_dict(value)
3430
response = self._branch._client.call(
3431
'Branch.set_config_option_dict',
3432
path, self._branch._lock_token, self._branch._repo_lock_token,
3433
serialised_dict, name, section or '')
3434
except errors.UnknownSmartMethod:
3435
medium = self._branch._client._medium
3436
medium._remember_remote_is_before((2, 2))
3437
return self._vfs_set_option(value, name, section)
3439
raise errors.UnexpectedSmartServerResponse(response)
2788
3441
def _real_object(self):
2789
3442
self._branch._ensure_real()
2790
3443
return self._branch._real_branch