| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
import os |
| 14 |
import subprocess |
| 15 |
import sys |
| 16 |
|
| 17 |
from rsyncfns import ( |
| 18 |
CHKDIR, FROMDIR, RSYNC, RSYNC_PEER, SCRATCHDIR, SRCDIR, TMPDIR, TODIR, |
| 19 |
all_plus, allspace, dots, |
| 20 |
checkdiff, checkit, makepath, rsync_argv, run_rsync, test_fail, rsync_path_arg, rsh_cmd, |
| 21 |
) |
| 22 |
|
| 23 |
|
| 24 |
os.environ['CVSIGNORE'] = '*.junk' |
| 25 |
|
| 26 |
script_name = os.path.basename(sys.argv[0] if sys.argv[0] else __file__) |
| 27 |
if 'lsh' in script_name: |
| 28 |
os.environ['RSYNC_RSH'] = rsh_cmd() |
| 29 |
rpath = [f'--rsync-path={rsync_path_arg()}'] |
| 30 |
host = 'lh:' |
| 31 |
else: |
| 32 |
rpath = [] |
| 33 |
host = '' |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
makepath( |
| 38 |
FROMDIR / 'foo/down/to/you', |
| 39 |
FROMDIR / 'foo/sub', |
| 40 |
FROMDIR / 'bar/down/to/foo/too', |
| 41 |
FROMDIR / 'bar/down/to/bar/baz', |
| 42 |
FROMDIR / 'mid/for/foo/and/that/is/who', |
| 43 |
FROMDIR / 'new/keep/this', |
| 44 |
FROMDIR / 'new/lose/this', |
| 45 |
) |
| 46 |
|
| 47 |
(FROMDIR / '.filt').write_text( |
| 48 |
"exclude down\n" |
| 49 |
": .filt-temp\n" |
| 50 |
"clear\n" |
| 51 |
"- .filt\n" |
| 52 |
"- *.bak\n" |
| 53 |
"- *.old\n" |
| 54 |
) |
| 55 |
(FROMDIR / 'foo' / 'file1').write_text("filtered-1\n") |
| 56 |
(FROMDIR / 'foo' / 'file2').write_text("removed\n") |
| 57 |
(FROMDIR / 'foo' / 'file2.old').write_text("cvsout\n") |
| 58 |
(FROMDIR / 'foo' / '.filt').write_text("include .filt\n- /file1\n") |
| 59 |
(FROMDIR / 'foo' / 'sub' / 'file1').write_text("not-filtered-1\n") |
| 60 |
|
| 61 |
(FROMDIR / 'bar' / '.filt').write_text( |
| 62 |
"- home-cvs-exclude\n" |
| 63 |
"dir-merge .filt2\n" |
| 64 |
"+ to\n" |
| 65 |
) |
| 66 |
(FROMDIR / 'bar' / 'down' / 'to' / 'home-cvs-exclude').write_text("cvsout\n") |
| 67 |
(FROMDIR / 'bar' / 'down' / 'to' / '.filt2').write_text("- .filt2\n") |
| 68 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / '.filt2').write_text("+ *.junk\n") |
| 69 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'file1').write_text("keeper\n") |
| 70 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'file1.bak').write_text("cvsout\n") |
| 71 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'file3').write_text("gone\n") |
| 72 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'file4').write_text("lost\n") |
| 73 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / '+ file3').write_text("weird\n") |
| 74 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'file4.junk').write_text("cvsout-but-filtin\n") |
| 75 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'to').write_text("smashed\n") |
| 76 |
(FROMDIR / 'bar' / 'down' / 'to' / 'bar' / '.filt2').write_text("- *.deep\n") |
| 77 |
(FROMDIR / 'bar' / 'down' / 'to' / 'bar' / 'baz' / 'file5.deep').write_text("filtout\n") |
| 78 |
|
| 79 |
|
| 80 |
(FROMDIR / 'mid' / '.filt2').write_text("- extra\n") |
| 81 |
(FROMDIR / 'mid' / 'one-in-one-out').write_text("cvsout\n") |
| 82 |
(FROMDIR / 'mid' / '.cvsignore').write_text("one-in-one-out\n") |
| 83 |
(FROMDIR / 'mid' / 'one-for-all').write_text("cvsin\n") |
| 84 |
(FROMDIR / 'mid' / '.filt').write_text(":C\n") |
| 85 |
(FROMDIR / 'mid' / 'for' / 'one-in-one-out').write_text("cvsin\n") |
| 86 |
(FROMDIR / 'mid' / 'for' / 'foo' / 'extra').write_text("expunged\n") |
| 87 |
(FROMDIR / 'mid' / 'for' / 'foo' / 'keep').write_text("retained\n") |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
excl = SCRATCHDIR / 'exclude-from' |
| 92 |
excl.write_text( |
| 93 |
"!\n" |
| 94 |
"# If the second line of these two lines does anything, it's a bug.\n" |
| 95 |
"+ **/bar\n" |
| 96 |
"- /bar\n" |
| 97 |
"# This should match against the whole path, not just the name.\n" |
| 98 |
"+ foo**too\n" |
| 99 |
"# These should float at the end of the path.\n" |
| 100 |
"+ foo/s?b/\n" |
| 101 |
"- foo/*/\n" |
| 102 |
"# Test how /** differs from /***\n" |
| 103 |
"- new/keep/**\n" |
| 104 |
"- new/lose/***\n" |
| 105 |
"# Test some normal excludes. Competing lines are paired.\n" |
| 106 |
"+ t[o]/\n" |
| 107 |
"- to\n" |
| 108 |
"+ file4\n" |
| 109 |
"- file[2-9]\n" |
| 110 |
"- /mid/for/foo/extra\n" |
| 111 |
) |
| 112 |
|
| 113 |
(SCRATCHDIR / '.cvsignore').write_text("home-cvs-exclude\n") |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 120 |
'-f', '-_foo/too/', '-f', '-_foo/down/', |
| 121 |
'-f', '-_foo/and/', '-f', '-_new/', |
| 122 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 123 |
|
| 124 |
checkit(['-av', *rpath, '--prune-empty-dirs', |
| 125 |
f'{host}{FROMDIR}/', f'{TODIR}/'], CHKDIR, TODIR) |
| 126 |
|
| 127 |
import shutil |
| 128 |
shutil.rmtree(TODIR, ignore_errors=True) |
| 129 |
|
| 130 |
|
| 131 |
os.symlink('too', FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'sym') |
| 132 |
|
| 133 |
|
| 134 |
up1 = SCRATCHDIR / 'up1' |
| 135 |
up2 = SCRATCHDIR / 'up2' |
| 136 |
up1.mkdir() |
| 137 |
up2.mkdir() |
| 138 |
(up1 / 'dst-newness').touch() |
| 139 |
(up2 / 'src-newness').touch() |
| 140 |
(up1 / 'same-newness').touch() |
| 141 |
(up2 / 'same-newness').touch() |
| 142 |
(up1 / 'extra-src').touch() |
| 143 |
(up2 / 'extra-dest').touch() |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
checkit(['-avv', *rpath, f'{host}{FROMDIR}/', f'{CHKDIR}/'], FROMDIR, CHKDIR) |
| 148 |
import time |
| 149 |
time.sleep(1) |
| 150 |
shutil.rmtree(CHKDIR / 'foo' / 'down', ignore_errors=True) |
| 151 |
shutil.rmtree(CHKDIR / 'mid' / 'for' / 'foo' / 'and', ignore_errors=True) |
| 152 |
shutil.rmtree(CHKDIR / 'new' / 'keep' / 'this', ignore_errors=True) |
| 153 |
shutil.rmtree(CHKDIR / 'new' / 'lose', ignore_errors=True) |
| 154 |
for f in (CHKDIR / 'foo').glob('file[235-9]'): |
| 155 |
f.unlink() |
| 156 |
(CHKDIR / 'bar' / 'down' / 'to' / 'foo' / 'to').unlink() |
| 157 |
for f in (CHKDIR / 'bar' / 'down' / 'to' / 'foo').glob('file[235-9]'): |
| 158 |
f.unlink() |
| 159 |
(CHKDIR / 'mid' / 'for' / 'foo' / 'extra').unlink() |
| 160 |
|
| 161 |
(up1 / 'src-newness').touch() |
| 162 |
(up2 / 'dst-newness').touch() |
| 163 |
|
| 164 |
|
| 165 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 166 |
'--existing', '--include=*/', '--exclude=*', |
| 167 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 168 |
|
| 169 |
|
| 170 |
checkit(['-avv', *rpath, f'--exclude-from={excl}', |
| 171 |
'--delete-during', f'{host}{FROMDIR}/', f'{TODIR}/'], |
| 172 |
CHKDIR, TODIR) |
| 173 |
|
| 174 |
|
| 175 |
for f in (CHKDIR / 'foo').glob('*.old'): |
| 176 |
f.unlink() |
| 177 |
for f in (CHKDIR / 'bar' / 'down' / 'to' / 'foo').glob('*.bak'): |
| 178 |
f.unlink() |
| 179 |
for f in (CHKDIR / 'bar' / 'down' / 'to' / 'foo').glob('*.junk'): |
| 180 |
f.unlink() |
| 181 |
(CHKDIR / 'bar' / 'down' / 'to' / 'home-cvs-exclude').unlink() |
| 182 |
(CHKDIR / 'mid' / 'one-in-one-out').unlink() |
| 183 |
|
| 184 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 185 |
'--existing', '--filter=exclude,! */', |
| 186 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
checkit(['-avv', *rpath, f'--filter=merge {excl}', |
| 191 |
'-f:C', '-f-C', '--delete-excluded', '--delete-during', |
| 192 |
f'{host}{FROMDIR}/', f'{TODIR}/'], |
| 193 |
CHKDIR, TODIR) |
| 194 |
|
| 195 |
|
| 196 |
(CHKDIR / 'foo' / 'file1').unlink() |
| 197 |
for f in (CHKDIR / 'bar' / 'down' / 'to' / 'bar' / 'baz').glob('*.deep'): |
| 198 |
f.unlink() |
| 199 |
for src in (FROMDIR / 'bar' / 'down' / 'to' / 'foo').glob('*.junk'): |
| 200 |
cp_touch_dst = CHKDIR / 'bar' / 'down' / 'to' / 'foo' |
| 201 |
cp_touch_dst.mkdir(exist_ok=True) |
| 202 |
from rsyncfns import cp_touch |
| 203 |
cp_touch(src, cp_touch_dst) |
| 204 |
from rsyncfns import cp_touch |
| 205 |
cp_touch(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / 'to', |
| 206 |
CHKDIR / 'bar' / 'down' / 'to' / 'foo') |
| 207 |
|
| 208 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 209 |
'--existing', '-f', 'show .filt*', '-f', 'hide,! */', '--del', |
| 210 |
f'{host}{FROMDIR}/', f'{TODIR}/') |
| 211 |
|
| 212 |
(TODIR / 'bar' / 'down' / 'to' / 'bar' / 'baz' / 'nodel.deep').write_text("retained\n") |
| 213 |
cp_touch(TODIR / 'bar' / 'down' / 'to' / 'bar' / 'baz' / 'nodel.deep', |
| 214 |
CHKDIR / 'bar' / 'down' / 'to' / 'bar' / 'baz') |
| 215 |
|
| 216 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 217 |
'--existing', '--filter=-! */', |
| 218 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
filtered_excl = SCRATCHDIR / 'exclude-from-filtered' |
| 224 |
filtered_excl.write_text( |
| 225 |
'\n'.join(ln for ln in excl.read_text().splitlines() if '!' not in ln) |
| 226 |
+ '\n' |
| 227 |
) |
| 228 |
|
| 229 |
|
| 230 |
def run_with_stdin_filter(args, label="merge"): |
| 231 |
"""Run rsync with `args`, feeding `filtered_excl` content on stdin |
| 232 |
(which `merge_-` in the filter list picks up). checkit-equivalent |
| 233 |
that also re-uses CHKDIR/TODIR for the listing comparison.""" |
| 234 |
print(f"Running: rsync {' '.join(args)}") |
| 235 |
with open(filtered_excl, 'rb') as inp: |
| 236 |
proc = subprocess.run(rsync_argv(*args), stdin=inp) |
| 237 |
if proc.returncode != 0: |
| 238 |
test_fail(f"{label}: rsync exited {proc.returncode}") |
| 239 |
|
| 240 |
|
| 241 |
run_with_stdin_filter( |
| 242 |
['-avv', *rpath, '-f', 'dir-merge_.filt', '-f', 'merge_-', |
| 243 |
'--delete-during', f'{host}{FROMDIR}/', f'{TODIR}/'], |
| 244 |
"dir-merge .filt + merge from stdin", |
| 245 |
) |
| 246 |
from rsyncfns import verify_dirs |
| 247 |
verify_dirs(CHKDIR, TODIR, label="dir-merge + merge-from-stdin") |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
(CHKDIR / '.filt').unlink() |
| 252 |
(CHKDIR / 'bar' / '.filt').unlink() |
| 253 |
(CHKDIR / 'bar' / 'down' / 'to' / '.filt2').unlink() |
| 254 |
(CHKDIR / 'bar' / 'down' / 'to' / 'foo' / '.filt2').unlink() |
| 255 |
(CHKDIR / 'bar' / 'down' / 'to' / 'bar' / '.filt2').unlink() |
| 256 |
(CHKDIR / 'mid' / '.filt').unlink() |
| 257 |
|
| 258 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 259 |
'--existing', '--include=*/', '--exclude=*', |
| 260 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 261 |
|
| 262 |
|
| 263 |
run_with_stdin_filter( |
| 264 |
['-avv', *rpath, '-f', ':s_.filt', '-f', '.s_-', |
| 265 |
'-f', 'P_nodel.deep', |
| 266 |
'--delete-before', f'{host}{FROMDIR}/', f'{TODIR}/'], |
| 267 |
"delete-before with merge", |
| 268 |
) |
| 269 |
verify_dirs(CHKDIR, TODIR, label="delete-before with merge") |
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
(FROMDIR / 'bar' / 'down' / '.excl').write_text("file3\n") |
| 274 |
(FROMDIR / 'bar' / 'down' / 'to' / 'foo' / '.excl').write_text( |
| 275 |
"+ file3\n*.bak\n" |
| 276 |
) |
| 277 |
|
| 278 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 279 |
'--del', f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 280 |
(CHKDIR / 'bar' / 'down' / 'to' / 'foo' / 'file1.bak').unlink() |
| 281 |
(CHKDIR / 'bar' / 'down' / 'to' / 'foo' / 'file3').unlink() |
| 282 |
(CHKDIR / 'bar' / 'down' / 'to' / 'foo' / '+ file3').unlink() |
| 283 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 284 |
'--existing', '--filter=-! */', |
| 285 |
f'{host}{FROMDIR}/', f'{CHKDIR}/') |
| 286 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', |
| 287 |
'--delete-excluded', '--exclude=*', |
| 288 |
f'{host}{FROMDIR}/', f'{TODIR}/') |
| 289 |
|
| 290 |
checkit(['-avv', *rpath, '-f', 'dir-merge,-_.excl', |
| 291 |
f'{host}{FROMDIR}/', f'{TODIR}/'], CHKDIR, TODIR) |
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
relative_opts = ['--relative', '--chmod=Du+w', '--copy-unsafe-links'] |
| 296 |
run_rsync('-av', f'--rsync-path={rsync_path_arg()}', *relative_opts, |
| 297 |
f'{host}{FROMDIR}/foo', f'{CHKDIR}/') |
| 298 |
shutil.rmtree(str(CHKDIR) + str(FROMDIR) + '/foo/down', ignore_errors=True) |
| 299 |
run_rsync('-av', *relative_opts, '--existing', '--filter=-! */', |
| 300 |
f'{FROMDIR}/foo', f'{CHKDIR}/') |
| 301 |
|
| 302 |
checkit(['-avv', *rpath, *relative_opts, |
| 303 |
f'--exclude={FROMDIR}/foo/down', |
| 304 |
f'{host}{FROMDIR}/foo', str(TODIR)], |
| 305 |
str(CHKDIR) + str(FROMDIR) + '/foo', |
| 306 |
str(TODIR) + str(FROMDIR) + '/foo') |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
checkdiff( |
| 311 |
['-aiiO', *rpath, '--update', '--info=skip', |
| 312 |
f'{host}{SCRATCHDIR}/up1/', f'{SCRATCHDIR}/up2/'], |
| 313 |
"dst-newness is newer\n" |
| 314 |
f">f{all_plus} extra-src\n" |
| 315 |
f".f{allspace} same-newness\n" |
| 316 |
f">f..t.{dots} src-newness\n", |
| 317 |
filter=lambda txt: '\n'.join( |
| 318 |
ln for ln in txt.splitlines() |
| 319 |
if not ln.startswith('.d' + allspace) |
| 320 |
) + ('\n' if txt else ''), |
| 321 |
) |