From 58c2bf276134be007d7188df292f6758ea7f9795 Mon Sep 17 00:00:00 2001
From: Sebastian Utz <su@rtme.net>
Date: Fri, 14 Jun 2024 10:34:11 +0200
Subject: [PATCH] CFR: Build as a self-contained program using PyInstaller

---
 CHANGES.md                 |  1 +
 cratedb_toolkit/cfr/cli.py |  8 ++++++++
 pyproject.toml             | 10 ++++++++--
 3 files changed, 17 insertions(+), 2 deletions(-)

diff --git a/CHANGES.md b/CHANGES.md
index 0a881545..75ea8f69 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -7,6 +7,7 @@
 - MongoDB: Added adapter amalgamating PyMongo to use CrateDB as backend
 - SQLAlchemy: Clean up and refactor SQLAlchemy polyfills
   to `cratedb_toolkit.util.sqlalchemy`
+- CFR: Build as a self-contained program using PyInstaller
 
 ## 2024/06/18 v0.0.14
 - Add `ctk cfr` and `ctk wtf` diagnostics programs
diff --git a/cratedb_toolkit/cfr/cli.py b/cratedb_toolkit/cfr/cli.py
index e39b0b98..0850305c 100644
--- a/cratedb_toolkit/cfr/cli.py
+++ b/cratedb_toolkit/cfr/cli.py
@@ -1,6 +1,7 @@
 # Copyright (c) 2021-2024, Crate.io Inc.
 # Distributed under the terms of the AGPLv3 license, see LICENSE.
 import logging
+import multiprocessing
 import sys
 
 import click
@@ -77,3 +78,10 @@ def sys_import(ctx: click.Context, source: str):
     except Exception as ex:
         error_logger(ctx)(ex)
         sys.exit(1)
+
+
+if getattr(sys, "frozen", False):
+    # https://github.com/pyinstaller/pyinstaller/issues/6368
+    multiprocessing.freeze_support()
+    # https://stackoverflow.com/questions/45090083/freeze-a-program-created-with-pythons-click-pacage
+    cli(sys.argv[1:])
diff --git a/pyproject.toml b/pyproject.toml
index 8eab8f01..148cbfdf 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -110,8 +110,8 @@ all = [
   "cratedb-toolkit[full,influxdb,mongodb]",
 ]
 cfr = [
-  "pandas<3,>=1",
-  "pyarrow<17",
+  "pandas<2.2",
+  "pyarrow<16.1",
 ]
 cloud = [
   "croud==1.11.1",
@@ -167,6 +167,9 @@ release = [
   "build<2",
   "twine<6",
 ]
+release-cfr = [
+  "pyinstaller<7",
+]
 service = [
   "fastapi<0.112",
   "uvicorn<0.31",
@@ -195,6 +198,7 @@ documentation = "https://github.com/crate-workbench/cratedb-toolkit"
 homepage = "https://github.com/crate-workbench/cratedb-toolkit"
 repository = "https://github.com/crate-workbench/cratedb-toolkit"
 [project.scripts]
+cratedb-cfr = "cratedb_toolkit.cfr.cli:cli"
 cratedb-retention = "cratedb_toolkit.retention.cli:cli"
 cratedb-toolkit = "cratedb_toolkit.cli:cli"
 cratedb-wtf = "cratedb_toolkit.wtf.cli:cli"
@@ -355,3 +359,5 @@ release = [
 ]
 
 test = { cmd = "pytest" }
+
+build-cfr = { cmd = "pyinstaller cratedb_toolkit/cfr/cli.py --onefile --name cratedb-cfr"}