/brz/remove-bazaar

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/brz/remove-bazaar

« back to all changes in this revision

Viewing changes to bzrlib/remote.py

  • Committer: Andrew Bennetts
  • Date: 2008-11-20 10:30:56 UTC
  • mfrom: (3695.2.6 hpss-push-rpc)
  • mto: This revision was merged to the branch mainline in revision 3981.
  • Revision ID: andrew.bennetts@canonical.com-20081120103056-05g6c6nv30ceyxdx
Merge RemoteVersionedFiles class from hpss-push-rpc.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2006, 2007, 2008 Canonical Ltd
 
2
#
 
3
# This program is free software; you can redistribute it and/or modify
 
4
# it under the terms of the GNU General Public License as published by
 
5
# the Free Software Foundation; either version 2 of the License, or
 
6
# (at your option) any later version.
 
7
#
 
8
# This program is distributed in the hope that it will be useful,
 
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
11
# GNU General Public License for more details.
 
12
#
 
13
# You should have received a copy of the GNU General Public License
 
14
# along with this program; if not, write to the Free Software
 
15
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
16
 
 
17
# TODO: At some point, handle upgrades by just passing the whole request
 
18
# across to run on the server.
 
19
 
 
20
import bz2
 
21
from cStringIO import StringIO
 
22
 
 
23
from bzrlib import (
 
24
    branch,
 
25
    debug,
 
26
    errors,
 
27
    graph,
 
28
    lockdir,
 
29
    repository,
 
30
    revision,
 
31
    symbol_versioning,
 
32
    urlutils,
 
33
)
 
34
from bzrlib.branch import BranchReferenceFormat
 
35
from bzrlib.bzrdir import BzrDir, RemoteBzrDirFormat
 
36
from bzrlib.decorators import needs_read_lock, needs_write_lock
 
37
from bzrlib.errors import (
 
38
    NoSuchRevision,
 
39
    SmartProtocolError,
 
40
    )
 
41
from bzrlib.lockable_files import LockableFiles
 
42
from bzrlib.smart import client, vfs
 
43
from bzrlib.revision import ensure_null, NULL_REVISION
 
44
from bzrlib.trace import mutter, note, warning
 
45
from bzrlib.versionedfile import VersionedFiles
 
46
 
 
47
 
 
48
class _RpcHelper(object):
 
49
    """Mixin class that helps with issuing RPCs."""
 
50
 
 
51
    def _call(self, method, *args, **err_context):
 
52
        try:
 
53
            return self._client.call(method, *args)
 
54
        except errors.ErrorFromSmartServer, err:
 
55
            self._translate_error(err, **err_context)
 
56
        
 
57
    def _call_expecting_body(self, method, *args, **err_context):
 
58
        try:
 
59
            return self._client.call_expecting_body(method, *args)
 
60
        except errors.ErrorFromSmartServer, err:
 
61
            self._translate_error(err, **err_context)
 
62
        
 
63
    def _call_with_body_bytes_expecting_body(self, method, args, body_bytes,
 
64
                                             **err_context):
 
65
        try:
 
66
            return self._client.call_with_body_bytes_expecting_body(
 
67
                method, args, body_bytes)
 
68
        except errors.ErrorFromSmartServer, err:
 
69
            self._translate_error(err, **err_context)
 
70
        
 
71
# Note: RemoteBzrDirFormat is in bzrdir.py
 
72
 
 
73
class RemoteBzrDir(BzrDir, _RpcHelper):
 
74
    """Control directory on a remote server, accessed via bzr:// or similar."""
 
75
 
 
76
    def __init__(self, transport, _client=None):
 
77
        """Construct a RemoteBzrDir.
 
78
 
 
79
        :param _client: Private parameter for testing. Disables probing and the
 
80
            use of a real bzrdir.
 
81
        """
 
82
        BzrDir.__init__(self, transport, RemoteBzrDirFormat())
 
83
        # this object holds a delegated bzrdir that uses file-level operations
 
84
        # to talk to the other side
 
85
        self._real_bzrdir = None
 
86
 
 
87
        if _client is None:
 
88
            medium = transport.get_smart_medium()
 
89
            self._client = client._SmartClient(medium)
 
90
        else:
 
91
            self._client = _client
 
92
            return
 
93
 
 
94
        path = self._path_for_remote_call(self._client)
 
95
        response = self._call('BzrDir.open', path)
 
96
        if response not in [('yes',), ('no',)]:
 
97
            raise errors.UnexpectedSmartServerResponse(response)
 
98
        if response == ('no',):
 
99
            raise errors.NotBranchError(path=transport.base)
 
100
 
 
101
    def _ensure_real(self):
 
102
        """Ensure that there is a _real_bzrdir set.
 
103
 
 
104
        Used before calls to self._real_bzrdir.
 
105
        """
 
106
        if not self._real_bzrdir:
 
107
            self._real_bzrdir = BzrDir.open_from_transport(
 
108
                self.root_transport, _server_formats=False)
 
109
 
 
110
    def _translate_error(self, err, **context):
 
111
        _translate_error(err, bzrdir=self, **context)
 
112
 
 
113
    def cloning_metadir(self, stacked=False):
 
114
        self._ensure_real()
 
115
        return self._real_bzrdir.cloning_metadir(stacked)
 
116
 
 
117
    def create_repository(self, shared=False):
 
118
        self._ensure_real()
 
119
        self._real_bzrdir.create_repository(shared=shared)
 
120
        return self.open_repository()
 
121
 
 
122
    def destroy_repository(self):
 
123
        """See BzrDir.destroy_repository"""
 
124
        self._ensure_real()
 
125
        self._real_bzrdir.destroy_repository()
 
126
 
 
127
    def create_branch(self):
 
128
        self._ensure_real()
 
129
        real_branch = self._real_bzrdir.create_branch()
 
130
        return RemoteBranch(self, self.find_repository(), real_branch)
 
131
 
 
132
    def destroy_branch(self):
 
133
        """See BzrDir.destroy_branch"""
 
134
        self._ensure_real()
 
135
        self._real_bzrdir.destroy_branch()
 
136
 
 
137
    def create_workingtree(self, revision_id=None, from_branch=None):
 
138
        raise errors.NotLocalUrl(self.transport.base)
 
139
 
 
140
    def find_branch_format(self):
 
141
        """Find the branch 'format' for this bzrdir.
 
142
 
 
143
        This might be a synthetic object for e.g. RemoteBranch and SVN.
 
144
        """
 
145
        b = self.open_branch()
 
146
        return b._format
 
147
 
 
148
    def get_branch_reference(self):
 
149
        """See BzrDir.get_branch_reference()."""
 
150
        path = self._path_for_remote_call(self._client)
 
151
        response = self._call('BzrDir.open_branch', path)
 
152
        if response[0] == 'ok':
 
153
            if response[1] == '':
 
154
                # branch at this location.
 
155
                return None
 
156
            else:
 
157
                # a branch reference, use the existing BranchReference logic.
 
158
                return response[1]
 
159
        else:
 
160
            raise errors.UnexpectedSmartServerResponse(response)
 
161
 
 
162
    def _get_tree_branch(self):
 
163
        """See BzrDir._get_tree_branch()."""
 
164
        return None, self.open_branch()
 
165
 
 
166
    def open_branch(self, _unsupported=False):
 
167
        if _unsupported:
 
168
            raise NotImplementedError('unsupported flag support not implemented yet.')
 
169
        reference_url = self.get_branch_reference()
 
170
        if reference_url is None:
 
171
            # branch at this location.
 
172
            return RemoteBranch(self, self.find_repository())
 
173
        else:
 
174
            # a branch reference, use the existing BranchReference logic.
 
175
            format = BranchReferenceFormat()
 
176
            return format.open(self, _found=True, location=reference_url)
 
177
                
 
178
    def open_repository(self):
 
179
        path = self._path_for_remote_call(self._client)
 
180
        verb = 'BzrDir.find_repositoryV2'
 
181
        try:
 
182
            response = self._call(verb, path)
 
183
        except errors.UnknownSmartMethod:
 
184
            verb = 'BzrDir.find_repository'
 
185
            response = self._call(verb, path)
 
186
        if response[0] != 'ok':
 
187
            raise errors.UnexpectedSmartServerResponse(response)
 
188
        if verb == 'BzrDir.find_repository':
 
189
            # servers that don't support the V2 method don't support external
 
190
            # references either.
 
191
            response = response + ('no', )
 
192
        if not (len(response) == 5):
 
193
            raise SmartProtocolError('incorrect response length %s' % (response,))
 
194
        if response[1] == '':
 
195
            format = RemoteRepositoryFormat()
 
196
            format.rich_root_data = (response[2] == 'yes')
 
197
            format.supports_tree_reference = (response[3] == 'yes')
 
198
            # No wire format to check this yet.
 
199
            format.supports_external_lookups = (response[4] == 'yes')
 
200
            # Used to support creating a real format instance when needed.
 
201
            format._creating_bzrdir = self
 
202
            return RemoteRepository(self, format)
 
203
        else:
 
204
            raise errors.NoRepositoryPresent(self)
 
205
 
 
206
    def open_workingtree(self, recommend_upgrade=True):
 
207
        self._ensure_real()
 
208
        if self._real_bzrdir.has_workingtree():
 
209
            raise errors.NotLocalUrl(self.root_transport)
 
210
        else:
 
211
            raise errors.NoWorkingTree(self.root_transport.base)
 
212
 
 
213
    def _path_for_remote_call(self, client):
 
214
        """Return the path to be used for this bzrdir in a remote call."""
 
215
        return client.remote_path_from_transport(self.root_transport)
 
216
 
 
217
    def get_branch_transport(self, branch_format):
 
218
        self._ensure_real()
 
219
        return self._real_bzrdir.get_branch_transport(branch_format)
 
220
 
 
221
    def get_repository_transport(self, repository_format):
 
222
        self._ensure_real()
 
223
        return self._real_bzrdir.get_repository_transport(repository_format)
 
224
 
 
225
    def get_workingtree_transport(self, workingtree_format):
 
226
        self._ensure_real()
 
227
        return self._real_bzrdir.get_workingtree_transport(workingtree_format)
 
228
 
 
229
    def can_convert_format(self):
 
230
        """Upgrading of remote bzrdirs is not supported yet."""
 
231
        return False
 
232
 
 
233
    def needs_format_conversion(self, format=None):
 
234
        """Upgrading of remote bzrdirs is not supported yet."""
 
235
        return False
 
236
 
 
237
    def clone(self, url, revision_id=None, force_new_repo=False,
 
238
              preserve_stacking=False):
 
239
        self._ensure_real()
 
240
        return self._real_bzrdir.clone(url, revision_id=revision_id,
 
241
            force_new_repo=force_new_repo, preserve_stacking=preserve_stacking)
 
242
 
 
243
    def get_config(self):
 
244
        self._ensure_real()
 
245
        return self._real_bzrdir.get_config()
 
246
 
 
247
 
 
248
class RemoteRepositoryFormat(repository.RepositoryFormat):
 
249
    """Format for repositories accessed over a _SmartClient.
 
250
 
 
251
    Instances of this repository are represented by RemoteRepository
 
252
    instances.
 
253
 
 
254
    The RemoteRepositoryFormat is parameterized during construction
 
255
    to reflect the capabilities of the real, remote format. Specifically
 
256
    the attributes rich_root_data and supports_tree_reference are set
 
257
    on a per instance basis, and are not set (and should not be) at
 
258
    the class level.
 
259
    """
 
260
 
 
261
    _matchingbzrdir = RemoteBzrDirFormat()
 
262
 
 
263
    def initialize(self, a_bzrdir, shared=False):
 
264
        if not isinstance(a_bzrdir, RemoteBzrDir):
 
265
            prior_repo = self._creating_bzrdir.open_repository()
 
