Skip to content

Commit 776c5c7

Browse files
committed
Download Python on demand
1 parent 45ce526 commit 776c5c7

File tree

2 files changed

+48
-17
lines changed

2 files changed

+48
-17
lines changed

bin/package_command.dart

Lines changed: 46 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import 'dart:convert';
22
import 'dart:io';
3-
import 'dart:isolate';
43

54
import 'package:archive/archive_io.dart';
65
import 'package:args/command_runner.dart';
6+
import 'package:http/http.dart' as http;
77
import 'package:path/path.dart' as path;
88
import 'package:toml/toml.dart';
99

@@ -41,17 +41,6 @@ class PackageCommand extends Command {
4141

4242
try {
4343
final currentPath = Directory.current.path;
44-
final packagePath = (await Isolate.resolvePackageUri(
45-
Uri.parse('package:serious_python/.')))
46-
?.path;
47-
48-
if (packagePath == null) {
49-
stdout.writeln("Cannot resolve 'serious_python' package path.");
50-
exit(1);
51-
}
52-
53-
final iosDistPath =
54-
path.join(Directory(packagePath).parent.absolute.path, "ios", "dist");
5544

5645
// source dir
5746
var sourceDirPath = argResults!.rest.first;
@@ -126,10 +115,7 @@ class PackageCommand extends Command {
126115
extraArgs.add("--pre");
127116
}
128117

129-
final pythonPath =
130-
path.join(iosDistPath, "hostpython3", "bin", "python");
131-
132-
await runExec(pythonPath, [
118+
await runPython([
133119
'-m',
134120
'pip',
135121
'install',
@@ -147,7 +133,7 @@ class PackageCommand extends Command {
147133
});
148134

149135
// compile all python code
150-
await runExec(pythonPath, ['-m', 'compileall', '-b', tempDir.path]);
136+
await runPython(['-m', 'compileall', '-b', tempDir.path]);
151137
}
152138

153139
// remove unnecessary files
@@ -234,4 +220,47 @@ class PackageCommand extends Command {
234220
}
235221
return proc.exitCode;
236222
}
223+
224+
Future runPython(List<String> args,
225+
{Map<String, String>? environment}) async {
226+
var pythonDir =
227+
Directory(path.join(Directory.systemTemp.path, "hostpython3.10"));
228+
229+
if (!await pythonDir.exists()) {
230+
stdout
231+
.writeln("Downloading and extracting Python into ${pythonDir.path}");
232+
233+
var isArm64 = Platform.version.contains("arm64");
234+
235+
String arch = "";
236+
if (Platform.isMacOS && !isArm64) {
237+
arch = 'x86_64-apple-darwin';
238+
} else if (Platform.isMacOS && isArm64) {
239+
arch = 'aarch64-apple-darwin';
240+
} else if (Platform.isLinux) {
241+
arch = 'x86_64-unknown-linux-gnu';
242+
} else if (Platform.isWindows) {
243+
arch = 'x86_64-pc-windows-msvc-static';
244+
}
245+
246+
final url =
247+
"https://github.com/indygreg/python-build-standalone/releases/download/20230507/cpython-3.10.11+20230507-$arch-install_only.tar.gz";
248+
249+
await pythonDir.create(recursive: true);
250+
251+
// Download the release asset
252+
var response = await http.get(Uri.parse(url));
253+
var archivePath = path.join(pythonDir.path, 'python.tar.gz');
254+
await File(archivePath).writeAsBytes(response.bodyBytes);
255+
256+
// Extract the archive
257+
await Process.run('tar', ['-xzf', archivePath, '-C', pythonDir.path]);
258+
} else {
259+
stdout.writeln("Python has already downloaded to ${pythonDir.path}");
260+
}
261+
262+
// Run the python executable
263+
var pythonPath = path.join(pythonDir.path, 'python', 'bin', 'python3');
264+
return await runExec(pythonPath, args, environment: environment);
265+
}
237266
}

pubspec.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,15 @@ dependencies:
2323
args: ^2.4.1
2424
toml: ^0.14.0
2525
ffi: ^2.0.1
26+
http: ^0.13.4
2627

2728
dev_dependencies:
2829
flutter_test:
2930
sdk: flutter
3031
flutter_lints: ^2.0.0
3132

3233
ffigen: ^9.0.0
34+
3335

3436
# For information on the generic Dart part of this file, see the
3537
# following page: https://dart.dev/tools/pub/pubspec

0 commit comments

Comments
 (0)