author | wenzelm |
Tue, 21 Jun 2022 14:51:17 +0200 | |
changeset 75571 | ac5e633ad9b3 |
parent 75554 | be33ca6f45d7 |
child 76131 | 8b695e59db3f |
permissions | -rw-r--r-- |
75523 | 1 |
/* Title: Pure/General/rsync.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Support for rsync: see also https://rsync.samba.org |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
9 |
||
10 |
object Rsync { |
|
75527 | 11 |
sealed case class Context(progress: Progress, |
12 |
port: Int = SSH.default_port, |
|
13 |
archive: Boolean = true, |
|
75554 | 14 |
protect_args: Boolean = true // requires rsync 3.0.0, or later |
75527 | 15 |
) { |
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
16 |
def command: String = |
75527 | 17 |
"rsync --rsh=" + Bash.string("ssh -p " + port) + |
18 |
(if (archive) " --archive" else "") + |
|
19 |
(if (protect_args) " --protect-args" else "") |
|
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
20 |
} |
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
21 |
|
75526 | 22 |
def exec( |
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
23 |
context: Context, |
75523 | 24 |
verbose: Boolean = false, |
25 |
thorough: Boolean = false, |
|
26 |
prune_empty_dirs: Boolean = false, |
|
27 |
dry_run: Boolean = false, |
|
28 |
clean: Boolean = false, |
|
29 |
list: Boolean = false, |
|
30 |
filter: List[String] = Nil, |
|
31 |
args: List[String] = Nil |
|
32 |
): Process_Result = { |
|
33 |
val script = |
|
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
34 |
context.command + |
75523 | 35 |
(if (verbose) " --verbose" else "") + |
36 |
(if (thorough) " --ignore-times" else " --omit-dir-times") + |
|
37 |
(if (prune_empty_dirs) " --prune-empty-dirs" else "") + |
|
38 |
(if (dry_run) " --dry-run" else "") + |
|
39 |
(if (clean) " --delete-excluded" else "") + |
|
40 |
(if (list) " --list-only --no-human-readable" else "") + |
|
41 |
filter.map(s => " --filter=" + Bash.string(s)).mkString + |
|
42 |
(if (args.nonEmpty) " " + Bash.strings(args) else "") |
|
75525
68162e4f60a7
clarified signature: more explicit type Rsync.Context;
wenzelm
parents:
75524
diff
changeset
|
43 |
context.progress.bash(script, echo = true) |
75523 | 44 |
} |
45 |
||
75526 | 46 |
def init(context: Context, target: String, |
75523 | 47 |
contents: List[File.Content] = Nil |
48 |
): Unit = |
|
49 |
Isabelle_System.with_tmp_dir("sync") { tmp_dir => |
|
50 |
val init_dir = Isabelle_System.make_directory(tmp_dir + Path.explode("init")) |
|
51 |
contents.foreach(_.write(init_dir)) |
|
75534
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
52 |
exec(context.copy(archive = false), |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
53 |
thorough = true, |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
54 |
args = |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
55 |
List(if (contents.nonEmpty) "--archive" else "--dirs", |
1d937b12204d
more robust: no change of directory attributes of initial test, notably target without .hg_sync meta data;
wenzelm
parents:
75527
diff
changeset
|
56 |
File.bash_path(init_dir) + "/.", target)).check |
75523 | 57 |
} |
75524 | 58 |
|
59 |
def terminate(target: String): String = |
|
60 |
if (target.endsWith(":") || target.endsWith("/")) target + "." |
|
61 |
else if (target.endsWith(":.") || target.endsWith("/.")) target |
|
62 |
else target + "/." |
|
63 |
||
64 |
def append(target: String, rest: String): String = |
|
65 |
if (target.endsWith(":") || target.endsWith("/")) target + rest |
|
66 |
else target + "/" + rest |
|
75523 | 67 |
} |