-
Notifications
You must be signed in to change notification settings - Fork 20
/
rsync-repos
executable file
·63 lines (55 loc) · 2.34 KB
/
rsync-repos
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#!/usr/bin/env python
#
# Use rsync to intelligently sync OSTree repositories. This
# avoids a few race conditions and issues that could otherwise
# occur if one simply does the whole repository in a single run.
#
# Known issues:
# - Ignores the fact that detached metadata (e.g. GPG signatures)
# can be updated asynchronously.
#
# Copyright 2016 Colin Walters <[email protected]>
# Licensed under the new-BSD license (http://www.opensource.org/licenses/bsd-license.php)
import os, sys, subprocess, argparse, shlex
def fatal(msg):
print >>sys.stderr, msg
sys.exit(1)
parser = argparse.ArgumentParser()
parser.add_argument("--src", help="Source path",
action='store', required=True)
parser.add_argument("--dest", help="Destination path",
action='store', required=True)
parser.add_argument("--rsync-opts", help="Additional rsync options (parsed via shell)",
action='store', default=None)
parser.add_argument("--rsync-opt", help="Additional (single) rsync option",
action='append', default=[])
args = parser.parse_args()
if not args.src.endswith('/'):
args.src = args.src + '/'
if not args.dest.endswith('/'):
args.dest = args.dest + '/'
rsync_opts = []
if args.rsync_opts is not None:
rsync_opts.extend(shlex.split(args.rsync_opts))
rsync_opts.extend(args.rsync_opt)
def rsync(paths, opts, ignore_missing_args=False):
argv = ['rsync', '-rlpt']
for path in paths:
argv.append('--include=' + path)
argv.extend(['--exclude=*', args.src, args.dest])
argv.extend(rsync_opts)
argv.extend(opts)
print("Executing: {}".format(subprocess.list2cmdline(argv)))
subprocess.check_call(argv)
OBJECTS_AND_DELTAS = ['/objects', '/objects/**', '/deltas', '/deltas/**']
REFS_AND_SUMMARY = ['/refs', '/refs/**', '/summary*', '/summaries', '/summaries/**']
CONFIG = ['/config']
# We rsync in reverse data dependence order - the summary and refs
# point to objects + deltas. Our first pass over the objects doesn't
# perform any deletions, as that would create race conditions. We
# do handle deletions for refs and summary.
rsync(OBJECTS_AND_DELTAS, ['--ignore-existing'])
rsync(REFS_AND_SUMMARY, ['--delete'])
# Finally, we handle any deletions for objects and deltas.
rsync(OBJECTS_AND_DELTAS, ['--ignore-existing', '--delete'])
rsync(CONFIG, ['--ignore-existing'])