#if os(macOS) import Foundation import OrgCore /// Runs Babel jobs as processes, in the file's folder or `:dir`: the interpreter reads the /// program on standard input, or from the file `job.script` is written to. public enum BabelRunner { public struct Failure: Error, CustomStringConvertible { public let description: String } /// PATH with the usual install locations, which an app launched from the Dock lacks. public static var environment: [String: String] { var environment = ProcessInfo.processInfo.environment let extra = ["/opt/homebrew/bin", "/usr/local/bin", "/Library/TeX/texbin", "/usr/bin", "/bin"] let path = (environment["PATH"] ?? "").split(separator: ":").map(String.init) environment["PATH"] = (path + extra.filter { !path.contains($0) }).joined(separator: ":") return environment } /// Whether `program` is an executable file on `environment`'s PATH, or the path of one. public static func installed(_ program: String) -> Bool { let paths = program.contains("/") ? [program] : (environment["PATH"] ?? "").split(separator: ":").map { "\($0)/\(program)" } return paths.contains { FileManager.default.isExecutableFile(atPath: $0) } } /// The words `/bin/sh` makes of `command`, after expanding `~`, variables and quotes. Empty for /// a command substitution, which would otherwise run here as well as when the block runs. public static func words(_ command: String) -> [String] { if command.contains("$(") || command.contains("`") { return [] } guard command.contains(where: { "~$'\"`\\".contains($0) }) else { return command.split(whereSeparator: \.isWhitespace).map(String.init) } let process = Process() process.executableURL = URL(fileURLWithPath: "/bin/sh") process.arguments = ["-c", "printf '%s\\0' " + command] process.environment = environment let out = Pipe() process.standardOutput = out process.standardError = FileHandle.nullDevice guard (try? process.run()) != nil else { return [] } let data = out.fileHandleForReading.readDataToEndOfFile() process.waitUntilExit() return String(decoding: data, as: UTF8.self).split(separator: "\0").map(String.init) } public static func run(_ job: BabelJob, fileDirectory: URL, timeout: Duration = .seconds(300)) async throws -> BabelOutput { for programs in job.programs where !programs.contains(where: installed) { let names = programs.count == 1 ? programs[0] : "Neither " + programs.joined(separator: " nor ") throw Failure(description: "\(names) \(programs.count == 1 ? "isn't" : "is") installed, so \(job.language) blocks can't run.") } let folder = FileManager.default.temporaryDirectory.appendingPathComponent("orgstar-babel-\(UUID().uuidString)") try FileManager.default.createDirectory(at: folder, withIntermediateDirectories: true) defer { try? FileManager.default.removeItem(at: folder) } let resultFile = folder.appendingPathComponent("result") var input = job.input.replacingOccurrences(of: Babel.resultFileToken, with: resultFile.path) for (token, contents) in job.dataFiles { let file = folder.appendingPathComponent("sql-data-\(token.filter(\.isNumber))") try contents.write(to: file, atomically: true, encoding: .utf8) input = input.replacingOccurrences(of: token, with: file.path) } if case .session(let name, let executable, let kind) = job.runner { let directory = job.directory.map { URL(fileURLWithPath: $0, relativeTo: fileDirectory).standardizedFileURL } ?? fileDirectory return try await BabelSessions.shared.run(job, name: name, executable: executable, kind: kind, directory: directory, resultFile: resultFile) } let process = Process() process.executableURL = URL(fileURLWithPath: "/usr/bin/env") process.environment = environment switch job.runner { case .process(let executable, var arguments): if let script = job.script { let file = folder.appendingPathComponent("script") try script.write(to: file, atomically: true, encoding: .utf8) try FileManager.default.setAttributes([.posixPermissions: 0o755], ofItemAtPath: file.path) arguments = arguments.map { $0.replacingOccurrences(of: Babel.scriptFileToken, with: file.path) } } // In the shell, as Org runs it, so `:cmd "node --no-warnings"` or `~/bin/node` works. process.arguments = ["/bin/sh", "-c", executable + " \"$@\"", "sh"] + arguments case .emacs: // `Programs` runs these natively when Emacs is missing. let script = folder.appendingPathComponent("babel.el") try input.write(to: script, atomically: true, encoding: .utf8) process.arguments = [EmacsRunner.executable()?.path ?? "emacs", "-Q", "--batch", "-l", script.path] case .session: return BabelOutput(stdout: "") } let directory = job.directory.map { URL(fileURLWithPath: $0, relativeTo: fileDirectory).standardizedFileURL } ?? fileDirectory guard FileManager.default.fileExists(atPath: directory.path) else { throw Failure(description: "No folder \(directory.path) for :dir.") } process.currentDirectoryURL = directory // As Emacs's `call-process` does, so `pwd` prints the folder as written, not resolved. process.environment?["PWD"] = directory.path let stdout = folder.appendingPathComponent("stdout") FileManager.default.createFile(atPath: stdout.path, contents: nil) let out = try FileHandle(forWritingTo: stdout) let stderr = folder.appendingPathComponent("stderr") FileManager.default.createFile(atPath: stderr.path, contents: nil) let err = try FileHandle(forWritingTo: stderr) // The input from a file, as `org-babel-eval` gives it. let stdin = folder.appendingPathComponent("stdin") if case .process = job.runner { try input.write(to: stdin, atomically: true, encoding: .utf8) } else { FileManager.default.createFile(atPath: stdin.path, contents: nil) } process.standardInput = try FileHandle(forReadingFrom: stdin) process.standardOutput = out process.standardError = err do { try process.run() } catch { throw Failure(description: "Can't start \(process.arguments?.first ?? "the interpreter"): \(error.localizedDescription)") } let deadline = ContinuousClock.now + timeout while process.isRunning { if ContinuousClock.now > deadline { process.terminate() throw Failure(description: "The block took longer than \(timeout) and was stopped.") } do { try await Task.sleep(for: .milliseconds(30)) } catch { process.terminate() throw CancellationError() } } try out.close() try err.close() let output = (try? String(contentsOf: stdout, encoding: .utf8)) ?? "" let errors = (try? String(contentsOf: stderr, encoding: .utf8)) ?? "" let result = try? String(contentsOf: resultFile, encoding: .utf8) if case .process(let executable, _) = job.runner, job.programs.isEmpty, process.terminationStatus == 127, output.isEmpty { throw Failure(description: "\(executable) isn't installed.") } return BabelOutput(stdout: output, resultFile: result, stderr: errors, status: process.terminationStatus) } } #endif