266
            prior_repo._ensure_real()
 
267
            return prior_repo._real_repository._format.initialize(
 
268
                a_bzrdir, shared=shared)
 
269
        return a_bzrdir.create_repository(shared=shared)
 
270
    
 
271
    def open(self, a_bzrdir):
 
272
        if not isinstance(a_bzrdir, RemoteBzrDir):
 
273
            raise AssertionError('%r is not a RemoteBzrDir' % (a_bzrdir,))
 
274
        return a_bzrdir.open_repository()
 
275
 
 
276
    def get_format_description(self):
 
277
        return 'bzr remote repository'
 
278
 
 
279
    def __eq__(self, other):
 
280
        return self.__class__ == other.__class__
 
281
 
 
282
    def check_conversion_target(self, target_format):
 
283
        if self.rich_root_data and not target_format.rich_root_data:
 
284
            raise errors.BadConversionTarget(
 
285
                'Does not support rich root data.', target_format)
 
286
        if (self.supports_tree_reference and
 
287
            not getattr(target_format, 'supports_tree_reference', False)):
 
288
            raise errors.BadConversionTarget(
 
289
                'Does not support nested trees', target_format)
 
290
 
 
291
 
 
292
class _UnstackedParentsProvider(object):
 
293
    """ParentsProvider for RemoteRepository that ignores stacking."""
 
294
 
 
295
    def __init__(self, remote_repository):
 
296
        self._remote_repository = remote_repository
 
297
 
 
298
    def get_parent_map(self, revision_ids):
 
299
        """See RemoteRepository.get_parent_map."""
 
300
        return self._remote_repository._get_parent_map(revision_ids)
 
301
 
 
302
 
 
303
class RemoteRepository(_RpcHelper):
 
304
    """Repository accessed over rpc.
 
305
 
 
306
    For the moment most operations are performed using local transport-backed
 
307
    Repository objects.
 
308
    """
 
309
 
 
310
    def __init__(self, remote_bzrdir, format, real_repository=None, _client=None):
 
311
        """Create a RemoteRepository instance.
 
312
        
 
313
        :param remote_bzrdir: The bzrdir hosting this repository.
 
314
        :param format: The RemoteFormat object to use.
 
315
        :param real_repository: If not None, a local implementation of the
 
316
            repository logic for the repository, usually accessing the data
 
317
            via the VFS.
 
318
        :param _client: Private testing parameter - override the smart client
 
319
            to be used by the repository.
 
320
        """
 
321
        if real_repository:
 
322
            self._real_repository = real_repository
 
323
        else:
 
324
            self._real_repository = None
 
325
        self.bzrdir = remote_bzrdir
 
326
        if _client is None:
 
327
            self._client = remote_bzrdir._client
 
328
        else:
 
329
            self._client = _client
 
330
        self._format = format
 
331
        self._lock_mode = None
 
332
        self._lock_token = None
 
333
        self._lock_count = 0
 
334
        self._leave_lock = False
 
335
        # A cache of looked up revision parent data; reset at unlock time.
 
336
        self._parents_map = None
 
337
        if 'hpss' in debug.debug_flags:
 
338
            self._requested_parents = None
 
339
        # For tests:
 
340
        # These depend on the actual remote format, so force them off for
 
341
        # maximum compatibility. XXX: In future these should depend on the
 
342
        # remote repository instance, but this is irrelevant until we perform
 
343
        # reconcile via an RPC call.
 
344
        self._reconcile_does_inventory_gc = False
 
345
        self._reconcile_fixes_text_parents = False
 
346
        self._reconcile_backsup_inventory = False
 
347
        self.base = self.bzrdir.transport.base
 
348
        # Additional places to query for data.
 
349
        self._fallback_repositories = []
 
350
        self.texts = RemoteVersionedFiles(self, 'texts')
 
351
        self.inventories = RemoteVersionedFiles(self, 'inventories')
 
352
        self.signatures = RemoteVersionedFiles(self, 'signatures')
 
353
        self.revisions = RemoteVersionedFiles(self, 'revisions')
 
354
        self._vf_objs = [
 
355
            self.texts, self.inventories, self.signatures, self.revisions]
 
356
 
 
357
    def __str__(self):
 
358
        return "%s(%s)" % (self.__class__.__name__, self.base)
 
359
 
 
360
    __repr__ = __str__
 
361
 
 
362
    def abort_write_group(self, suppress_errors=False):
 
363
        """Complete a write group on the decorated repository.
 
364
        
 
365
        Smart methods peform operations in a single step so this api
 
366
        is not really applicable except as a compatibility thunk
 
367
        for older plugins that don't use e.g. the CommitBuilder
 
368
        facility.
 
369
 
 
370
        :param suppress_errors: see Repository.abort_write_group.
 
371
        """
 
372
        self._ensure_real()
 
373
        return self._real_repository.abort_write_group(
 
374
            suppress_errors=suppress_errors)
 
375
 
 
376
    def commit_write_group(self):
 
377
        """Complete a write group on the decorated repository.
 
378
        
 
379
        Smart methods peform operations in a single step so this api
 
380
        is not really applicable except as a compatibility thunk
 
381
        for older plugins that don't use e.g. the CommitBuilder
 
382
        facility.
 
383
        """
 
384
        self._ensure_real()
 
385
        return self._real_repository.commit_write_group()
 
386
 
 
387
    def _ensure_real(self):
 
388
        """Ensure that there is a _real_repository set.
 
389
 
 
390
        Used before calls to self._real_repository.
 
391
        """
 
392
        if self._real_repository is None:
 
393
            self.bzrdir._ensure_real()
 
394
            self._set_real_repository(
 
395
                self.bzrdir._real_bzrdir.open_repository())
 
396
 
 
397
    def _translate_error(self, err, **context):
 
398
        self.bzrdir._translate_error(err, repository=self, **context)
 
399
 
 
400
    def find_text_key_references(self):
 
401
        """Find the text key references within the repository.
 
402
 
 
403
        :return: a dictionary mapping (file_id, revision_id) tuples to altered file-ids to an iterable of
 
404
        revision_ids. Each altered file-ids has the exact revision_ids that
 
405
        altered it listed explicitly.
 
406
        :return: A dictionary mapping text keys ((fileid, revision_id) tuples)
 
407
            to whether they were referred to by the inventory of the
 
408
            revision_id that they contain. The inventory texts from all present
 
409
            revision ids are assessed to generate this report.
 
410
        """
 
411
        self._ensure_real()
 
412
        return self._real_repository.find_text_key_references()
 
413
 
 
414
    def _generate_text_key_index(self):
 
415
        """Generate a new text key index for the repository.
 
416
 
 
417
        This is an expensive function that will take considerable time to run.
 
418
 
 
419
        :return: A dict mapping (file_id, revision_id) tuples to a list of
 
420
            parents, also (file_id, revision_id) tuples.
 
421
        """
 
422
        self._ensure_real()
 
423
        return self._real_repository._generate_text_key_index()
 
424
 
 
425
    @symbol_versioning.deprecated_method(symbol_versioning.one_four)
 
426
    def get_revision_graph(self, revision_id=None):
 
427
        """See Repository.get_revision_graph()."""
 
428
        return self._get_revision_graph(revision_id)
 
429
 
 
430
    def _get_revision_graph(self, revision_id):
 
431
        """Private method for using with old (< 1.2) servers to fallback."""
 
432
        if revision_id is None:
 
433
            revision_id = ''
 
434
        elif revision.is_null(revision_id):
 
435
            return {}
 
436
 
 
437
        path = self.bzrdir._path_for_remote_call(self._client)
 
438
        response = self._call_expecting_body(
 
439
            'Repository.get_revision_graph', path, revision_id)
 
440
        response_tuple, response_handler = response
 
441
        if response_tuple[0] != 'ok':
 
442
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
443
        coded = response_handler.read_body_bytes()
 
444
        if coded == '':
 
445
            # no revisions in this repository!
 
446
            return {}
 
447
        lines = coded.split('\n')
 
448
        revision_graph = {}
 
449
        for line in lines:
 
450
            d = tuple(line.split())
 
451
            revision_graph[d[0]] = d[1:]
 
452
            
 
453
        return revision_graph
 
454
 
 
455
    def has_revision(self, revision_id):
 
456
        """See Repository.has_revision()."""
 
457
        if revision_id == NULL_REVISION:
 
458
            # The null revision is always present.
 
459
            return True
 
460
        path = self.bzrdir._path_for_remote_call(self._client)
 
461
        response = self._call('Repository.has_revision', path, revision_id)
 
462
        if response[0] not in ('yes', 'no'):
 
463
            raise errors.UnexpectedSmartServerResponse(response)
 
464
        if response[0] == 'yes':
 
465
            return True
 
466
        for fallback_repo in self._fallback_repositories:
 
467
            if fallback_repo.has_revision(revision_id):
 
468
                return True
 
469
        return False
 
470
 
 
471
    def has_revisions(self, revision_ids):
 
472
        """See Repository.has_revisions()."""
 
473
        # FIXME: This does many roundtrips, particularly when there are
 
474
        # fallback repositories.  -- mbp 20080905
 
475
        result = set()
 
476
        for revision_id in revision_ids:
 
477
            if self.has_revision(revision_id):
 
478
                result.add(revision_id)
 
479
        return result
 
480
 
 
481
    def has_same_location(self, other):
 
482
        return (self.__class__ == other.__class__ and
 
483
                self.bzrdir.transport.base == other.bzrdir.transport.base)
 
484
 
 
485
    def get_graph(self, other_repository=None):
 
486
        """Return the graph for this repository format"""
 
487
        parents_provider = self._make_parents_provider()
 
488
        if (other_repository is not None and
 
489
            other_repository.bzrdir.transport.base !=
 
490
            self.bzrdir.transport.base):
 
491
            parents_provider = graph._StackedParentsProvider(
 
492
                [parents_provider, other_repository._make_parents_provider()])
 
493
        return graph.Graph(parents_provider)
 
494
 
 
495
    def gather_stats(self, revid=None, committers=None):
 
496
        """See Repository.gather_stats()."""
 
497
        path = self.bzrdir._path_for_remote_call(self._client)
 
498
        # revid can be None to indicate no revisions, not just NULL_REVISION
 
499
        if revid is None or revision.is_null(revid):
 
500
            fmt_revid = ''
 
501
        else:
 
502
            fmt_revid = revid
 
503
        if committers is None or not committers:
 
504
            fmt_committers = 'no'
 
505
        else:
 
506
            fmt_committers = 'yes'
 
507
        response_tuple, response_handler = self._call_expecting_body(
 
508
            'Repository.gather_stats', path, fmt_revid, fmt_committers)
 
509
        if response_tuple[0] != 'ok':
 
510
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
511
 
 
512
        body = response_handler.read_body_bytes()
 
513
        result = {}
 
514
        for line in body.split('\n'):
 
515
            if not line:
 
516
                continue
 
517
            key, val_text = line.split(':')
 
518
            if key in ('revisions', 'size', 'committers'):
 
519
                result[key] = int(val_text)
 
520
            elif key in ('firstrev', 'latestrev'):
 
521
                values = val_text.split(' ')[1:]
 
522
                result[key] = (float(values[0]), long(values[1]))
 
523
 
 
524
        return result
 
525
 
 
526
    def find_branches(self, using=False):
 
527
        """See Repository.find_branches()."""
 
528
        # should be an API call to the server.
 
529
        self._ensure_real()
 
530
        return self._real_repository.find_branches(using=using)
 
531
 
 
532
    def get_physical_lock_status(self):
 
533
        """See Repository.get_physical_lock_status()."""
 
534
        # should be an API call to the server.
 
535
        self._ensure_real()
 
536
        return self._real_repository.get_physical_lock_status()
 
537
 
 
538
    def is_in_write_group(self):
 
