~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2007-08-23 13:57:32 UTC
  • mfrom: (577.1.2 bzrtools)
  • Revision ID: abentley@panoramicfeedback.com-20070823135732-i86dvwgmgpm0vagh
MergeĀ fromĀ home

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright (C) 2005 Aaron Bentley
2
 
# <aaron.bentley@utoronto.ca>
 
1
# Copyright (C) 2005, 2006, 2007 Aaron Bentley <aaron.bentley@utoronto.ca>
 
2
# Copyright (C) 2007 John Arbash Meinel
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
24
24
import sys
25
25
 
26
26
import bzrlib
 
27
from bzrlib import urlutils
27
28
import bzrlib.errors
28
29
from bzrlib.errors import (BzrCommandError, NotBranchError, NoSuchFile,
29
 
                           UnsupportedFormatError, TransportError, 
30
 
                           NoWorkingTree, PermissionDenied)
 
30
                           UnsupportedFormatError, TransportError,
 
31
                           NoWorkingTree, PermissionDenied, ConnectionError)
31
32
from bzrlib.bzrdir import BzrDir, BzrDirFormat
 
33
from bzrlib.transport import get_transport
32
34
 
33
35
def temp_tree():
34
36
    dirname = tempfile.mkdtemp("temp-branch")
40
42
def is_clean(cur_tree):
41
43
    """
42
44
    Return true if no files are modifed or unknown
43
 
    >>> import bzrlib.add
44
 
    >>> tree = temp_tree()
45
 
    >>> is_clean(tree)
46
 
    (True, [])
47
 
    >>> fooname = os.path.join(tree.basedir, "foo")
48
 
    >>> file(fooname, "wb").write("bar")
49
 
    >>> is_clean(tree)
50
 
    (True, [u'foo'])
51
 
    >>> bzrlib.add.smart_add_tree(tree, [tree.basedir])
52
 
    ([u'foo'], {})
53
 
    >>> is_clean(tree)
54
 
    (False, [])
55
 
    >>> tree.commit("added file", rev_id='commit-id')
56
 
    'commit-id'
57
 
    >>> is_clean(tree)
58
 
    (True, [])
59
 
    >>> rm_tree(tree)
60
45
    """
61
46
    old_tree = cur_tree.basis_tree()
62
47
    new_tree = cur_tree
63
48
    non_source = []
64
 
    for path, file_class, kind, file_id, entry in new_tree.list_files():
65
 
        if file_class in ('?', 'I'):
66
 
            non_source.append(path)
67
 
    delta = new_tree.changes_from(old_tree, want_unchanged=False)
 
49
    new_tree.lock_read()
 
50
    try:
 
51
        for path, file_class, kind, file_id, entry in new_tree.list_files():
 
52
            if file_class in ('?', 'I'):
 
53
                non_source.append(path)
 
54
        delta = new_tree.changes_from(old_tree, want_unchanged=False)
 
55
    finally:
 
56
        new_tree.unlock()
68
57
    return not delta.has_changed(), non_source
69
58
 
70
59
def set_push_data(tree, location):
112
101
    def __init__(self, rsync_name):
113
102
        Exception.__init__(self, "%s not found." % rsync_name)
114
103
 
115
 
def rsync(source, target, ssh=False, excludes=(), silent=False, 
 
104
def rsync(source, target, ssh=False, excludes=(), silent=False,
116
105
          rsync_name="rsync"):
117
106
    """
118
107
    >>> new_dir = tempfile.mkdtemp()
147
136
    except OSError, e:
148
137
        if e.errno == errno.ENOENT:
149
138
            raise NoRsync(rsync_name)
150
 
            
 
139
 
151
140
    proc.stdin.write('\n'.join(excludes)+'\n')
152
141
    proc.stdin.close()
153
142
    if silent:
189
178
        raise RsyncUnknownStatus(proc.returncode)
190
179
    return [l.split(' ')[-1].rstrip('\n') for l in result.splitlines(True)]
191
180
 
192
 
exclusions = ('.bzr/x-push-data', '.bzr/branch/x-push/data', '.bzr/parent', 
 
181
exclusions = ('.bzr/x-push-data', '.bzr/branch/x-push/data', '.bzr/parent',
193
182
              '.bzr/branch/parent', '.bzr/x-pull-data', '.bzr/x-pull',
194
183
              '.bzr/pull', '.bzr/stat-cache', '.bzr/x-rsync-data',
195
184
              '.bzr/basis-inventory', '.bzr/inventory.backup.weave')
229
218
        return False
230
219
    for local, remote in zip(remote_history, local_history):
231
220
        if local != remote:
232
 
            return False 
 
221
            return False
233
222
    return True
234
223
 
235
224
def empty_or_absent(location):
245
234
        if not location.endswith('/'):
246
235
            location += '/'
247
236
        push_location = location
248
 
    
 
237
 
249
238
    if push_location is None:
250
239
        raise BzrCommandError("No rspush location known or specified.")
251
240
 
288
277
                " specified location.  Please ensure that"
289
278
                ' "%s" is of the form "machine:/path".' % push_location)
290
279
    print "Pushing to %s" % push_location
291
 
    rsync(tree.basedir+'/', push_location, ssh=usessh, 
 
280
    rsync(tree.basedir+'/', push_location, ssh=usessh,
292
281
          excludes=final_exclusions)
293
282
 
294
283
    set_push_data(tree, push_location)
305
294
    """Screen-scrape Apache listings"""
306
295
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
307
296
        ' <a href="'
308
 
    lines = t.get('.')
 
297
    t = t.clone()
 
298
    t._remote_path = lambda x: t.base
 
299
    lines = t.get('')
309
300
    expr = re.compile('<a[^>]*href="([^>]*)"[^>]*>', flags=re.I)
310
301
    for line in lines:
311
302
        match = expr.search(line)
351
342
    try:
352
343
        bzrdir = bzrdir_from_transport(t)
353
344
        yield bzrdir
 
345
    except (ConnectionError):
 
346
        raise
354
347
    except (NotBranchError, UnsupportedFormatError, TransportError,
355
348
            PermissionDenied):
356
349
        pass
367
360
    except (NoSuchFile, PermissionDenied, TransportError):
368
361
        pass
369
362
 
370
 
    
 
363
 
371
364
def bzrdir_from_transport(t):
372
365
    """Open a bzrdir from a transport (not a location)"""
373
366
    format = BzrDirFormat.find_format(t)
375
368
    return format.open(t)
376
369
 
377
370
 
 
371
def open_from_url(location):
 
372
    location = urlutils.normalize_url(location)
 
373
    dirname, basename = urlutils.split(location)
 
374
    return get_transport(dirname).get(basename)
 
375
 
 
376
 
378
377
def run_tests():
379
378
    import doctest
380
379
    result = doctest.testmod()