~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to bzrtools.py

  • Committer: Robert Collins
  • Date: 2005-09-13 10:46:27 UTC
  • mto: (147.2.6) (364.1.3 bzrtools)
  • mto: This revision was merged to the branch mainline in revision 324.
  • Revision ID: robertc@robertcollins.net-20050913104627-51f938950a907475
handle inaccessible sibling archives somewhat - note version-0 is still not handled

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
 
1
# Copyright (C) 2005 Aaron Bentley
 
2
# <aaron.bentley@utoronto.ca>
3
3
#
4
4
#    This program is free software; you can redistribute it and/or modify
5
5
#    it under the terms of the GNU General Public License as published by
14
14
#    You should have received a copy of the GNU General Public License
15
15
#    along with this program; if not, write to the Free Software
16
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
 
 
 
17
import bzrlib
 
18
import os
 
19
import os.path
 
20
import sys
 
21
import tempfile
 
22
import shutil
 
23
from subprocess import Popen, PIPE
 
24
 
 
25
def temp_branch():
 
26
    dirname = tempfile.mkdtemp("temp-branch")
 
27
    return bzrlib.branch.Branch(dirname, init=True)
 
28
 
 
29
def rm_branch(br):
 
30
    shutil.rmtree(br.base)
 
31
 
 
32
def is_clean(cur_branch):
 
33
    """
 
34
    Return true if no files are modifed or unknown
 
35
    >>> import bzrlib.add
 
36
    >>> br = temp_branch()
 
37
    >>> is_clean(br)
 
38
    (True, [])
 
39
    >>> fooname = os.path.join(br.base, "foo")
 
40
    >>> file(fooname, "wb").write("bar")
 
41
    >>> is_clean(br)
 
42
    (True, ['foo'])
 
43
    >>> bzrlib.add.smart_add_branch(br, [br.base])
 
44
    1
 
45
    >>> is_clean(br)
 
46
    (False, [])
 
47
    >>> br.commit("added file")
 
48
    added foo
 
49
    >>> is_clean(br)
 
50
    (True, [])
 
51
    >>> rm_branch(br)
 
52
    """
 
53
    from bzrlib.diff import compare_trees
 
54
    old_tree = cur_branch.basis_tree()
 
55
    new_tree = cur_branch.working_tree()
 
56
    non_source = []
 
57
    for path, file_class, kind, file_id in new_tree.list_files():
 
58
        if file_class in ('?', 'I'):
 
59
            non_source.append(path)
 
60
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
 
61
    if len(delta.added) > 0 or len(delta.removed) > 0 or \
 
62
        len(delta.modified) > 0:
 
63
        return False, non_source
 
64
    return True, non_source 
 
65
 
 
66
def set_pull_data(br, location, rev_id):
 
67
    pull_file = file (br.controlfilename("x-pull-data"), "wb")
 
68
    pull_file.write("%s\n%s\n" % (location, rev_id))
 
69
 
 
70
def get_pull_data(br):
 
71
    """
 
72
    >>> br = temp_branch()
 
73
    >>> get_pull_data(br)
 
74
    (None, None)
 
75
    >>> set_pull_data(br, 'http://somewhere', '888-777')
 
76
    >>> get_pull_data(br)
 
77
    ('http://somewhere', '888-777')
 
78
    >>> rm_branch(br)
 
79
    """
 
80
    filename = br.controlfilename("x-pull-data")
 
81
    if not os.path.exists(filename):
 
82
        return (None, None)
 
83
    pull_file = file (filename, "rb")
 
84
    location, rev_id = [f.rstrip('\n') for f in pull_file]
 
85
    return location, rev_id
 
86
 
 
87
def set_push_data(br, location):
 
88
    push_file = file (br.controlfilename("x-push-data"), "wb")
 
89
    push_file.write("%s\n" % location)
 
90
 
 
91
def get_push_data(br):
 
92
    """
 
93
    >>> br = temp_branch()
 
94
    >>> get_push_data(br) is None
 
95
    True
 
96
    >>> set_push_data(br, 'http://somewhere')
 
97
    >>> get_push_data(br)
 
98
    'http://somewhere'
 
99
    >>> rm_branch(br)
 
100
    """
 
101
    filename = br.controlfilename("x-push-data")
 
102
    if not os.path.exists(filename):
 
103
        return None
 
104
    push_file = file (filename, "rb")
 
105
    (location,) = [f.rstrip('\n') for f in push_file]
 
106
    return location
 
107
 
 
108
"""
 
109
>>> shell_escape('hello')
 
110
'\h\e\l\l\o'
 
111
"""
 
112
def shell_escape(arg):
 
113
    return "".join(['\\'+c for c in arg])
 
114
 
 
115
def safe_system(args):
 
116
    """
 
117
    >>> real_system = os.system
 
118
    >>> os.system = sys.stdout.write
 
119
    >>> safe_system(['a', 'b', 'cd'])
 
120
    \\a \\b \\c\\d
 
121
    >>> os.system = real_system
 
122
    """
 
123
    arg_str = " ".join([shell_escape(a) for a in args])
 
124
    return os.system(arg_str)
 
125
 
 
126
def rsync(source, target, ssh=False, excludes=()):
 
127
    """
 
128
    >>> real_system = os.system
 
129
    >>> os.system = sys.stdout.write
 
130
    >>> rsync("a", "b")
 
131
    ['rsync', '-av', '--delete', 'a', 'b']
 
132
    >>> rsync("a", "b", excludes=("*.py",))
 
133
    ['rsync', '-av', '--delete', '--exclude-from', '-', 'a', 'b']
 
134
    >>> os.system = real_system
 
135
    """
 
136
    cmd = ["rsync", "-av", "--delete"]
 
137
    if ssh:
 
138
        cmd.extend(('-e', 'ssh'))
 
139
    if len(excludes) > 0:
 
140
        cmd.extend(('--exclude-from', '-'))
 
141
    cmd.extend((source, target))
 
142
    proc = Popen(cmd, stdin=PIPE)
 
143
    proc.stdin.write('\n'.join(excludes)+'\n')
 
144
    proc.stdin.close()
 
145
    proc.wait()
 
146
    return cmd
 
147
 
 
148
exclusions = ('.bzr/x-push-data', '.bzr/x-pull-data', '.bzr/stat-cache')
 
149
 
 
150
 
 
151
def push(cur_branch, location=None):
 
152
    push_location = get_push_data(cur_branch)
 
153
    if location is not None:
 
154
        if not location.endswith('/'):
 
155
            location += '/'
 
156
        push_location = location
 
157
    
 
158
    if push_location is None:
 
159
        print "No push location saved.  Please specify one on the command line."
 
160
        sys.exit(1)
 
161
 
 
162
    clean, non_source = is_clean(cur_branch)
 
163
    if not clean:
 
164
        print """Error: This tree has uncommitted changes or unknown (?) files.
 
165
Use "bzr status" to list them."""
 
166
        sys.exit(1)
 
167
    non_source.extend(exclusions)
 
168
 
 
169
    print "Pushing to %s" % push_location
 
170
    rsync(cur_branch.base+'/', push_location, ssh=True, excludes=non_source)
 
171
 
 
172
    set_push_data(cur_branch, push_location)
112
173
 
113
174
def run_tests():
114
175
    import doctest