Skip to content

Commit

Permalink
Fix for subprocess.run() encoding on Windows (#2283)
Browse files Browse the repository at this point in the history
* Switch console to UTF-8 while running subprocess.run on Windows

* Fix "charmap" error on Windows

* Path to docs
  • Loading branch information
FeodorFitsner authored Dec 30, 2023
1 parent dcf6f60 commit 31ef1bf
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# 0.18.0

* `flet build` command to package Flet app for any platform ([#2271](https://github.com/flet-dev/flet/issues/2271)).
* `flet build` command to package Flet app for any platform ([docs](https://flet.dev/docs/guides/python/packaging-app-for-distribution)).
* Added TextStyle for the Text control ([#2270](https://github.com/flet-dev/flet/issues/2270)).
* Refactor code, add Enum deprecation utils ([#2259](https://github.com/flet-dev/flet/issues/2259)).
* `CupertinoAppBar` control ([#2278](https://github.com/flet-dev/flet/issues/2278)).
Expand Down
2 changes: 1 addition & 1 deletion package/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# 0.18.0

* `flet build` command to package Flet app for any platform ([#2271](https://github.com/flet-dev/flet/issues/2271)).
* `flet build` command to package Flet app for any platform ([docs](https://flet.dev/docs/guides/python/packaging-app-for-distribution)).
* Added TextStyle for the Text control ([#2270](https://github.com/flet-dev/flet/issues/2270)).
* Refactor code, add Enum deprecation utils ([#2259](https://github.com/flet-dev/flet/issues/2259)).
* `CupertinoAppBar` control ([#2278](https://github.com/flet-dev/flet/issues/2278)).
Expand Down
51 changes: 30 additions & 21 deletions sdk/python/packages/flet/src/flet/cli/commands/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,12 @@
import yaml
from flet.cli.commands.base import BaseCommand
from flet_core.utils import random_string, slugify
from flet_runtime.utils import copy_tree
from flet_runtime.utils import copy_tree, is_windows
from rich import print

if is_windows():
from ctypes import windll

PYODIDE_ROOT_URL = "https://cdn.jsdelivr.net/pyodide/v0.24.1/full"


Expand Down Expand Up @@ -509,11 +512,8 @@ def fallback_image(yaml_path: str, images: list):

# generate icons
print("Generating app icons...", end="")
icons_result = subprocess.run(
[dart_exe, "run", "flutter_launcher_icons"],
cwd=str(self.flutter_dir),
capture_output=self.verbose < 2,
text=True,
icons_result = self.run(
[dart_exe, "run", "flutter_launcher_icons"], cwd=str(self.flutter_dir)
)
if icons_result.returncode != 0:
if icons_result.stdout:
Expand All @@ -527,11 +527,9 @@ def fallback_image(yaml_path: str, images: list):
# generate splash
if target_platform in ["web", "ipa", "apk", "aab"]:
print("Generating splash screens...", end="")
splash_result = subprocess.run(
splash_result = self.run(
[dart_exe, "run", "flutter_native_splash:create"],
cwd=str(self.flutter_dir),
capture_output=self.verbose < 2,
text=True,
)
if splash_result.returncode != 0:
if splash_result.stdout:
Expand Down Expand Up @@ -586,12 +584,7 @@ def fallback_image(yaml_path: str, images: list):
]
)

package_result = subprocess.run(
package_args,
cwd=str(self.flutter_dir),
capture_output=self.verbose < 2,
text=True,
)
package_result = self.run(package_args, cwd=str(self.flutter_dir))

if package_result.returncode != 0:
if package_result.stdout:
Expand Down Expand Up @@ -629,12 +622,7 @@ def fallback_image(yaml_path: str, images: list):
if self.verbose > 0:
print(build_args)

build_result = subprocess.run(
build_args,
cwd=str(self.flutter_dir),
capture_output=self.verbose < 2,
text=True,
)
build_result = self.run(build_args, cwd=str(self.flutter_dir))

if build_result.returncode != 0:
if build_result.stdout:
Expand Down Expand Up @@ -703,6 +691,27 @@ def copy_icon_image(self, src_path: Path, dest_path: Path, image_name: str):
return Path(images[0]).name
return None

def run(self, args, cwd):
if is_windows():
# Source: https://stackoverflow.com/a/77374899/1435891
# Save the current console output code page and switch to 65001 (UTF-8)
previousCp = windll.kernel32.GetConsoleOutputCP()
windll.kernel32.SetConsoleOutputCP(65001)

r = subprocess.run(
args,
cwd=cwd,
capture_output=self.verbose < 2,
text=True,
encoding="utf8",
)

if is_windows():
# Restore the previous output console code page.
windll.kernel32.SetConsoleOutputCP(previousCp)

return r

def cleanup(self, exit_code: int):
if self.verbose > 0:
print(f"Deleting Flutter bootstrap directory {self.flutter_dir}")
Expand Down

0 comments on commit 31ef1bf

Please sign in to comment.