utils.py 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. import logging
  2. import subprocess
  3. import os.path
  4. logger = logging.getLogger(__name__)
  5. def is_git_repo(path: str) -> bool:
  6. """
  7. Check if a git repo exists at a given path
  8. Parameters:
  9. path (str): Path to the directory to check
  10. """
  11. if path is None:
  12. raise ValueError("path is required, cannot be None")
  13. if not os.path.exists(path=path):
  14. raise FileNotFoundError(f"The directory '{path}' does not exist.")
  15. if not os.path.isdir(s=path):
  16. # a file cannot be a git repository
  17. return False
  18. cmd = ['git', 'rev-parse', '--is-inside-work-tree']
  19. logger.debug(f"Running {' '.join(cmd)}")
  20. ret = subprocess.run(cmd, cwd=path, shell=False)
  21. return ret.returncode == 0
  22. def is_valid_hex_string(test_str: str) -> bool:
  23. """
  24. Check if a string contains hexadecimal digits only
  25. Parameters:
  26. test_str (str): String to test
  27. """
  28. if test_str is None:
  29. raise ValueError("test_str cannot be None")
  30. return all(c in '1234567890abcdef' for c in test_str)