121
123
arg_str = " ".join([shell_escape(a) for a in args])
122
124
return os.system(arg_str)
124
class RsyncUnknownStatus(Exception):
125
def __init__(self, status):
126
Exception.__init__(self, "Unknown status: %d" % status)
128
class NoRsync(Exception):
129
def __init__(self, rsync_name):
130
Exception.__init__(self, "%s not found." % rsync_name)
132
def rsync(source, target, ssh=False, excludes=(), silent=False,
135
>>> rsync("a", "b", silent=True)
136
Traceback (most recent call last):
137
RsyncNoFile: No such file a
138
>>> rsync("a", "b", excludes=("*.py",), silent=True)
139
Traceback (most recent call last):
140
RsyncNoFile: No such file a
141
>>> rsync("a", "b", excludes=("*.py",), silent=True, rsync_name="rsyncc")
142
Traceback (most recent call last):
143
NoRsync: rsyncc not found.
145
cmd = [rsync_name, "-av", "--delete"]
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"]
147
138
cmd.extend(('-e', 'ssh'))
148
139
if len(excludes) > 0:
149
140
cmd.extend(('--exclude-from', '-'))
150
141
cmd.extend((source, target))
158
proc = Popen(cmd, stdin=PIPE, stderr=stderr, stdout=stdout)
160
if e.errno == errno.ENOENT:
161
raise NoRsync(rsync_name)
142
proc = Popen(cmd, stdin=PIPE)
163
143
proc.stdin.write('\n'.join(excludes)+'\n')
164
144
proc.stdin.close()
171
if proc.returncode == 12:
172
raise RsyncStreamIO()
173
elif proc.returncode == 23:
174
raise RsyncNoFile(source)
175
elif proc.returncode != 0:
176
raise RsyncUnknownStatus(proc.returncode)
180
def rsync_ls(source, ssh=False, silent=True):
183
cmd.extend(('-e', 'ssh'))
189
proc = Popen(cmd, stderr=stderr, stdout=PIPE)
190
result = proc.stdout.read()
196
if proc.returncode == 12:
197
raise RsyncStreamIO()
198
elif proc.returncode == 23:
199
raise RsyncNoFile(source)
200
elif proc.returncode != 0:
201
raise RsyncUnknownStatus(proc.returncode)
202
return [l.split(' ')[-1].rstrip('\n') for l in result.splitlines(True)]
204
exclusions = ('.bzr/x-push-data', '.bzr/parent', '.bzr/x-pull-data',
205
'.bzr/x-pull', '.bzr/pull', '.bzr/stat-cache',
209
def read_revision_history(fname):
210
return [l.rstrip('\r\n') for l in
211
codecs.open(fname, 'rb', 'utf-8').readlines()]
213
class RsyncNoFile(Exception):
214
def __init__(self, path):
215
Exception.__init__(self, "No such file %s" % path)
217
class RsyncStreamIO(Exception):
219
Exception.__init__(self, "Error in rsync protocol data stream.")
221
def get_revision_history(location):
222
tempdir = tempfile.mkdtemp('push')
224
history_fname = os.path.join(tempdir, 'revision-history')
225
cmd = rsync(location+'.bzr/revision-history', history_fname,
227
history = read_revision_history(history_fname)
229
shutil.rmtree(tempdir)
232
def history_subset(location, branch):
233
remote_history = get_revision_history(location)
234
local_history = branch.revision_history()
235
if len(remote_history) > len(local_history):
237
for local, remote in zip(remote_history, local_history):
242
def empty_or_absent(location):
244
files = rsync_ls(location)
245
return files == ['.']
249
def push(cur_branch, location=None, overwrite=False):
148
exclusions = ('.bzr/x-push-data', '.bzr/x-pull-data', '.bzr/stat-cache')
151
def push(cur_branch, location=None):
250
152
push_location = get_push_data(cur_branch)
251
153
if location is not None:
252
154
if not location.endswith('/'):