Skip to content

Commit

Permalink
Set up python more properly
Browse files Browse the repository at this point in the history
  • Loading branch information
fasterthanlime committed Sep 1, 2024
1 parent 3929287 commit a239a3a
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ junit.xml
.DS_Store
/vendor
profile.json
*.egg-info
7 changes: 7 additions & 0 deletions .zed/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
"features": []
}
}
},
"pyright": {
"settings": {
"python": {
"pythonPath": "scripts/.venv/bin/python"
}
}
}
}
}
27 changes: 27 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[project]
name = "loona"
version = "0.1.0"
description = "Scripts"
requires-python = ">=3.11"
dependencies = [
"termcolor~=2.4.0",
"pandas~=2.2.2",
"openpyxl~=3.1.5",
]

[tool.setuptools]
packages = ["scripts"]

[tool.pyright]
include = ["scripts"]
exclude = [
"**/__pycache__",
"**/node_modules",
".venv",
]
venvPath = "."
venv = ".venv"

[build-system]
requires = ["setuptools>=45", "wheel"]
build-backend = "setuptools.build_meta"
32 changes: 30 additions & 2 deletions scripts/bench.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env -S PYTHONUNBUFFERED=1 FORCE_COLOR=1 uv run --with 'termcolor~=2.4.0' --with 'pandas~=2.2.2' --with 'openpyxl~=3.1.5'
#!/usr/bin/env -S PYTHONUNBUFFERED=1 FORCE_COLOR=1 uv run

import ctypes
import os
import signal
import subprocess
Expand All @@ -12,9 +13,20 @@
# Change to the script's directory
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# Define constants for prctl
PR_SET_PDEATHSIG = 1

# Load the libc shared library
libc = ctypes.CDLL("libc.so.6")

def set_pdeathsig():
"""Set the parent death signal to SIGKILL."""
# Call prctl with PR_SET_PDEATHSIG and SIGKILL
libc.prctl(PR_SET_PDEATHSIG, signal.SIGKILL)

# Set trap to kill the process group on script exit
def kill_group():
os.killpg(0, signal.SIGTERM)
os.killpg(0, signal.SIGKILL)

def abort_and_cleanup(signum, frame):
print(colored("\nAborting and cleaning up...", "red"))
Expand Down Expand Up @@ -200,3 +212,19 @@ def abort_and_cleanup(signum, frame):
abort_and_cleanup(None, None)

print(colored("✨ All done, good bye! 👋", "cyan"))

print(colored("Processes still running in the group:", "yellow"))
try:
pgid = os.getpgid(0)
ps_output = subprocess.check_output(["ps", "-o", "pid,cmd", "--no-headers", "-g", str(pgid)]).decode().strip()
if ps_output:
for line in ps_output.split('\n'):
pid, cmd = line.strip().split(None, 1)
print(f"PID: {pid}, Command: {cmd}")
else:
print("No processes found in the group.")
except subprocess.CalledProcessError:
print("Error: Unable to retrieve process information.")

print(colored("Cleaning up...", "red"))
kill_group()
Loading

0 comments on commit a239a3a

Please sign in to comment.