utils.py 1.9 KB

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