You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
3.5 KiB

  1. import os
  2. import re
  3. import subprocess
  4. import sys
  5. from functools import partial
  6. from multiprocessing.dummy import Pool
  7. from gooey.gui import events
  8. from gooey.gui.pubsub import pub
  9. from gooey.gui.util.casting import safe_float
  10. from gooey.gui.util.taskkill import taskkill
  11. from gooey.util.functional import unit, bind
  12. class ProcessController(object):
  13. def __init__(self, progress_regex, progress_expr, encoding):
  14. self._process = None
  15. self.progress_regex = progress_regex
  16. self.progress_expr = progress_expr
  17. self.encoding = encoding
  18. self.wasForcefullyStopped = False
  19. def was_success(self):
  20. self._process.communicate()
  21. return self._process.returncode == 0
  22. def poll(self):
  23. if not self._process:
  24. raise Exception('Not started!')
  25. self._process.poll()
  26. def stop(self):
  27. if self.running():
  28. self.wasForcefullyStopped = True
  29. taskkill(self._process.pid)
  30. def running(self):
  31. return self._process and self.poll() is None
  32. def run(self, command):
  33. self.wasForcefullyStopped = False
  34. env = os.environ.copy()
  35. env["GOOEY"] = "1"
  36. env["PYTHONIOENCODING"] = self.encoding
  37. try:
  38. self._process = subprocess.Popen(
  39. command.encode(sys.getfilesystemencoding()),
  40. bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  41. stderr=subprocess.STDOUT, shell=True, env=env)
  42. except:
  43. self._process = subprocess.Popen(
  44. command,
  45. bufsize=1, stdout=subprocess.PIPE, stdin=subprocess.PIPE,
  46. stderr=subprocess.STDOUT, shell=True, env=env)
  47. Pool(1).apply_async(self._forward_stdout, (self._process,))
  48. def _forward_stdout(self, process):
  49. '''
  50. Reads the stdout of `process` and forwards lines and progress
  51. to any interested subscribers
  52. '''
  53. while True:
  54. line = process.stdout.readline()
  55. if not line:
  56. break
  57. pub.send_message(events.CONSOLE_UPDATE, msg=line.decode(self.encoding))
  58. pub.send_message(events.PROGRESS_UPDATE,
  59. progress=self._extract_progress(line))
  60. pub.send_message(events.EXECUTION_COMPLETE)
  61. def _extract_progress(self, text):
  62. '''
  63. Finds progress information in the text using the
  64. user-supplied regex and calculation instructions
  65. '''
  66. # monad-ish dispatch to avoid the if/else soup
  67. find = partial(re.search, string=text.strip().decode(self.encoding))
  68. regex = unit(self.progress_regex)
  69. match = bind(regex, find)
  70. result = bind(match, self._calculate_progress)
  71. return result
  72. def _calculate_progress(self, match):
  73. '''
  74. Calculates the final progress value found by the regex
  75. '''
  76. if not self.progress_expr:
  77. return safe_float(match.group(1))
  78. else:
  79. return self._eval_progress(match)
  80. def _eval_progress(self, match):
  81. '''
  82. Runs the user-supplied progress calculation rule
  83. '''
  84. _locals = {k: safe_float(v) for k, v in match.groupdict().items()}
  85. if "x" not in _locals:
  86. _locals["x"] = [safe_float(x) for x in match.groups()]
  87. try:
  88. return int(eval(self.progress_expr, {}, _locals))
  89. except:
  90. return None