539
        """Return True if there is an open write group.
 
540
 
 
541
        write groups are only applicable locally for the smart server..
 
542
        """
 
543
        if self._real_repository:
 
544
            return self._real_repository.is_in_write_group()
 
545
 
 
546
    def is_locked(self):
 
547
        return self._lock_count >= 1
 
548
 
 
549
    def is_shared(self):
 
550
        """See Repository.is_shared()."""
 
551
        path = self.bzrdir._path_for_remote_call(self._client)
 
552
        response = self._call('Repository.is_shared', path)
 
553
        if response[0] not in ('yes', 'no'):
 
554
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
555
        return response[0] == 'yes'
 
556
 
 
557
    def is_write_locked(self):
 
558
        return self._lock_mode == 'w'
 
559
 
 
560
    def lock_read(self):
 
561
        # wrong eventually - want a local lock cache context
 
562
        if not self._lock_mode:
 
563
            self._lock_mode = 'r'
 
564
            self._lock_count = 1
 
565
            self._parents_map = {}
 
566
            for vf in self._vf_objs:
 
567
                vf._parents_map = {}
 
568
            if 'hpss' in debug.debug_flags:
 
569
                self._requested_parents = set()
 
570
            if self._real_repository is not None:
 
571
                self._real_repository.lock_read()
 
572
        else:
 
573
            self._lock_count += 1
 
574
 
 
575
    def _remote_lock_write(self, token):
 
576
        path = self.bzrdir._path_for_remote_call(self._client)
 
577
        if token is None:
 
578
            token = ''
 
579
        err_context = {'token': token}
 
580
        response = self._call('Repository.lock_write', path, token,
 
581
                              **err_context)
 
582
        if response[0] == 'ok':
 
583
            ok, token = response
 
584
            return token
 
585
        else:
 
586
            raise errors.UnexpectedSmartServerResponse(response)
 
587
 
 
588
    def lock_write(self, token=None, _skip_rpc=False):
 
589
        if not self._lock_mode:
 
590
            if _skip_rpc:
 
591
                if self._lock_token is not None:
 
592
                    if token != self._lock_token:
 
593
                        raise errors.TokenMismatch(token, self._lock_token)
 
594
                self._lock_token = token
 
595
            else:
 
596
                self._lock_token = self._remote_lock_write(token)
 
597
            # if self._lock_token is None, then this is something like packs or
 
598
            # svn where we don't get to lock the repo, or a weave style repository
 
599
            # where we cannot lock it over the wire and attempts to do so will
 
600
            # fail.
 
601
            if self._real_repository is not None:
 
602
                self._real_repository.lock_write(token=self._lock_token)
 
603
            if token is not None:
 
604
                self._leave_lock = True
 
605
            else:
 
606
                self._leave_lock = False
 
607
            self._lock_mode = 'w'
 
608
            self._lock_count = 1
 
609
            self._parents_map = {}
 
610
            for vf in self._vf_objs:
 
611
                vf._parents_map = {}
 
612
            if 'hpss' in debug.debug_flags:
 
613
                self._requested_parents = set()
 
614
        elif self._lock_mode == 'r':
 
615
            raise errors.ReadOnlyError(self)
 
616
        else:
 
617
            self._lock_count += 1
 
618
        return self._lock_token or None
 
619
 
 
620
    def leave_lock_in_place(self):
 
621
        if not self._lock_token:
 
622
            raise NotImplementedError(self.leave_lock_in_place)
 
623
        self._leave_lock = True
 
624
 
 
625
    def dont_leave_lock_in_place(self):
 
626
        if not self._lock_token:
 
627
            raise NotImplementedError(self.dont_leave_lock_in_place)
 
628
        self._leave_lock = False
 
629
 
 
630
    def _set_real_repository(self, repository):
 
631
        """Set the _real_repository for this repository.
 
632
 
 
633
        :param repository: The repository to fallback to for non-hpss
 
634
            implemented operations.
 
635
        """
 
636
        if self._real_repository is not None:
 
637
            raise AssertionError('_real_repository is already set')
 
638
        if isinstance(repository, RemoteRepository):
 
639
            raise AssertionError()
 
640
        self._real_repository = repository
 
641
        for fb in self._fallback_repositories:
 
642
            self._real_repository.add_fallback_repository(fb)
 
643
        if self._lock_mode == 'w':
 
644
            # if we are already locked, the real repository must be able to
 
645
            # acquire the lock with our token.
 
646
            self._real_repository.lock_write(self._lock_token)
 
647
        elif self._lock_mode == 'r':
 
648
            self._real_repository.lock_read()
 
649
 
 
650
    def start_write_group(self):
 
651
        """Start a write group on the decorated repository.
 
652
        
 
653
        Smart methods peform operations in a single step so this api
 
654
        is not really applicable except as a compatibility thunk
 
655
        for older plugins that don't use e.g. the CommitBuilder
 
656
        facility.
 
657
        """
 
658
        self._ensure_real()
 
659
        return self._real_repository.start_write_group()
 
660
 
 
661
    def _unlock(self, token):
 
662
        path = self.bzrdir._path_for_remote_call(self._client)
 
663
        if not token:
 
664
            # with no token the remote repository is not persistently locked.
 
665
            return
 
666
        err_context = {'token': token}
 
667
        response = self._call('Repository.unlock', path, token,
 
668
                              **err_context)
 
669
        if response == ('ok',):
 
670
            return
 
671
        else:
 
672
            raise errors.UnexpectedSmartServerResponse(response)
 
673
 
 
674
    def unlock(self):
 
675
        self._lock_count -= 1
 
676
        if self._lock_count > 0:
 
677
            return
 
678
        self._parents_map = None
 
679
        for vf in self._vf_objs:
 
680
            vf._parents_map = None
 
681
        if 'hpss' in debug.debug_flags:
 
682
            self._requested_parents = None
 
683
        old_mode = self._lock_mode
 
684
        self._lock_mode = None
 
685
        try:
 
686
            # The real repository is responsible at present for raising an
 
687
            # exception if it's in an unfinished write group.  However, it
 
688
            # normally will *not* actually remove the lock from disk - that's
 
689
            # done by the server on receiving the Repository.unlock call.
 
690
            # This is just to let the _real_repository stay up to date.
 
691
            if self._real_repository is not None:
 
692
                self._real_repository.unlock()
 
693
        finally:
 
694
            # The rpc-level lock should be released even if there was a
 
695
            # problem releasing the vfs-based lock.
 
696
            if old_mode == 'w':
 
697
                # Only write-locked repositories need to make a remote method
 
698
                # call to perfom the unlock.
 
699
                old_token = self._lock_token
 
700
                self._lock_token = None
 
701
                if not self._leave_lock:
 
702
                    self._unlock(old_token)
 
703
 
 
704
    def break_lock(self):
 
705
        # should hand off to the network
 
706
        self._ensure_real()
 
707
        return self._real_repository.break_lock()
 
708
 
 
709
    def _get_tarball(self, compression):
 
710
        """Return a TemporaryFile containing a repository tarball.
 
711
        
 
712
        Returns None if the server does not support sending tarballs.
 
713
        """
 
714
        import tempfile
 
715
        path = self.bzrdir._path_for_remote_call(self._client)
 
716
        try:
 
717
            response, protocol = self._call_expecting_body(
 
718
                'Repository.tarball', path, compression)
 
719
        except errors.UnknownSmartMethod:
 
720
            protocol.cancel_read_body()
 
721
            return None
 
722
        if response[0] == 'ok':
 
723
            # Extract the tarball and return it
 
724
            t = tempfile.NamedTemporaryFile()
 
725
            # TODO: rpc layer should read directly into it...
 
726
            t.write(protocol.read_body_bytes())
 
727
            t.seek(0)
 
728
            return t
 
729
        raise errors.UnexpectedSmartServerResponse(response)
 
730
 
 
731
    def sprout(self, to_bzrdir, revision_id=None):
 
732
        # TODO: Option to control what format is created?
 
733
        self._ensure_real()
 
734
        dest_repo = self._real_repository._format.initialize(to_bzrdir,
 
735
                                                             shared=False)
 
736
        dest_repo.fetch(self, revision_id=revision_id)
 
737
        return dest_repo
 
738
 
 
739
    ### These methods are just thin shims to the VFS object for now.
 
740
 
 
741
    def revision_tree(self, revision_id):
 
742
        self._ensure_real()
 
743
        return self._real_repository.revision_tree(revision_id)
 
744
 
 
745
    def get_serializer_format(self):
 
746
        self._ensure_real()
 
747
        return self._real_repository.get_serializer_format()
 
748
 
 
749
    def get_commit_builder(self, branch, parents, config, timestamp=None,
 
750
                           timezone=None, committer=None, revprops=None,
 
751
                           revision_id=None):
 
752
        # FIXME: It ought to be possible to call this without immediately
 
753
        # triggering _ensure_real.  For now it's the easiest thing to do.
 
754
        self._ensure_real()
 
755
        real_repo = self._real_repository
 
756
        builder = real_repo.get_commit_builder(branch, parents,
 
757
                config, timestamp=timestamp, timezone=timezone,
 
758
                committer=committer, revprops=revprops, revision_id=revision_id)
 
759
        return builder
 
760
 
 
761
    def add_fallback_repository(self, repository):
 
762
        """Add a repository to use for looking up data not held locally.
 
763
        
 
764
        :param repository: A repository.
 
765
        """
 
766
        # XXX: At the moment the RemoteRepository will allow fallbacks
 
767
        # unconditionally - however, a _real_repository will usually exist,
 
768
        # and may raise an error if it's not accommodated by the underlying
 
769
        # format.  Eventually we should check when opening the repository
 
770
        # whether it's willing to allow them or not.
 
771
        #
 
772
        # We need to accumulate additional repositories here, to pass them in
 
773
        # on various RPC's.
 
774
        self._fallback_repositories.append(repository)
 
775
        # They are also seen by the fallback repository.  If it doesn't exist
 
776
        # yet they'll be added then.  This implicitly copies them.
 
777
        self._ensure_real()
 
778
 
 
779
    def add_inventory(self, revid, inv, parents):
 
780
        self._ensure_real()
 
781
        return self._real_repository.add_inventory(revid, inv, parents)
 
782
 
 
783
    def add_revision(self, rev_id, rev, inv=None, config=None):
 
784
        self._ensure_real()
 
785
        return self._real_repository.add_revision(
 
786
            rev_id, rev, inv=inv, config=config)
 
787
 
 
788
    @needs_read_lock
 
789
    def get_inventory(self, revision_id):
 
790
        self._ensure_real()
 
791
        return self._real_repository.get_inventory(revision_id)
 
792
 
 
793
    def iter_inventories(self, revision_ids):
 
794
        self._ensure_real()
 
795
        return self._real_repository.iter_inventories(revision_ids)
 
796
 
 
797
    @needs_read_lock
 
798
    def get_revision(self, revision_id):
 
799
        self._ensure_real()
 
800
        return self._real_repository.get_revision(revision_id)
 
801
 
 
802
    def get_transaction(self):
 
803
        self._ensure_real()
 
804
        return self._real_repository.get_transaction()
 
805
 
 
806
    @needs_read_lock
 
807
    def clone(self, a_bzrdir, revision_id=None):
 
808
        self._ensure_real()
 
809
        return self._real_repository.clone(a_bzrdir, revision_id=revision_id)
 
810
 
 
811
    def make_working_trees(self):
 
812
        """See Repository.make_working_trees"""
 
813
        self._ensure_real()
 
814
        return self._real_repository.make_working_trees()
 
815
 
 
816
    def revision_ids_to_search_result(self, result_set):
 
817
        """Convert a set of revision ids to a graph SearchResult."""
 
818
        result_parents = set()
 
819
        for parents in self.get_graph().get_parent_map(
 
820
            result_set).itervalues():
 
821
            result_parents.update(parents)
 
