Skip to content

Commit

Permalink
[Issue-#186] - Fix - Windows issues on mox cli command and utils argp…
Browse files Browse the repository at this point in the history
…arser (#191)

* fix: windows issues on mox cli command and utils argparser

- `init.py`: windows error `(UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f40d')` when the param `encoding="utf-8" for `open` is not set
- `compile.py`: windows does not support `fork` method for `multiprocessing.set_start_method()`, hence changed to "spawn"
- `vars.py`: added general constant `IS_WINDOWS` for future use and avoid any duplicates
- fixed utils CLI command running an error when no arguments passed + fixed some var typo

* fix: format with ruff reorder import
  • Loading branch information
s3bc40 authored Jan 29, 2025
1 parent cb2445b commit 4d2106b
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 8 deletions.
16 changes: 10 additions & 6 deletions moccasin/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ def main(argv: list) -> int:
return 0


def generate_main_parser_and_sub_parsers() -> (
Tuple[argparse.ArgumentParser, argparse.Action]
):
def generate_main_parser_and_sub_parsers() -> Tuple[
argparse.ArgumentParser, argparse.Action
]:
parent_parser = create_parent_parser()
main_parser = argparse.ArgumentParser(
prog="Moccasin CLI",
Expand Down Expand Up @@ -650,17 +650,21 @@ def generate_main_parser_and_sub_parsers() -> (
# ------------------------------------------------------------------
# UTILS COMMAND
# ------------------------------------------------------------------
utils_paraser = sub_parsers.add_parser(
utils_parser = sub_parsers.add_parser(
"utils",
aliases=["u", "util"],
help="Helpful utilities - right now it's just the one.",
description="Helpful utilities.\n",
parents=[parent_parser],
)
utils_subparaser = utils_paraser.add_subparsers(dest="utils_command")
utils_parser.add_argument(
"utils_command", help="Name of the utility command to get."
)

utils_subparser = utils_parser.add_subparsers(dest="utils_command")

# Zero
utils_subparaser.add_parser(
utils_subparser.add_parser(
"zero",
aliases=["zero-address", "zero_address", "address-zero", "address_zero"],
help="Get the zero address.",
Expand Down
8 changes: 7 additions & 1 deletion moccasin/commands/compile.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
BUILD_FOLDER,
CONTRACTS_FOLDER,
ERAVM,
IS_WINDOWS,
MOCCASIN_GITHUB,
)
from moccasin.logging import logger
Expand Down Expand Up @@ -107,7 +108,12 @@ def compile_project(
f"Compiling {len(contracts_to_compile)} contracts to {build_folder_relpath}/..."
)

multiprocessing.set_start_method("fork", force=False)
# @dev check if OS is Windows since fork
# is not supported on Windows, change it to spawn method
start_method = "fork"
if IS_WINDOWS:
start_method = "spawn"
multiprocessing.set_start_method(start_method, force=False)

n_cpus = max(1, _get_cpu_count() - 2)
jobs = []
Expand Down
2 changes: 1 addition & 1 deletion moccasin/commands/init.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,5 +100,5 @@ def _create_files(

def _write_file(path: Path, contents: str, overwrite: bool = False) -> None:
if not path.exists() or overwrite:
with path.open("w") as fp:
with path.open("w", encoding="utf-8") as fp:
fp.write(contents)
4 changes: 4 additions & 0 deletions moccasin/constants/vars.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
from pathlib import Path

# File Names
Expand Down Expand Up @@ -29,6 +30,9 @@
PYPI = "pypi"
GITHUB = "github"

# OS specific
IS_WINDOWS = platform.system() == "Windows"

# Complex Vars
DEFAULT_PROJECT_FOLDERS = [
CONTRACTS_FOLDER,
Expand Down

0 comments on commit 4d2106b

Please sign in to comment.