~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 Aaron Bentley
2
 
# <aaron.bentley@utoronto.ca>
 
1
# Copyright (C) 2005-2009, 2011-2013 Aaron Bentley <aaron@aaronbentley.com>
 
2
# Copyright (C) 2007 John Arbash Meinel
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
 
import bzrlib
18
 
import os
19
 
import os.path
20
 
import sys
21
 
import tempfile
22
 
import shutil
23
 
 
24
 
def temp_branch():
25
 
    dirname = tempfile.mkdtemp("temp-branch")
26
 
    return bzrlib.Branch(dirname, init=True)
27
 
 
28
 
def rm_branch(br):
29
 
    shutil.rmtree(br.base)
30
 
 
31
 
def is_clean(cur_branch):
32
 
    """
33
 
    Return true if no files are modifed or unknown
34
 
    >>> br = temp_branch()
35
 
    >>> is_clean(br)
36
 
    True
37
 
    >>> fooname = os.path.join(br.base, "foo")
38
 
    >>> file(fooname, "wb").write("bar")
39
 
    >>> is_clean(br)
40
 
    False
41
 
    >>> bzrlib.add.smart_add([fooname])
42
 
    >>> is_clean(br)
43
 
    False
44
 
    >>> br.commit("added file")
45
 
    >>> is_clean(br)
46
 
    True
47
 
    >>> rm_branch(br)
48
 
    """
49
 
    from bzrlib.diff import compare_trees
50
 
    old_tree = cur_branch.basis_tree()
51
 
    new_tree = cur_branch.working_tree()
52
 
    for path, file_class, kind, file_id in new_tree.list_files():
53
 
        if file_class == '?':
54
 
            return False
55
 
    delta = compare_trees(old_tree, new_tree, want_unchanged=False)
56
 
    if len(delta.added) > 0 or len(delta.removed) > 0 or \
57
 
        len(delta.modified) > 0:
58
 
        return False
59
 
    return True
60
 
 
61
 
def set_pull_data(br, location, rev_id):
62
 
    pull_file = file (br.controlfilename("x-pull-data"), "wb")
63
 
    pull_file.write("%s\n%s\n" % (location, rev_id))
64
 
 
65
 
def get_pull_data(br):
66
 
    """
67
 
    >>> br = temp_branch()
68
 
    >>> get_pull_data(br)
69
 
    (None, None)
70
 
    >>> set_pull_data(br, 'http://somewhere', '888-777')
71
 
    >>> get_pull_data(br)
72
 
    ('http://somewhere', '888-777')
73
 
    >>> rm_branch(br)
74
 
    """
75
 
    filename = br.controlfilename("x-pull-data")
76
 
    if not os.path.exists(filename):
77
 
        return (None, None)
78
 
    pull_file = file (filename, "rb")
79
 
    location, rev_id = [f.rstrip('\n') for f in pull_file]
80
 
    return location, rev_id
81
 
 
82
 
def set_push_data(br, location):
83
 
    push_file = file (br.controlfilename("x-push-data"), "wb")
84
 
    push_file.write("%s\n" % location)
85
 
 
86
 
def get_push_data(br):
87
 
    """
88
 
    >>> br = temp_branch()
89
 
    >>> get_push_data(br) is None
90
 
    True
91
 
    >>> set_push_data(br, 'http://somewhere')
92
 
    >>> get_push_data(br)
93
 
    'http://somewhere'
94
 
    >>> rm_branch(br)
95
 
    """
96
 
    filename = br.controlfilename("x-push-data")
97
 
    if not os.path.exists(filename):
98
 
        return None
99
 
    push_file = file (filename, "rb")
100
 
    (location,) = [f.rstrip('\n') for f in push_file]
101
 
    return location
102
 
 
103
 
"""
104
 
>>> shell_escape('hello')
105
 
'\h\e\l\l\o'
106
 
"""
107
 
def shell_escape(arg):
108
 
    return "".join(['\\'+c for c in arg])
109
 
 
110
 
def safe_system(args):
111
 
    """
112
 
    >>> real_system = os.system
113
 
    >>> os.system = sys.stdout.write
114
 
    >>> safe_system(['a', 'b', 'cd'])
115
 
    \\a \\b \\c\\d
116
 
    >>> os.system = real_system
117
 
    """
118
 
    arg_str = " ".join([shell_escape(a) for a in args])
119
 
    return os.system(arg_str)
120
 
 
121
 
def rsync(source, target, ssh=False, exclude_globs=()):
122
 
    """
123
 
    >>> real_system = os.system
124
 
    >>> os.system = sys.stdout.write
125
 
    >>> rsync("a", "b")
126
 
    \\r\\s\\y\\n\\c \\-\\a\\v \\-\\-\\d\\e\\l\\e\\t\\e \\a \\b
127
 
    >>> rsync("a", "b", exclude_globs=("*.py",))
128
 
    \\r\\s\\y\\n\\c \\-\\a\\v \\-\\-\\d\\e\\l\\e\\t\\e\
129
 
 \\-\\-\\e\\x\\c\\l\\u\\d\\e \\*\\.\\p\\y \\a \\b
130
 
    >>> os.system = real_system
131
 
    """
132
 
    cmd = ["rsync", "-av", "--delete"]
133
 
    if ssh:
134
 
        cmd.extend(('-e', 'ssh'))
135
 
    for exclude in exclude_globs:
136
 
        cmd.extend(('--exclude', exclude))
137
 
    cmd.extend((source, target))
138
 
    safe_system(cmd)
139
 
 
140
 
exclusions = ('x-push-data', 'x-pull-data')
141
 
 
142
 
 
143
 
def pull(cur_branch, location=None, overwrite=False):
144
 
    pull_location, pull_revision = get_pull_data(cur_branch)
145
 
    if pull_location is not None:
146
 
        if not overwrite and cur_branch.last_patch() != pull_revision:
147
 
            print "Aborting: This branch has had commits, so pull would lose data."
148
 
            sys.exit(1)
149
 
    if location is not None:
150
 
        pull_location = location
151
 
        if not pull_location.endswith('/'):
152
 
            pull_location+='/'
153
 
 
154
 
    if pull_location is None:
155
 
        print "No pull location saved.  Please specify one on the command line."
156
 
        sys.exit(1)
157
 
 
158
 
    if not is_clean(cur_branch):
159
 
        print "Error: This tree has uncommitted changes or unknown (?) files."
160
 
        sys.exit(1)
161
 
 
162
 
    print "Synchronizing with %s" % pull_location
163
 
    rsync (pull_location, cur_branch.base+'/', exclude_globs=exclusions)
164
 
 
165
 
    set_pull_data(cur_branch, pull_location, cur_branch.last_patch())
166
 
 
167
 
 
168
 
def push(cur_branch, location=None):
169
 
    push_location = get_push_data(cur_branch)
170
 
    if location is not None:
171
 
        if not location.endswith('/'):
172
 
            location += '/'
173
 
        push_location = location
174
 
    
175
 
    if push_location is None:
176
 
        print "No push location saved.  Please specify one on the command line."
177
 
        sys.exit(1)
178
 
 
179
 
    if not is_clean(cur_branch):
180
 
        print """Error: This tree has uncommitted changes or unknown (?) files.
181
 
Use "bzr status" to list them."""
182
 
        sys.exit(1)
183
 
 
184
 
    print "Pushing to %s" % push_location
185
 
    rsync(cur_branch.base+'/', push_location, ssh=True,
186
 
          exclude_globs=exclusions)
187
 
 
188
 
    set_push_data(cur_branch, push_location)
 
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
 
189
112
 
190
113
def run_tests():
191
114
    import doctest