~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Aaron Bentley
  • Date: 2013-08-20 03:02:43 UTC
  • Revision ID: aaron@aaronbentley.com-20130820030243-r8v1xfbcnd8f10p4
Fix zap command for 2.6/7

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2005-2009, 2011-2013 Aaron Bentley <aaron@aaronbentley.com>
 
2
# Copyright (C) 2007 John Arbash Meinel
 
3
#
 
4
#    This program is free software; you can redistribute it and/or modify
 
5
#    it under the terms of the GNU General Public License as published by
 
6
#    the Free Software Foundation; either version 2 of the License, or
 
7
#    (at your option) any later version.
 
8
#
 
9
#    This program is distributed in the hope that it will be useful,
 
10
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
#    GNU General Public License for more details.
 
13
#
 
14
#    You should have received a copy of the GNU General Public License
 
15
#    along with this program; if not, write to the Free Software
 
16
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
17
from contextlib import contextmanager
 
18
import re
 
19
 
 
20
from bzrlib import urlutils
 
21
from bzrlib.errors import (
 
22
    BzrCommandError,
 
23
    NotBranchError,
 
24
    NoSuchFile,
 
25
    )
 
26
from bzrlib.bzrdir import BzrDir
 
27
from bzrlib.transport import get_transport
 
28
 
 
29
 
 
30
@contextmanager
 
31
def read_locked(lockable):
 
32
    """Read-lock a tree, branch or repository in this context."""
 
33
    lockable.lock_read()
 
34
    try:
 
35
        yield lockable
 
36
    finally:
 
37
        lockable.unlock()
 
38
 
 
39
 
 
40
def short_committer(committer):
 
41
    new_committer = re.sub('<.*>', '', committer).strip(' ')
 
42
    if len(new_committer) < 2:
 
43
        return committer
 
44
    return new_committer
 
45
 
 
46
 
 
47
def apache_ls(t):
 
48
    """Screen-scrape Apache listings"""
 
49
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
 
50
        ' <a href="'
 
51
    t = t.clone()
 
52
    t._remote_path = lambda x: t.base
 
53
    try:
 
54
        lines = t.get('')
 
55
    except NoSuchFile:
 
56
        return
 
57
    expr = re.compile('<a[^>]*href="([^>]*)\/"[^>]*>', flags=re.I)
 
58
    for line in lines:
 
59
        match = expr.search(line)
 
60
        if match is None:
 
61
            continue
 
62
        url = match.group(1)
 
63
        if url.startswith('http://') or url.startswith('/') or '../' in url:
 
64
            continue
 
65
        if '?' in url:
 
66
            continue
 
67
        yield url.rstrip('/')
 
68
 
 
69
 
 
70
def list_branches(t):
 
71
    def is_inside(branch):
 
72
        return bool(branch.base.startswith(t.base))
 
73
 
 
74
    if t.base.startswith('http://'):
 
75
        def evaluate(bzrdir):
 
76
            try:
 
77
                branch = bzrdir.open_branch()
 
78
                if is_inside(branch):
 
79
                    return True, branch
 
80
                else:
 
81
                    return True, None
 
82
            except NotBranchError:
 
83
                return True, None
 
84
        return [b for b in BzrDir.find_bzrdirs(t, list_current=apache_ls,
 
85
                evaluate=evaluate) if b is not None]
 
86
    elif not t.listable():
 
87
        raise BzrCommandError("Can't list this type of location.")
 
88
    return [b for b in BzrDir.find_branches(t) if is_inside(b)]
 
89
 
 
90
 
 
91
def evaluate_branch_tree(bzrdir):
 
92
    try:
 
93
        tree, branch = bzrdir._get_tree_branch()
 
94
    except NotBranchError:
 
95
        return True, None
 
96
    else:
 
97
        return True, (branch, tree)
 
98
 
 
99
 
 
100
def iter_branch_tree(t, lister=None):
 
101
    return (x for x in BzrDir.find_bzrdirs(t, evaluate=evaluate_branch_tree,
 
102
            list_current=lister) if x is not None)
 
103
 
 
104
 
 
105
def open_from_url(location):
 
106
    location = urlutils.normalize_url(location)
 
107
    dirname, basename = urlutils.split(location)
 
108
    if location.endswith('/') and not basename.endswith('/'):
 
109
        basename += '/'
 
110
    return get_transport(dirname).get(basename)
 
111
 
 
112
 
 
113
def run_tests():
 
114
    import doctest
 
115
    result = doctest.testmod()
 
116
    if result[1] > 0:
 
117
        if result[0] == 0:
 
118
            print "All tests passed"
 
119
    else:
 
120
        print "No tests to run"
 
121
if __name__ == "__main__":
 
122
    run_tests()