Advertisement
Guest User

processtext

a guest
Sep 25th, 2018
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. import fcntl
  2. import os
  3. import select
  4. import shlex
  5. import subprocess
  6. import sys
  7.  
  8. class bcolors:
  9. HEADER = '\033[95m'
  10. OKBLUE = '\033[94m'
  11. OKGREEN = '\033[92m'
  12. WARNING = '\033[93m'
  13. FAIL = '\033[91m'
  14. ENDC = '\033[0m'
  15. BOLD = '\033[1m'
  16. UNDERLINE = '\033[4m'
  17.  
  18. def runtext(command, stdin = None, output = None, encoding = None):
  19. if(output == None):
  20. output = "all"
  21. if(encoding == None):
  22. encoding = "utf-8"
  23.  
  24. ret = subprocess.CompletedProcess(command, 0)
  25. process = subprocess.Popen(shlex.split(command), stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, bufsize=1, encoding=encoding)
  26. if(stdin != None):
  27. process.stdin.write(stdin)
  28. process.stdin.write("\n")
  29.  
  30. ##subprocess.[stderr|stdout].readline() lock the script. This is so it isn't so.
  31. flags = fcntl.fcntl(process.stdout, fcntl.F_GETFL) # get current p.stdout flags
  32. fcntl.fcntl(process.stdout, fcntl.F_SETFL, flags | os.O_NONBLOCK)
  33. flags = fcntl.fcntl(process.stderr, fcntl.F_GETFL) # get current p.stderr flags
  34. fcntl.fcntl(process.stderr, fcntl.F_SETFL, flags | os.O_NONBLOCK)
  35. flags = fcntl.fcntl(process.stdin, fcntl.F_GETFL) # get current p.stderr flags
  36. fcntl.fcntl(process.stdin, fcntl.F_SETFL, flags | os.O_NONBLOCK)
  37.  
  38. while(True):
  39. while sys.stdin in select.select([sys.stdin], [], [], 0)[0]:
  40. line = sys.stdin.readline()
  41. if line:
  42. print(line)
  43. process.stdin.write(line)
  44. #else: # an empty line means stdin has been closed
  45. #print('eof')
  46. #exit(0)
  47. else:
  48. #print("41")
  49. _output = ''
  50. error = ''
  51. try:
  52. temp = process.stdout.readline()
  53. except OSError:
  54. temp = ''
  55. while(temp != ''):
  56. #print("49")
  57. _output += temp
  58. try:
  59. temp = process.stdout.readline()
  60. except OSError:
  61. temp = ''
  62.  
  63. try:
  64. #print("57")
  65. temp = process.stderr.readline()
  66. except OSError:
  67. temp = ''
  68. while(temp != ''):
  69. #print("62")
  70. error += temp
  71. try:
  72. temp = process.stderr.readline()
  73. except OSError:
  74. temp = ''
  75. if(_output == '' and error == '' and process.poll() != None):
  76. break
  77. if(_output):
  78. #print("out:")
  79. ret.stdout = ar.string.append(_output, ret.stdout)
  80. if(output == "all"):
  81. print(ar.string.trim(_output))
  82. #print(_output)
  83. if(error):
  84. #print("err:")
  85. ret.stderr = ar.string.append(error, ret.stderr)
  86. if(output == "all" or output == "error"):
  87. print(bcolors.FAIL + ar.string.trim(error) + bcolors.ENDC)
  88. #print(error)
  89. ret.returncode = process.poll()
  90. ##If we raise here, ret is not returned and output and error information cannot be accessed.
  91. #ret.check_returncode()
  92. return ret
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement