src/Pure/General/rsync.scala
author wenzelm
Tue, 10 Dec 2024 16:37:09 +0100
changeset 81569 f8b28356ab94
parent 80234 cce5670be9f9
permissions -rw-r--r--
more LaTeX markup for printed entities;
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
77793
6d03e2114ec5 tuned comments;
wenzelm
parents: 77792
diff changeset
     4
Support for rsync, based on Isabelle component.
75523
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 {
77787
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    11
  object Context {
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    12
    def apply(
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    13
      progress: Progress = new Progress,
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    14
      ssh: SSH.System = SSH.Local,
80234
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    15
      chmod: String = "",
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    16
      chown: String = "",
77795
4c4bd44ff683 more options;
wenzelm
parents: 77793
diff changeset
    17
      archive: Boolean = true,
4c4bd44ff683 more options;
wenzelm
parents: 77793
diff changeset
    18
      stats: Boolean = true
77788
c2ce9ac85859 use remote copy of locally installed rsync component: for uniform version and options;
wenzelm
parents: 77787
diff changeset
    19
    ): Context = {
c2ce9ac85859 use remote copy of locally installed rsync component: for uniform version and options;
wenzelm
parents: 77787
diff changeset
    20
      val directory = Components.provide(Component_Rsync.home, ssh = ssh, progress = progress)
80234
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    21
      new Context(directory, progress, chmod, chown, archive, stats)
77788
c2ce9ac85859 use remote copy of locally installed rsync component: for uniform version and options;
wenzelm
parents: 77787
diff changeset
    22
    }
77787
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    23
  }
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    24
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    25
  final class Context private(
77790
ed68c546746c misc tuning and clarification;
wenzelm
parents: 77788
diff changeset
    26
    directory: Components.Directory,
77787
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    27
    val progress: Progress,
80234
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    28
    chmod: String,
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    29
    chown: String,
77787
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    30
    archive: Boolean,
77795
4c4bd44ff683 more options;
wenzelm
parents: 77793
diff changeset
    31
    stats: Boolean
75527
a66fd84a30b7 clarified context with global defaults;
wenzelm
parents: 75526
diff changeset
    32
  ) {
77791
3e72fab0e699 tuned output;
wenzelm
parents: 77790
diff changeset
    33
    override def toString: String = directory.toString
3e72fab0e699 tuned output;
wenzelm
parents: 77790
diff changeset
    34
77790
ed68c546746c misc tuning and clarification;
wenzelm
parents: 77788
diff changeset
    35
    def ssh: SSH.System = directory.ssh
77787
b20ac2c26ea3 clarified signature: more abstract;
wenzelm
parents: 77785
diff changeset
    36
76136
1bb677cceea4 let rsync re-use ssh connection via control path;
wenzelm
parents: 76131
diff changeset
    37
    def command: String = {
77790
ed68c546746c misc tuning and clarification;
wenzelm
parents: 77788
diff changeset
    38
      val program = Component_Rsync.remote_program(directory)
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77518
diff changeset
    39
      val ssh_command = ssh.client_command
77792
b81b2c50fc7c use "rsync --secluded-args" by default, discontinue obsolete option -P of sync tools;
wenzelm
parents: 77791
diff changeset
    40
      File.bash_path(Component_Rsync.local_program) + " --secluded-args" +
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77518
diff changeset
    41
        if_proper(ssh_command, " --rsh=" + Bash.string(ssh_command)) +
77790
ed68c546746c misc tuning and clarification;
wenzelm
parents: 77788
diff changeset
    42
        " --rsync-path=" + Bash.string(quote(File.standard_path(program))) +
80234
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    43
        if_proper(chmod, " --chmod=" + Bash.string(chmod)) +
cce5670be9f9 support "rsync --chmod --chown" via Rsync.Context;
wenzelm
parents: 80233
diff changeset
    44
        if_proper(chown, " --chown=" + Bash.string(chown)) +
80233
wenzelm
parents: 80192
diff changeset
    45
        if_proper(archive, " --archive") +
wenzelm
parents: 80192
diff changeset
    46
        if_proper(stats, " --stats")
76136
1bb677cceea4 let rsync re-use ssh connection via control path;
wenzelm
parents: 76131
diff changeset
    47
    }
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77518
diff changeset
    48
80192
36e6ba1527f0 clarified signature;
wenzelm
parents: 77852
diff changeset
    49
    def target(path: Path, direct: Boolean = false): String =
36e6ba1527f0 clarified signature;
wenzelm
parents: 77852
diff changeset
    50
      Url.dir_path(ssh.rsync_path(path), direct = direct)
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    51
  }
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    52
75526
57b6a28e4eba tuned signature;
wenzelm
parents: 75525
diff changeset
    53
  def exec(
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    54
    context: Context,
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    55
    thorough: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    56
    prune_empty_dirs: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    57
    dry_run: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    58
    clean: Boolean = false,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    59
    filter: List[String] = Nil,
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    60
    args: List[String] = Nil
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    61
  ): Process_Result = {
77517
wenzelm
parents: 77369
diff changeset
    62
    val progress = context.progress
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    63
    val script =
75525
68162e4f60a7 clarified signature: more explicit type Rsync.Context;
wenzelm
parents: 75524
diff changeset
    64
      context.command +
80233
wenzelm
parents: 80192
diff changeset
    65
        if_proper(progress.verbose, " --verbose") +
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    66
        (if (thorough) " --ignore-times" else " --omit-dir-times") +
80233
wenzelm
parents: 80192
diff changeset
    67
        if_proper(prune_empty_dirs, " --prune-empty-dirs") +
wenzelm
parents: 80192
diff changeset
    68
        if_proper(dry_run, " --dry-run") +
wenzelm
parents: 80192
diff changeset
    69
        if_proper(clean, " --delete-excluded") +
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    70
        filter.map(s => " --filter=" + Bash.string(s)).mkString +
77369
wenzelm
parents: 76617
diff changeset
    71
        if_proper(args, " " + Bash.strings(args))
77517
wenzelm
parents: 77369
diff changeset
    72
    progress.bash(script, echo = true)
75523
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    73
  }
0dcaf0e5107b clarified modules;
wenzelm
parents:
diff changeset
    74
}