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
20
from bzrlib import urlutils
21
from bzrlib.errors import (
26
from bzrlib.bzrdir import BzrDir
27
from bzrlib.transport import get_transport
31
def read_locked(lockable):
32
"""Read-lock a tree, branch or repository in this context."""
40
def short_committer(committer):
41
new_committer = re.sub('<.*>', '', committer).strip(' ')
42
if len(new_committer) < 2:
48
"""Screen-scrape Apache listings"""
49
apache_dir = '<img border="0" src="/icons/folder.gif" alt="[dir]">'\
52
t._remote_path = lambda x: t.base
57
expr = re.compile('<a[^>]*href="([^>]*)\/"[^>]*>', flags=re.I)
59
match = expr.search(line)
63
if url.startswith('http://') or url.startswith('/') or '../' in url:
71
def is_inside(branch):
72
return bool(branch.base.startswith(t.base))
74
if t.base.startswith('http://'):
77
branch = bzrdir.open_branch()
82
except NotBranchError:
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)]
91
def evaluate_branch_tree(bzrdir):
93
tree, branch = bzrdir._get_tree_branch()
94
except NotBranchError:
97
return True, (branch, tree)
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)
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('/'):
110
return get_transport(dirname).get(basename)
25
dirname = tempfile.mkdtemp("temp-branch")
26
return bzrlib.Branch(dirname, init=True)
29
shutil.rmtree(br.base)
31
def is_clean(cur_branch):
33
Return true if no files are modifed or unknown
34
>>> br = temp_branch()
37
>>> fooname = os.path.join(br.base, "foo")
38
>>> file(fooname, "wb").write("bar")
41
>>> bzrlib.add.smart_add([fooname])
44
>>> br.commit("added file")
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():
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:
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))
65
def get_pull_data(br):
67
>>> br = temp_branch()
70
>>> set_pull_data(br, 'http://somewhere', '888-777')
72
('http://somewhere', '888-777')
75
filename = br.controlfilename("x-pull-data")
76
if not os.path.exists(filename):
78
pull_file = file (filename, "rb")
79
location, rev_id = [f.rstrip('\n') for f in pull_file]
80
return location, rev_id
82
def set_push_data(br, location):
83
push_file = file (br.controlfilename("x-push-data"), "wb")
84
push_file.write("%s\n" % location)
86
def get_push_data(br):
88
>>> br = temp_branch()
89
>>> get_push_data(br) is None
91
>>> set_push_data(br, 'http://somewhere')
96
filename = br.controlfilename("x-push-data")
97
if not os.path.exists(filename):
99
push_file = file (filename, "rb")
100
(location,) = [f.rstrip('\n') for f in push_file]
104
>>> shell_escape('hello')
107
def shell_escape(arg):
108
return "".join(['\\'+c for c in arg])
110
def safe_system(args):
112
>>> real_system = os.system
113
>>> os.system = sys.stdout.write
114
>>> safe_system(['a', 'b', 'cd'])
116
>>> os.system = real_system
118
arg_str = " ".join([shell_escape(a) for a in args])
119
return os.system(arg_str)
121
def rsync(source, target, ssh=False, exclude_globs=()):
123
>>> real_system = os.system
124
>>> os.system = sys.stdout.write
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
132
cmd = ["rsync", "-av", "--delete"]
134
cmd.extend(('-e', 'ssh'))
135
for exclude in exclude_globs:
136
cmd.extend(('--exclude', exclude))
137
cmd.extend((source, target))
140
exclusions = ('x-push-data', 'x-pull-data')
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."
149
if location is not None:
150
pull_location = location
151
if not pull_location.endswith('/'):
154
if pull_location is None:
155
print "No pull location saved. Please specify one on the command line."
158
if not is_clean(cur_branch):
159
print "Error: This tree has uncommitted changes or unknown (?) files."
162
print "Synchronizing with %s" % pull_location
163
rsync (pull_location, cur_branch.base+'/', exclude_globs=exclusions)
165
set_pull_data(cur_branch, pull_location, cur_branch.last_patch())
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('/'):
173
push_location = location
175
if push_location is None:
176
print "No push location saved. Please specify one on the command line."
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."""
184
print "Pushing to %s" % push_location
185
rsync(cur_branch.base+'/', push_location, ssh=True,
186
exclude_globs=exclusions)
188
set_push_data(cur_branch, push_location)