src/Pure/General/rsync.scala
author wenzelm
Tue, 21 Jun 2022 14:51:17 +0200
changeset 75571 ac5e633ad9b3
parent 75554 be33ca6f45d7
child 76131 8b695e59db3f
permissions -rw-r--r--
tuned signature: more operations;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/rsync.scala
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     3
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     4
Support for rsync: see also https://rsync.samba.org
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     5
*/
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     6
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     7
package isabelle
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     8
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
     9
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    10
object Rsync {
75527
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    11
  sealed case class Context(progress: Progress,
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    12
    port: Int = SSH.default_port,
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    13
    archive: Boolean = true,
75554
be33ca6f45d7 more comments;
wenzelm
parents: 75534
diff changeset
    14
    protect_args: Boolean = true  // requires rsync 3.0.0, or later
75527
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    15
  ) {
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    16
    def command: String =
75527
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    17
      "rsync --rsh=" + Bash.string("ssh -p " + port) +
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    18
        (if (archive) " --archive" else "") +
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    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
57b6a28e4eba tuned signature;
wenzelm
parents: 75525
diff changeset
    22
  def exec(
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    23
    context: Context,
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    24
    verbose: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    25
    thorough: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    26
    prune_empty_dirs: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    27
    dry_run: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    28
    clean: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    29
    list: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    30
    filter: List[String] = Nil,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    31
    args: List[String] = Nil
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    32
  ): Process_Result = {
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    33
    val script =
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    34
      context.command +
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    35
        (if (verbose) " --verbose" else "") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    36
        (if (thorough) " --ignore-times" else " --omit-dir-times") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    37
        (if (prune_empty_dirs) " --prune-empty-dirs" else "") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    38
        (if (dry_run) " --dry-run" else "") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    39
        (if (clean) " --delete-excluded" else "") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    40
        (if (list) " --list-only --no-human-readable" else "") +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    41
        filter.map(s => " --filter=" + Bash.string(s)).mkString +
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    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
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    44
  }
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    45
75526
57b6a28e4eba tuned signature;
wenzelm
parents: 75525
diff changeset
    46
  def init(context: Context, target: String,
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    47
    contents: List[File.Content] = Nil
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    48
  ): Unit =
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    49
    Isabelle_System.with_tmp_dir("sync") { tmp_dir =>
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    50
      val init_dir = Isabelle_System.make_directory(tmp_dir + Path.explode("init"))
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    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
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    57
    }
75524
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    58
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    59
  def terminate(target: String): String =
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    60
    if (target.endsWith(":") || target.endsWith("/")) target + "."
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    61
    else if (target.endsWith(":.") || target.endsWith("/.")) target
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    62
    else target + "/."
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    63
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    64
  def append(target: String, rest: String): String =
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    65
    if (target.endsWith(":") || target.endsWith("/")) target + rest
ff8012edac89 clarified signature;
wenzelm
parents: 75523
diff changeset
    66
    else target + "/" + rest
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    67
}