sky.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import logging
  2. import os
  3. import subprocess
  4. import fetch_from
  5. class UnsupportedProtocolException(Exception):
  6. pass
  7. def executable_path():
  8. return "/usr/local/bin/sky"
  9. def is_avaliable():
  10. if not os.path.exists(executable_path()):
  11. return False
  12. try:
  13. subprocess.check_output([executable_path(), "--version"])
  14. return True
  15. except subprocess.CalledProcessError:
  16. return False
  17. except OSError:
  18. return False
  19. def fetch(skynet_id, file_name, timeout=None):
  20. if not is_avaliable():
  21. raise UnsupportedProtocolException("Skynet is not available")
  22. target_dir = os.path.abspath(fetch_from.uniq_string_generator())
  23. os.mkdir(target_dir)
  24. cmd_args = [executable_path(), "get", "-N", "Backbone", "--user", "--wait", "--dir", target_dir, skynet_id]
  25. if timeout is not None:
  26. cmd_args += ["--timeout", str(timeout)]
  27. logging.info("Call skynet with args: %s", cmd_args)
  28. stdout = subprocess.check_output(cmd_args).strip()
  29. logging.debug("Skynet call with args %s is finished, result is %s", cmd_args, stdout)
  30. return os.path.join(target_dir, file_name)