~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2006-03-22 22:18:19 UTC
  • Revision ID: abentley@panoramicfeedback.com-20060322221819-0e627e73d1232926
Added zap command

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, NotBranchError, NoSuchFile,
29
 
                           UnsupportedFormatError, TransportError, 
30
 
                           NoWorkingTree, PermissionDenied)
31
 
from bzrlib.bzrdir import BzrDir, BzrDirFormat
 
28
from bzrlib.errors import BzrCommandError
 
29
from bzrlib.bzrdir import BzrDir
32
30
 
33
31
def temp_tree():
34
32
    dirname = tempfile.mkdtemp("temp-branch")
303
301
    return new_committer
304
302
 
305
303
 
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
 
 
380
304
def run_tests():
381
305
    import doctest
382
306
    result = doctest.testmod()