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.

98 lines
2.8 KiB

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