src/Pure/General/ssh.scala
changeset 73634 c88faa1e09e1
parent 73367 77ef8bef0593
child 73702 7202e12cb324
equal deleted inserted replaced
73633:7e465e166bb2 73634:c88faa1e09e1
   316   class Session private[SSH](
   316   class Session private[SSH](
   317     val options: Options,
   317     val options: Options,
   318     val session: JSch_Session,
   318     val session: JSch_Session,
   319     on_close: () => Unit,
   319     on_close: () => Unit,
   320     val nominal_host: String,
   320     val nominal_host: String,
   321     val nominal_user: String) extends System with AutoCloseable
   321     val nominal_user: String) extends System
   322   {
   322   {
   323     def update_options(new_options: Options): Session =
   323     def update_options(new_options: Options): Session =
   324       new Session(new_options, session, on_close, nominal_host, nominal_user)
   324       new Session(new_options, session, on_close, nominal_host, nominal_user)
   325 
   325 
   326     def host: String = if (session.getHost == null) "" else session.getHost
   326     def host: String = if (session.getHost == null) "" else session.getHost
   345     /* sftp channel */
   345     /* sftp channel */
   346 
   346 
   347     val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp]
   347     val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp]
   348     sftp.connect(connect_timeout(options))
   348     sftp.connect(connect_timeout(options))
   349 
   349 
   350     def close(): Unit = { sftp.disconnect; session.disconnect; on_close() }
   350     override def close(): Unit = { sftp.disconnect; session.disconnect; on_close() }
   351 
   351 
   352     val settings: Map[String, String] =
   352     val settings: Map[String, String] =
   353     {
   353     {
   354       val home = sftp.getHome
   354       val home = sftp.getHome
   355       Map("HOME" -> home, "USER_HOME" -> home)
   355       Map("HOME" -> home, "USER_HOME" -> home)
   436     }
   436     }
   437 
   437 
   438     def open_input(path: Path): InputStream = sftp.get(remote_path(path))
   438     def open_input(path: Path): InputStream = sftp.get(remote_path(path))
   439     def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
   439     def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
   440 
   440 
   441     def read_file(path: Path, local_path: Path): Unit =
   441     override def read_file(path: Path, local_path: Path): Unit =
   442       sftp.get(remote_path(path), File.platform_path(local_path))
   442       sftp.get(remote_path(path), File.platform_path(local_path))
   443     def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_))
   443     def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_))
   444     def read(path: Path): String = using(open_input(path))(File.read_stream)
   444     def read(path: Path): String = using(open_input(path))(File.read_stream)
   445 
   445 
   446     def write_file(path: Path, local_path: Path): Unit =
   446     override def write_file(path: Path, local_path: Path): Unit =
   447       sftp.put(File.platform_path(local_path), remote_path(path))
   447       sftp.put(File.platform_path(local_path), remote_path(path))
   448     def write_bytes(path: Path, bytes: Bytes): Unit =
   448     def write_bytes(path: Path, bytes: Bytes): Unit =
   449       using(open_output(path))(bytes.write_stream)
   449       using(open_output(path))(bytes.write_stream)
   450     def write(path: Path, text: String): Unit =
   450     def write(path: Path, text: String): Unit =
   451       using(open_output(path))(stream => Bytes(text).write_stream(stream))
   451       using(open_output(path))(stream => Bytes(text).write_stream(stream))
   461     }
   461     }
   462 
   462 
   463     override def execute(command: String,
   463     override def execute(command: String,
   464         progress_stdout: String => Unit = (_: String) => (),
   464         progress_stdout: String => Unit = (_: String) => (),
   465         progress_stderr: String => Unit = (_: String) => (),
   465         progress_stderr: String => Unit = (_: String) => (),
       
   466         settings: Boolean = true,
   466         strict: Boolean = true): Process_Result =
   467         strict: Boolean = true): Process_Result =
   467       exec(command).result(progress_stdout, progress_stderr, strict)
   468       exec(command).result(progress_stdout, progress_stderr, strict)
   468 
   469 
   469     override def isabelle_platform: Isabelle_Platform = Isabelle_Platform(ssh = Some(this))
   470     override def isabelle_platform: Isabelle_Platform = Isabelle_Platform(ssh = Some(this))
   470 
   471 
   477       execute("rm -r -f " + Bash.string(remote_dir)).check
   478       execute("rm -r -f " + Bash.string(remote_dir)).check
   478 
   479 
   479     def tmp_dir(): String =
   480     def tmp_dir(): String =
   480       execute("mktemp -d -t tmp.XXXXXXXXXX").check.out
   481       execute("mktemp -d -t tmp.XXXXXXXXXX").check.out
   481 
   482 
   482     def with_tmp_dir[A](body: Path => A): A =
   483     override def with_tmp_dir[A](body: Path => A): A =
   483     {
   484     {
   484       val remote_dir = tmp_dir()
   485       val remote_dir = tmp_dir()
   485       try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) }
   486       try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) }
   486     }
   487     }
   487   }
   488   }
   488 
   489 
   489 
   490 
   490   /* system operations */
   491   /* system operations */
   491 
   492 
   492   trait System
   493   trait System extends AutoCloseable
   493   {
   494   {
       
   495     def close(): Unit = ()
       
   496 
   494     def hg_url: String = ""
   497     def hg_url: String = ""
   495 
   498 
   496     def expand_path(path: Path): Path = path.expand
   499     def expand_path(path: Path): Path = path.expand
   497     def bash_path(path: Path): String = File.bash_path(path)
   500     def bash_path(path: Path): String = File.bash_path(path)
   498     def is_dir(path: Path): Boolean = path.is_dir
   501     def is_dir(path: Path): Boolean = path.is_dir
   499     def is_file(path: Path): Boolean = path.is_file
   502     def is_file(path: Path): Boolean = path.is_file
   500     def make_directory(path: Path): Path = Isabelle_System.make_directory(path)
   503     def make_directory(path: Path): Path = Isabelle_System.make_directory(path)
       
   504     def with_tmp_dir[A](body: Path => A): A = Isabelle_System.with_tmp_dir("tmp")(body)
       
   505     def read_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path1, path2)
       
   506     def write_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path2, path1)
   501 
   507 
   502     def execute(command: String,
   508     def execute(command: String,
   503         progress_stdout: String => Unit = (_: String) => (),
   509         progress_stdout: String => Unit = (_: String) => (),
   504         progress_stderr: String => Unit = (_: String) => (),
   510         progress_stderr: String => Unit = (_: String) => (),
       
   511         settings: Boolean = true,
   505         strict: Boolean = true): Process_Result =
   512         strict: Boolean = true): Process_Result =
   506       Isabelle_System.bash(command, progress_stdout = progress_stdout,
   513       Isabelle_System.bash(command,
   507         progress_stderr = progress_stderr, strict = strict)
   514         progress_stdout = progress_stdout,
       
   515         progress_stderr = progress_stderr,
       
   516         env = if (settings) Isabelle_System.settings() else null,
       
   517         strict = strict)
   508 
   518 
   509     def isabelle_platform: Isabelle_Platform = Isabelle_Platform()
   519     def isabelle_platform: Isabelle_Platform = Isabelle_Platform()
   510   }
   520   }
   511 
   521 
   512   object Local extends System
   522   object Local extends System