aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2011-01-08 13:36:21 +0100
committerGuido Günther <agx@sigxcpu.org>2011-01-08 15:23:16 +0100
commit79ed2e0f088d0b374f5dbed12051770236268238 (patch)
tree96b8cb9e94425115759aaa97f324c2ac0c5c4a2b
parentfc1d47d222e76e1c89b8c022f7f285bddbc17f96 (diff)
Use tristate option for --color=value
this allows true and false as alias for on and off.
-rwxr-xr-xgbp-clone2
-rwxr-xr-xgbp-pq3
-rwxr-xr-xgbp-pull2
-rw-r--r--gbp/config.py15
-rw-r--r--gbp/log.py5
-rw-r--r--gbp/tristate.py22
-rwxr-xr-xgit-buildpackage2
-rwxr-xr-xgit-dch2
-rwxr-xr-xgit-import-dsc2
-rwxr-xr-xgit-import-orig2
10 files changed, 42 insertions, 15 deletions
diff --git a/gbp-clone b/gbp-clone
index a2429fe..e75cb2c 100755
--- a/gbp-clone
+++ b/gbp-clone
@@ -45,7 +45,7 @@ def main(argv):
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
(options, args) = parser.parse_args(argv)
gbp.log.setup(options.color, options.verbose)
diff --git a/gbp-pq b/gbp-pq
index 7c35a99..a262398 100755
--- a/gbp-pq
+++ b/gbp-pq
@@ -162,7 +162,8 @@ def main(argv):
parser.add_boolean_config_file_option(option_name="patch-numbers", dest="patch_numbers")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
+
(options, args) = parser.parse_args(argv)
gbp.log.setup(options.color, options.verbose)
diff --git a/gbp-pull b/gbp-pull
index e7d9815..102168d 100755
--- a/gbp-pull
+++ b/gbp-pull
@@ -80,7 +80,7 @@ def main(argv):
branch_group.add_boolean_config_file_option(option_name="pristine-tar", dest="pristine_tar")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
(options, args) = parser.parse_args(argv)
diff --git a/gbp/config.py b/gbp/config.py
index c989afe..e623c3b 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -3,7 +3,7 @@
# (C) 2006,2007,2010 Guido Guenther <agx@sigxcpu.org>
"""handles command line and config file option parsing for the gbp commands"""
-from optparse import OptionParser, OptionGroup, Option
+from optparse import OptionParser, OptionGroup, Option, OptionValueError
from ConfigParser import SafeConfigParser
from copy import copy
import os.path
@@ -11,15 +11,26 @@ try:
from gbp.gbp_version import gbp_version
except ImportError:
gbp_version = "[Unknown version]"
+import gbp.tristate
def expand_path(option, opt, value):
value = os.path.expandvars(value)
return os.path.expanduser(value)
+def check_tristate(option, opt, value):
+ try:
+ val = gbp.tristate.Tristate(value)
+ except TypeError:
+ raise OptionValueError(
+ "option %s: invalid value: %r" % (opt, value))
+ else:
+ return val
+
class GbpOption(Option):
- TYPES = Option.TYPES + ('path',)
+ TYPES = Option.TYPES + ('path', 'tristate')
TYPE_CHECKER = copy(Option.TYPE_CHECKER)
TYPE_CHECKER['path'] = expand_path
+ TYPE_CHECKER['tristate'] = check_tristate
class GbpOptionParser(OptionParser):
"""
diff --git a/gbp/log.py b/gbp/log.py
index 07de1cf..86e4943 100644
--- a/gbp/log.py
+++ b/gbp/log.py
@@ -18,6 +18,7 @@
"""Simple colored logging classes"""
import sys
+import gbp.tristate
logger = None
@@ -53,9 +54,9 @@ class Logger(object):
if type(color) == type(True):
self.color = color
else:
- if color.lower() == "on":
+ if color.is_on():
self.color = True
- elif color.lower() == "auto":
+ elif color.is_auto():
if (sys.stderr.isatty() and
sys.stdout.isatty()):
self.color = True
diff --git a/gbp/tristate.py b/gbp/tristate.py
index ae47d03..95391b9 100644
--- a/gbp/tristate.py
+++ b/gbp/tristate.py
@@ -16,10 +16,11 @@
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
class Tristate(object):
- """Tri-state value: on, off or auto"""
- AUTO = -1
- ON = True
- OFF = False
+ """Tri-state value: on, off or auto """
+ ON = True # state is on == do it
+ OFF = False # state is off == don't do it
+ AUTO = -1 # autodetect == do if possible
+
# We accept true as alias for on and false as alias for off
_VALID_NAMES = [ 'on', 'off', 'true', 'false', 'auto' ]
@@ -63,3 +64,16 @@ class Tristate(object):
def is_off(self):
return [False, True][self._state == self.OFF]
+ def do(self, function, *args, **kwargs):
+ """
+ Run function if tristate is on or auto, only report a failure if
+ tristate is on since failing is o.k. for autodetect.
+ """
+ if self.is_off():
+ return True
+
+ success = function(*args, **kwargs)
+ if not success:
+ return [True, False][self.is_on()]
+
+ return True
diff --git a/git-buildpackage b/git-buildpackage
index 42049d3..a7a0cec 100755
--- a/git-buildpackage
+++ b/git-buildpackage
@@ -247,7 +247,7 @@ def main(argv):
parser.add_boolean_config_file_option(option_name = "ignore-new", dest="ignore_new")
parser.add_option("--git-verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
tag_group.add_option("--git-tag", action="store_true", dest="tag", default=False,
help="create a tag after a successful build")
tag_group.add_option("--git-tag-only", action="store_true", dest="tag_only", default=False,
diff --git a/git-dch b/git-dch
index 69c2792..d2271be 100755
--- a/git-dch
+++ b/git-dch
@@ -340,7 +340,7 @@ def main(argv):
help="options to pass to git-log, default is '%(git-log)s'")
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
range_group.add_option("-s", "--since", dest="since", help="commit to start from (e.g. HEAD^^^, debian/0.4.3)")
range_group.add_option("-a", "--auto", action="store_true", dest="auto", default=False,
help="autocomplete changelog from last snapshot or tag")
diff --git a/git-import-dsc b/git-import-dsc
index ee7a72b..409e345 100755
--- a/git-import-dsc
+++ b/git-import-dsc
@@ -170,7 +170,7 @@ def main(argv):
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
parser.add_option("--download", action="store_true", dest="download", default=False,
help="download source package")
branch_group.add_config_file_option(option_name="debian-branch",
diff --git a/git-import-orig b/git-import-orig
index 10d85b1..9a09605 100755
--- a/git-import-orig
+++ b/git-import-orig
@@ -235,7 +235,7 @@ def main(argv):
dest='interactive')
parser.add_option("-v", "--verbose", action="store_true", dest="verbose", default=False,
help="verbose command execution")
- parser.add_config_file_option(option_name="color", dest="color")
+ parser.add_config_file_option(option_name="color", dest="color", type='tristate')
# Accepted for compatibility
parser.add_option("--no-dch", dest='no_dch', action="store_true",