-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPythonManager.swift
More file actions
192 lines (155 loc) · 6.61 KB
/
Copy pathPythonManager.swift
File metadata and controls
192 lines (155 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// PythonManager.swift
// StreamTV
//
// Manages Python virtual environment and dependencies
//
import Foundation
class PythonManager {
static let shared = PythonManager()
private let appSupportURL: URL
private let venvURL: URL
private let requirementsPath: String?
private init() {
let fileManager = FileManager.default
let appSupport = fileManager.urls(for: .applicationSupportDirectory, in: .userDomainMask).first!
appSupportURL = appSupport.appendingPathComponent("StreamTV")
venvURL = appSupportURL.appendingPathComponent("venv")
// Get requirements.txt from bundle
if let requirementsURL = Bundle.main.path(forResource: "requirements", ofType: "txt") {
requirementsPath = requirementsURL
} else {
requirementsPath = nil
}
// Create Application Support directory if needed
try? fileManager.createDirectory(at: appSupportURL, withIntermediateDirectories: true)
}
// MARK: - Python Version Check
func checkPythonVersion() -> (installed: Bool, version: String?) {
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/which")
process.arguments = ["python3"]
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
do {
try process.run()
process.waitUntilExit()
if process.terminationStatus == 0 {
let data = pipe.fileHandleForReading.readDataToEndOfFile()
let pythonPath = String(data: data, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? ""
// Get version
let versionProcess = Process()
versionProcess.executableURL = URL(fileURLWithPath: pythonPath)
versionProcess.arguments = ["--version"]
let versionPipe = Pipe()
versionProcess.standardOutput = versionPipe
versionProcess.standardError = versionPipe
try versionProcess.run()
versionProcess.waitUntilExit()
if versionProcess.terminationStatus == 0 {
let versionData = versionPipe.fileHandleForReading.readDataToEndOfFile()
let version = String(data: versionData, encoding: .utf8)?.trimmingCharacters(in: .whitespacesAndNewlines) ?? "Unknown"
return (true, version)
}
}
} catch {
print("Error checking Python: \(error)")
}
return (false, nil)
}
// MARK: - Virtual Environment Management
func venvExists() -> Bool {
let pythonPath = venvURL.appendingPathComponent("bin/python3")
return FileManager.default.fileExists(atPath: pythonPath.path)
}
func createVenv() throws {
let (installed, _) = checkPythonVersion()
guard installed else {
throw PythonManagerError.pythonNotInstalled
}
let process = Process()
process.executableURL = URL(fileURLWithPath: "/usr/bin/python3")
process.arguments = ["-m", "venv", venvURL.path]
process.currentDirectoryURL = appSupportURL
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
let errorData = pipe.fileHandleForReading.readDataToEndOfFile()
let errorMessage = String(data: errorData, encoding: .utf8) ?? "Unknown error"
throw PythonManagerError.venvCreationFailed(errorMessage)
}
}
// MARK: - Dependency Installation
func installDependencies() throws {
guard venvExists() else {
throw PythonManagerError.venvNotFound
}
guard let requirementsPath = requirementsPath else {
throw PythonManagerError.requirementsNotFound
}
let pipPath = venvURL.appendingPathComponent("bin/pip3")
guard FileManager.default.fileExists(atPath: pipPath.path) else {
throw PythonManagerError.pipNotFound
}
let process = Process()
process.executableURL = pipPath
process.arguments = ["install", "-r", requirementsPath]
process.currentDirectoryURL = appSupportURL
let pipe = Pipe()
process.standardOutput = pipe
process.standardError = pipe
try process.run()
process.waitUntilExit()
guard process.terminationStatus == 0 else {
let errorData = pipe.fileHandleForReading.readDataToEndOfFile()
let errorMessage = String(data: errorData, encoding: .utf8) ?? "Unknown error"
throw PythonManagerError.dependencyInstallationFailed(errorMessage)
}
}
// MARK: - Python Path
func pythonPath() -> String? {
let pythonPath = venvURL.appendingPathComponent("bin/python3")
guard FileManager.default.fileExists(atPath: pythonPath.path) else {
return nil
}
return pythonPath.path
}
func streamtvModulePath() -> String? {
guard let resourcePath = Bundle.main.resourcePath else {
return nil
}
let streamtvPath = (resourcePath as NSString).appendingPathComponent("streamtv")
guard FileManager.default.fileExists(atPath: streamtvPath) else {
return nil
}
return streamtvPath
}
}
enum PythonManagerError: LocalizedError {
case pythonNotInstalled
case venvNotFound
case venvCreationFailed(String)
case requirementsNotFound
case pipNotFound
case dependencyInstallationFailed(String)
var errorDescription: String? {
switch self {
case .pythonNotInstalled:
return "Python 3.10+ is not installed. Please install Python from python.org or via Homebrew."
case .venvNotFound:
return "Virtual environment not found. Please create it first."
case .venvCreationFailed(let message):
return "Failed to create virtual environment: \(message)"
case .requirementsNotFound:
return "requirements.txt not found in app bundle."
case .pipNotFound:
return "pip not found in virtual environment."
case .dependencyInstallationFailed(let message):
return "Failed to install dependencies: \(message)"
}
}
}