~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-07-05 05:32:42 UTC
  • Revision ID: aaron.bentley@utoronto.ca-20060705053242-b9bbea9d1acd32d6
Make Ellerman's cdiff colours the default

Show diffs side-by-side

added added

removed removed

Lines of Context:
25
25
 
26
26
import bzrlib
27
27
import bzrlib.errors
28
 
from bzrlib.errors import BzrCommandError
29
 
from bzrlib.bzrdir import BzrDir
 
28
from bzrlib.errors import (BzrCommandError, NotBranchError, NoSuchFile,
 
29
                           UnsupportedFormatError, TransportError, 
 
30
                           NoWorkingTree, PermissionDenied)
 
31
from bzrlib.bzrdir import BzrDir, BzrDirFormat
30
32
 
31
33
def temp_tree():
32
34
    dirname = tempfile.mkdtemp("temp-branch")
50
52
    ([u'foo'], {})
51
53
    >>> is_clean(tree)
52
54
    (False, [])
53
 
    >>> tree.commit("added file")
 
55
    >>> tree.commit("added file", rev_id='commit-id')
 
56
    'commit-id'
54
57
    >>> is_clean(tree)
55
58
    (True, [])
56
59
    >>> rm_tree(tree)
66
69
    return not delta.has_changed(), non_source
67
70
 
68
71
def set_push_data(tree, location):
69
 
    push_file = file (tree.branch.control_files.controlfilename("x-push-data"), "wb")
70
 
    push_file.write("%s\n" % location)
 
72
    tree.branch.control_files.put_utf8("x-push-data", "%s\n" % location)
71
73
 
72
74
def get_push_data(tree):
73
75
    """
76
78
    True
77
79
    >>> set_push_data(tree, 'http://somewhere')
78
80
    >>> get_push_data(tree)
79
 
    'http://somewhere'
 
81
    u'http://somewhere'
80
82
    >>> rm_tree(tree)
81
83
    """
82
 
    filename = tree.branch.control_files.controlfilename("x-push-data")
83
 
    if not os.path.exists(filename):
 
84
    try:
 
85
        location = tree.branch.control_files.get_utf8('x-push-data').read()
 
86
    except NoSuchFile:
84
87
        return None
85
 
    push_file = file (filename, "rb")
86
 
    (location,) = [f.rstrip('\n') for f in push_file]
87
 
    return location
 
88
    return location.rstrip('\n')
88
89
 
89
90
"""
90
91
>>> shell_escape('hello')
239
240
    except RsyncNoFile:
240
241
        return True
241
242
 
242
 
def push(tree, location=None, overwrite=False, working_tree=True):
 
243
def rspush(tree, location=None, overwrite=False, working_tree=True):
243
244
    push_location = get_push_data(tree)
244
245
    if location is not None:
245
246
        if not location.endswith('/'):
247
248
        push_location = location
248
249
    
249
250
    if push_location is None:
250
 
        if tree.branch.get_push_location() is None:
251
 
            raise BzrCommandError("No push location known or specified.")
252
 
        else:
253
 
            raise bzrlib.errors.MustUseDecorated
254
 
 
255
 
    if push_location.find('://') != -1:
256
 
        raise bzrlib.errors.MustUseDecorated
257
 
 
258
 
    if push_location.find(':') == -1:
259
 
        raise bzrlib.errors.MustUseDecorated
 
251
        raise BzrCommandError("No rspush location known or specified.")
 
252
 
 
253
    if (push_location.find('://') != -1 or
 
254
        push_location.find(':') == -1):
 
255
        raise BzrCommandError("Invalid rsync path %r." % push_location)
260
256
 
261
257
    if working_tree:
262
258
        clean, non_source = is_clean(tree)
301
297
    return new_committer
302
298
 
303
299
 
 
300
def apache_ls(t):
 
301
    """Screen-scrape Apache listings"""
 
302
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
 
303
        ' <a href="'
 
304
    lines = t.get('.')
 
305
    expr = re.compile('<a[^>]*href="([^>]*)"[^>]*>', flags=re.I)
 
306
    for line in lines:
 
307
        match = expr.search(line)
 
308
        if match is None:
 
309
            continue
 
310
        url = match.group(1)
 
311
        if url.startswith('http://') or url.startswith('/') or '../' in url:
 
312
            continue
 
313
        if '?' in url:
 
314
            continue
 
315
        yield url.rstrip('/')
 
316
 
 
317
 
 
318
def iter_branches(t, lister=None):
 
319
    """Iterate through all the branches under a transport"""
 
320
    for bzrdir in iter_bzrdirs(t, lister):
 
321
        try:
 
322
            branch = bzrdir.open_branch()
 
323
            if branch.bzrdir is bzrdir:
 
324
                yield branch
 
325
        except (NotBranchError, UnsupportedFormatError):
 
326
            pass
 
327
 
 
328
 
 
329
def iter_branch_tree(t, lister=None):
 
330
    for bzrdir in iter_bzrdirs(t, lister):
 
331
        try:
 
332
            wt = bzrdir.open_workingtree()
 
333
            yield wt.branch, wt
 
334
        except NoWorkingTree, UnsupportedFormatError:
 
335
            try:
 
336
                branch = bzrdir.open_branch()
 
337
                if branch.bzrdir is bzrdir:
 
338
                    yield branch, None
 
339
            except (NotBranchError, UnsupportedFormatError):
 
340
                continue
 
341
 
 
342
 
 
343
def iter_bzrdirs(t, lister=None):
 
344
    if lister is None:
 
345
        def lister(t):
 
346
            return t.list_dir('.')
 
347
    try:
 
348
        bzrdir = bzrdir_from_transport(t)
 
349
        yield bzrdir
 
350
    except (NotBranchError, UnsupportedFormatError, TransportError,
 
351
            PermissionDenied):
 
352
        pass
 
353
    try:
 
354
        for directory in lister(t):
 
355
            if directory == ".bzr":
 
356
                continue
 
357
            try:
 
358
                subt = t.clone(directory)
 
359
            except UnicodeDecodeError:
 
360
                continue
 
361
            for bzrdir in iter_bzrdirs(subt, lister):
 
362
                yield bzrdir
 
363
    except (NoSuchFile, PermissionDenied, TransportError):
 
364
        pass
 
365
 
 
366
    
 
367
def bzrdir_from_transport(t):
 
368
    """Open a bzrdir from a transport (not a location)"""
 
369
    format = BzrDirFormat.find_format(t)
 
370
    BzrDir._check_supported(format, False)
 
371
    return format.open(t)
 
372
 
 
373
 
304
374
def run_tests():
305
375
    import doctest
306
376
    result = doctest.testmod()