Skip to content
This repository has been archived by the owner on Dec 10, 2019. It is now read-only.

Commit

Permalink
Add -t/--timeout/VCPROMPT_TIMEOUT option.
Browse files Browse the repository at this point in the history
  • Loading branch information
djl committed Mar 1, 2013
1 parent cd05563 commit 6625c7b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG
@@ -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
Expand Down
4 changes: 4 additions & 0 deletions README.markdown
Expand Up @@ -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.
Expand Down
11 changes: 10 additions & 1 deletion bin/vcprompt
Expand Up @@ -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
Expand Down Expand Up @@ -47,6 +48,7 @@ from xml.dom.minidom import parseString
import optparse
import os
import re
import signal
import sys

try:
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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


Expand Down

4 comments on commit 6625c7b

@gregglind
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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!

@mbfisher
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

M

@gregglind
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 )

@djl
Copy link
Owner Author

@djl djl commented on 6625c7b Mar 4, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.