Skip to content

Commit

Permalink
--exclude option for package command. Python distributive stabili…
Browse files Browse the repository at this point in the history
…ty fix. (#50)

* --exclude option for package command

* Always re-create Python directory

* Bump package version to 0.6.1
  • Loading branch information
FeodorFitsner authored Dec 21, 2023
1 parent 2702b50 commit 387609c
Show file tree
Hide file tree
Showing 19 changed files with 119 additions and 60 deletions.
5 changes: 5 additions & 0 deletions src/serious_python/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.6.1

* `--exclude` option for `package` command - to exclude directories and files from Python app package.
* Re-create temp Python distributive directory on every run of `package` command.

## 0.6.0

* `--verbose` flag - verbose output.
Expand Down
78 changes: 50 additions & 28 deletions src/serious_python/bin/package_command.dart
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ const junkFilesAndDirectories = ["__pycache__", "bin"];

class PackageCommand extends Command {
bool _verbose = false;
Directory? _pythonDir;

@override
final name = "package";
Expand Down Expand Up @@ -56,6 +57,9 @@ class PackageCommand extends Command {
argParser.addOption('platform',
help:
"Make pip to install dependencies for this platform, e.g. 'emscripten_3_1_45_wasm32'. An attempt to install native Python modules will raise an error.");
argParser.addOption('exclude',
help:
"List of relative paths to exclude from app package, e.g. \"assets,build\".");
}

// [run] may also return a Future.
Expand Down Expand Up @@ -86,6 +90,7 @@ class PackageCommand extends Command {
String? reqDepsArg = argResults?['req-deps'];
String? findLinksArg = argResults?['find-links'];
String? platformArg = argResults?['platform'];
String? excludeArg = argResults?['exclude'];
_verbose = argResults?["verbose"];

if (mobile && web) {
Expand Down Expand Up @@ -130,7 +135,13 @@ class PackageCommand extends Command {
// copy app to a temp dir
stdout.writeln(
"Copying Python app from ${sourceDir.path} to ${tempDir.path}");
await copyDirectory(sourceDir, tempDir);
await copyDirectory(
sourceDir,
tempDir,
sourceDir.path,
excludeArg != null
? excludeArg.split(",").map((s) => s.trim()).toList()
: []);

// discover dependencies
List<String> dependencies = [];
Expand Down Expand Up @@ -310,6 +321,10 @@ class PackageCommand extends Command {
"Deleting sitecustomize directory ${sitecustomizeDir.path}");
await sitecustomizeDir.delete(recursive: true);
}
if (_pythonDir != null && await _pythonDir!.exists()) {
stdout.writeln("Deleting Python directory ${_pythonDir!.path}");
await _pythonDir!.delete(recursive: true);
}
}
}

Expand All @@ -330,13 +345,18 @@ class PackageCommand extends Command {
return null;
}

Future<void> copyDirectory(Directory source, Directory destination) async {
Future<void> copyDirectory(Directory source, Directory destination,
String rootDir, List<String> excludeList) async {
await for (var entity in source.list()) {
if (excludeList.contains(path.relative(entity.path, from: rootDir))) {
continue;
}
if (entity is Directory) {
final newDirectory =
Directory(path.join(destination.path, path.basename(entity.path)));
await newDirectory.create();
await copyDirectory(entity.absolute, newDirectory);
await copyDirectory(
entity.absolute, newDirectory, rootDir, excludeList);
} else if (entity is File) {
await entity
.copy(path.join(destination.path, path.basename(entity.path)));
Expand Down Expand Up @@ -385,21 +405,8 @@ class PackageCommand extends Command {

Future<int> runPython(List<String> args,
{Map<String, String>? environment}) async {
var pythonDir =
Directory(path.join(Directory.systemTemp.path, "hostpython3.11"));

var pythonExePath = Platform.isWindows
? path.join(pythonDir.path, 'python', 'python.exe')
: path.join(pythonDir.path, 'python', 'bin', 'python3');

if (!await File(pythonExePath).exists()) {
stdout
.writeln("Downloading and extracting Python into ${pythonDir.path}");

if (await pythonDir.exists()) {
await pythonDir.delete(recursive: true);
}
await pythonDir.create(recursive: true);
if (_pythonDir == null) {
_pythonDir = await Directory.systemTemp.createTemp('hostpython3.11_');

var isArm64 = Platform.version.contains("arm64");

Expand All @@ -416,20 +423,35 @@ class PackageCommand extends Command {
arch = 'x86_64-pc-windows-msvc-shared';
}

final url =
"https://github.com/indygreg/python-build-standalone/releases/download/20231002/cpython-3.11.6+20231002-$arch-install_only.tar.gz";
var pythonArchiveFilename =
"cpython-3.11.6+20231002-$arch-install_only.tar.gz";

// Download the release asset
var response = await http.get(Uri.parse(url));
var archivePath = path.join(pythonDir.path, 'python.tar.gz');
await File(archivePath).writeAsBytes(response.bodyBytes);
var pythonArchivePath =
path.join(Directory.systemTemp.path, pythonArchiveFilename);

// Extract the archive
await Process.run('tar', ['-xzf', archivePath, '-C', pythonDir.path]);
} else {
verbose("Python executable found at $pythonExePath");
if (!await File(pythonArchivePath).exists()) {
// download Python distr from GitHub
final url =
"https://github.com/indygreg/python-build-standalone/releases/download/20231002/$pythonArchiveFilename";

stdout.writeln(
"Downloading Python distributive from $url to $pythonArchivePath");

var response = await http.get(Uri.parse(url));
await File(pythonArchivePath).writeAsBytes(response.bodyBytes);
}

// extract Python from archive
stdout.writeln(
"Extracting Python distributive from $pythonArchivePath to ${_pythonDir!.path}");
await Process.run(
'tar', ['-xzf', pythonArchivePath, '-C', _pythonDir!.path]);
}

var pythonExePath = Platform.isWindows
? path.join(_pythonDir!.path, 'python', 'python.exe')
: path.join(_pythonDir!.path, 'python', 'bin', 'python3');

// Run the python executable
verbose([pythonExePath, ...args].join(" "));
return await runExec(pythonExePath, args, environment: environment);
Expand Down
14 changes: 10 additions & 4 deletions src/serious_python/example/flask_example/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,30 +1,36 @@
PODS:
- FlutterMacOS (1.0.0)
- package_info_plus (0.0.1):
- FlutterMacOS
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- serious_python_darwin (0.3.1):
- serious_python_darwin (0.6.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- package_info_plus (from `Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- serious_python_darwin (from `Flutter/ephemeral/.symlinks/plugins/serious_python_darwin/darwin`)

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral
package_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos
path_provider_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
serious_python_darwin:
:path: Flutter/ephemeral/.symlinks/plugins/serious_python_darwin/darwin

SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
path_provider_foundation: eaf5b3e458fc0e5fbb9940fb09980e853fe058b8
serious_python_darwin: 3b3e9802e671014eb062d16976ef5394dea763ce
package_info_plus: 02d7a575e80f194102bef286361c6c326e4c29ce
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
serious_python_darwin: 6deda6a52f401fcf7e9b9a83c3a9387a9c08d599

PODFILE CHECKSUM: 236401fc2c932af29a9fcf0e97baeeb2d750d367
PODFILE CHECKSUM: 9ebaf0ce3d369aaa26a9ea0e159195ed94724cf3

COCOAPODS: 1.12.1
12 changes: 6 additions & 6 deletions src/serious_python/example/flask_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -302,42 +302,42 @@ packages:
path: "../.."
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_android:
dependency: transitive
description:
path: "../../../serious_python_android"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_darwin:
dependency: transitive
description:
path: "../../../serious_python_darwin"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_linux:
dependency: transitive
description:
path: "../../../serious_python_linux"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_platform_interface:
dependency: transitive
description:
path: "../../../serious_python_platform_interface"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_windows:
dependency: transitive
description:
path: "../../../serious_python_windows"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down
12 changes: 6 additions & 6 deletions src/serious_python/example/flet_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -509,42 +509,42 @@ packages:
path: "../.."
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_android:
dependency: transitive
description:
path: "../../../serious_python_android"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_darwin:
dependency: transitive
description:
path: "../../../serious_python_darwin"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_linux:
dependency: transitive
description:
path: "../../../serious_python_linux"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_platform_interface:
dependency: transitive
description:
path: "../../../serious_python_platform_interface"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_windows:
dependency: transitive
description:
path: "../../../serious_python_windows"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
shake:
dependency: transitive
description:
Expand Down
10 changes: 8 additions & 2 deletions src/serious_python/example/run_example/macos/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,29 +1,35 @@
PODS:
- FlutterMacOS (1.0.0)
- package_info_plus (0.0.1):
- FlutterMacOS
- path_provider_foundation (0.0.1):
- Flutter
- FlutterMacOS
- serious_python_darwin (0.3.1):
- serious_python_darwin (0.6.1):
- Flutter
- FlutterMacOS

DEPENDENCIES:
- FlutterMacOS (from `Flutter/ephemeral`)
- package_info_plus (from `Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos`)
- path_provider_foundation (from `Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin`)
- serious_python_darwin (from `Flutter/ephemeral/.symlinks/plugins/serious_python_darwin/darwin`)

EXTERNAL SOURCES:
FlutterMacOS:
:path: Flutter/ephemeral
package_info_plus:
:path: Flutter/ephemeral/.symlinks/plugins/package_info_plus/macos
path_provider_foundation:
:path: Flutter/ephemeral/.symlinks/plugins/path_provider_foundation/darwin
serious_python_darwin:
:path: Flutter/ephemeral/.symlinks/plugins/serious_python_darwin/darwin

SPEC CHECKSUMS:
FlutterMacOS: 8f6f14fa908a6fb3fba0cd85dbd81ec4b251fb24
package_info_plus: 02d7a575e80f194102bef286361c6c326e4c29ce
path_provider_foundation: 29f094ae23ebbca9d3d0cec13889cd9060c0e943
serious_python_darwin: 482c39edd868a8f319567c03024fc3e546be81e7
serious_python_darwin: 6deda6a52f401fcf7e9b9a83c3a9387a9c08d599

PODFILE CHECKSUM: 9ebaf0ce3d369aaa26a9ea0e159195ed94724cf3

Expand Down
12 changes: 6 additions & 6 deletions src/serious_python/example/run_example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -317,42 +317,42 @@ packages:
path: "../.."
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_android:
dependency: transitive
description:
path: "../../../serious_python_android"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_darwin:
dependency: transitive
description:
path: "../../../serious_python_darwin"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_linux:
dependency: transitive
description:
path: "../../../serious_python_linux"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_platform_interface:
dependency: transitive
description:
path: "../../../serious_python_platform_interface"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
serious_python_windows:
dependency: transitive
description:
path: "../../../serious_python_windows"
relative: true
source: path
version: "0.6.0"
version: "0.6.1"
sky_engine:
dependency: transitive
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion src/serious_python/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: serious_python
description: A cross-platform plugin for adding embedded Python runtime to your Flutter apps.
homepage: https://flet.dev
repository: https://github.com/flet-dev/serious-python
version: 0.6.0
version: 0.6.1

platforms:
ios:
Expand Down
5 changes: 5 additions & 0 deletions src/serious_python_android/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## 0.6.1

* `--exclude` option for `package` command - to exclude directories and files from Python app package.
* Re-create temp Python distributive directory on every run of `package` command.

## 0.6.0

* `--verbose` flag - verbose output.
Expand Down
Loading

0 comments on commit 387609c

Please sign in to comment.