compile_current_file.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. #!/usr/bin/env python
  2. # Copyright 2016 The Chromium Authors. All rights reserved.
  3. # Use of this source code is governed by a BSD-style license that can be
  4. # found in the LICENSE file.
  5. import datetime
  6. import fnmatch
  7. import logging
  8. import os
  9. import os.path
  10. import queue as Queue
  11. import sublime
  12. import sublime_plugin
  13. import subprocess
  14. import sys
  15. import tempfile
  16. import threading
  17. import time
  18. # Path to the version of ninja checked in into Chrome.
  19. rel_path_to_ninja = os.path.join('third_party', 'depot_tools', 'ninja')
  20. class PrintOutputCommand(sublime_plugin.TextCommand):
  21. def run(self, edit, **args):
  22. self.view.set_read_only(False)
  23. self.view.insert(edit, self.view.size(), args['text'])
  24. self.view.show(self.view.size())
  25. self.view.set_read_only(True)
  26. class CompileCurrentFile(sublime_plugin.TextCommand):
  27. # static thread so that we don't try to run more than once at a time.
  28. thread = None
  29. lock = threading.Lock()
  30. def __init__(self, args):
  31. super(CompileCurrentFile, self).__init__(args)
  32. self.thread_id = threading.current_thread().ident
  33. self.text_to_draw = ""
  34. self.interrupted = False
  35. def description(self):
  36. return ("Compiles the file in the current view using Ninja, so all that "
  37. "this file and it's project depends on will be built first\n"
  38. "Note that this command is a toggle so invoking it while it runs "
  39. "will interrupt it.")
  40. def draw_panel_text(self):
  41. """Draw in the output.exec panel the text accumulated in self.text_to_draw.
  42. This must be called from the main UI thread (e.g., using set_timeout).
  43. """
  44. assert self.thread_id == threading.current_thread().ident
  45. logging.debug("draw_panel_text called.")
  46. self.lock.acquire()
  47. text_to_draw = self.text_to_draw
  48. self.text_to_draw = ""
  49. self.lock.release()
  50. if len(text_to_draw):
  51. self.output_panel.run_command('print_output', {'text': text_to_draw})
  52. self.view.window().run_command("show_panel", {"panel": "output.exec"})
  53. logging.debug("Added text:\n%s.", text_to_draw)
  54. def update_panel_text(self, text_to_draw):
  55. self.lock.acquire()
  56. self.text_to_draw += text_to_draw
  57. self.lock.release()
  58. sublime.set_timeout(self.draw_panel_text, 0)
  59. def execute_command(self, command, cwd):
  60. """Execute the provided command and send ouput to panel.
  61. Because the implementation of subprocess can deadlock on windows, we use
  62. a Queue that we write to from another thread to avoid blocking on IO.
  63. Args:
  64. command: A list containing the command to execute and it's arguments.
  65. Returns:
  66. The exit code of the process running the command or,
  67. 1 if we got interrupted.
  68. -1 if we couldn't start the process
  69. -2 if we couldn't poll the running process
  70. """
  71. logging.debug("Running command: %s", command)
  72. def EnqueueOutput(out, queue):
  73. """Read all the output from the given handle and insert it into the queue.
  74. Args:
  75. queue: The Queue object to write to.
  76. """
  77. while True:
  78. # This readline will block until there is either new input or the handle
  79. # is closed. Readline will only return None once the handle is close, so
  80. # even if the output is being produced slowly, this function won't exit
  81. # early.
  82. # The potential dealock here is acceptable because this isn't run on the
  83. # main thread.
  84. data = out.readline()
  85. if not data:
  86. break
  87. queue.put(data, block=True)
  88. out.close()
  89. try:
  90. os.chdir(cwd)
  91. proc = subprocess.Popen(command, stdout=subprocess.PIPE, shell=True,
  92. stderr=subprocess.STDOUT, stdin=subprocess.PIPE)
  93. except OSError as e:
  94. logging.exception('Execution of %s raised exception: %s.', (command, e))
  95. return -1
  96. # Use a Queue to pass the text from the reading thread to this one.
  97. stdout_queue = Queue.Queue()
  98. stdout_thread = threading.Thread(target=EnqueueOutput,
  99. args=(proc.stdout, stdout_queue))
  100. stdout_thread.daemon = True # Ensure this exits if the parent dies
  101. stdout_thread.start()
  102. # We use the self.interrupted flag to stop this thread.
  103. while not self.interrupted:
  104. try:
  105. exit_code = proc.poll()
  106. except OSError as e:
  107. logging.exception('Polling execution of %s raised exception: %s.',
  108. command, e)
  109. return -2
  110. # Try to read output content from the queue
  111. current_content = ""
  112. for _ in range(2048):
  113. try:
  114. current_content += stdout_queue.get_nowait().decode('utf-8')
  115. except Queue.Empty:
  116. break
  117. self.update_panel_text(current_content)
  118. current_content = ""
  119. if exit_code is not None:
  120. while stdout_thread.isAlive() or not stdout_queue.empty():
  121. try:
  122. current_content += stdout_queue.get(
  123. block=True, timeout=1).decode('utf-8')
  124. except Queue.Empty:
  125. # Queue could still potentially contain more input later.
  126. pass
  127. time_length = datetime.datetime.now() - self.start_time
  128. self.update_panel_text("%s\nDone!\n(%s seconds)" %
  129. (current_content, time_length.seconds))
  130. return exit_code
  131. # We sleep a little to give the child process a chance to move forward
  132. # before we poll it again.
  133. time.sleep(0.1)
  134. # If we get here, it's because we were interrupted, kill the process.
  135. proc.terminate()
  136. return 1
  137. def run(self, edit, target_build):
  138. """The method called by Sublime Text to execute our command.
  139. Note that this command is a toggle, so if the thread is are already running,
  140. calling run will interrupt it.
  141. Args:
  142. edit: Sumblime Text specific edit brace.
  143. target_build: Release/Debug/Other... Used for the subfolder of out.
  144. """
  145. # There can only be one... If we are running, interrupt and return.
  146. if self.thread and self.thread.is_alive():
  147. self.interrupted = True
  148. self.thread.join(5.0)
  149. self.update_panel_text("\n\nInterrupted current command:\n%s\n" % command)
  150. self.thread = None
  151. return
  152. # It's nice to display how long it took to build.
  153. self.start_time = datetime.datetime.now()
  154. # Output our results in the same panel as a regular build.
  155. self.output_panel = self.view.window().get_output_panel("exec")
  156. self.output_panel.set_read_only(True)
  157. self.view.window().run_command("show_panel", {"panel": "output.exec"})
  158. # TODO(mad): Not sure if the project folder is always the first one... ???
  159. project_folder = self.view.window().folders()[0]
  160. self.update_panel_text("Compiling current file %s\n" %
  161. self.view.file_name())
  162. # The file must be somewhere under the project folder...
  163. if (project_folder.lower() !=
  164. self.view.file_name()[:len(project_folder)].lower()):
  165. self.update_panel_text(
  166. "ERROR: File %s is not in current project folder %s\n" %
  167. (self.view.file_name(), project_folder))
  168. else:
  169. output_dir = os.path.join(project_folder, 'out', target_build)
  170. source_relative_path = os.path.relpath(self.view.file_name(),
  171. output_dir)
  172. # On Windows the caret character needs to be escaped as it's an escape
  173. # character.
  174. carets = '^'
  175. if sys.platform.startswith('win'):
  176. carets = '^^'
  177. command = [
  178. os.path.join(project_folder, rel_path_to_ninja), "-C",
  179. os.path.join(project_folder, 'out', target_build),
  180. source_relative_path + carets]
  181. self.update_panel_text(' '.join(command) + '\n')
  182. self.interrupted = False
  183. self.thread = threading.Thread(target=self.execute_command,
  184. kwargs={"command":command,
  185. "cwd": output_dir})
  186. self.thread.start()
  187. time_length = datetime.datetime.now() - self.start_time
  188. logging.debug("Took %s seconds on UI thread to startup",
  189. time_length.seconds)
  190. self.view.window().focus_view(self.view)