src/Pure/General/ssh.scala
author wenzelm
Thu, 31 Aug 2017 11:42:10 +0200
changeset 66570 9af879e222cc
parent 65717 556c34fd0554
child 66923 914935f8a462
permissions -rw-r--r--
clarified signature; tuned ssh.prefix;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/ssh.scala
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     3
64124
818265654e60 tuned comment;
wenzelm
parents: 64123
diff changeset
     4
SSH client based on JSch (see also http://www.jcraft.com/jsch/examples).
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     5
*/
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    10
import java.io.{InputStream, OutputStream, ByteArrayOutputStream}
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    11
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    12
import scala.collection.{mutable, JavaConversions}
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    13
64222
184e3a932778 clarified signature;
wenzelm
parents: 64191
diff changeset
    14
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session, SftpException,
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    15
  OpenSSHConfig, UserInfo, Channel => JSch_Channel, ChannelExec, ChannelSftp, SftpATTRS}
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    16
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    17
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    18
object SSH
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    19
{
64185
wenzelm
parents: 64166
diff changeset
    20
  /* target machine: user@host syntax */
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    21
64185
wenzelm
parents: 64166
diff changeset
    22
  object Target
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    23
  {
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    24
    val Pattern = "^([^@]+)@(.+)$".r
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    25
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    26
    def parse(s: String): (String, String) =
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    27
      s match {
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    28
        case Pattern(user, host) => (user, host)
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    29
        case _ => ("", s)
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    30
      }
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    31
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    32
    def unapplySeq(s: String): Option[List[String]] =
64185
wenzelm
parents: 64166
diff changeset
    33
      parse(s) match {
wenzelm
parents: 64166
diff changeset
    34
        case (_, "") => None
wenzelm
parents: 64166
diff changeset
    35
        case (user, host) => Some(List(user, host))
wenzelm
parents: 64166
diff changeset
    36
      }
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    37
  }
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    38
64142
954451356017 proper type for Library.using;
wenzelm
parents: 64141
diff changeset
    39
  val default_port = 22
954451356017 proper type for Library.using;
wenzelm
parents: 64141
diff changeset
    40
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    41
  def connect_timeout(options: Options): Int =
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    42
    options.seconds("ssh_connect_timeout").ms.toInt
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    43
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    44
  def alive_interval(options: Options): Int =
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    45
    options.seconds("ssh_alive_interval").ms.toInt
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    46
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    47
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    48
  /* init context */
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    49
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    50
  def init_context(options: Options): Context =
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    51
  {
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    52
    val config_dir = Path.explode(options.string("ssh_config_dir"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    53
    if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    54
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    55
    val jsch = new JSch
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    56
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    57
    val config_file = Path.explode(options.string("ssh_config_file"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    58
    if (config_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    59
      jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file)))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    60
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    61
    val known_hosts = config_dir + Path.explode("known_hosts")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    62
    if (!known_hosts.is_file) known_hosts.file.createNewFile
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    63
    jsch.setKnownHosts(File.platform_path(known_hosts))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    64
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    65
    val identity_files =
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    66
      Library.space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    67
    for (identity_file <- identity_files if identity_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    68
      jsch.addIdentity(File.platform_path(identity_file))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    69
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    70
    new Context(options, jsch)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    71
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    72
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    73
  class Context private[SSH](val options: Options, val jsch: JSch)
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    74
  {
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    75
    def update_options(new_options: Options): Context = new Context(new_options, jsch)
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    76
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65010
diff changeset
    77
    def open_session(host: String, user: String = "", port: Int = 0): Session =
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    78
    {
65594
659305708959 clarified treatment of default port;
wenzelm
parents: 65010
diff changeset
    79
      val session =
65717
wenzelm
parents: 65636
diff changeset
    80
        jsch.getSession(proper_string(user) getOrElse null, host,
wenzelm
parents: 65636
diff changeset
    81
          if (port > 0) port else default_port)
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    82
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    83
      session.setUserInfo(No_User_Info)
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    84
      session.setServerAliveInterval(alive_interval(options))
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    85
      session.setConfig("MaxAuthTries", "3")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    86
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    87
      if (options.bool("ssh_compression")) {
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    88
        session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    89
        session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    90
        session.setConfig("compression_level", "9")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    91
      }
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    92
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    93
      session.connect(connect_timeout(options))
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    94
      new Session(options, session)
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    95
    }
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    96
  }
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    97
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    98
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    99
  /* logging */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   100
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   101
  def logging(verbose: Boolean = true, debug: Boolean = false)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   102
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   103
    JSch.setLogger(if (verbose) new Logger(debug) else null)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   104
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   105
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   106
  private class Logger(debug: Boolean) extends JSch_Logger
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   107
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   108
    def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   109
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   110
    def log(level: Int, msg: String)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   111
    {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   112
      level match {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   113
        case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   114
        case JSch_Logger.WARN => Output.warning(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   115
        case _ => Output.writeln(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   116
      }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   117
    }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   118
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   119
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   120
64128
wenzelm
parents: 64127
diff changeset
   121
  /* user info */
wenzelm
parents: 64127
diff changeset
   122
wenzelm
parents: 64127
diff changeset
   123
  object No_User_Info extends UserInfo
wenzelm
parents: 64127
diff changeset
   124
  {
wenzelm
parents: 64127
diff changeset
   125
    def getPassphrase: String = null
wenzelm
parents: 64127
diff changeset
   126
    def getPassword: String = null
wenzelm
parents: 64127
diff changeset
   127
    def promptPassword(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   128
    def promptPassphrase(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   129
    def promptYesNo(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   130
    def showMessage(msg: String): Unit = Output.writeln(msg)
wenzelm
parents: 64127
diff changeset
   131
  }
wenzelm
parents: 64127
diff changeset
   132
wenzelm
parents: 64127
diff changeset
   133
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   134
  /* port forwarding */
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   135
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   136
  object Port_Forwarding
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   137
  {
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   138
    def open(ssh: Session, ssh_close: Boolean,
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   139
      local_host: String, local_port: Int, remote_host: String, remote_port: Int): Port_Forwarding =
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   140
    {
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   141
      val port = ssh.session.setPortForwardingL(local_host, local_port, remote_host, remote_port)
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   142
      new Port_Forwarding(ssh, ssh_close, local_host, port, remote_host, remote_port)
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   143
    }
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   144
  }
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   145
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   146
  class Port_Forwarding private[SSH](
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   147
    ssh: SSH.Session,
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   148
    ssh_close: Boolean,
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   149
    val local_host: String,
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   150
    val local_port: Int,
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   151
    val remote_host: String,
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   152
    val remote_port: Int)
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   153
  {
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   154
    override def toString: String =
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   155
      local_host + ":" + local_port + ":" + remote_host + ":" + remote_port
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   156
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   157
    def close()
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   158
    {
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   159
      ssh.session.delPortForwardingL(local_host, local_port)
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   160
      if (ssh_close) ssh.close()
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   161
    }
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   162
  }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   163
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   164
64191
wenzelm
parents: 64190
diff changeset
   165
  /* Sftp channel */
wenzelm
parents: 64190
diff changeset
   166
wenzelm
parents: 64190
diff changeset
   167
  type Attrs = SftpATTRS
wenzelm
parents: 64190
diff changeset
   168
64233
ef6f7e8a018c clarified signature: more static types;
wenzelm
parents: 64228
diff changeset
   169
  sealed case class Dir_Entry(name: Path, attrs: Attrs)
64191
wenzelm
parents: 64190
diff changeset
   170
  {
wenzelm
parents: 64190
diff changeset
   171
    def is_file: Boolean = attrs.isReg
wenzelm
parents: 64190
diff changeset
   172
    def is_dir: Boolean = attrs.isDir
wenzelm
parents: 64190
diff changeset
   173
  }
wenzelm
parents: 64190
diff changeset
   174
wenzelm
parents: 64190
diff changeset
   175
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   176
  /* exec channel */
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   177
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   178
  private val exec_wait_delay = Time.seconds(0.3)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   179
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   180
  class Exec private[SSH](session: Session, channel: ChannelExec)
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   181
  {
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   182
    override def toString: String = "exec " + session.toString
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   183
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   184
    def close() { channel.disconnect }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   185
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   186
    val exit_status: Future[Int] =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   187
      Future.thread("ssh_wait") {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   188
        while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   189
        channel.getExitStatus
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   190
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   191
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   192
    val stdin: OutputStream = channel.getOutputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   193
    val stdout: InputStream = channel.getInputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   194
    val stderr: InputStream = channel.getErrStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   195
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   196
    // connect after preparing streams
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   197
    channel.connect(connect_timeout(session.options))
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   198
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   199
    def result(
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   200
      progress_stdout: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   201
      progress_stderr: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   202
      strict: Boolean = true): Process_Result =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   203
    {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   204
      stdin.close
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   205
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   206
      def read_lines(stream: InputStream, progress: String => Unit): List[String] =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   207
      {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   208
        val result = new mutable.ListBuffer[String]
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   209
        val line_buffer = new ByteArrayOutputStream(100)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   210
        def line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   211
        {
64326
ff3c383b9163 extra trim_line for the sake of Windows;
wenzelm
parents: 64325
diff changeset
   212
          val line = Library.trim_line(line_buffer.toString(UTF8.charset_name))
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   213
          progress(line)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   214
          result += line
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   215
          line_buffer.reset
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   216
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   217
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   218
        var c = 0
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   219
        var finished = false
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   220
        while (!finished) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   221
          while ({ c = stream.read; c != -1 && c != 10 }) line_buffer.write(c)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   222
          if (c == 10) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   223
          else if (channel.isClosed) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   224
            if (line_buffer.size > 0) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   225
            finished = true
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   226
          }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   227
          else Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   228
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   229
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   230
        result.toList
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   231
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   232
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   233
      val out_lines = Future.thread("ssh_stdout") { read_lines(stdout, progress_stdout) }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   234
      val err_lines = Future.thread("ssh_stderr") { read_lines(stderr, progress_stderr) }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   235
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   236
      def terminate()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   237
      {
64136
7c5191489457 close more thoroughly;
wenzelm
parents: 64135
diff changeset
   238
        close
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   239
        out_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   240
        err_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   241
        exit_status.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   242
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   243
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   244
      val rc =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   245
        try { exit_status.join }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   246
        catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   247
64136
7c5191489457 close more thoroughly;
wenzelm
parents: 64135
diff changeset
   248
      close
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   249
      if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   250
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   251
      Process_Result(rc, out_lines.join, err_lines.join)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   252
    }
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   253
  }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   254
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   255
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   256
  /* session */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   257
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   258
  class Session private[SSH](val options: Options, val session: JSch_Session) extends System
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   259
  {
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   260
    def update_options(new_options: Options): Session = new Session(new_options, session)
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   261
64347
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   262
    def user_prefix: String = if (session.getUserName == null) "" else session.getUserName + "@"
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   263
    def host: String = if (session.getHost == null) "" else session.getHost
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   264
    def port_suffix: String = if (session.getPort == default_port) "" else ":" + session.getPort
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   265
    override def hg_url: String = "ssh://" + user_prefix + host + port_suffix + "/"
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   266
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   267
    override def prefix: String =
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   268
      user_prefix + host + port_suffix + ":"
64347
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   269
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   270
    override def toString: String =
64347
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   271
      user_prefix + host + port_suffix + (if (session.isConnected) "" else " (disconnected)")
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   272
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   273
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   274
    /* port forwarding */
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   275
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   276
    def port_forwarding(
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   277
        remote_port: Int, remote_host: String = "localhost",
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   278
        local_port: Int = 0, local_host: String = "localhost",
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   279
        ssh_close: Boolean = false): Port_Forwarding =
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   280
      Port_Forwarding.open(this, ssh_close, local_host, local_port, remote_host, remote_port)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   281
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   282
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   283
    /* sftp channel */
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   284
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   285
    val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp]
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   286
    sftp.connect(connect_timeout(options))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   287
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   288
    def close() { sftp.disconnect; session.disconnect }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   289
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   290
    val settings: Map[String, String] =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   291
    {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   292
      val home = sftp.getHome
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   293
      Map("HOME" -> home, "USER_HOME" -> home)
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   294
    }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   295
    override def expand_path(path: Path): Path = path.expand_env(settings)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   296
    def remote_path(path: Path): String = expand_path(path).implode
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64258
diff changeset
   297
    def bash_path(path: Path): String = Bash.string(remote_path(path))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   298
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   299
    def chmod(permissions: Int, path: Path): Unit = sftp.chmod(permissions, remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   300
    def mv(path1: Path, path2: Path): Unit = sftp.rename(remote_path(path1), remote_path(path2))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   301
    def rm(path: Path): Unit = sftp.rm(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   302
    def mkdir(path: Path): Unit = sftp.mkdir(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   303
    def rmdir(path: Path): Unit = sftp.rmdir(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   304
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   305
    def stat(path: Path): Option[Dir_Entry] =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   306
      try { Some(Dir_Entry(expand_path(path), sftp.stat(remote_path(path)))) }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   307
      catch { case _: SftpException => None }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   308
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   309
    override def is_file(path: Path): Boolean = stat(path).map(_.is_file) getOrElse false
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   310
    override def is_dir(path: Path): Boolean = stat(path).map(_.is_dir) getOrElse false
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   311
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   312
    override def mkdirs(path: Path): Unit =
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   313
      if (!is_dir(path)) {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   314
        execute(
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   315
          "perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   316
        if (!is_dir(path)) error("Failed to create directory: " + quote(remote_path(path)))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   317
      }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   318
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   319
    def read_dir(path: Path): List[Dir_Entry] =
64191
wenzelm
parents: 64190
diff changeset
   320
    {
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   321
      val dir = sftp.ls(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   322
      (for {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   323
        i <- (0 until dir.size).iterator
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   324
        a = dir.get(i).asInstanceOf[AnyRef]
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   325
        name = Untyped.get[String](a, "filename")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   326
        attrs = Untyped.get[Attrs](a, "attrs")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   327
        if name != "." && name != ".."
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   328
      } yield Dir_Entry(Path.basic(name), attrs)).toList
64191
wenzelm
parents: 64190
diff changeset
   329
    }
64135
865dda40e1cc provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents: 64134
diff changeset
   330
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   331
    def find_files(root: Path, pred: Dir_Entry => Boolean = _ => true): List[Dir_Entry] =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   332
    {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   333
      def find(dir: Path): List[Dir_Entry] =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   334
        read_dir(dir).flatMap(entry =>
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   335
          {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   336
            val file = dir + entry.name
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   337
            if (entry.is_dir) find(file)
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   338
            else if (pred(entry)) List(entry.copy(name = file))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   339
            else Nil
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   340
          })
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   341
      find(root)
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   342
    }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   343
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   344
    def open_input(path: Path): InputStream = sftp.get(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   345
    def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   346
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   347
    def read_file(path: Path, local_path: Path): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   348
      sftp.get(remote_path(path), File.platform_path(local_path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   349
    def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   350
    def read(path: Path): String = using(open_input(path))(File.read_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   351
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   352
    def write_file(path: Path, local_path: Path): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   353
      sftp.put(File.platform_path(local_path), remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   354
    def write_bytes(path: Path, bytes: Bytes): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   355
      using(open_output(path))(bytes.write_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   356
    def write(path: Path, text: String): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   357
      using(open_output(path))(stream => Bytes(text).write_stream(stream))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   358
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   359
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   360
    /* exec channel */
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   361
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   362
    def exec(command: String): Exec =
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   363
    {
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   364
      val channel = session.openChannel("exec").asInstanceOf[ChannelExec]
64190
c62b99e3ec07 provide USER_HOME, such that symbolic Path.explode("~") can be used remotely;
wenzelm
parents: 64185
diff changeset
   365
      channel.setCommand("export USER_HOME=\"$HOME\"\n" + command)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   366
      new Exec(this, channel)
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   367
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   368
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   369
    override def execute(command: String,
64191
wenzelm
parents: 64190
diff changeset
   370
        progress_stdout: String => Unit = (_: String) => (),
wenzelm
parents: 64190
diff changeset
   371
        progress_stderr: String => Unit = (_: String) => (),
wenzelm
parents: 64190
diff changeset
   372
        strict: Boolean = true): Process_Result =
wenzelm
parents: 64190
diff changeset
   373
      exec(command).result(progress_stdout, progress_stderr, strict)
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   374
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   375
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   376
    /* tmp dirs */
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   377
64306
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   378
    def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir))
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   379
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   380
    def rm_tree(remote_dir: String): Unit =
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64258
diff changeset
   381
      execute("rm -r -f " + Bash.string(remote_dir)).check
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   382
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   383
    def tmp_dir(): String =
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   384
      execute("mktemp -d -t tmp.XXXXXXXXXX").check.out
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   385
64233
ef6f7e8a018c clarified signature: more static types;
wenzelm
parents: 64228
diff changeset
   386
    def with_tmp_dir[A](body: Path => A): A =
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   387
    {
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   388
      val remote_dir = tmp_dir()
64233
ef6f7e8a018c clarified signature: more static types;
wenzelm
parents: 64228
diff changeset
   389
      try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) }
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   390
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   391
  }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   392
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   393
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   394
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   395
  /* system operations */
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   396
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   397
  trait System
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   398
  {
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   399
    def hg_url: String = ""
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   400
    def prefix: String = ""
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   401
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   402
    def expand_path(path: Path): Path = path.expand
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   403
    def is_file(path: Path): Boolean = path.is_file
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   404
    def is_dir(path: Path): Boolean = path.is_dir
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   405
    def mkdirs(path: Path): Unit = Isabelle_System.mkdirs(path)
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   406
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   407
    def execute(command: String,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   408
        progress_stdout: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   409
        progress_stderr: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   410
        strict: Boolean = true): Process_Result =
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   411
      Isabelle_System.bash(command, progress_stdout = progress_stdout,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   412
        progress_stderr = progress_stderr, strict = strict)
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   413
  }
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   414
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   415
  object Local extends System
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   416
}