SPB Git

Repository Public

Paths.swift 2.4 KB 47 lines swift

1import Foundation2import CryptoKit34/// Locations used by the Python integration. `supportDirectory` can be redirected (tests, previews).5public enum PythonPaths {6    private static let lock = NSLock()7    nonisolated(unsafe) private static var override: URL?89    /// ~/Library/Application Support/ORIX Sheet (or the `ORIX_SHEET_SUPPORT_DIR` environment override).10    public static var supportDirectory: URL {11        get {12            lock.lock(); defer { lock.unlock() }13            if let override { return override }14            if let env = ProcessInfo.processInfo.environment["ORIX_SHEET_SUPPORT_DIR"], !env.isEmpty { return URL(fileURLWithPath: env, isDirectory: true) }15            let base = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask).first ?? URL(fileURLWithPath: NSHomeDirectory()).appendingPathComponent("Library/Application Support")16            return base.appendingPathComponent("ORIX Sheet", isDirectory: true)17        }18        set { lock.lock(); override = newValue; lock.unlock() }19    }20    public static func resetSupportDirectory() { lock.lock(); override = nil; lock.unlock() }2122    public static var auditLog: URL { supportDirectory.appendingPathComponent("python-audit.jsonl") }23    public static var trustStore: URL { supportDirectory.appendingPathComponent("python-trust.json") }24    public static var managedVenv: URL { supportDirectory.appendingPathComponent("python/venv", isDirectory: true) }25    /// Holds only matplotlib's font-list cache, copied in and out of each run by the host.26    public static var fontCache: URL { supportDirectory.appendingPathComponent("python/cache", isDirectory: true) }2728    static func ensureDirectory(_ url: URL) throws {29        try FileManager.default.createDirectory(at: url, withIntermediateDirectories: true, attributes: [.posixPermissions: 0o700])30    }3132    /// Prefix of every run directory, inside the user's temporary directory.33    public static let runDirectoryPrefix = "orix-python-"3435    static func resource(_ name: String) throws -> URL {36        guard let url = Bundle.module.url(forResource: name, withExtension: nil, subdirectory: "python") else { throw PythonRunError.resourcesMissing(name) }37        return url38    }39}4041public enum PythonHash {42    /// Lower-case hexadecimal SHA-256 of the exact UTF-8 text.43    public static func sha256(_ text: String) -> String {44        SHA256.hash(data: Data(text.utf8)).map { String(format: "%02x", $0) }.joined()45    }46}47
Files main
Menu

Theme