~abentley/bzrtools/bzrtools.dev

« back to all changes in this revision

Viewing changes to scriptlib.py

  • Committer: Aaron Bentley
  • Date: 2005-06-07 18:52:04 UTC
  • Revision ID: abentley@panoramicfeedback.com-20050607185204-5fc1f0e3d393b909
Added NEWS, obsoleted bzr-pull

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