123
122
arg_str = " ".join([shell_escape(a) for a in args])
124
123
return os.system(arg_str)
126
def rsync(source, target, ssh=False, excludes=()):
128
>>> real_system = os.system
129
>>> os.system = sys.stdout.write
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
136
cmd = ["rsync", "-av", "--delete"]
125
class RsyncUnknownStatus(Exception):
126
def __init__(self, status):
127
Exception.__init__(self, "Unknown status: %d" % status)
129
class NoRsync(Exception):
130
def __init__(self, rsync_name):
131
Exception.__init__(self, "%s not found." % rsync_name)
133
def rsync(source, target, ssh=False, excludes=(), silent=False,
136
>>> rsync("a", "b", silent=True)
137
Traceback (most recent call last):
138
RsyncNoFile: No such file a
139
>>> rsync("a", "b", excludes=("*.py",), silent=True)
140
Traceback (most recent call last):
141
RsyncNoFile: No such file a
142
>>> rsync("a", "b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
143
Traceback (most recent call last):
144
NoRsync: rsyncc not found.
146
cmd = [rsync_name, "-av", "--delete"]
138
148
cmd.extend(('-e', 'ssh'))
139
149
if len(excludes) > 0:
140
150
cmd.extend(('--exclude-from', '-'))
141
151
cmd.extend((source, target))
142
proc = Popen(cmd, stdin=PIPE)
159
proc = Popen(cmd, stdin=PIPE, stderr=stderr, stdout=stdout)
161
if e.errno == errno.ENOENT:
162
raise NoRsync(rsync_name)
143
164
proc.stdin.write('\n'.join(excludes)+'\n')
144
165
proc.stdin.close()
172
if proc.returncode == 12:
173
raise RsyncStreamIO()
174
elif proc.returncode == 23:
175
raise RsyncNoFile(source)
176
elif proc.returncode != 0:
177
raise RsyncUnknownStatus(proc.returncode)
148
exclusions = ('.bzr/x-push-data', '.bzr/x-pull-data', '.bzr/stat-cache')
151
def push(cur_branch, location=None):
181
def rsync_ls(source, ssh=False, silent=True):
184
cmd.extend(('-e', 'ssh'))
190
proc = Popen(cmd, stderr=stderr, stdout=PIPE)
191
result = proc.stdout.read()
197
if proc.returncode == 12:
198
raise RsyncStreamIO()
199
elif proc.returncode == 23:
200
raise RsyncNoFile(source)
201
elif proc.returncode != 0:
202
raise RsyncUnknownStatus(proc.returncode)
203
return [l.split(' ')[-1].rstrip('\n') for l in result.splitlines(True)]
205
exclusions = ('.bzr/x-push-data', '.bzr/parent', '.bzr/x-pull-data',
206
'.bzr/x-pull', '.bzr/pull', '.bzr/stat-cache',
210
def read_revision_history(fname):
211
return [l.rstrip('\r\n') for l in
212
codecs.open(fname, 'rb', 'utf-8').readlines()]
214
class RsyncNoFile(Exception):
215
def __init__(self, path):
216
Exception.__init__(self, "No such file %s" % path)
218
class RsyncStreamIO(Exception):
220
Exception.__init__(self, "Error in rsync protocol data stream.")
222
def get_revision_history(location):
223
tempdir = tempfile.mkdtemp('push')
225
history_fname = os.path.join(tempdir, 'revision-history')
226
cmd = rsync(location+'.bzr/revision-history', history_fname,
228
history = read_revision_history(history_fname)
230
shutil.rmtree(tempdir)
233
def history_subset(location, branch):
234
remote_history = get_revision_history(location)
235
local_history = branch.revision_history()
236
if len(remote_history) > len(local_history):
238
for local, remote in zip(remote_history, local_history):
243
def empty_or_absent(location):
245
files = rsync_ls(location)
246
return files == ['.']
250
def push(cur_branch, location=None, overwrite=False):
152
251
push_location = get_push_data(cur_branch)
153
252
if location is not None:
154
253
if not location.endswith('/'):