822
        included_keys = result_set.intersection(result_parents)
 
823
        start_keys = result_set.difference(included_keys)
 
824
        exclude_keys = result_parents.difference(result_set)
 
825
        result = graph.SearchResult(start_keys, exclude_keys,
 
826
            len(result_set), result_set)
 
827
        return result
 
828
 
 
829
    @needs_read_lock
 
830
    def search_missing_revision_ids(self, other, revision_id=None, find_ghosts=True):
 
831
        """Return the revision ids that other has that this does not.
 
832
        
 
833
        These are returned in topological order.
 
834
 
 
835
        revision_id: only return revision ids included by revision_id.
 
836
        """
 
837
        return repository.InterRepository.get(
 
838
            other, self).search_missing_revision_ids(revision_id, find_ghosts)
 
839
 
 
840
    def fetch(self, source, revision_id=None, pb=None, find_ghosts=False):
 
841
        # Not delegated to _real_repository so that InterRepository.get has a
 
842
        # chance to find an InterRepository specialised for RemoteRepository.
 
843
        if self.has_same_location(source):
 
844
            # check that last_revision is in 'from' and then return a
 
845
            # no-operation.
 
846
            if (revision_id is not None and
 
847
                not revision.is_null(revision_id)):
 
848
                self.get_revision(revision_id)
 
849
            return 0, []
 
850
        inter = repository.InterRepository.get(source, self)
 
851
        try:
 
852
            return inter.fetch(revision_id=revision_id, pb=pb, find_ghosts=find_ghosts)
 
853
        except NotImplementedError:
 
854
            raise errors.IncompatibleRepositories(source, self)
 
855
 
 
856
    def create_bundle(self, target, base, fileobj, format=None):
 
857
        self._ensure_real()
 
858
        self._real_repository.create_bundle(target, base, fileobj, format)
 
859
 
 
860
    @needs_read_lock
 
861
    def get_ancestry(self, revision_id, topo_sorted=True):
 
862
        self._ensure_real()
 
863
        return self._real_repository.get_ancestry(revision_id, topo_sorted)
 
864
 
 
865
    def fileids_altered_by_revision_ids(self, revision_ids):
 
866
        self._ensure_real()
 
867
        return self._real_repository.fileids_altered_by_revision_ids(revision_ids)
 
868
 
 
869
    def _get_versioned_file_checker(self, revisions, revision_versions_cache):
 
870
        self._ensure_real()
 
871
        return self._real_repository._get_versioned_file_checker(
 
872
            revisions, revision_versions_cache)
 
873
        
 
874
    def iter_files_bytes(self, desired_files):
 
875
        """See Repository.iter_file_bytes.
 
876
        """
 
877
        self._ensure_real()
 
878
        return self._real_repository.iter_files_bytes(desired_files)
 
879
 
 
880
    @property
 
881
    def _fetch_order(self):
 
882
        """Decorate the real repository for now.
 
883
 
 
884
        In the long term getting this back from the remote repository as part
 
885
        of open would be more efficient.
 
886
        """
 
887
        self._ensure_real()
 
888
        return self._real_repository._fetch_order
 
889
 
 
890
    @property
 
891
    def _fetch_uses_deltas(self):
 
892
        """Decorate the real repository for now.
 
893
 
 
894
        In the long term getting this back from the remote repository as part
 
895
        of open would be more efficient.
 
896
        """
 
897
        self._ensure_real()
 
898
        return self._real_repository._fetch_uses_deltas
 
899
 
 
900
    @property
 
901
    def _fetch_reconcile(self):
 
902
        """Decorate the real repository for now.
 
903
 
 
904
        In the long term getting this back from the remote repository as part
 
905
        of open would be more efficient.
 
906
        """
 
907
        self._ensure_real()
 
908
        return self._real_repository._fetch_reconcile
 
909
 
 
910
    def get_parent_map(self, revision_ids):
 
911
        """See bzrlib.Graph.get_parent_map()."""
 
912
        return self._make_parents_provider().get_parent_map(revision_ids)
 
913
 
 
914
    def _get_parent_map(self, keys):
 
915
        """Implementation of get_parent_map() that ignores fallbacks."""
 
916
        # Hack to build up the caching logic.
 
917
        ancestry = self._parents_map
 
918
        if ancestry is None:
 
919
            # Repository is not locked, so there's no cache.
 
920
            missing_revisions = set(keys)
 
921
            ancestry = {}
 
922
        else:
 
923
            missing_revisions = set(key for key in keys if key not in ancestry)
 
924
        if missing_revisions:
 
925
            parent_map = self._get_parent_map_rpc(missing_revisions)
 
926
            if 'hpss' in debug.debug_flags:
 
927
                mutter('retransmitted revisions: %d of %d',
 
928
                        len(set(ancestry).intersection(parent_map)),
 
929
                        len(parent_map))
 
930
            ancestry.update(parent_map)
 
931
        present_keys = [k for k in keys if k in ancestry]
 
932
        if 'hpss' in debug.debug_flags:
 
933
            if self._requested_parents is not None and len(ancestry) != 0:
 
934
                self._requested_parents.update(present_keys)
 
935
                mutter('Current RemoteRepository graph hit rate: %d%%',
 
936
                    100.0 * len(self._requested_parents) / len(ancestry))
 
937
        return dict((k, ancestry[k]) for k in present_keys)
 
938
 
 
939
    def _get_parent_map_rpc(self, keys):
 
940
        """Helper for get_parent_map that performs the RPC."""
 
941
        medium = self._client._medium
 
942
        if medium._is_remote_before((1, 2)):
 
943
            # We already found out that the server can't understand
 
944
            # Repository.get_parent_map requests, so just fetch the whole
 
945
            # graph.
 
946
            # XXX: Note that this will issue a deprecation warning. This is ok
 
947
            # :- its because we're working with a deprecated server anyway, and
 
948
            # the user will almost certainly have seen a warning about the
 
949
            # server version already.
 
950
            rg = self.get_revision_graph()
 
951
            # There is an api discrepency between get_parent_map and
 
952
            # get_revision_graph. Specifically, a "key:()" pair in
 
953
            # get_revision_graph just means a node has no parents. For
 
954
            # "get_parent_map" it means the node is a ghost. So fix up the
 
955
            # graph to correct this.
 
956
            #   https://bugs.launchpad.net/bzr/+bug/214894
 
957
            # There is one other "bug" which is that ghosts in
 
958
            # get_revision_graph() are not returned at all. But we won't worry
 
959
            # about that for now.
 
960
            for node_id, parent_ids in rg.iteritems():
 
961
                if parent_ids == ():
 
962
                    rg[node_id] = (NULL_REVISION,)
 
963
            rg[NULL_REVISION] = ()
 
964
            return rg
 
965
 
 
966
        keys = set(keys)
 
967
        if None in keys:
 
968
            raise ValueError('get_parent_map(None) is not valid')
 
969
        if NULL_REVISION in keys:
 
970
            keys.discard(NULL_REVISION)
 
971
            found_parents = {NULL_REVISION:()}
 
972
            if not keys:
 
973
                return found_parents
 
974
        else:
 
975
            found_parents = {}
 
976
        # TODO(Needs analysis): We could assume that the keys being requested
 
977
        # from get_parent_map are in a breadth first search, so typically they
 
978
        # will all be depth N from some common parent, and we don't have to
 
979
        # have the server iterate from the root parent, but rather from the
 
980
        # keys we're searching; and just tell the server the keyspace we
 
981
        # already have; but this may be more traffic again.
 
982
 
 
983
        # Transform self._parents_map into a search request recipe.
 
984
        # TODO: Manage this incrementally to avoid covering the same path
 
985
        # repeatedly. (The server will have to on each request, but the less
 
986
        # work done the better).
 
987
        parents_map = self._parents_map
 
988
        if parents_map is None:
 
989
            # Repository is not locked, so there's no cache.
 
990
            parents_map = {}
 
991
        start_set = set(parents_map)
 
992
        result_parents = set()
 
993
        for parents in parents_map.itervalues():
 
994
            result_parents.update(parents)
 
995
        stop_keys = result_parents.difference(start_set)
 
996
        included_keys = start_set.intersection(result_parents)
 
997
        start_set.difference_update(included_keys)
 
998
        recipe = (start_set, stop_keys, len(parents_map))
 
999
        path = self.bzrdir._path_for_remote_call(self._client)
 
1000
        for key in keys:
 
1001
            if type(key) is not str:
 
1002
                raise ValueError(
 
1003
                    "key %r not a plain string" % (key,))
 
1004
        verb = 'Repository.get_parent_map'
 
1005
        args = (path,) + tuple(keys)
 
1006
        try:
 
1007
            response = self._call_with_body_bytes_expecting_body(
 
1008
                verb, args, _serialise_search_recipe(recipe))
 
1009
        except errors.UnknownSmartMethod:
 
1010
            # Server does not support this method, so get the whole graph.
 
1011
            # Worse, we have to force a disconnection, because the server now
 
1012
            # doesn't realise it has a body on the wire to consume, so the
 
1013
            # only way to recover is to abandon the connection.
 
1014
            warning(
 
1015
                'Server is too old for fast get_parent_map, reconnecting.  '
 
1016
                '(Upgrade the server to Bazaar 1.2 to avoid this)')
 
1017
            medium.disconnect()
 
1018
            # To avoid having to disconnect repeatedly, we keep track of the
 
1019
            # fact the server doesn't understand remote methods added in 1.2.
 
1020
            medium._remember_remote_is_before((1, 2))
 
1021
            return self.get_revision_graph(None)
 
1022
        response_tuple, response_handler = response
 
1023
        if response_tuple[0] not in ['ok']:
 
1024
            response_handler.cancel_read_body()
 
1025
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1026
        if response_tuple[0] == 'ok':
 
1027
            coded = bz2.decompress(response_handler.read_body_bytes())
 
1028
            if coded == '':
 
1029
                # no revisions found
 
1030
                return {}
 
1031
            lines = coded.split('\n')
 
1032
            revision_graph = {}
 
1033
            for line in lines:
 
1034
                d = tuple(line.split())
 
1035
                if len(d) > 1:
 
1036
                    revision_graph[d[0]] = d[1:]
 
1037
                else:
 
1038
                    # No parents - so give the Graph result (NULL_REVISION,).
 
1039
                    revision_graph[d[0]] = (NULL_REVISION,)
 
1040
            return revision_graph
 
1041
 
 
1042
    @needs_read_lock
 
1043
    def get_signature_text(self, revision_id):
 
1044
        self._ensure_real()
 
1045
        return self._real_repository.get_signature_text(revision_id)
 
1046
 
 
1047
    @needs_read_lock
 
1048
    @symbol_versioning.deprecated_method(symbol_versioning.one_three)
 
1049
    def get_revision_graph_with_ghosts(self, revision_ids=None):
 
1050
        self._ensure_real()
 
1051
        return self._real_repository.get_revision_graph_with_ghosts(
 
1052
            revision_ids=revision_ids)
 
1053
 
 
1054
    @needs_read_lock
 
1055
    def get_inventory_xml(self, revision_id):
 
1056
        self._ensure_real()
 
1057
        return self._real_repository.get_inventory_xml(revision_id)
 
1058
 
 
1059
    def deserialise_inventory(self, revision_id, xml):
 
1060
        self._ensure_real()
 
1061
        return self._real_repository.deserialise_inventory(revision_id, xml)
 
1062
 
 
1063
    def reconcile(self, other=None, thorough=False):
 
1064
        self._ensure_real()
 
1065
        return self._real_repository.reconcile(other=other, thorough=thorough)
 
1066
        
 
1067
    def all_revision_ids(self):
 
1068
        self._ensure_real()
 
1069
        return self._real_repository.all_revision_ids()
 
1070
    
 
1071
    @needs_read_lock
 
