~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Robert Collins
  • Date: 2006-04-24 01:45:23 UTC
  • mto: (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 366.
  • Revision ID: robertc@robertcollins.net-20060424014523-ef18f22034f17ec2
Backout debugging tweak to fai.py

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")
301
303
    return new_committer
302
304
 
303
305
 
 
306
def apache_ls(t):
 
307
    """Screen-scrape Apache listings"""
 
308
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
 
309
        ' <a href="'
 
310
    lines = t.get('.')
 
311
    expr = re.compile('<a[^>]*href="([^>]*)"[^>]*>', flags=re.I)
 
312
    for line in lines:
 
313
        match = expr.search(line)
 
314
        if match is None:
 
315
            continue
 
316
        url = match.group(1)
 
317
        if url.startswith('http://') or url.startswith('/') or '../' in url:
 
318
            continue
 
319
        if '?' in url:
 
320
            continue
 
321
        yield url.rstrip('/')
 
322
 
 
323
 
 
324
def iter_branches(t, lister=None):
 
325
    """Iterate through all the branches under a transport"""
 
326
    for bzrdir in iter_bzrdirs(t, lister):
 
327
        try:
 
328
            branch = bzrdir.open_branch()
 
329
            if branch.bzrdir is bzrdir:
 
330
                yield branch
 
331
        except (NotBranchError, UnsupportedFormatError):
 
332
            pass
 
333
 
 
334
 
 
335
def iter_branch_tree(t, lister=None):
 
336
    for bzrdir in iter_bzrdirs(t, lister):
 
337
        try:
 
338
            wt = bzrdir.open_workingtree()
 
339
            yield wt.branch, wt
 
340
        except NoWorkingTree, UnsupportedFormatError:
 
341
            try:
 
342
                branch = bzrdir.open_branch()
 
343
                if branch.bzrdir is bzrdir:
 
344
                    yield branch, None
 
345
            except (NotBranchError, UnsupportedFormatError):
 
346
                continue
 
347
 
 
348
 
 
349
def iter_bzrdirs(t, lister=None):
 
350
    if lister is None:
 
351
        def lister(t):
 
352
            return t.list_dir('.')
 
353
    try:
 
354
        bzrdir = bzrdir_from_transport(t)
 
355
        yield bzrdir
 
356
    except (NotBranchError, UnsupportedFormatError, TransportError,
 
357
            PermissionDenied):
 
358
        pass
 
359
    try:
 
360
        for directory in lister(t):
 
361
            if directory == ".bzr":
 
362
                continue
 
363
            try:
 
364
                subt = t.clone(directory)
 
365
            except UnicodeDecodeError:
 
366
                continue
 
367
            for bzrdir in iter_bzrdirs(subt, lister):
 
368
                yield bzrdir
 
369
    except (NoSuchFile, PermissionDenied, TransportError):
 
370
        pass
 
371
 
 
372
    
 
373
def bzrdir_from_transport(t):
 
374
    """Open a bzrdir from a transport (not a location)"""
 
375
    format = BzrDirFormat.find_format(t)
 
376
    BzrDir._check_supported(format, False)
 
377
    return format.open(t)
 
378
 
 
379
 
304
380
def run_tests():
305
381
    import doctest
306
382
    result = doctest.testmod()