| OLD | NEW |
| 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 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 codecs | 7 import codecs |
| 8 import cStringIO | 8 import cStringIO |
| 9 import datetime | 9 import datetime |
| 10 import logging | 10 import logging |
| (...skipping 1038 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1049 work_queue.exceptions.put((sys.exc_info(), self)) | 1049 work_queue.exceptions.put((sys.exc_info(), self)) |
| 1050 finally: | 1050 finally: |
| 1051 logging.info('_Worker.run(%s) done', self.item.name) | 1051 logging.info('_Worker.run(%s) done', self.item.name) |
| 1052 work_queue.ready_cond.acquire() | 1052 work_queue.ready_cond.acquire() |
| 1053 try: | 1053 try: |
| 1054 work_queue.ready_cond.notifyAll() | 1054 work_queue.ready_cond.notifyAll() |
| 1055 finally: | 1055 finally: |
| 1056 work_queue.ready_cond.release() | 1056 work_queue.ready_cond.release() |
| 1057 | 1057 |
| 1058 | 1058 |
| 1059 def GetEditor(git, git_editor=None): | 1059 def GetEditor(git_editor=None): |
| 1060 """Returns the most plausible editor to use. | 1060 """Returns the most plausible editor to use. |
| 1061 | 1061 |
| 1062 In order of preference: | 1062 In order of preference: |
| 1063 - GIT_EDITOR/SVN_EDITOR environment variable | 1063 - GIT_EDITOR/SVN_EDITOR environment variable |
| 1064 - core.editor git configuration variable (if supplied by git-cl) | 1064 - core.editor git configuration variable (if supplied by git-cl) |
| 1065 - VISUAL environment variable | 1065 - VISUAL environment variable |
| 1066 - EDITOR environment variable | 1066 - EDITOR environment variable |
| 1067 - vi (non-Windows) or notepad (Windows) | 1067 - vi (non-Windows) or notepad (Windows) |
| 1068 | 1068 |
| 1069 In the case of git-cl, this matches git's behaviour, except that it does not | 1069 In the case of git-cl, this matches git's behaviour, except that it does not |
| 1070 include dumb terminal detection. | 1070 include dumb terminal detection. |
| 1071 | |
| 1072 In the case of gcl, this matches svn's behaviour, except that it does not | |
| 1073 accept a command-line flag or check the editor-cmd configuration variable. | |
| 1074 """ | 1071 """ |
| 1075 if git: | 1072 editor = os.environ.get('GIT_EDITOR') or git_editor |
| 1076 editor = os.environ.get('GIT_EDITOR') or git_editor | |
| 1077 else: | |
| 1078 editor = os.environ.get('SVN_EDITOR') | |
| 1079 if not editor: | 1073 if not editor: |
| 1080 editor = os.environ.get('VISUAL') | 1074 editor = os.environ.get('VISUAL') |
| 1081 if not editor: | 1075 if not editor: |
| 1082 editor = os.environ.get('EDITOR') | 1076 editor = os.environ.get('EDITOR') |
| 1083 if not editor: | 1077 if not editor: |
| 1084 if sys.platform.startswith('win'): | 1078 if sys.platform.startswith('win'): |
| 1085 editor = 'notepad' | 1079 editor = 'notepad' |
| 1086 else: | 1080 else: |
| 1087 editor = 'vi' | 1081 editor = 'vi' |
| 1088 return editor | 1082 return editor |
| 1089 | 1083 |
| 1090 | 1084 |
| 1091 def RunEditor(content, git, git_editor=None): | 1085 def RunEditor(content, git, git_editor=None): |
| 1092 """Opens up the default editor in the system to get the CL description.""" | 1086 """Opens up the default editor in the system to get the CL description.""" |
| 1093 file_handle, filename = tempfile.mkstemp(text=True, prefix='cl_description') | 1087 file_handle, filename = tempfile.mkstemp(text=True, prefix='cl_description') |
| 1094 # Make sure CRLF is handled properly by requiring none. | 1088 # Make sure CRLF is handled properly by requiring none. |
| 1095 if '\r' in content: | 1089 if '\r' in content: |
| 1096 print >> sys.stderr, ( | 1090 print >> sys.stderr, ( |
| 1097 '!! Please remove \\r from your change description !!') | 1091 '!! Please remove \\r from your change description !!') |
| 1098 fileobj = os.fdopen(file_handle, 'w') | 1092 fileobj = os.fdopen(file_handle, 'w') |
| 1099 # Still remove \r if present. | 1093 # Still remove \r if present. |
| 1100 content = re.sub('\r?\n', '\n', content) | 1094 content = re.sub('\r?\n', '\n', content) |
| 1101 # Some editors complain when the file doesn't end in \n. | 1095 # Some editors complain when the file doesn't end in \n. |
| 1102 if not content.endswith('\n'): | 1096 if not content.endswith('\n'): |
| 1103 content += '\n' | 1097 content += '\n' |
| 1104 fileobj.write(content) | 1098 fileobj.write(content) |
| 1105 fileobj.close() | 1099 fileobj.close() |
| 1106 | 1100 |
| 1107 try: | 1101 try: |
| 1108 editor = GetEditor(git, git_editor=git_editor) | 1102 editor = GetEditor(git_editor=git_editor) |
| 1109 if not editor: | 1103 if not editor: |
| 1110 return None | 1104 return None |
| 1111 cmd = '%s %s' % (editor, filename) | 1105 cmd = '%s %s' % (editor, filename) |
| 1112 if sys.platform == 'win32' and os.environ.get('TERM') == 'msys': | 1106 if sys.platform == 'win32' and os.environ.get('TERM') == 'msys': |
| 1113 # Msysgit requires the usage of 'env' to be present. | 1107 # Msysgit requires the usage of 'env' to be present. |
| 1114 cmd = 'env ' + cmd | 1108 cmd = 'env ' + cmd |
| 1115 try: | 1109 try: |
| 1116 # shell=True to allow the shell to handle all forms of quotes in | 1110 # shell=True to allow the shell to handle all forms of quotes in |
| 1117 # $EDITOR. | 1111 # $EDITOR. |
| 1118 subprocess2.check_call(cmd, shell=True) | 1112 subprocess2.check_call(cmd, shell=True) |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1225 # Just incase we have some ~/blah paths. | 1219 # Just incase we have some ~/blah paths. |
| 1226 target = os.path.abspath(os.path.expanduser(target)) | 1220 target = os.path.abspath(os.path.expanduser(target)) |
| 1227 if os.path.isfile(target) and os.access(target, os.X_OK): | 1221 if os.path.isfile(target) and os.access(target, os.X_OK): |
| 1228 return target | 1222 return target |
| 1229 if sys.platform.startswith('win'): | 1223 if sys.platform.startswith('win'): |
| 1230 for suffix in ('.bat', '.cmd', '.exe'): | 1224 for suffix in ('.bat', '.cmd', '.exe'): |
| 1231 alt_target = target + suffix | 1225 alt_target = target + suffix |
| 1232 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): | 1226 if os.path.isfile(alt_target) and os.access(alt_target, os.X_OK): |
| 1233 return alt_target | 1227 return alt_target |
| 1234 return None | 1228 return None |
| OLD | NEW |