filereader.py 841 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. import os
  2. import socket
  3. from urllib.parse import urlparse
  4. from openpilot.tools.lib.url_file import URLFile
  5. DATA_ENDPOINT = os.getenv("DATA_ENDPOINT", "http://data-raw.comma.internal/")
  6. def internal_source_available():
  7. try:
  8. hostname = urlparse(DATA_ENDPOINT).hostname
  9. if hostname:
  10. socket.gethostbyname(hostname)
  11. return True
  12. except socket.gaierror:
  13. pass
  14. return False
  15. def resolve_name(fn):
  16. if fn.startswith("cd:/"):
  17. return fn.replace("cd:/", DATA_ENDPOINT)
  18. return fn
  19. def file_exists(fn):
  20. fn = resolve_name(fn)
  21. if fn.startswith(("http://", "https://")):
  22. return URLFile(fn).get_length_online() != -1
  23. return os.path.exists(fn)
  24. def FileReader(fn, debug=False):
  25. fn = resolve_name(fn)
  26. if fn.startswith(("http://", "https://")):
  27. return URLFile(fn, debug=debug)
  28. return open(fn, "rb")