| 139 | |
| 140 | |
| 141 | class CommandThread(threading.Thread): |
| 142 | |
| 143 | def __init__(self, command, on_done, working_dir="", fallback_encoding="", console_encoding="", **kwargs): |
| 144 | threading.Thread.__init__(self) |
| 145 | self.command = command |
| 146 | self.on_done = on_done |
| 147 | self.working_dir = working_dir |
| 148 | if 'stdin' in kwargs: |
| 149 | self.stdin = kwargs['stdin'].encode() |
| 150 | else: |
| 151 | self.stdin = None |
| 152 | self.stdout = kwargs.get('stdout', subprocess.PIPE) |
| 153 | self.console_encoding = console_encoding |
| 154 | self.fallback_encoding = fallback_encoding |
| 155 | self.kwargs = kwargs |
| 156 | |
| 157 | def run(self): |
| 158 | try: |
| 159 | # Per http://bugs.python.org/issue8557 shell=True is required to |
| 160 | # get $PATH on Windows. Yay portable code. |
| 161 | shell = os.name == 'nt' |
| 162 | |
| 163 | if self.console_encoding: |
| 164 | self.command = [s.encode(self.console_encoding) for s in self.command] |
| 165 | |
| 166 | proc = subprocess.Popen(self.command, |
| 167 | stdout=self.stdout, stderr=subprocess.STDOUT, |
| 168 | stdin=subprocess.PIPE, |
| 169 | cwd=self.working_dir if self.working_dir != '' else None, |
| 170 | shell=shell, universal_newlines=False) |
| 171 | output = proc.communicate(self.stdin)[0] |
| 172 | if not output: |
| 173 | output = '' |
| 174 | # if sublime's python gets bumped to 2.7 we can just do: |
| 175 | # output = subprocess.check_output(self.command) |
| 176 | main_thread(self.on_done, |
| 177 | _make_text_safeish(output, self.fallback_encoding), **self.kwargs) |
| 178 | except subprocess.CalledProcessError as e: |
| 179 | main_thread(self.on_done, e.returncode) |
| 180 | except OSError as e: |
| 181 | if e.errno == 2: |
| 182 | main_thread(sublime.error_message, |
| 183 | "'%s' binary could not be found in PATH\n\nConsider using `vcs` property to specify PATH\n\nPATH is: %s" % (self.command[0], os.environ['PATH'])) |
| 184 | else: |
| 185 | raise e |
| 186 | |
| 187 | |
| 188 | class EditViewCommand(sublime_plugin.TextCommand): |
no outgoing calls
no test coverage detected
searching dependent graphs…