1072
    def get_deltas_for_revisions(self, revisions):
 
1073
        self._ensure_real()
 
1074
        return self._real_repository.get_deltas_for_revisions(revisions)
 
1075
 
 
1076
    @needs_read_lock
 
1077
    def get_revision_delta(self, revision_id):
 
1078
        self._ensure_real()
 
1079
        return self._real_repository.get_revision_delta(revision_id)
 
1080
 
 
1081
    @needs_read_lock
 
1082
    def revision_trees(self, revision_ids):
 
1083
        self._ensure_real()
 
1084
        return self._real_repository.revision_trees(revision_ids)
 
1085
 
 
1086
    @needs_read_lock
 
1087
    def get_revision_reconcile(self, revision_id):
 
1088
        self._ensure_real()
 
1089
        return self._real_repository.get_revision_reconcile(revision_id)
 
1090
 
 
1091
    @needs_read_lock
 
1092
    def check(self, revision_ids=None):
 
1093
        self._ensure_real()
 
1094
        return self._real_repository.check(revision_ids=revision_ids)
 
1095
 
 
1096
    def copy_content_into(self, destination, revision_id=None):
 
1097
        self._ensure_real()
 
1098
        return self._real_repository.copy_content_into(
 
1099
            destination, revision_id=revision_id)
 
1100
 
 
1101
    def _copy_repository_tarball(self, to_bzrdir, revision_id=None):
 
1102
        # get a tarball of the remote repository, and copy from that into the
 
1103
        # destination
 
1104
        from bzrlib import osutils
 
1105
        import tarfile
 
1106
        # TODO: Maybe a progress bar while streaming the tarball?
 
1107
        note("Copying repository content as tarball...")
 
1108
        tar_file = self._get_tarball('bz2')
 
1109
        if tar_file is None:
 
1110
            return None
 
1111
        destination = to_bzrdir.create_repository()
 
1112
        try:
 
1113
            tar = tarfile.open('repository', fileobj=tar_file,
 
1114
                mode='r|bz2')
 
1115
            tmpdir = osutils.mkdtemp()
 
1116
            try:
 
1117
                _extract_tar(tar, tmpdir)
 
1118
                tmp_bzrdir = BzrDir.open(tmpdir)
 
1119
                tmp_repo = tmp_bzrdir.open_repository()
 
1120
                tmp_repo.copy_content_into(destination, revision_id)
 
1121
            finally:
 
1122
                osutils.rmtree(tmpdir)
 
1123
        finally:
 
1124
            tar_file.close()
 
1125
        return destination
 
1126
        # TODO: Suggestion from john: using external tar is much faster than
 
1127
        # python's tarfile library, but it may not work on windows.
 
1128
 
 
1129
    @needs_write_lock
 
1130
    def pack(self):
 
1131
        """Compress the data within the repository.
 
1132
 
 
1133
        This is not currently implemented within the smart server.
 
1134
        """
 
1135
        self._ensure_real()
 
1136
        return self._real_repository.pack()
 
1137
 
 
1138
    def set_make_working_trees(self, new_value):
 
1139
        self._ensure_real()
 
1140
        self._real_repository.set_make_working_trees(new_value)
 
1141
 
 
1142
    @needs_write_lock
 
1143
    def sign_revision(self, revision_id, gpg_strategy):
 
1144
        self._ensure_real()
 
1145
        return self._real_repository.sign_revision(revision_id, gpg_strategy)
 
1146
 
 
1147
    @needs_read_lock
 
1148
    def get_revisions(self, revision_ids):
 
1149
        self._ensure_real()
 
1150
        return self._real_repository.get_revisions(revision_ids)
 
1151
 
 
1152
    def supports_rich_root(self):
 
1153
        self._ensure_real()
 
1154
        return self._real_repository.supports_rich_root()
 
1155
 
 
1156
    def iter_reverse_revision_history(self, revision_id):
 
1157
        self._ensure_real()
 
1158
        return self._real_repository.iter_reverse_revision_history(revision_id)
 
1159
 
 
1160
    @property
 
1161
    def _serializer(self):
 
1162
        self._ensure_real()
 
1163
        return self._real_repository._serializer
 
1164
 
 
1165
    def store_revision_signature(self, gpg_strategy, plaintext, revision_id):
 
1166
        self._ensure_real()
 
1167
        return self._real_repository.store_revision_signature(
 
1168
            gpg_strategy, plaintext, revision_id)
 
1169
 
 
1170
    def add_signature_text(self, revision_id, signature):
 
1171
        self._ensure_real()
 
1172
        return self._real_repository.add_signature_text(revision_id, signature)
 
1173
 
 
1174
    def has_signature_for_revision_id(self, revision_id):
 
1175
        self._ensure_real()
 
1176
        return self._real_repository.has_signature_for_revision_id(revision_id)
 
1177
 
 
1178
    def item_keys_introduced_by(self, revision_ids, _files_pb=None):
 
1179
        self._ensure_real()
 
1180
        return self._real_repository.item_keys_introduced_by(revision_ids,
 
1181
            _files_pb=_files_pb)
 
1182
 
 
1183
    def revision_graph_can_have_wrong_parents(self):
 
1184
        # The answer depends on the remote repo format.
 
1185
        self._ensure_real()
 
1186
        return self._real_repository.revision_graph_can_have_wrong_parents()
 
1187
 
 
1188
    def _find_inconsistent_revision_parents(self):
 
1189
        self._ensure_real()
 
1190
        return self._real_repository._find_inconsistent_revision_parents()
 
1191
 
 
1192
    def _check_for_inconsistent_revision_parents(self):
 
1193
        self._ensure_real()
 
1194
        return self._real_repository._check_for_inconsistent_revision_parents()
 
1195
 
 
1196
    def _make_parents_provider(self):
 
1197
        providers = [_UnstackedParentsProvider(self)]
 
1198
        providers.extend(r._make_parents_provider() for r in
 
1199
                         self._fallback_repositories)
 
1200
        return graph._StackedParentsProvider(providers)
 
1201
 
 
1202
def _serialise_search_recipe(recipe):
 
1203
    """Serialise a graph search recipe.
 
1204
 
 
1205
    :param recipe: A search recipe (start, stop, count).
 
1206
    :return: Serialised bytes.
 
1207
    """
 
1208
    start_keys = ' '.join(recipe[0])
 
1209
    stop_keys = ' '.join(recipe[1])
 
1210
    count = str(recipe[2])
 
1211
    return '\n'.join((start_keys, stop_keys, count))
 
1212
 
 
1213
def _serialise_search_tuple_key_recipe((start, stop, count)):
 
1214
    """Serialise a graph search recipe.
 
1215
 
 
1216
    :param recipe: A search recipe (start, stop, count).
 
1217
    :return: Serialised bytes.
 
1218
    """
 
1219
    # Format:
 
1220
    #  recipe = keys NEWLINE keys NEWLINE count
 
1221
    #  count = INTEGER
 
1222
    #  keys = key 0x00 key 0x00 key ...
 
1223
    #  key = key_elem SPACE key_elem SPACE key_elem ...
 
1224
    buf = StringIO()
 
1225
    start_keys = (' '.join(key) for key in start)
 
1226
    buf.write('\0'.join(start_keys))
 
1227
    buf.write('\n')
 
1228
    stop_keys = (' '.join(key) for key in stop)
 
1229
    buf.write('\0'.join(stop_keys))
 
1230
    buf.write('\n')
 
1231
    buf.write(str(count))
 
1232
    return buf.getvalue()
 
1233
 
 
1234
 
 
1235
def _deserialise_search_result(bytes):
 
1236
    graph_dict = {}
 
1237
    for line in bytes.split('\n'):
 
1238
        keys_bytes = line.split('\0')
 
1239
        keys = [tuple(key_bytes.split(' ')) for key_bytes in keys_bytes]
 
1240
        parents = tuple(keys[1:])
 
1241
        if parents == (('',),):
 
1242
            parents = ()
 
1243
        graph_dict[keys[0]] = parents
 
1244
    return graph_dict
 
1245
 
 
1246
 
 
1247
class RemoteVersionedFiles(VersionedFiles):
 
1248
 
 
1249
    def __init__(self, remote_repo, vf_name):
 
1250
        self.remote_repo = remote_repo
 
1251
        self.vf_name = vf_name
 
1252
        self._parents_map = None
 
1253
        if 'hpss' in debug.debug_flags:
 
1254
            self._requested_parents = None
 
1255
 
 
1256
    def _get_real_vf(self):
 
1257
        self.remote_repo._ensure_real()
 
1258
        return getattr(self.remote_repo._real_repository, self.vf_name)
 
1259
 
 
1260
    def add_lines(self, version_id, parents, lines, parent_texts=None,
 
1261
            left_matching_blocks=None, nostore_sha=None, random_id=False,
 
1262
            check_content=True):
 
1263
        real_vf = self._get_real_vf()
 
1264
        return real_vf.add_lines(version_id, parents, lines,
 
1265
            parent_texts=parent_texts,
 
1266
            left_matching_blocks=left_matching_blocks,
 
1267
            nostore_sha=nostore_sha, random_id=random_id,
 
1268
            check_content=check_content)
 
1269
 
 
1270
    def add_mpdiffs(self, records):
 
1271
        real_vf = self._get_real_vf()
 
1272
        return real_vf.add_mpdiffs(records)
 
1273
 
 
1274
    def annotate(self, key):
 
1275
        real_vf = self._get_real_vf()
 
1276
        return real_vf.annotate(key)
 
1277
 
 
1278
    def check(self, progress_bar=None):
 
1279
        real_vf = self._get_real_vf()
 
1280
        return real_vf.check(progress_bar=progress_bar)
 
1281
 
 
1282
    def get_parent_map(self, keys):
 
1283
        # XXX: lots of duplication with RemoteRepository.get_parent_map.
 
1284
        client = self.remote_repo._client
 
1285
        medium = client._medium
 
1286
        if medium._is_remote_before((1, 8)):
 
1287
            real_vf = self._get_real_vf()
 
1288
            return real_vf.get_parent_map(keys)
 
1289
        # Hack to build up the caching logic.
 
1290
        ancestry = self._parents_map
 
1291
        if ancestry is None:
 
1292
            # Repository is not locked, so there's no cache.
 
1293
            missing_revisions = set(keys)
 
1294
            ancestry = {}
 
1295
        else:
 
1296
            missing_revisions = set(key for key in keys if key not in ancestry)
 
1297
        if missing_revisions:
 
1298
            try:
 
1299
                parent_map = self._get_parent_map(missing_revisions)
 
1300
            except errors.UnknownSmartMethod:
 
1301
                medium._remember_remote_is_before((1, 8))
 
1302
                real_vf = self._get_real_vf()
 
1303
                return real_vf.get_parent_map(keys)
 
1304
            if 'hpss' in debug.debug_flags:
 
1305
                mutter('retransmitted revisions: %d of %d',
 
1306
                        len(set(ancestry).intersection(parent_map)),
 
1307
                        len(parent_map))
 
1308
            ancestry.update(parent_map)
 
1309
        present_keys = [k for k in keys if k in ancestry]
 
1310
        if 'hpss' in debug.debug_flags:
 
1311
            if self._requested_parents is not None and len(ancestry) != 0:
 
1312
                self._requested_parents.update(present_keys)
 
1313
                mutter('Current RemoteRepository graph hit rate: %d%%',
 
1314
                    100.0 * len(self._requested_parents) / len(ancestry))
 
1315
        return dict((k, ancestry[k]) for k in present_keys)
 
1316
        
 
1317
    def _get_parent_map(self, keys):
 
1318
        # XXX: lots of duplication with RemoteRepository._get_parent_map.
 
1319
        if None in keys:
 
1320
            raise ValueError('get_parent_map(None) is not valid')
 
1321
        if NULL_REVISION in keys:
 
