Advertisement
Guest User

Untitled

a guest
Oct 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import getpass
  2. from fabric.api import *
  3. from fabric.operations import _prefix_env_vars, _prefix_commands, subprocess, win32, os, error, _AttributeString
  4.  
  5.  
  6. class return_codes:
  7. def __init__(self, ok_ret_codes):
  8. self.previous_value = env.ok_ret_codes
  9. env.ok_ret_codes = ok_ret_codes
  10.  
  11. def __enter__(self):
  12. return 0
  13.  
  14. def __exit__(self, exc_type, exc_value, traceback):
  15. env.ok_ret_codes = self.previous_value
  16.  
  17.  
  18. def lsudo(command, capture=False, shell=None):
  19. with return_codes([0, 1]):
  20. with hide('output', 'running'), settings(warn_only=True):
  21. while local('sudo -n true', capture=True).return_code == 1:
  22. whoami = local('whoami', capture=True)
  23. env.sudo_password = getpass.getpass('[sudo] password for {user}: '.format(user=whoami))
  24. local('echo %s|sudo -S true' % env.sudo_password, capture=True)
  25.  
  26. given_command = command
  27. # Apply cd(), path() etc
  28. with_env = _prefix_env_vars(command, local=True)
  29. wrapped_command = _prefix_commands(with_env, 'local')
  30. if output.debug:
  31. print("[localhost] local: %s" % (wrapped_command))
  32. elif output.running:
  33. print("[localhost] local: " + given_command)
  34. # Adding sudo wrap
  35. if env.get('sudo_password') is not None:
  36. wrapped_command = 'echo %s|sudo -S %s' % (env.sudo_password, wrapped_command)
  37. else:
  38. wrapped_command = 'sudo %s' % wrapped_command
  39.  
  40. # Tie in to global output controls as best we can; our capture argument
  41. # takes precedence over the output settings.
  42. dev_null = None
  43. if capture:
  44. out_stream = subprocess.PIPE
  45. err_stream = subprocess.PIPE
  46. else:
  47. dev_null = open(os.devnull, 'w+')
  48. # Non-captured, hidden streams are discarded.
  49. out_stream = None if output.stdout else dev_null
  50. err_stream = None if output.stderr else dev_null
  51. try:
  52. cmd_arg = wrapped_command if win32 else [wrapped_command]
  53. p = subprocess.Popen(cmd_arg, shell=True, stdout=out_stream,
  54. stderr=err_stream, executable=shell,
  55. close_fds=(not win32))
  56. (stdout, stderr) = p.communicate()
  57. finally:
  58. if dev_null is not None:
  59. dev_null.close()
  60. # Handle error condition (deal with stdout being None, too)
  61. out = _AttributeString(stdout.strip() if stdout else "")
  62. err = _AttributeString(stderr.strip() if stderr else "")
  63. out.command = given_command
  64. out.real_command = wrapped_command
  65. out.failed = False
  66. out.return_code = p.returncode
  67. out.stderr = err
  68. if p.returncode not in env.ok_ret_codes:
  69. out.failed = True
  70. msg = "local() encountered an error (return code %s) while executing '%s'" % (p.returncode, command)
  71. error(message=msg, stdout=out, stderr=err)
  72. out.succeeded = not out.failed
  73. # If we were capturing, this will be a string; otherwise it will be None.
  74. return out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement