diff --git a/README.md b/README.md index 96bba4e..4af4ed8 100644 --- a/README.md +++ b/README.md @@ -42,4 +42,8 @@ Run `./lint.sh` to find linting errors using `pylint`, `flake8` and `mypy`. * Cross compiler toolchain images in linux: * https://github.com/dockcross/dockcross * MacOS images - * https://hub.docker.com/r/sickcodes/docker-osx \ No newline at end of file + * https://hub.docker.com/r/sickcodes/docker-osx + +# Releases + + * 1.0.3: Native windows compile is available if the host is also windows. \ No newline at end of file diff --git a/pyproject.toml b/pyproject.toml index e9c160f..b9564f8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -11,10 +11,11 @@ keywords = ["template-python-cmd"] license = { text = "BSD 3-Clause License" } classifiers = ["Programming Language :: Python :: 3"] dependencies = [ - "docker-run-cmd~=1.0.10" + "docker-run-cmd~=1.0.10", + "isolated-environment" ] # Change this with the version number bump. -version = "1.0.2" +version = "1.0.3" [tool.setuptools] package-dir = {"" = "src"} diff --git a/src/python_compile/cli.py b/src/python_compile/cli.py index 2703389..149f5f0 100644 --- a/src/python_compile/cli.py +++ b/src/python_compile/cli.py @@ -9,6 +9,8 @@ from docker_run_cmd.api import docker_run +from python_compile.native_windows_build import run_native_windows_build + HERE = Path(__file__).parent ASSETS = HERE / "assets" @@ -31,7 +33,7 @@ def parse_args() -> argparse.Namespace: parser.add_argument( "--platform", type=str, - help="If specified, then the docker platform will be used", + help="If specified, then the docker platform will be used, like linux/amd64", required=False, ) parser.add_argument( @@ -51,11 +53,19 @@ def parse_args() -> argparse.Namespace: def main() -> int: """Main entry point for the template_python_cmd package.""" args = parse_args() - print(args) os_system = args.os if not os_system: print("You must provide an os") return 1 + if os_system == "windows": + if os.name != "nt": + print("You must run this on a windows machine") + return 1 + rtn = run_native_windows_build( + app_py=args.input, requirements_txt=args.requirements + ) + return rtn + dockerpath: Path = DOCKER_FILE_MAP[os_system] assert dockerpath.exists(), f"dockerpath {dockerpath} does not exist" py_path = args.input diff --git a/src/python_compile/native_windows_build.py b/src/python_compile/native_windows_build.py new file mode 100644 index 0000000..9bbb335 --- /dev/null +++ b/src/python_compile/native_windows_build.py @@ -0,0 +1,51 @@ +import subprocess +import tempfile +from pathlib import Path + +from isolated_environment import IsolatedEnvironment, Requirements + +HERE = Path(__file__).parent +VENV_PATH = HERE / "nuitka_venv" + +REQUIREMENTS = [ + "nuitka==2.0.1", + "zstandard==0.17.0", + "chardet==5.2.0", + "ordered-set==4.1.0", + "python-dotenv==1.0.0", + "tqdm==4.66.1", +] + +REQS = Requirements(REQUIREMENTS) + + +def run_native_windows_build(app_py: Path, requirements_txt: Path) -> int: + """Run the native windows build.""" + print("Running native windows build") + with tempfile.TemporaryDirectory() as tmp_dir: + print(f"Creating temporary directory: {tmp_dir}") + env_path = Path(tmp_dir) / "nuitka_venv" + iso_env = IsolatedEnvironment(env_path, REQS) + subprocess.run( + ["pip", "install", "-r", requirements_txt], + env=iso_env.environment(), + check=True, + ) + # python -m nuitka --standalone --follow-imports --onefile --lto=yes --python-flag=-OO "$@"' + subprocess.run( + [ + "python", + "-m", + "nuitka", + "--mingw", + "--standalone", + "--follow-imports", + "--onefile", + "--lto=yes", + "--python-flag=-OO", + app_py, + ], + env=iso_env.environment(), + check=True, + ) + return 0