KAP Tool#

To upload a Python package compatible with Linux to Kappa-Automate, you need to obtain the Linux-compatible distribution files for the package. We have developed the KAP Tool to simplify this procedure.

Follow these steps:

  • Copy the code below into a Python file named kap.py:

import os
import sys
import tempfile
import shutil
import subprocess
import argparse
import textwrap
import zipfile

from pathlib import Path

parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter)
subparsers = parser.add_subparsers(title='available commands')

pack_parser = subparsers.add_parser('pack', help="Create a KAPPA-Automate package based on requirements file")
pack_parser.add_argument('requirements', type=str, help='Create a KAPPA-Automate user task package from the given requirements file')

install_parser = subparsers.add_parser('install', help="Create a KAPPA-Automate package based on requirements file and install it on KAPPA-Automate")
install_parser.add_argument('requirements', type=str, help='Create a KAPPA-Automate user task package from the given requirements file')

parser.epilog = textwrap.dedent(f"{pack_parser.format_usage()}{install_parser.format_usage()}")

args = parser.parse_args(args=None if sys.argv[1:] else ['--help'])

with tempfile.TemporaryDirectory() as temp_dir:
    print("Downloading:")
    shutil.rmtree(temp_dir)
    download_dir = Path(temp_dir) / "downloaded_packages"
    os.makedirs(download_dir)

    # Python Processing mS base image is ubuntu:noble - Ubuntu 24.04 (glibc 2.39)
    download_command = [
        sys.executable, "-m", "pip", "download",
        f"--dest={download_dir}",
        "--platform=manylinux_2_35_x86_64",   # Ubuntu 22.04+ (glibc 2.35)
        "--platform=manylinux_2_28_x86_64",   # Ubuntu 20.04+ (glibc 2.28)
        "--platform=manylinux_2_17_x86_64",   # Legacy (glibc 2.17)
        "--platform=manylinux2014_x86_64",    # Legacy alias for manylinux_2_17
        "--python-version=3.12",
        "--implementation=cp",
        "--abi=cp312",
        "--only-binary=:all:",
        "-r", args.requirements
    ]

    print(f"Executing: {' '.join(download_command[4:])}")
    try:
        result = subprocess.run(download_command, capture_output=True, text=True)
        if result.returncode == 0:
            print("Download successful!")
            if result.stdout.strip():
                print(result.stdout)
        else:
            print(f"Package download failed: {result.stderr.strip()}")
            exit(1)
    except Exception as e:
        print(f"Package download failed: {e}")
        exit(1)

    print("Unpacking:")

    ka_package_dir = Path(temp_dir) / "ka_package"
    os.makedirs(ka_package_dir)

    for whl_file in os.listdir(download_dir):
        with zipfile.ZipFile(Path(download_dir) / whl_file) as f:
            f.extractall(ka_package_dir)
        print(f"{whl_file} -> {ka_package_dir}")

    print("Re-packing:")

    requirements_path = Path(args.requirements)
    ka_package_file = requirements_path.parent / (requirements_path.stem + ".zip")
    with zipfile.ZipFile(ka_package_file, "w", zipfile.ZIP_DEFLATED) as zf:
        src_path = Path(ka_package_dir).expanduser().resolve(strict=True)
        for file in src_path.rglob('*'):
            zf.write(file, file.relative_to(src_path))
            print(f"{file} -> {ka_package_file}")
  • Create a “requirements.txt” file listing all packages you want to use.

  • From a terminal in the directory containing kap.py, run: python -m kap pack requirements.txt

This will generate a zip file containing all the specified packages.

Example: If you only need numpy 2.0.0, add numpy==2.0.0 to your requirements.txt file and run the command above.