1322
            keys.discard(NULL_REVISION)
 
1323
            found_parents = {NULL_REVISION:()}
 
1324
            if not keys:
 
1325
                return found_parents
 
1326
        else:
 
1327
            found_parents = {}
 
1328
        # TODO(Needs analysis): We could assume that the keys being requested
 
1329
        # from get_parent_map are in a breadth first search, so typically they
 
1330
        # will all be depth N from some common parent, and we don't have to
 
1331
        # have the server iterate from the root parent, but rather from the
 
1332
        # keys we're searching; and just tell the server the keyspace we
 
1333
        # already have; but this may be more traffic again.
 
1334
 
 
1335
        # Transform self._parents_map into a search request recipe.
 
1336
        # TODO: Manage this incrementally to avoid covering the same path
 
1337
        # repeatedly. (The server will have to on each request, but the less
 
1338
        # work done the better).
 
1339
        parents_map = self._parents_map
 
1340
        if parents_map is None:
 
1341
            # Repository is not locked, so there's no cache.
 
1342
            parents_map = {}
 
1343
        start_set = set(parents_map)
 
1344
        result_parents = set()
 
1345
        for parents in parents_map.itervalues():
 
1346
            result_parents.update(parents)
 
1347
        stop_keys = result_parents.difference(start_set)
 
1348
        included_keys = start_set.intersection(result_parents)
 
1349
        start_set.difference_update(included_keys)
 
1350
        recipe = (start_set, stop_keys, len(parents_map))
 
1351
        client = self.remote_repo._client
 
1352
        path = self.remote_repo.bzrdir._path_for_remote_call(client)
 
1353
        for key in keys:
 
1354
            if type(key) is not tuple:
 
1355
                raise ValueError(
 
1356
                    "key %r not a tuple of strs" % (key,))
 
1357
            for key_elem in key:
 
1358
                if type(key_elem) is not str:
 
1359
                    raise ValueError(
 
1360
                        "key %r not a tuple of strs" % (key,))
 
1361
        verb = 'VersionedFiles.get_parent_map'
 
1362
        args = (path, self.vf_name) + tuple(keys)
 
1363
        response = client.call_with_body_bytes_expecting_body(
 
1364
            verb, args, _serialise_search_tuple_key_recipe(recipe))
 
1365
        response_tuple, response_handler = response
 
1366
        if response_tuple[0] not in ['ok']:
 
1367
            response_handler.cancel_read_body()
 
1368
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1369
        if response_tuple[0] == 'ok':
 
1370
            coded = bz2.decompress(response_handler.read_body_bytes())
 
1371
            revision_graph = _deserialise_search_result(coded)
 
1372
            # The server doesn't return explicit results for keys it doesn't
 
1373
            # have, so add empty entries for the missing keys to the result
 
1374
            # dict.
 
1375
            missing = keys.difference(revision_graph)
 
1376
            for missing_key in missing:
 
1377
                revision_graph[missing_key] = ()
 
1378
            return revision_graph
 
1379
 
 
1380
    def get_record_stream(self, keys, ordering, include_delta_closure):
 
1381
        real_vf = self._get_real_vf()
 
1382
        return real_vf.get_record_stream(keys, ordering, include_delta_closure)
 
1383
 
 
1384
    def get_sha1s(self, keys):
 
1385
        real_vf = self._get_real_vf()
 
1386
        return real_vf.get_sha1s(keys)
 
1387
 
 
1388
    def insert_record_stream(self, stream):
 
1389
        real_vf = self._get_real_vf()
 
1390
        return real_vf.insert_record_stream(stream)
 
1391
 
 
1392
    def iter_lines_added_or_present_in_keys(self, keys, pb=None):
 
1393
        real_vf = self._get_real_vf()
 
1394
        return real_vf.iter_lines_added_or_present_in_keys(keys, pb=pb)
 
1395
 
 
1396
    def keys(self):
 
1397
        real_vf = self._get_real_vf()
 
1398
        return real_vf.keys()
 
1399
 
 
1400
    def make_mpdiffs(self, keys):
 
1401
        real_vf = self._get_real_vf()
 
1402
        return real_vf.make_mpdiffs(keys)
 
1403
 
 
1404
    def autopack(self):
 
1405
        path = self.bzrdir._path_for_remote_call(self._client)
 
1406
        try:
 
1407
            response = self._call('PackRepository.autopack', path)
 
1408
        except errors.UnknownSmartMethod:
 
1409
            self._ensure_real()
 
1410
            self._real_repository._pack_collection.autopack()
 
1411
            return
 
1412
        if self._real_repository is not None:
 
1413
            # Reset the real repository's cache of pack names.
 
1414
            # XXX: At some point we may be able to skip this and just rely on
 
1415
            # the automatic retry logic to do the right thing, but for now we
 
1416
            # err on the side of being correct rather than being optimal.
 
1417
            self._real_repository._pack_collection.reload_pack_names()
 
1418
        if response[0] != 'ok':
 
1419
            raise errors.UnexpectedSmartServerResponse(response)
 
1420
 
 
1421
 
 
1422
class RemoteBranchLockableFiles(LockableFiles):
 
1423
    """A 'LockableFiles' implementation that talks to a smart server.
 
1424
    
 
1425
    This is not a public interface class.
 
1426
    """
 
1427
 
 
1428
    def __init__(self, bzrdir, _client):
 
1429
        self.bzrdir = bzrdir
 
1430
        self._client = _client
 
1431
        self._need_find_modes = True
 
1432
        LockableFiles.__init__(
 
1433
            self, bzrdir.get_branch_transport(None),
 
1434
            'lock', lockdir.LockDir)
 
1435
 
 
1436
    def _find_modes(self):
 
1437
        # RemoteBranches don't let the client set the mode of control files.
 
1438
        self._dir_mode = None
 
1439
        self._file_mode = None
 
1440
 
 
1441
 
 
1442
class RemoteBranchFormat(branch.BranchFormat):
 
1443
 
 
1444
    def __eq__(self, other):
 
1445
        return (isinstance(other, RemoteBranchFormat) and 
 
1446
            self.__dict__ == other.__dict__)
 
1447
 
 
1448
    def get_format_description(self):
 
1449
        return 'Remote BZR Branch'
 
1450
 
 
1451
    def get_format_string(self):
 
1452
        return 'Remote BZR Branch'
 
1453
 
 
1454
    def open(self, a_bzrdir):
 
1455
        return a_bzrdir.open_branch()
 
1456
 
 
1457
    def initialize(self, a_bzrdir):
 
1458
        return a_bzrdir.create_branch()
 
1459
 
 
1460
    def supports_tags(self):
 
1461
        # Remote branches might support tags, but we won't know until we
 
1462
        # access the real remote branch.
 
1463
        return True
 
1464
 
 
1465
 
 
1466
class RemoteBranch(branch.Branch, _RpcHelper):
 
1467
    """Branch stored on a server accessed by HPSS RPC.
 
1468
 
 
1469
    At the moment most operations are mapped down to simple file operations.
 
1470
    """
 
1471
 
 
1472
    def __init__(self, remote_bzrdir, remote_repository, real_branch=None,
 
1473
        _client=None):
 
1474
        """Create a RemoteBranch instance.
 
1475
 
 
1476
        :param real_branch: An optional local implementation of the branch
 
1477
            format, usually accessing the data via the VFS.
 
1478
        :param _client: Private parameter for testing.
 
1479
        """
 
1480
        # We intentionally don't call the parent class's __init__, because it
 
1481
        # will try to assign to self.tags, which is a property in this subclass.
 
1482
        # And the parent's __init__ doesn't do much anyway.
 
1483
        self._revision_id_to_revno_cache = None
 
1484
        self._revision_history_cache = None
 
1485
        self._last_revision_info_cache = None
 
1486
        self.bzrdir = remote_bzrdir
 
1487
        if _client is not None:
 
1488
            self._client = _client
 
1489
        else:
 
1490
            self._client = remote_bzrdir._client
 
1491
        self.repository = remote_repository
 
1492
        if real_branch is not None:
 
1493
            self._real_branch = real_branch
 
1494
            # Give the remote repository the matching real repo.
 
1495
            real_repo = self._real_branch.repository
 
1496
            if isinstance(real_repo, RemoteRepository):
 
1497
                real_repo._ensure_real()
 
1498
                real_repo = real_repo._real_repository
 
1499
            self.repository._set_real_repository(real_repo)
 
1500
            # Give the branch the remote repository to let fast-pathing happen.
 
1501
            self._real_branch.repository = self.repository
 
1502
        else:
 
1503
            self._real_branch = None
 
1504
        # Fill out expected attributes of branch for bzrlib api users.
 
1505
        self._format = RemoteBranchFormat()
 
1506
        self.base = self.bzrdir.root_transport.base
 
1507
        self._control_files = None
 
1508
        self._lock_mode = None
 
1509
        self._lock_token = None
 
1510
        self._repo_lock_token = None
 
1511
        self._lock_count = 0
 
1512
        self._leave_lock = False
 
1513
        # The base class init is not called, so we duplicate this:
 
1514
        hooks = branch.Branch.hooks['open']
 
1515
        for hook in hooks:
 
1516
            hook(self)
 
1517
        self._setup_stacking()
 
1518
 
 
1519
    def _setup_stacking(self):
 
1520
        # configure stacking into the remote repository, by reading it from
 
1521
        # the vfs branch.
 
1522
        try:
 
1523
            fallback_url = self.get_stacked_on_url()
 
1524
        except (errors.NotStacked, errors.UnstackableBranchFormat,
 
1525
            errors.UnstackableRepositoryFormat), e:
 
1526
            return
 
1527
        # it's relative to this branch...
 
1528
        fallback_url = urlutils.join(self.base, fallback_url)
 
1529
        transports = [self.bzrdir.root_transport]
 
1530
        if self._real_branch is not None:
 
1531
            transports.append(self._real_branch._transport)
 
1532
        stacked_on = branch.Branch.open(fallback_url,
 
1533
                                        possible_transports=transports)
 
1534
        self.repository.add_fallback_repository(stacked_on.repository)
 
1535
 
 
1536
    def _get_real_transport(self):
 
1537
        # if we try vfs access, return the real branch's vfs transport
 
1538
        self._ensure_real()
 
1539
        return self._real_branch._transport
 
1540
 
 
1541
    _transport = property(_get_real_transport)
 
1542
 
 
1543
    def __str__(self):
 
1544
        return "%s(%s)" % (self.__class__.__name__, self.base)
 
1545
 
 
1546
    __repr__ = __str__
 
1547
 
 
1548
    def _ensure_real(self):
 
1549
        """Ensure that there is a _real_branch set.
 
1550
 
 
1551
        Used before calls to self._real_branch.
 
1552
        """
 
1553
        if self._real_branch is None:
 
1554
            if not vfs.vfs_enabled():
 
1555
                raise AssertionError('smart server vfs must be enabled '
 
1556
                    'to use vfs implementation')
 
1557
            self.bzrdir._ensure_real()
 
1558
            self._real_branch = self.bzrdir._real_bzrdir.open_branch()
 
1559
            if self.repository._real_repository is None:
 
1560
                # Give the remote repository the matching real repo.
 
1561
                real_repo = self._real_branch.repository
 
1562
                if isinstance(real_repo, RemoteRepository):
 
1563
                    real_repo._ensure_real()
 
1564
                    real_repo = real_repo._real_repository
 
1565
                self.repository._set_real_repository(real_repo)
 
1566
            # Give the real branch the remote repository to let fast-pathing
 
1567
            # happen.
 
1568
            self._real_branch.repository = self.repository
 
1569
            if self._lock_mode == 'r':
 
1570
                self._real_branch.lock_read()
 
1571
            elif self._lock_mode == 'w':
 
1572
                self._real_branch.lock_write(token=self._lock_token)
 
