~abentley/bzrtools/bzrtools.dev

783 by Aaron Bentley
Remove rspush
1
# Copyright (C) 2005-2009, 2011-2012 Aaron Bentley <aaron@aaronbentley.com>
513 by Aaron Bentley
Fix change dectection for dirstate
2
# Copyright (C) 2007 John Arbash Meinel
89 by Aaron Bentley
Added copyright/GPL notices
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
147.1.59 by Aaron Bentley
Reverted bzrtools.py to mainline version.
17
import re
18
783 by Aaron Bentley
Remove rspush
19
from bzrlib import urlutils
583 by Aaron Bentley
rspush requires standalone trees
20
from bzrlib.errors import (
21
    BzrCommandError,
22
    NotBranchError,
23
    NoSuchFile,
24
    )
776.2.1 by Jelmer Vernooij
Remove unused imports and fix two imports.
25
from bzrlib.bzrdir import BzrDir
563 by Aaron Bentley
Allow importing directly from a URL
26
from bzrlib.transport import get_transport
147.1.59 by Aaron Bentley
Reverted bzrtools.py to mainline version.
27
321.1.2 by Aaron Bentley
Applied Robert's random fixes as non-merges
28
292 by Aaron Bentley
Introduced branch-history command
29
def short_committer(committer):
30
    new_committer = re.sub('<.*>', '', committer).strip(' ')
31
    if len(new_committer) < 2:
32
        return committer
33
    return new_committer
34
35
354 by Aaron Bentley
Added apache index scraping to the branches command
36
def apache_ls(t):
37
    """Screen-scrape Apache listings"""
355 by Aaron Bentley
Handled more hrefs
38
    apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
39
        ' <a href="'
571 by Aaron Bentley
Branches works with Apache again
40
    t = t.clone()
41
    t._remote_path = lambda x: t.base
602 by Aaron Bentley
Update branches to new find_branches API
42
    try:
43
        lines = t.get('')
776.2.1 by Jelmer Vernooij
Remove unused imports and fix two imports.
44
    except NoSuchFile:
602 by Aaron Bentley
Update branches to new find_branches API
45
        return
46
    expr = re.compile('<a[^>]*href="([^>]*)\/"[^>]*>', flags=re.I)
354 by Aaron Bentley
Added apache index scraping to the branches command
47
    for line in lines:
355 by Aaron Bentley
Handled more hrefs
48
        match = expr.search(line)
49
        if match is None:
50
            continue
51
        url = match.group(1)
52
        if url.startswith('http://') or url.startswith('/') or '../' in url:
53
            continue
357 by Aaron Bentley
Tweakage of the apache scraper
54
        if '?' in url:
55
            continue
355 by Aaron Bentley
Handled more hrefs
56
        yield url.rstrip('/')
354 by Aaron Bentley
Added apache index scraping to the branches command
57
58
603 by Aaron Bentley
Update branches, multi-pull to new APIs, create trees
59
def list_branches(t):
60
    def is_inside(branch):
61
        return bool(branch.base.startswith(t.base))
62
63
    if t.base.startswith('http://'):
64
        def evaluate(bzrdir):
65
            try:
66
                branch = bzrdir.open_branch()
67
                if is_inside(branch):
68
                    return True, branch
69
                else:
70
                    return True, None
604 by Aaron Bentley
Fix error handling
71
            except NotBranchError:
603 by Aaron Bentley
Update branches, multi-pull to new APIs, create trees
72
                return True, None
73
        return [b for b in BzrDir.find_bzrdirs(t, list_current=apache_ls,
74
                evaluate=evaluate) if b is not None]
75
    elif not t.listable():
76
        raise BzrCommandError("Can't list this type of location.")
77
    return [b for b in BzrDir.find_branches(t) if is_inside(b)]
78
79
80
def evaluate_branch_tree(bzrdir):
81
    try:
82
        tree, branch = bzrdir._get_tree_branch()
83
    except NotBranchError:
84
        return True, None
85
    else:
86
        return True, (branch, tree)
353 by Aaron Bentley
Added multi-pull, working on branches and checkouts
87
354 by Aaron Bentley
Added apache index scraping to the branches command
88
89
def iter_branch_tree(t, lister=None):
603 by Aaron Bentley
Update branches, multi-pull to new APIs, create trees
90
    return (x for x in BzrDir.find_bzrdirs(t, evaluate=evaluate_branch_tree,
91
            list_current=lister) if x is not None)
352 by Aaron Bentley
Added branches subcommand
92
93
563 by Aaron Bentley
Allow importing directly from a URL
94
def open_from_url(location):
95
    location = urlutils.normalize_url(location)
96
    dirname, basename = urlutils.split(location)
695 by Aaron Bentley
bzr patch handles URLs with trailing slashes
97
    if location.endswith('/') and not basename.endswith('/'):
98
        basename += '/'
563 by Aaron Bentley
Allow importing directly from a URL
99
    return get_transport(dirname).get(basename)
100
101
19 by abentley
librified most of the pull script
102
def run_tests():
16 by abentley
Got is_clean under test, added setters/getters for pull data
103
    import doctest
18 by abentley
Finished implementing bzr-pull
104
    result = doctest.testmod()
19 by abentley
librified most of the pull script
105
    if result[1] > 0:
106
        if result[0] == 0:
107
            print "All tests passed"
108
    else:
109
        print "No tests to run"
110
if __name__ == "__main__":
111
    run_tests()