salt-unity 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #!/usr/bin/env python2
  2. # Import python libs
  3. import sys
  4. # Import salt libs
  5. import salt.scripts
  6. import salt.utils.platform
  7. def get_avail():
  8. '''
  9. Return the available salt commands
  10. '''
  11. ret = []
  12. for fun in dir(salt.scripts):
  13. if fun.startswith('salt'):
  14. ret.append(fun[5:])
  15. return ret
  16. def redirect():
  17. '''
  18. Change the args and redirect to another salt script
  19. '''
  20. avail = get_avail()
  21. if len(sys.argv) < 2:
  22. msg = 'Must pass in a salt command, available commands are:'
  23. for cmd in avail:
  24. msg += '\n{0}'.format(cmd)
  25. print(msg)
  26. sys.exit(1)
  27. cmd = sys.argv[1]
  28. if cmd not in avail:
  29. # Fall back to the salt command
  30. sys.argv[0] = 'salt'
  31. s_fun = salt.scripts.salt_main
  32. else:
  33. sys.argv[0] = 'salt-{0}'.format(cmd)
  34. sys.argv.pop(1)
  35. s_fun = getattr(salt.scripts, 'salt_{0}'.format(cmd))
  36. s_fun()
  37. if __name__ == '__main__':
  38. if salt.utils.platform.is_windows():
  39. # Since this file does not have a '.py' extension, when running on
  40. # Windows, spawning any addional processes will fail due to Python
  41. # not being able to load this 'module' in the new process.
  42. # Work around this by creating a '.pyc' file which will enable the
  43. # spawned process to load this 'module' and proceed.
  44. import os.path
  45. import py_compile
  46. cfile = os.path.splitext(__file__)[0] + '.pyc'
  47. if not os.path.exists(cfile):
  48. py_compile.compile(__file__, cfile)
  49. redirect()