utils.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. """Utility functions for the benchmarks."""
  2. import os
  3. import subprocess
  4. from pathlib import Path
  5. import httpx
  6. from httpx import HTTPError
  7. def get_python_version(venv_path: Path, os_name):
  8. """Get the python version of python in a virtual env.
  9. Args:
  10. venv_path: Path to virtual environment.
  11. os_name: Name of os.
  12. Returns:
  13. The python version.
  14. """
  15. python_executable = (
  16. venv_path / "bin" / "python"
  17. if "windows" not in os_name
  18. else venv_path / "Scripts" / "python.exe"
  19. )
  20. try:
  21. output = subprocess.check_output(
  22. [str(python_executable), "--version"], stderr=subprocess.STDOUT
  23. )
  24. python_version = output.decode("utf-8").strip().split()[1]
  25. return ".".join(python_version.split(".")[:-1])
  26. except subprocess.CalledProcessError:
  27. return None
  28. def get_directory_size(directory: Path):
  29. """Get the size of a directory in bytes.
  30. Args:
  31. directory: The directory to check.
  32. Returns:
  33. The size of the dir in bytes.
  34. """
  35. total_size = 0
  36. for dirpath, _, filenames in os.walk(directory):
  37. for f in filenames:
  38. fp = Path(dirpath) / f
  39. total_size += fp.stat().st_size
  40. return total_size
  41. def send_data_to_posthog(event, properties):
  42. """Send data to PostHog.
  43. Args:
  44. event: The event to send.
  45. properties: The properties to send.
  46. Raises:
  47. HTTPError: When there is an error sending data to PostHog.
  48. """
  49. event_data = {
  50. "api_key": "phc_JoMo0fOyi0GQAooY3UyO9k0hebGkMyFJrrCw1Gt5SGb",
  51. "event": event,
  52. "properties": properties,
  53. }
  54. with httpx.Client() as client:
  55. response = client.post("https://app.posthog.com/capture/", json=event_data)
  56. if response.status_code != 200:
  57. raise HTTPError(
  58. f"Error sending data to PostHog: {response.status_code} - {response.text}"
  59. )