OLD | NEW |
1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Generic utils.""" | 5 """Generic utils.""" |
6 | 6 |
7 import errno | 7 import errno |
8 import logging | 8 import logging |
9 import os | 9 import os |
10 import Queue | 10 import Queue |
11 import re | 11 import re |
12 import stat | 12 import stat |
13 import sys | 13 import sys |
| 14 import tempfile |
14 import threading | 15 import threading |
15 import time | 16 import time |
16 | 17 |
17 import subprocess2 | 18 import subprocess2 |
18 | 19 |
19 | 20 |
20 class Error(Exception): | 21 class Error(Exception): |
21 """gclient exception class.""" | 22 """gclient exception class.""" |
22 pass | 23 pass |
23 | 24 |
(...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
680 logging.info('Caught exception in thread %s' % self.item.name) | 681 logging.info('Caught exception in thread %s' % self.item.name) |
681 logging.info(str(sys.exc_info())) | 682 logging.info(str(sys.exc_info())) |
682 work_queue.exceptions.put(sys.exc_info()) | 683 work_queue.exceptions.put(sys.exc_info()) |
683 logging.info('_Worker.run(%s) done' % self.item.name) | 684 logging.info('_Worker.run(%s) done' % self.item.name) |
684 | 685 |
685 work_queue.ready_cond.acquire() | 686 work_queue.ready_cond.acquire() |
686 try: | 687 try: |
687 work_queue.ready_cond.notifyAll() | 688 work_queue.ready_cond.notifyAll() |
688 finally: | 689 finally: |
689 work_queue.ready_cond.release() | 690 work_queue.ready_cond.release() |
| 691 |
| 692 |
| 693 def GetEditor(git): |
| 694 """Returns the most plausible editor to use.""" |
| 695 if git: |
| 696 editor = os.environ.get('GIT_EDITOR') |
| 697 else: |
| 698 editor = os.environ.get('SVN_EDITOR') |
| 699 if not editor: |
| 700 editor = os.environ.get('EDITOR') |
| 701 if not editor: |
| 702 if sys.platform.startswith('win'): |
| 703 editor = 'notepad' |
| 704 else: |
| 705 editor = 'vim' |
| 706 return editor |
| 707 |
| 708 |
| 709 def RunEditor(content, git): |
| 710 """Opens up the default editor in the system to get the CL description.""" |
| 711 file_handle, filename = tempfile.mkstemp(text=True) |
| 712 # Make sure CRLF is handled properly by requiring none. |
| 713 if '\r' in content: |
| 714 print >> sys.stderr, ('!! Please remove \\r from your content !!') |
| 715 fileobj = os.fdopen(file_handle, 'w') |
| 716 # Still remove \r if present. |
| 717 fileobj.write(re.sub('\r?\n', '\n', content)) |
| 718 fileobj.close() |
| 719 |
| 720 try: |
| 721 cmd = '%s %s' % (GetEditor(git), filename) |
| 722 if sys.platform == 'win32' and os.environ.get('TERM') == 'msys': |
| 723 # Msysgit requires the usage of 'env' to be present. |
| 724 cmd = 'env ' + cmd |
| 725 try: |
| 726 # shell=True to allow the shell to handle all forms of quotes in |
| 727 # $EDITOR. |
| 728 subprocess2.check_call(cmd, shell=True) |
| 729 except subprocess2.CalledProcessError: |
| 730 return None |
| 731 return FileRead(filename) |
| 732 finally: |
| 733 os.remove(filename) |
OLD | NEW |