1573
 
 
1574
    def _translate_error(self, err, **context):
 
1575
        self.repository._translate_error(err, branch=self, **context)
 
1576
 
 
1577
    def _clear_cached_state(self):
 
1578
        super(RemoteBranch, self)._clear_cached_state()
 
1579
        if self._real_branch is not None:
 
1580
            self._real_branch._clear_cached_state()
 
1581
 
 
1582
    def _clear_cached_state_of_remote_branch_only(self):
 
1583
        """Like _clear_cached_state, but doesn't clear the cache of
 
1584
        self._real_branch.
 
1585
 
 
1586
        This is useful when falling back to calling a method of
 
1587
        self._real_branch that changes state.  In that case the underlying
 
1588
        branch changes, so we need to invalidate this RemoteBranch's cache of
 
1589
        it.  However, there's no need to invalidate the _real_branch's cache
 
1590
        too, in fact doing so might harm performance.
 
1591
        """
 
1592
        super(RemoteBranch, self)._clear_cached_state()
 
1593
        
 
1594
    @property
 
1595
    def control_files(self):
 
1596
        # Defer actually creating RemoteBranchLockableFiles until its needed,
 
1597
        # because it triggers an _ensure_real that we otherwise might not need.
 
1598
        if self._control_files is None:
 
1599
            self._control_files = RemoteBranchLockableFiles(
 
1600
                self.bzrdir, self._client)
 
1601
        return self._control_files
 
1602
 
 
1603
    def _get_checkout_format(self):
 
1604
        self._ensure_real()
 
1605
        return self._real_branch._get_checkout_format()
 
1606
 
 
1607
    def get_physical_lock_status(self):
 
1608
        """See Branch.get_physical_lock_status()."""
 
1609
        # should be an API call to the server, as branches must be lockable.
 
1610
        self._ensure_real()
 
1611
        return self._real_branch.get_physical_lock_status()
 
1612
 
 
1613
    def get_stacked_on_url(self):
 
1614
        """Get the URL this branch is stacked against.
 
1615
 
 
1616
        :raises NotStacked: If the branch is not stacked.
 
1617
        :raises UnstackableBranchFormat: If the branch does not support
 
1618
            stacking.
 
1619
        :raises UnstackableRepositoryFormat: If the repository does not support
 
1620
            stacking.
 
1621
        """
 
1622
        try:
 
1623
            # there may not be a repository yet, so we can't use
 
1624
            # self._translate_error, so we can't use self._call either.
 
1625
            response = self._client.call('Branch.get_stacked_on_url',
 
1626
                self._remote_path())
 
1627
        except errors.ErrorFromSmartServer, err:
 
1628
            # there may not be a repository yet, so we can't call through
 
1629
            # its _translate_error
 
1630
            _translate_error(err, branch=self)
 
1631
        except errors.UnknownSmartMethod, err:
 
1632
            self._ensure_real()
 
1633
            return self._real_branch.get_stacked_on_url()
 
1634
        if response[0] != 'ok':
 
1635
            raise errors.UnexpectedSmartServerResponse(response)
 
1636
        return response[1]
 
1637
 
 
1638
    def lock_read(self):
 
1639
        self.repository.lock_read()
 
1640
        if not self._lock_mode:
 
1641
            self._lock_mode = 'r'
 
1642
            self._lock_count = 1
 
1643
            if self._real_branch is not None:
 
1644
                self._real_branch.lock_read()
 
1645
        else:
 
1646
            self._lock_count += 1
 
1647
 
 
1648
    def _remote_lock_write(self, token):
 
1649
        if token is None:
 
1650
            branch_token = repo_token = ''
 
1651
        else:
 
1652
            branch_token = token
 
1653
            repo_token = self.repository.lock_write()
 
1654
            self.repository.unlock()
 
1655
        err_context = {'token': token}
 
1656
        response = self._call(
 
1657
            'Branch.lock_write', self._remote_path(), branch_token,
 
1658
            repo_token or '', **err_context)
 
1659
        if response[0] != 'ok':
 
1660
            raise errors.UnexpectedSmartServerResponse(response)
 
1661
        ok, branch_token, repo_token = response
 
1662
        return branch_token, repo_token
 
1663
            
 
1664
    def lock_write(self, token=None):
 
1665
        if not self._lock_mode:
 
1666
            # Lock the branch and repo in one remote call.
 
1667
            remote_tokens = self._remote_lock_write(token)
 
1668
            self._lock_token, self._repo_lock_token = remote_tokens
 
1669
            if not self._lock_token:
 
1670
                raise SmartProtocolError('Remote server did not return a token!')
 
1671
            # Tell the self.repository object that it is locked.
 
1672
            self.repository.lock_write(
 
1673
                self._repo_lock_token, _skip_rpc=True)
 
1674
 
 
1675
            if self._real_branch is not None:
 
1676
                self._real_branch.lock_write(token=self._lock_token)
 
1677
            if token is not None:
 
1678
                self._leave_lock = True
 
1679
            else:
 
1680
                self._leave_lock = False
 
1681
            self._lock_mode = 'w'
 
1682
            self._lock_count = 1
 
1683
        elif self._lock_mode == 'r':
 
1684
            raise errors.ReadOnlyTransaction
 
1685
        else:
 
1686
            if token is not None:
 
1687
                # A token was given to lock_write, and we're relocking, so
 
1688
                # check that the given token actually matches the one we
 
1689
                # already have.
 
1690
                if token != self._lock_token:
 
1691
                    raise errors.TokenMismatch(token, self._lock_token)
 
1692
            self._lock_count += 1
 
1693
            # Re-lock the repository too.
 
1694
            self.repository.lock_write(self._repo_lock_token)
 
1695
        return self._lock_token or None
 
1696
 
 
1697
    def _unlock(self, branch_token, repo_token):
 
1698
        err_context = {'token': str((branch_token, repo_token))}
 
1699
        response = self._call(
 
1700
            'Branch.unlock', self._remote_path(), branch_token,
 
1701
            repo_token or '', **err_context)
 
1702
        if response == ('ok',):
 
1703
            return
 
1704
        raise errors.UnexpectedSmartServerResponse(response)
 
1705
 
 
1706
    def unlock(self):
 
1707
        try:
 
1708
            self._lock_count -= 1
 
1709
            if not self._lock_count:
 
1710
                self._clear_cached_state()
 
1711
                mode = self._lock_mode
 
1712
                self._lock_mode = None
 
1713
                if self._real_branch is not None:
 
1714
                    if (not self._leave_lock and mode == 'w' and
 
1715
                        self._repo_lock_token):
 
1716
                        # If this RemoteBranch will remove the physical lock
 
1717
                        # for the repository, make sure the _real_branch
 
1718
                        # doesn't do it first.  (Because the _real_branch's
 
1719
                        # repository is set to be the RemoteRepository.)
 
1720
                        self._real_branch.repository.leave_lock_in_place()
 
1721
                    self._real_branch.unlock()
 
1722
                if mode != 'w':
 
1723
                    # Only write-locked branched need to make a remote method
 
1724
                    # call to perfom the unlock.
 
1725
                    return
 
1726
                if not self._lock_token:
 
1727
                    raise AssertionError('Locked, but no token!')
 
1728
                branch_token = self._lock_token
 
1729
                repo_token = self._repo_lock_token
 
1730
                self._lock_token = None
 
1731
                self._repo_lock_token = None
 
1732
                if not self._leave_lock:
 
1733
                    self._unlock(branch_token, repo_token)
 
1734
        finally:
 
1735
            self.repository.unlock()
 
1736
 
 
1737
    def break_lock(self):
 
1738
        self._ensure_real()
 
1739
        return self._real_branch.break_lock()
 
1740
 
 
1741
    def leave_lock_in_place(self):
 
1742
        if not self._lock_token:
 
1743
            raise NotImplementedError(self.leave_lock_in_place)
 
1744
        self._leave_lock = True
 
1745
 
 
1746
    def dont_leave_lock_in_place(self):
 
1747
        if not self._lock_token:
 
1748
            raise NotImplementedError(self.dont_leave_lock_in_place)
 
1749
        self._leave_lock = False
 
1750
 
 
1751
    def _last_revision_info(self):
 
1752
        response = self._call('Branch.last_revision_info', self._remote_path())
 
1753
        if response[0] != 'ok':
 
1754
            raise SmartProtocolError('unexpected response code %s' % (response,))
 
1755
        revno = int(response[1])
 
1756
        last_revision = response[2]
 
1757
        return (revno, last_revision)
 
1758
 
 
1759
    def _gen_revision_history(self):
 
1760
        """See Branch._gen_revision_history()."""
 
1761
        response_tuple, response_handler = self._call_expecting_body(
 
1762
            'Branch.revision_history', self._remote_path())
 
1763
        if response_tuple[0] != 'ok':
 
1764
            raise errors.UnexpectedSmartServerResponse(response_tuple)
 
1765
        result = response_handler.read_body_bytes().split('\x00')
 
1766
        if result == ['']:
 
1767
            return []
 
1768
        return result
 
1769
 
 
1770
    def _remote_path(self):
 
1771
        return self.bzrdir._path_for_remote_call(self._client)
 
1772
 
 
1773
    def _set_last_revision_descendant(self, revision_id, other_branch,
 
1774
            allow_diverged=False, allow_overwrite_descendant=False):
 
1775
        err_context = {'other_branch': other_branch}
 
1776
        response = self._call('Branch.set_last_revision_ex',
 
1777
            self._remote_path(), self._lock_token, self._repo_lock_token,
 
1778
            revision_id, int(allow_diverged), int(allow_overwrite_descendant),
 
1779
            **err_context)
 
1780
        self._clear_cached_state()
 
1781
        if len(response) != 3 and response[0] != 'ok':
 
1782
            raise errors.UnexpectedSmartServerResponse(response)
 
1783
        new_revno, new_revision_id = response[1:]
 
1784
        self._last_revision_info_cache = new_revno, new_revision_id
 
1785
        if self._real_branch is not None:
 
1786
            cache = new_revno, new_revision_id
 
1787
            self._real_branch._last_revision_info_cache = cache
 
1788
 
 
1789
    def _set_last_revision(self, revision_id):
 
1790
        self._clear_cached_state()
 
1791
        response = self._call('Branch.set_last_revision',
 
1792
            self._remote_path(), self._lock_token, self._repo_lock_token,
 
1793
            revision_id)
 
1794
        if response != ('ok',):
 
1795
            raise errors.UnexpectedSmartServerResponse(response)
 
1796
 
 
1797
    @needs_write_lock
 
1798
    def set_revision_history(self, rev_history):
 
1799
        # Send just the tip revision of the history; the server will generate
 
1800
        # the full history from that.  If the revision doesn't exist in this
 
1801
        # branch, NoSuchRevision will be raised.
 
1802
        if rev_history == []:
 
1803
            rev_id = 'null:'
 
1804
        else:
 
1805
            rev_id = rev_history[-1]
 
1806
        self._set_last_revision(rev_id)
 
1807
        self._cache_revision_history(rev_history)
 
1808
 
 
1809
    def get_parent(self):
 
1810
        self._ensure_real()
 
1811
        return self._real_branch.get_parent()
 
1812
        
 
1813
    def set_parent(self, url):
 
1814
        self._ensure_real()
 
1815
        return self._real_branch.set_parent(url)
 
1816
        
 
1817
    def set_stacked_on_url(self, stacked_location):
 
1818
        """Set the URL this branch is stacked against.
 
1819
 
 
1820
        :raises UnstackableBranchFormat: If the branch does not support
 
1821
            stacking.
 
1822
        :raises UnstackableRepositoryFormat: If the repository does not support
 
1823
            stacking.
 
1824
        """
 
1825
        self._ensure_real()
 
1826
        return self._real_branch.set_stacked_on_url(stacked_location)
 
