|
| 1 | +import os |
| 2 | +import pathlib |
| 3 | +import re |
| 4 | +import sys |
| 5 | + |
| 6 | +try: |
| 7 | + # noinspection PyUnusedImports |
| 8 | + from setuptools.command.bdist_wheel import bdist_wheel as _bdist_wheel |
| 9 | + # noinspection PyUnusedImports |
| 10 | + from setuptools import setup, find_packages |
| 11 | + import wheel |
| 12 | +except ImportError: |
| 13 | + sys.exit( |
| 14 | + f"[x] Missing required dependencies (either wheel or setuptools)\n" |
| 15 | + " Suggested action: Run 'compilerHelper.ps1' to generate the DLLs before building." |
| 16 | + ) |
| 17 | + |
| 18 | + |
| 19 | +def check_bin_exists(bin_path: str): |
| 20 | + try: |
| 21 | + # Check that bin exists and has x86 & x64 DLLs |
| 22 | + if not os.path.isdir(bin_path): |
| 23 | + sys.exit( |
| 24 | + "[x] 'pyCTools/bin/' folder not found.\n" |
| 25 | + " Suggested action: Run 'compilerHelper.ps1' to generate the DLLs before building.\n" |
| 26 | + ) |
| 27 | + |
| 28 | + # Check subfolders and DLL presence |
| 29 | + for arch in ("x86", "x64"): |
| 30 | + arch_path = os.path.join(bin_path, arch) |
| 31 | + if not os.path.isdir(arch_path) or not any(f.lower().endswith(".dll") for f in os.listdir(arch_path)): |
| 32 | + sys.exit( |
| 33 | + f"[x] Missing DLLs in pyCTools/bin/{arch}/\n" |
| 34 | + " Suggested action: Run 'compilerHelper.ps1' to generate the DLLs before building.\n" |
| 35 | + ) |
| 36 | + except Exception as err: |
| 37 | + sys.exit(f"[x] Fatal error while checking files exist and are ready: {err}") |
| 38 | + |
| 39 | + |
| 40 | +def output_dir_init() -> pathlib.Path: |
| 41 | + try: |
| 42 | + # Ensure ../dist/libraryWheel/ exists |
| 43 | + output_dir = pathlib.Path(__file__).parent.parent / "dist" / "libraryWheel" |
| 44 | + output_dir.mkdir(parents=True, exist_ok=True) |
| 45 | + |
| 46 | + return output_dir |
| 47 | + except Exception as err: |
| 48 | + sys.exit( |
| 49 | + f"[x] Failed to create output directory: {err}\n" |
| 50 | + " Suggested action: Ensure you have write permissions in the parent directory.\n" |
| 51 | + ) |
| 52 | + |
| 53 | + |
| 54 | +def print_separator(title: str = None) -> None: |
| 55 | + """Print a separator with an optional title.""" |
| 56 | + print("\n" + "=" * 80) |
| 57 | + if title: |
| 58 | + print(title) |
| 59 | + print("=" * 80) |
| 60 | + |
| 61 | + |
| 62 | +def get_version() -> str: |
| 63 | + try: |
| 64 | + here = os.path.abspath(os.path.dirname(__file__)) |
| 65 | + init_path = os.path.join(here, "pyCTools", "__init__.py") |
| 66 | + version_regex = r'^VERSION\s*=\s*[\'"]([^\'"]*)[\'"]' |
| 67 | + |
| 68 | + with open(init_path, "r", encoding="utf-8") as f: |
| 69 | + content = f.read() |
| 70 | + match = re.search(version_regex, content, re.MULTILINE) |
| 71 | + if match: |
| 72 | + return match.group(1) |
| 73 | + else: |
| 74 | + sys.exit("[x] Could not find VERSION string in pyCTools/__init__.py") |
| 75 | + except Exception as err: |
| 76 | + sys.exit(f"[x] Error reading version from __init__.py: {err}\n") |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + # Begin |
| 81 | + print_separator("SETUP SCRIPT OUTPUT") |
| 82 | + |
| 83 | + # Get the version from the __init__.py file - manually read |
| 84 | + VERSION = get_version() |
| 85 | + # Check if the bin directory exists and contains the required DLLs |
| 86 | + check_bin_exists(bin_path=os.path.join("pyCTools", "bin")) |
| 87 | + |
| 88 | + try: |
| 89 | + o_dir = output_dir_init() |
| 90 | + |
| 91 | + print("\033[90m") |
| 92 | + setup( |
| 93 | + name="pyCTools", |
| 94 | + version=VERSION, |
| 95 | + packages=find_packages() + ["pyCTools.bin.x64", "pyCTools.bin.x86"], |
| 96 | + include_package_data=True, |
| 97 | + package_data={ |
| 98 | + "pyCTools": ["bin/x86/*.dll", "bin/x64/*.dll"], |
| 99 | + }, |
| 100 | + description="Your pyCTools package with bundled DLLs", |
| 101 | + author="Shahm Najeeb", |
| 102 | + |
| 103 | + url="https://github.com/DefinetlyNotAI/PyCTools", |
| 104 | + classifiers=[ |
| 105 | + "Programming Language :: Python :: 3", |
| 106 | + "Operating System :: Microsoft :: Windows", |
| 107 | + ], |
| 108 | + options={ |
| 109 | + "bdist_wheel": {"dist_dir": str(o_dir)}, |
| 110 | + "sdist": {"dist_dir": str(o_dir)}, |
| 111 | + }, |
| 112 | + ) |
| 113 | + print("\033[0m") |
| 114 | + |
| 115 | + except Exception as e: |
| 116 | + sys.exit(f"\033[0m[x] An error occurred during setup: {e}\n") |
| 117 | + |
| 118 | + print_separator() |
0 commit comments