123
126
arg_str = " ".join([shell_escape(a) for a in args])
124
127
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"]
129
class RsyncUnknownStatus(Exception):
130
def __init__(self, status):
131
Exception.__init__(self, "Unknown status: %d" % status)
133
class NoRsync(Exception):
134
def __init__(self, rsync_name):
135
Exception.__init__(self, "%s not found." % rsync_name)
137
def rsync(source, target, ssh=False, excludes=(), silent=False,
140
>>> rsync("a", "b", silent=True)
141
Traceback (most recent call last):
142
RsyncNoFile: No such file a
143
>>> rsync("a", "b", excludes=("*.py",), silent=True)
144
Traceback (most recent call last):
145
RsyncNoFile: No such file a
146
>>> rsync("a", "b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
147
Traceback (most recent call last):
148
NoRsync: rsyncc not found.
150
cmd = [rsync_name, "-av", "--delete"]
138
152
cmd.extend(('-e', 'ssh'))
139
153
if len(excludes) > 0:
140
154
cmd.extend(('--exclude-from', '-'))
141
155
cmd.extend((source, target))
142
proc = Popen(cmd, stdin=PIPE)
163
proc = Popen(cmd, stdin=PIPE, stderr=stderr, stdout=stdout)
165
if e.errno == errno.ENOENT:
166
raise NoRsync(rsync_name)
143
168
proc.stdin.write('\n'.join(excludes)+'\n')
144
169
proc.stdin.close()
176
if proc.returncode == 12:
177
raise RsyncStreamIO()
178
elif proc.returncode == 23:
179
raise RsyncNoFile(source)
180
elif proc.returncode != 0:
181
raise RsyncUnknownStatus(proc.returncode)
148
exclusions = ('.bzr/x-push-data', '.bzr/x-pull-data', '.bzr/stat-cache')
151
def push(cur_branch, location=None):
185
def rsync_ls(source, ssh=False, silent=True):
188
cmd.extend(('-e', 'ssh'))
194
proc = Popen(cmd, stderr=stderr, stdout=PIPE)
195
result = proc.stdout.read()
201
if proc.returncode == 12:
202
raise RsyncStreamIO()
203
elif proc.returncode == 23:
204
raise RsyncNoFile(source)
205
elif proc.returncode != 0:
206
raise RsyncUnknownStatus(proc.returncode)
207
return [l.split(' ')[-1].rstrip('\n') for l in result.splitlines(True)]
209
exclusions = ('.bzr/x-push-data', '.bzr/parent', '.bzr/x-pull-data',
210
'.bzr/x-pull', '.bzr/pull', '.bzr/stat-cache',
214
def read_revision_history(fname):
215
return [l.rstrip('\r\n') for l in
216
codecs.open(fname, 'rb', 'utf-8').readlines()]
218
class RsyncNoFile(Exception):
219
def __init__(self, path):
220
Exception.__init__(self, "No such file %s" % path)
222
class RsyncStreamIO(Exception):
224
Exception.__init__(self, "Error in rsync protocol data stream.")
226
def get_revision_history(location):
227
tempdir = tempfile.mkdtemp('push')
229
history_fname = os.path.join(tempdir, 'revision-history')
230
cmd = rsync(location+'.bzr/revision-history', history_fname,
232
history = read_revision_history(history_fname)
234
shutil.rmtree(tempdir)
237
def history_subset(location, branch):
238
remote_history = get_revision_history(location)
239
local_history = branch.revision_history()
240
if len(remote_history) > len(local_history):
242
for local, remote in zip(remote_history, local_history):
247
def empty_or_absent(location):
249
files = rsync_ls(location)
250
return files == ['.']
254
def push(cur_branch, location=None, overwrite=False):
152
255
push_location = get_push_data(cur_branch)
153
256
if location is not None:
154
257
if not location.endswith('/'):