1827
 
 
1828
    def sprout(self, to_bzrdir, revision_id=None):
 
1829
        branch_format = to_bzrdir._format._branch_format
 
1830
        if (branch_format is None or
 
1831
            isinstance(branch_format, RemoteBranchFormat)):
 
1832
            # The to_bzrdir specifies RemoteBranchFormat (or no format, which
 
1833
            # implies the same thing), but RemoteBranches can't be created at
 
1834
            # arbitrary URLs.  So create a branch in the same format as
 
1835
            # _real_branch instead.
 
1836
            # XXX: if to_bzrdir is a RemoteBzrDir, this should perhaps do
 
1837
            # to_bzrdir.create_branch to create a RemoteBranch after all...
 
1838
            self._ensure_real()
 
1839
            result = self._real_branch._format.initialize(to_bzrdir)
 
1840
            self.copy_content_into(result, revision_id=revision_id)
 
1841
            result.set_parent(self.bzrdir.root_transport.base)
 
1842
        else:
 
1843
            result = branch.Branch.sprout(
 
1844
                self, to_bzrdir, revision_id=revision_id)
 
1845
        return result
 
1846
 
 
1847
    @needs_write_lock
 
1848
    def pull(self, source, overwrite=False, stop_revision=None,
 
1849
             **kwargs):
 
1850
        self._clear_cached_state_of_remote_branch_only()
 
1851
        self._ensure_real()
 
1852
        return self._real_branch.pull(
 
1853
            source, overwrite=overwrite, stop_revision=stop_revision,
 
1854
            _override_hook_target=self, **kwargs)
 
1855
 
 
1856
    @needs_read_lock
 
1857
    def push(self, target, overwrite=False, stop_revision=None):
 
1858
        self._ensure_real()
 
1859
        return self._real_branch.push(
 
1860
            target, overwrite=overwrite, stop_revision=stop_revision,
 
1861
            _override_hook_source_branch=self)
 
1862
 
 
1863
    def is_locked(self):
 
1864
        return self._lock_count >= 1
 
1865
 
 
1866
    @needs_read_lock
 
1867
    def revision_id_to_revno(self, revision_id):
 
1868
        self._ensure_real()
 
1869
        return self._real_branch.revision_id_to_revno(revision_id)
 
1870
 
 
1871
    @needs_write_lock
 
1872
    def set_last_revision_info(self, revno, revision_id):
 
1873
        revision_id = ensure_null(revision_id)
 
1874
        try:
 
1875
            response = self._call('Branch.set_last_revision_info',
 
1876
                self._remote_path(), self._lock_token, self._repo_lock_token,
 
1877
                str(revno), revision_id)
 
1878
        except errors.UnknownSmartMethod:
 
1879
            self._ensure_real()
 
1880
            self._clear_cached_state_of_remote_branch_only()
 
1881
            self._real_branch.set_last_revision_info(revno, revision_id)
 
1882
            self._last_revision_info_cache = revno, revision_id
 
1883
            return
 
1884
        if response == ('ok',):
 
1885
            self._clear_cached_state()
 
1886
            self._last_revision_info_cache = revno, revision_id
 
1887
            # Update the _real_branch's cache too.
 
1888
            if self._real_branch is not None:
 
1889
                cache = self._last_revision_info_cache
 
1890
                self._real_branch._last_revision_info_cache = cache
 
1891
        else:
 
1892
            raise errors.UnexpectedSmartServerResponse(response)
 
1893
 
 
1894
    @needs_write_lock
 
1895
    def generate_revision_history(self, revision_id, last_rev=None,
 
1896
                                  other_branch=None):
 
1897
        medium = self._client._medium
 
1898
        if not medium._is_remote_before((1, 6)):
 
1899
            try:
 
1900
                self._set_last_revision_descendant(revision_id, other_branch,
 
1901
                    allow_diverged=True, allow_overwrite_descendant=True)
 
1902
                return
 
1903
            except errors.UnknownSmartMethod:
 
1904
                medium._remember_remote_is_before((1, 6))
 
1905
        self._clear_cached_state_of_remote_branch_only()
 
1906
        self._ensure_real()
 
1907
        self._real_branch.generate_revision_history(
 
1908
            revision_id, last_rev=last_rev, other_branch=other_branch)
 
1909
 
 
1910
    @property
 
1911
    def tags(self):
 
1912
        self._ensure_real()
 
1913
        return self._real_branch.tags
 
1914
 
 
1915
    def set_push_location(self, location):
 
1916
        self._ensure_real()
 
1917
        return self._real_branch.set_push_location(location)
 
1918
 
 
1919
    @needs_write_lock
 
1920
    def update_revisions(self, other, stop_revision=None, overwrite=False,
 
1921
                         graph=None):
 
1922
        """See Branch.update_revisions."""
 
1923
        other.lock_read()
 
1924
        try:
 
1925
            if stop_revision is None:
 
1926
                stop_revision = other.last_revision()
 
1927
                if revision.is_null(stop_revision):
 
1928
                    # if there are no commits, we're done.
 
1929
                    return
 
1930
            self.fetch(other, stop_revision)
 
1931
 
 
1932
            if overwrite:
 
1933
                # Just unconditionally set the new revision.  We don't care if
 
1934
                # the branches have diverged.
 
1935
                self._set_last_revision(stop_revision)
 
1936
            else:
 
1937
                medium = self._client._medium
 
1938
                if not medium._is_remote_before((1, 6)):
 
1939
                    try:
 
1940
                        self._set_last_revision_descendant(stop_revision, other)
 
1941
                        return
 
1942
                    except errors.UnknownSmartMethod:
 
1943
                        medium._remember_remote_is_before((1, 6))
 
1944
                # Fallback for pre-1.6 servers: check for divergence
 
1945
                # client-side, then do _set_last_revision.
 
1946
                last_rev = revision.ensure_null(self.last_revision())
 
1947
                if graph is None:
 
1948
                    graph = self.repository.get_graph()
 
1949
                if self._check_if_descendant_or_diverged(
 
1950
                        stop_revision, last_rev, graph, other):
 
1951
                    # stop_revision is a descendant of last_rev, but we aren't
 
1952
                    # overwriting, so we're done.
 
1953
                    return
 
1954
                self._set_last_revision(stop_revision)
 
1955
        finally:
 
1956
            other.unlock()
 
1957
 
 
1958
 
 
1959
def _extract_tar(tar, to_dir):
 
1960
    """Extract all the contents of a tarfile object.
 
1961
 
 
1962
    A replacement for extractall, which is not present in python2.4
 
1963
    """
 
1964
    for tarinfo in tar:
 
1965
        tar.extract(tarinfo, to_dir)
 
1966
 
 
1967
 
 
1968
def _translate_error(err, **context):
 
1969
    """Translate an ErrorFromSmartServer into a more useful error.
 
1970
 
 
1971
    Possible context keys:
 
1972
      - branch
 
1973
      - repository
 
1974
      - bzrdir
 
1975
      - token
 
1976
      - other_branch
 
1977
      - path
 
1978
 
 
1979
    If the error from the server doesn't match a known pattern, then
 
1980
    UnknownErrorFromSmartServer is raised.
 
1981
    """
 
1982
    def find(name):
 
1983
        try:
 
1984
            return context[name]
 
1985
        except KeyError, key_err:
 
1986
            mutter('Missing key %r in context %r', key_err.args[0], context)
 
1987
            raise err
 
1988
    def get_path():
 
1989
        """Get the path from the context if present, otherwise use first error
 
1990
        arg.
 
1991
        """
 
1992
        try:
 
1993
            return context['path']
 
1994
        except KeyError, key_err:
 
1995
            try:
 
1996
                return err.error_args[0]
 
1997
            except IndexError, idx_err:
 
1998
                mutter(
 
1999
                    'Missing key %r in context %r', key_err.args[0], context)
 
2000
                raise err
 
2001
 
 
2002
    if err.error_verb == 'NoSuchRevision':
 
2003
        raise NoSuchRevision(find('branch'), err.error_args[0])
 
2004
    elif err.error_verb == 'nosuchrevision':
 
2005
        raise NoSuchRevision(find('repository'), err.error_args[0])
 
2006
    elif err.error_tuple == ('nobranch',):
 
2007
        raise errors.NotBranchError(path=find('bzrdir').root_transport.base)
 
2008
    elif err.error_verb == 'norepository':
 
2009
        raise errors.NoRepositoryPresent(find('bzrdir'))
 
2010
    elif err.error_verb == 'LockContention':
 
2011
        raise errors.LockContention('(remote lock)')
 
2012
    elif err.error_verb == 'UnlockableTransport':
 
2013
        raise errors.UnlockableTransport(find('bzrdir').root_transport)
 
2014
    elif err.error_verb == 'LockFailed':
 
2015
        raise errors.LockFailed(err.error_args[0], err.error_args[1])
 
2016
    elif err.error_verb == 'TokenMismatch':
 
2017
        raise errors.TokenMismatch(find('token'), '(remote token)')
 
2018
    elif err.error_verb == 'Diverged':
 
2019
        raise errors.DivergedBranches(find('branch'), find('other_branch'))
 
2020
    elif err.error_verb == 'TipChangeRejected':
 
2021
        raise errors.TipChangeRejected(err.error_args[0].decode('utf8'))
 
2022
    elif err.error_verb == 'UnstackableBranchFormat':
 
2023
        raise errors.UnstackableBranchFormat(*err.error_args)
 
2024
    elif err.error_verb == 'UnstackableRepositoryFormat':
 
2025
        raise errors.UnstackableRepositoryFormat(*err.error_args)
 
2026
    elif err.error_verb == 'NotStacked':
 
2027
        raise errors.NotStacked(branch=find('branch'))
 
2028
    elif err.error_verb == 'PermissionDenied':
 
2029
        path = get_path()
 
2030
        if len(err.error_args) >= 2:
 
2031
            extra = err.error_args[1]
 
2032
        else:
 
2033
            extra = None
 
2034
        raise errors.PermissionDenied(path, extra=extra)
 
2035
    elif err.error_verb == 'ReadError':
 
2036
        path = get_path()
 
2037
        raise errors.ReadError(path)
 
2038
    elif err.error_verb == 'NoSuchFile':
 
2039
        path = get_path()
 
2040
        raise errors.NoSuchFile(path)
 
2041
    elif err.error_verb == 'FileExists':
 
2042
        raise errors.FileExists(err.error_args[0])
 
2043
    elif err.error_verb == 'DirectoryNotEmpty':
 
2044
        raise errors.DirectoryNotEmpty(err.error_args[0])
 
2045
    elif err.error_verb == 'ShortReadvError':
 
2046
        args = err.error_args
 
2047
        raise errors.ShortReadvError(
 
2048
            args[0], int(args[1]), int(args[2]), int(args[3]))
 
2049
    elif err.error_verb in ('UnicodeEncodeError', 'UnicodeDecodeError'):
 
2050
        encoding = str(err.error_args[0]) # encoding must always be a string
 
2051
        val = err.error_args[1]
 
2052
        start = int(err.error_args[2])
 
2053
        end = int(err.error_args[3])
 
2054
        reason = str(err.error_args[4]) # reason must always be a string
 
2055
        if val.startswith('u:'):
 
2056
            val = val[2:].decode('utf-8')
 
2057
        elif val.startswith('s:'):
 
2058
            val = val[2:].decode('base64')
 
2059
        if err.error_verb == 'UnicodeDecodeError':
 
2060
            raise UnicodeDecodeError(encoding, val, start, end, reason)
 
2061
        elif err.error_verb == 'UnicodeEncodeError':
 
2062
            raise UnicodeEncodeError(encoding, val, start, end, reason)
 
2063
    elif err.error_verb == 'ReadOnlyError':
 
2064
        raise errors.TransportNotPossible('readonly transport')
 
2065
    raise errors.UnknownErrorFromSmartServer(err)