Permalink
Browse files

Add -t/--timeout/VCPROMPT_TIMEOUT option.

  • Loading branch information...
1 parent cd05563 commit 6625c7b18f8d1a6a5c3c0d22f6a258f8346e7ead @djl committed Mar 1, 2013
Showing with 15 additions and 1 deletion.
  1. +1 −0 CHANGELOG
  2. +4 −0 README.markdown
  3. +10 −1 bin/vcprompt
View
@@ -1,6 +1,7 @@
1.0.2 (????-??-??)
------------------
+* Added: -t/--timeout option for limiting max execution time
* Fixed: '%m' token in Bazaar when working dir has modified and untracked files
* Fixed: '%u' token always being shown in Python 3.
* Fixed '%r' and '%h' tokens in Mercurual 1.5
View
@@ -42,6 +42,10 @@ OPTIONS
Prints all available version control systems to standard out.
+* `-t, --timeout`
+
+ The maximum execution time in seconds.
+
* `-h, --help`
Prints the help message and exists.
View
@@ -8,6 +8,7 @@ Options:
-f, --format FORMAT The format string to use.
-p, --path PATH The path to run vcprompt on.
-s, --systems Print all known VCSs and exit
+ -t, --timeout The max execution time in seconds
-v, --version Show program's version number and exit
-h, --help Show this help message and exit
@@ -47,6 +48,7 @@ from xml.dom.minidom import parseString
import optparse
import os
import re
+import signal
import sys
try:
@@ -62,6 +64,7 @@ except ImportError:
# user editable options
FORMAT = os.environ.get('VCPROMPT_FORMAT', '%s:%b')
UNKNOWN = os.environ.get('VCPROMPT_UNKNOWN', '(unknown)')
+TIMEOUT = os.environ.get('VCPROMPT_TIMEOUT', 0)
SYSTEMS = []
# status indicators
@@ -200,6 +203,7 @@ def main():
parser.add_option('-f', '--format', dest='format', default=FORMAT)
parser.add_option('-p', '--path', dest='path', default='.')
parser.add_option('-s', '--systems', action='callback', callback=systems)
+ parser.add_option('-t', '--timeout', dest='timeout', default=TIMEOUT, type="int")
parser.add_option('-A', '--staged', dest='staged', default=STAGED)
parser.add_option('-M', '--modified', dest='modified', default=MODIFIED)
parser.add_option('-U', '--untracked', dest='untracked', default=UNTRACKED)
@@ -215,8 +219,13 @@ def main():
parser.add_option(flag, dest=dest, default=default)
options, args = parser.parse_args()
- output = vcprompt(options)
+ # set max execution time
+ # exit with status 126 if timeout is reached
+ signal.signal(signal.SIGALRM, lambda x, y: sys.exit(126))
+ signal.alarm(options.timeout)
+
+ output = vcprompt(options)
return output

4 comments on commit 6625c7b

This is a lot simpler and more elegant than my own "thread out during popen" idea. Unfortunate that this won't work on Windows, but that is life!

Nice!

M

This might be better as signal.settimer (and reframed in int milliseconds)). My own use case... 200 ms is where I start to get annoyed.

( http://docs.python.org/2/library/signal.html#signal.setitimer )

Owner
djl replied Mar 4, 2013

This might be better as signal.settimer

That can't be done without dropping Python 2.4/2.5 support, though. I'm not totally opposed to that now RHEL and Debian are both shipping with 2.6.

Eh. Performance trumps supporting ancient systems. I'll look into it.

Please sign in to comment.