Skip to content

Commit 0fdb54c

Browse files
Refactor setup.py: rename file, streamline version retrieval, and enhance output instructions
This is to skip the deprecation issues of `python setup.py` running rawly Updated the README.md docs
1 parent d721f0f commit 0fdb54c

File tree

5 files changed

+249
-176
lines changed

5 files changed

+249
-176
lines changed

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,14 @@
1212
> * **Manual installation:**
1313
>
1414
> 1. Clone the repository and build the DLLs yourself.
15-
> 2. Run the following to create the wheel file:
15+
> 2. Update the `VERSION` variable in `pyCTools/__init__.py` to match your desired version for the build.
16+
> 3. Run the following to create the wheel file:
1617
> ```bash
17-
> cd tools python setup.py bdist_wheel
18+
> pip install build
19+
> cd tool
20+
> python setupHelper.py # Do not run `setup.py` directly, use this script instead as it handles many important things
1821
> ```
19-
> 3. The setup script will show you how to install the package locally with pip and how to create a virtual environment for testing.
22+
> 4. The setup script will show you how to install the package locally with pip and how to create a virtual environment for testing.
2023
> * **Automatic installation:**
2124
> Go to the [releases page](https://github.com/DefinetlyNotAI/PyCTools/releases) and select the version you wish to install, and click on it, then copy the top `pip` command that will allow you to install it
2225
> [Auto installation support from v0.2.0-beta and above]
@@ -69,7 +72,8 @@ This project provides a cross-language toolkit for Windows process inspection an
6972
│ ├── __init__.py # Package initializer
7073
│ ├── hwrng.py # Hardware RNG DLL wrapper
7174
│ ├── processInspect.py # Process inspection DLL wrapper
72-
│ └── _loadDLL.py # DLL loading logic used by wrappers
75+
│ ├── _loadDLL.py # DLL loading logic used by wrappers
76+
│ └── setup.py # Setup script for building and installing the package
7377
7478
├── tool/ # Build and distribution tools
7579
│ ├── compilerHelper.ps1 # Compiles C code into DLLs

pyCTools/__init__.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,8 @@
22
from pyCTools.processInspect import ProcessMetrics
33

44
import os
5+
56
if os.name != 'nt':
67
raise OSError('This package only supports Windows OS.')
8+
9+
VERSION = "0.2.0-beta"

setup.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
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+
author_email="[email protected]",
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()

tool/setup.py

Lines changed: 0 additions & 172 deletions
This file was deleted.

0 commit comments

Comments
 (0)