sky.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import logging
  2. import os, sys
  3. import subprocess
  4. # Explicitly enable local imports
  5. # Don't forget to add imported scripts to inputs of the calling command!
  6. sys.path.append(os.path.dirname(os.path.abspath(__file__)))
  7. import fetch_from
  8. class UnsupportedProtocolException(Exception):
  9. pass
  10. def executable_path():
  11. return "/usr/local/bin/sky"
  12. def is_avaliable():
  13. if not os.path.exists(executable_path()):
  14. return False
  15. try:
  16. subprocess.check_output([executable_path(), "--version"])
  17. return True
  18. except subprocess.CalledProcessError:
  19. return False
  20. except OSError:
  21. return False
  22. def fetch(skynet_id, file_name, timeout=None):
  23. if not is_avaliable():
  24. raise UnsupportedProtocolException("Skynet is not available")
  25. target_dir = os.path.abspath(fetch_from.uniq_string_generator())
  26. os.mkdir(target_dir)
  27. cmd_args = [executable_path(), "get", "-N", "Backbone", "--user", "--wait", "--dir", target_dir, skynet_id]
  28. if timeout is not None:
  29. cmd_args += ["--timeout", str(timeout)]
  30. logging.info("Call skynet with args: %s", cmd_args)
  31. stdout = subprocess.check_output(cmd_args).strip()
  32. logging.debug("Skynet call with args %s is finished, result is %s", cmd_args, stdout)
  33. return os.path.join(target_dir, file_name)