forked from flet-dev/serious-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserious_python_android.dart
More file actions
98 lines (82 loc) · 3.18 KB
/
serious_python_android.dart
File metadata and controls
98 lines (82 loc) · 3.18 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
import 'dart:async';
import 'dart:io';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:path/path.dart' as p;
import 'package:serious_python_platform_interface/serious_python_platform_interface.dart';
import 'src/cpython.dart';
/// An implementation of [SeriousPythonPlatform] that uses method channels.
class SeriousPythonAndroid extends SeriousPythonPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('android_plugin');
/// Registers this class as the default instance of [SeriousPythonPlatform]
static void registerWith() {
SeriousPythonPlatform.instance = SeriousPythonAndroid();
}
@override
Future<String?> getPlatformVersion() async {
final version =
await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}
@override
Future<String?> run(String appPath,
{String? script,
List<String>? modulePaths,
Map<String, String>? environmentVariables,
bool? sync}) async {
Future<void> setenv(String key, String value) =>
methodChannel.invokeMethod<String>(
'setEnvironmentVariable', {'name': key, 'value': value});
// load libpyjni.so to get JNI reference
try {
await methodChannel
.invokeMethod<String>('loadLibrary', {'libname': 'pyjni'});
await setenv("FLET_JNI_READY", "1");
} catch (e) {
debugPrint("Unable to load libpyjni.so library: $e");
}
// unpack python bundle
final nativeLibraryDir =
await methodChannel.invokeMethod<String>('getNativeLibraryDir');
debugPrint("getNativeLibraryDir: $nativeLibraryDir");
var bundlePath = "$nativeLibraryDir/libpythonbundle.so";
var sitePackagesZipPath = "$nativeLibraryDir/libpythonsitepackages.so";
if (!await File(bundlePath).exists()) {
throw Exception("Python bundle not found: $bundlePath");
}
var pythonLibPath =
await extractFileZip(bundlePath, targetPath: "python_bundle");
debugPrint("pythonLibPath: $pythonLibPath");
var programDirPath = p.dirname(appPath);
var moduleSearchPaths = [
programDirPath,
...?modulePaths,
"$pythonLibPath/modules",
"$pythonLibPath/stdlib"
];
if (await File(sitePackagesZipPath).exists()) {
var sitePackagesPath = await extractFileZip(sitePackagesZipPath,
targetPath: "python_site_packages");
debugPrint("sitePackagesPath: $sitePackagesPath");
moduleSearchPaths.add(sitePackagesPath);
}
await setenv("PYTHONINSPECT", "1");
await setenv("PYTHONDONTWRITEBYTECODE", "1");
await setenv("PYTHONNOUSERSITE", "1");
await setenv("PYTHONUNBUFFERED", "1");
await setenv("LC_CTYPE", "UTF-8");
await setenv("PYTHONHOME", pythonLibPath);
await setenv("PYTHONPATH", moduleSearchPaths.join(":"));
// set environment variables
if (environmentVariables != null) {
for (var v in environmentVariables.entries) {
await setenv(v.key, v.value);
}
}
return runPythonProgramFFI(
sync ?? false, "libpython3.14.so", appPath, script ?? "");
}
}