~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2007-12-22 02:01:03 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20071222020103-ggjszok7n974e1l2
Update branches, multi-pull to new APIs, create trees

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005, 2006, 2007 Aaron Bentley <aaron@aaronbentley.com>
 
1
# Copyright (C) 2005, 2006, 2007 Aaron Bentley <aaron.bentley@utoronto.ca>
2
2
# Copyright (C) 2007 John Arbash Meinel
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
24
24
import sys
25
25
 
26
26
import bzrlib
27
 
from bzrlib import revision as _mod_revision, trace, urlutils
 
27
from bzrlib import trace, urlutils
28
28
import bzrlib.errors
29
29
from bzrlib.errors import (
30
30
    BzrCommandError,
65
65
    return not delta.has_changed(), non_source
66
66
 
67
67
def set_push_data(tree, location):
68
 
    tree.branch._transport.put_bytes("x-push-data", "%s\n" % location)
 
68
    tree.branch.control_files.put_utf8("x-push-data", "%s\n" % location)
69
69
 
70
70
def get_push_data(tree):
71
71
    """
78
78
    >>> rm_tree(tree)
79
79
    """
80
80
    try:
81
 
        location = tree.branch._transport.get('x-push-data').read()
 
81
        location = tree.branch.control_files.get_utf8('x-push-data').read()
82
82
    except NoSuchFile:
83
83
        return None
84
 
    location = location.decode('utf-8')
85
84
    return location.rstrip('\n')
86
85
 
87
86
"""
113
112
 
114
113
def rsync(source, target, ssh=False, excludes=(), silent=False,
115
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
    """
116
131
    cmd = [rsync_name, "-av", "--delete"]
117
132
    if ssh:
118
133
        cmd.extend(('-e', 'ssh'))
182
197
    return [l.rstrip('\r\n') for l in
183
198
            codecs.open(fname, 'rb', 'utf-8').readlines()]
184
199
 
185
 
 
186
 
def read_revision_info(path):
187
 
    """Parse a last_revision file to determine revision_info"""
188
 
    line = open(path, 'rb').readlines()[0].strip('\n')
189
 
    revno, revision_id = line.split(' ', 1)
190
 
    revno = int(revno)
191
 
    return revno, revision_id
192
 
 
193
 
 
194
200
class RsyncNoFile(Exception):
195
201
    def __init__(self, path):
196
202
        Exception.__init__(self, "No such file %s" % path)
228
234
    return history
229
235
 
230
236
 
231
 
def get_revision_info(location, _rsync):
232
 
    """Get the revsision_info for an rsync-able branch"""
233
 
    tempdir = tempfile.mkdtemp('push')
234
 
    my_rsync = _rsync
235
 
    if my_rsync is None:
236
 
        my_rsync = rsync
237
 
    try:
238
 
        info_fname = os.path.join(tempdir, 'last-revision')
239
 
        cmd = rsync(location+'.bzr/branch/last-revision', info_fname,
240
 
                    silent=True)
241
 
        return read_revision_info(info_fname)
242
 
    finally:
243
 
        shutil.rmtree(tempdir)
244
 
 
245
 
 
246
237
def history_subset(location, branch, _rsync=None):
 
238
    remote_history = get_revision_history(location, _rsync)
247
239
    local_history = branch.revision_history()
248
 
    try:
249
 
        remote_history = get_revision_history(location, _rsync)
250
 
    except RsyncNoFile:
251
 
        revno, revision_id = get_revision_info(location, _rsync)
252
 
        if revision_id == _mod_revision.NULL_REVISION:
253
 
            return True
254
 
        return bool(revision_id.decode('utf-8') in local_history)
255
 
    else:
256
 
        if len(remote_history) > len(local_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:
257
244
            return False
258
 
        for local, remote in zip(remote_history, local_history):
259
 
            if local != remote:
260
 
                return False
261
 
        return True
262
 
 
 
245
    return True
263
246
 
264
247
def empty_or_absent(location):
265
248
    try:
333
316
                    ' "%s" is of the form "machine:/path".' % push_location)
334
317
        trace.note("Pushing to %s", push_location)
335
318
        my_rsync(tree.basedir+'/', push_location, ssh=usessh,
336
 
                 excludes=final_exclusions)
 
319
              excludes=final_exclusions)
337
320
 
338
321
        set_push_data(tree, push_location)
339
322
    finally:
382
365
                    return True, branch
383
366
                else:
384
367
                    return True, None
385
 
            except NotBranchError:
 
368
            except errors.NotBranchError:
386
369
                return True, None
387
370
        return [b for b in BzrDir.find_bzrdirs(t, list_current=apache_ls,
388
371
                evaluate=evaluate) if b is not None]