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