~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2008-01-11 03:01:54 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20080111030154-apm50v0b0tu93prh
Support branch6 formats in rspush

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import sys
25
25
 
26
26
import bzrlib
27
 
from bzrlib import trace, urlutils
 
27
from bzrlib import revision as _mod_revision, trace, urlutils
28
28
import bzrlib.errors
29
29
from bzrlib.errors import (
30
30
    BzrCommandError,
112
112
 
113
113
def rsync(source, target, ssh=False, excludes=(), silent=False,
114
114
          rsync_name="rsync"):
115
 
    """
116
 
    >>> new_dir = tempfile.mkdtemp()
117
 
    >>> old_dir = os.getcwd()
118
 
    >>> os.chdir(new_dir)
119
 
    >>> rsync("a", "b", silent=True)
120
 
    Traceback (most recent call last):
121
 
    RsyncNoFile: No such file...
122
 
    >>> rsync(new_dir + "/a", new_dir + "/b", excludes=("*.py",), silent=True)
123
 
    Traceback (most recent call last):
124
 
    RsyncNoFile: No such file...
125
 
    >>> rsync(new_dir + "/a", new_dir + "/b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
126
 
    Traceback (most recent call last):
127
 
    NoRsync: rsyncc not found.
128
 
    >>> os.chdir(old_dir)
129
 
    >>> os.rmdir(new_dir)
130
 
    """
131
115
    cmd = [rsync_name, "-av", "--delete"]
132
116
    if ssh:
133
117
        cmd.extend(('-e', 'ssh'))
197
181
    return [l.rstrip('\r\n') for l in
198
182
            codecs.open(fname, 'rb', 'utf-8').readlines()]
199
183
 
 
184
 
 
185
def read_revision_info(path):
 
186
    """Parse a last_revision file to determine revision_info"""
 
187
    line = open(path, 'rb').readlines()[0].strip('\n')
 
188
    revno, revision_id = line.split(' ', 1)
 
189
    revno = int(revno)
 
190
    return revno, revision_id
 
191
 
 
192
 
200
193
class RsyncNoFile(Exception):
201
194
    def __init__(self, path):
202
195
        Exception.__init__(self, "No such file %s" % path)
234
227
    return history
235
228
 
236
229
 
 
230
def get_revision_info(location, _rsync):
 
231
    """Get the revsision_info for an rsync-able branch"""
 
232
    tempdir = tempfile.mkdtemp('push')
 
233
    my_rsync = _rsync
 
234
    if my_rsync is None:
 
235
        my_rsync = rsync
 
236
    try:
 
237
        info_fname = os.path.join(tempdir, 'last-revision')
 
238
        cmd = rsync(location+'.bzr/branch/last-revision', info_fname,
 
239
                    silent=True)
 
240
        return read_revision_info(info_fname)
 
241
    finally:
 
242
        shutil.rmtree(tempdir)
 
243
 
 
244
 
237
245
def history_subset(location, branch, _rsync=None):
238
 
    remote_history = get_revision_history(location, _rsync)
239
246
    local_history = branch.revision_history()
240
 
    if len(remote_history) > len(local_history):
241
 
        return False
242
 
    for local, remote in zip(remote_history, local_history):
243
 
        if local != remote:
 
247
    try:
 
248
        remote_history = get_revision_history(location, _rsync)
 
249
    except RsyncNoFile:
 
250
        revno, revision_id = get_revision_info(location, _rsync)
 
251
        if revision_id == _mod_revision.NULL_REVISION:
 
252
            return True
 
253
        return bool(revision_id.decode('utf-8') in local_history)
 
254
    else:
 
255
        if len(remote_history) > len(local_history):
244
256
            return False
245
 
    return True
 
257
        for local, remote in zip(remote_history, local_history):
 
258
            if local != remote:
 
259
                return False
 
260
        return True
 
261
 
246
262
 
247
263
def empty_or_absent(location):
248
264
    try:
316
332
                    ' "%s" is of the form "machine:/path".' % push_location)
317
333
        trace.note("Pushing to %s", push_location)
318
334
        my_rsync(tree.basedir+'/', push_location, ssh=usessh,
319
 
              excludes=final_exclusions)
 
335
                 excludes=final_exclusions)
320
336
 
321
337
        set_push_data(tree, push_location)
322
338
    finally: