|
| 1 | + |
| 2 | +import "dart:io"; |
| 3 | + |
| 4 | +import "package:path/path.dart" as path; |
| 5 | +import "package:dio/dio.dart"; |
| 6 | +import "package:archive/archive_io.dart"; |
| 7 | + |
| 8 | +import "config.dart" as config; |
| 9 | +import "listener.dart"; |
| 10 | +import "shell/manager.dart"; |
| 11 | +import "shell/instance.dart"; |
| 12 | + |
| 13 | + |
| 14 | +/// PythonShell Class |
| 15 | +class PythonShell { |
| 16 | + PythonShell(PythonShellConfig shellConfig); |
| 17 | + |
| 18 | + void clear() => ShellManager.clear(); |
| 19 | + |
| 20 | + Future<void> initialize() async { |
| 21 | + print("Initializing shell..."); |
| 22 | + config.defaultPythonVersion = config.checkPythonVersion(config.defaultPythonVersion); |
| 23 | + if (!Directory(config.appDir).existsSync()) Directory(config.appDir).createSync(); |
| 24 | + if (!Directory(config.instanceDir).existsSync()) Directory(config.instanceDir).createSync(); |
| 25 | + if (!Directory(config.tempDir).existsSync()) Directory(config.tempDir).createSync(); |
| 26 | + |
| 27 | + String pythonDir = ""; |
| 28 | + if (Platform.isWindows) { |
| 29 | + print("Check default python binary files..."); |
| 30 | + pythonDir = path.join(config.appDir, "python"); |
| 31 | + if (!Directory(pythonDir).existsSync()) { |
| 32 | + String pythonBinaryFile = path.join(config.tempDir, "python.zip"); |
| 33 | + await Dio().download("https://www.python.org/ftp/python/${config.defaultPythonVersion}/python-${config.defaultPythonVersion}-embed-amd64.zip", pythonBinaryFile); |
| 34 | + await extractFileToDisk(pythonBinaryFile, pythonDir); |
| 35 | + File(pythonBinaryFile).deleteSync(); |
| 36 | + |
| 37 | + String pythonPthFile = path.join(pythonDir, "python${config.defaultPythonVersion.replaceAll(".${config.defaultPythonVersion.split(".").last}", "").replaceAll(".", "")}._pth"); |
| 38 | + File(pythonPthFile).writeAsStringSync(File(pythonPthFile).readAsStringSync().replaceAll("#import site", "import site")); |
| 39 | + } |
| 40 | + print("Python check finished."); |
| 41 | + |
| 42 | + config.defaultPythonPath = path.join(pythonDir, "python.exe"); |
| 43 | + } |
| 44 | + |
| 45 | + if (File(config.defaultPythonPath).existsSync()) { |
| 46 | + print("Default settings for virtualenv..."); |
| 47 | + var result = Process.runSync(config.defaultPythonPath, [ "-m", "pip", "install", "pip", "--upgrade" ]); |
| 48 | + if (result.stderr.toString().trim() != "") { |
| 49 | + String pipInstallFile = path.join(config.tempDir, "get-pip.py"); |
| 50 | + await Dio().download("https://bootstrap.pypa.io/pip/get-pip.py", pipInstallFile); |
| 51 | + Process.runSync(config.defaultPythonPath, [ pipInstallFile ]); |
| 52 | + File(pipInstallFile).deleteSync(); |
| 53 | + } |
| 54 | + |
| 55 | + Process.runSync(config.defaultPythonPath, [ "-m", "pip", "install", "virtualenv", "--upgrade" ]); |
| 56 | + print("Virtualenv settings finished."); |
| 57 | + } |
| 58 | + |
| 59 | + String defaultEnvDir = path.join(config.instanceDir, "default"); |
| 60 | + if (!Directory(defaultEnvDir).existsSync()) { |
| 61 | + print("Creating default env..."); |
| 62 | + ShellManager.createInstance(instanceName: "default"); |
| 63 | + print("Default env created."); |
| 64 | + } |
| 65 | + |
| 66 | + print("Shell initialized."); |
| 67 | + } |
| 68 | + |
| 69 | + Future<void> runFile(String pythonFile, { ShellInstance? instance, String? workingDirectory, ShellListener? listener, bool echo = true }) async { |
| 70 | + instance = instance ?? ShellManager.getInstance("default"); |
| 71 | + await instance.runFile(pythonFile, workingDirectory: workingDirectory, listener: listener, echo: echo); |
| 72 | + } |
| 73 | + |
| 74 | + Future<void> runString(String pythonCode, { ShellInstance? instance, String? workingDirectory, ShellListener? listener, bool echo = true }) async { |
| 75 | + instance = instance ?? ShellManager.getInstance("default"); |
| 76 | + await instance.runString(pythonCode, workingDirectory: workingDirectory, listener: listener, echo: echo); |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +/// PythonShell Configuration Class |
| 81 | +/// * [defaultPythonPath]: Default python path to use |
| 82 | +/// * [defaultPythonVersion]: Default python version to use |
| 83 | +class PythonShellConfig { |
| 84 | + |
| 85 | + PythonShellConfig({ |
| 86 | + defaultPythonPath = "python3", |
| 87 | + defaultPythonVersion = "3.9.13", |
| 88 | + }) { |
| 89 | + if ((Platform.isLinux || Platform.isMacOS)) { |
| 90 | + if (["python", "python2", "python3"].contains(defaultPythonPath)) { |
| 91 | + config.defaultPythonPath = "/usr/bin/$defaultPythonPath"; |
| 92 | + } |
| 93 | + else if (!File(defaultPythonPath).existsSync()) { |
| 94 | + config.defaultPythonPath = "/usr/bin/python3"; |
| 95 | + } |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments