aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGuido Günther <agx@sigxcpu.org>2014-04-01 20:29:22 +0200
committerGuido Günther <agx@sigxcpu.org>2014-04-01 22:42:13 +0200
commit4c6b06773876a35f55f8f5667af65a917f823757 (patch)
tree8623bdccf7d7d1a0d559bdb8f38dae4af0e652f0
parent03ada72d54480917c75e05568844e3f596e2cb64 (diff)
Add minimal 'config' command
This only allows to print single config values so far. Closes: #733470
-rw-r--r--docs/Makefile15
-rw-r--r--docs/common.ent1
-rw-r--r--docs/man.gbp-config.sgml11
-rw-r--r--docs/manpages/gbp-config.sgml109
-rw-r--r--docs/manpages/manpages.ent1
-rw-r--r--docs/manual.sgml1
-rw-r--r--gbp/config.py15
-rwxr-xr-xgbp/scripts/config.py87
-rw-r--r--tests/01_test_help.py1
-rw-r--r--tests/18_test_Config.py9
-rw-r--r--tests/19_test_gbp_scripts_config.py56
-rw-r--r--tests/data/gbp_config.conf4
-rw-r--r--tests/test_Config.py6
13 files changed, 305 insertions, 11 deletions
diff --git a/docs/Makefile b/docs/Makefile
index 93dd1352..1a98c49b 100644
--- a/docs/Makefile
+++ b/docs/Makefile
@@ -1,15 +1,18 @@
#!/usr/bin/make
-MAN1S = gbp-buildpackage \
+MAN1S = \
+ gbp \
+ gbp-clone \
+ gbp-config \
+ gbp-create-remote-repo \
+ gbp-dch \
gbp-import-dsc \
gbp-import-dscs \
gbp-import-orig \
- gbp-dch \
- gbp \
- gbp-pull \
- gbp-clone \
gbp-pq \
- gbp-create-remote-repo
+ gbp-pull \
+ $(NULL)
+
MAN5S = gbp.conf
MANUAL=manual-html
diff --git a/docs/common.ent b/docs/common.ent
index 58472f40..005d6746 100644
--- a/docs/common.ent
+++ b/docs/common.ent
@@ -8,6 +8,7 @@
<!ENTITY gbp-import-orig "<command>gbp import-orig</command>">
<!ENTITY gbp-import-dsc "<command>gbp import-dsc</command>">
<!ENTITY gbp-import-dscs "<command>gbp import-dscs</command>">
+ <!ENTITY gbp-config "<command>gbp config</command>">
<!ENTITY gbp-dch "<command>gbp dch</command>">
<!ENTITY gbp "<command>gbp</command>">
<!ENTITY gbp-pull "<command>gbp pull</command>">
diff --git a/docs/man.gbp-config.sgml b/docs/man.gbp-config.sgml
new file mode 100644
index 00000000..b0076e8e
--- /dev/null
+++ b/docs/man.gbp-config.sgml
@@ -0,0 +1,11 @@
+<!DOCTYPE reference PUBLIC "-//OASIS//DTD DocBook V4.1//EN" [
+ <!ENTITY % COMMON SYSTEM "common.ent">
+ %COMMON;
+ <!ENTITY % MANPAGES SYSTEM "manpages/manpages.ent">
+ %MANPAGES;
+]>
+
+<reference>
+<title>git-buildpackage Manual</title>
+&man.gbp.config;
+</reference>
diff --git a/docs/manpages/gbp-config.sgml b/docs/manpages/gbp-config.sgml
new file mode 100644
index 00000000..c07d758e
--- /dev/null
+++ b/docs/manpages/gbp-config.sgml
@@ -0,0 +1,109 @@
+<refentry id="man.gbp.config">
+ <refentryinfo>
+ <address>
+ &dhemail;
+ </address>
+ <author>
+ &dhfirstname;
+ &dhsurname;
+ </author>
+ </refentryinfo>
+ <refmeta>
+ <refentrytitle>gbp-config</refentrytitle>
+ &dhsection;
+ </refmeta>
+ <refnamediv>
+ <refname>gbp-config</refname>
+
+ <refpurpose>Query configuration values</refpurpose>
+ </refnamediv>
+ <refsynopsisdiv>
+ <cmdsynopsis>
+ &gbp-config;
+
+ <arg><option>--verbose</option></arg>
+ <arg><option>--color=</option><replaceable>[auto|on|off]</replaceable></arg>
+ <arg choice="plain"><replaceable>command.option</replaceable></arg>
+ </cmdsynopsis>
+ </refsynopsisdiv>
+ <refsect1>
+ <title>DESCRIPTION</title>
+ <para>
+ &gbp-config; prints values from the configuration files. It interpolates the
+ value for <replaceable>option</replaceable> of
+ <replaceable>command</replaceable>.
+ </para>
+ </refsect1>
+ <refsect1>
+ <title>OPTIONS</title>
+ <variablelist>
+ <varlistentry>
+ <term><option>--verbose</option></term>
+ <term><option>-v</option></term>
+ <listitem>
+ <para>verbose execution</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><option>--color=</option><replaceable>[auto|on|off]</replaceable>
+ </term>
+ <listitem>
+ <para>Whether to use colored output.</para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+ <refsect1>
+ <title>EXIT CODES</title>
+ <para>
+ When &gbp-config finishes it indicates success or failure with its exit code:
+ </para>
+ <variablelist>
+ <varlistentry>
+ <term><option>0</option></term>
+ <listitem>
+ <para>Success.</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><option>1</option></term>
+ <listitem>
+ <para>Failed to parse command line</para>
+ </listitem>
+ </varlistentry>
+ <varlistentry>
+ <term><option>2</option></term>
+ <listitem>
+ <para>The value did not exist</para>
+ </listitem>
+ </varlistentry>
+ </variablelist>
+ </refsect1>
+ <refsect1>
+ <title>EXAMPLES</title>
+ <para>Print the value <option>upstream-branch</option> that &gbp-buildpackage;
+ would use:</para>
+ <screen>
+ $ gbp config buildpackage.upstream-branch
+ upstream
+ </screen>
+ </refsect1>
+ <refsect1>
+ &man.gbp.config-files;
+ </refsect1>
+ <refsect1>
+ <title>SEE ALSO</title>
+ <para>
+ <citerefentry>
+ <refentrytitle>gbp.conf</refentrytitle>
+ &dhconfsection;
+ </citerefentry>
+ </para>
+ </refsect1>
+ <refsect1>
+ <title>AUTHOR</title>
+
+ <para>&dhusername; &dhemail;</para>
+
+ </refsect1>
+</refentry>
diff --git a/docs/manpages/manpages.ent b/docs/manpages/manpages.ent
index 3aee1def..d5882548 100644
--- a/docs/manpages/manpages.ent
+++ b/docs/manpages/manpages.ent
@@ -2,6 +2,7 @@
<!ENTITY man.gbp.importdsc SYSTEM "gbp-import-dsc.sgml">
<!ENTITY man.gbp.importdscs SYSTEM "gbp-import-dscs.sgml">
<!ENTITY man.gbp.buildpackage SYSTEM "gbp-buildpackage.sgml">
+<!ENTITY man.gbp.config SYSTEM "gbp-config.sgml">
<!ENTITY man.gbp.dch SYSTEM "gbp-dch.sgml">
<!ENTITY man.gbp SYSTEM "gbp.sgml">
<!ENTITY man.gbp.pull SYSTEM "gbp-pull.sgml">
diff --git a/docs/manual.sgml b/docs/manual.sgml
index 529f8109..af5e12fd 100644
--- a/docs/manual.sgml
+++ b/docs/manual.sgml
@@ -34,6 +34,7 @@
&man.gbp.importorig;
&man.gbp.dch;
&man.gbp.clone;
+ &man.gbp.config;
&man.gbp.pull;
&man.gbp.pq;
&man.gbp.create.remote.repo;
diff --git a/gbp/config.py b/gbp/config.py
index 1980daac..710eddf8 100644
--- a/gbp/config.py
+++ b/gbp/config.py
@@ -305,7 +305,7 @@ class GbpOptionParser(OptionParser):
files = envvar.split(':') if envvar else klass.def_config_files
return [ os.path.expanduser(f) for f in files ]
- def _parse_config_files(self):
+ def parse_config_files(self):
"""
Parse the possible config files and set appropriate values
default values
@@ -372,7 +372,7 @@ class GbpOptionParser(OptionParser):
self.prefix = prefix
self.config = {}
self.config_files = self.get_config_files()
- self._parse_config_files()
+ self.parse_config_files()
if self.command.startswith('git-') or self.command.startswith('gbp-'):
prog = self.command
@@ -447,6 +447,17 @@ class GbpOptionParser(OptionParser):
neg_help = "negates '--%s%s'" % (self.prefix, option_name)
self.add_config_file_option(option_name="no-%s" % option_name, dest=dest, help=neg_help, action="store_false")
+ def get_config_file_value(self, option_name):
+ """
+ Query a single interpolated config file value.
+
+ @param option_name: the config file option to look up
+ @type option_name: string
+ @returns: The config file option value or C{None} if it doesn't exist
+ @rtype: C{str} or C{None}
+ """
+ return self.config.get(option_name)
+
class GbpOptionGroup(OptionGroup):
def add_config_file_option(self, option_name, dest, help=None, **kwargs):
diff --git a/gbp/scripts/config.py b/gbp/scripts/config.py
new file mode 100755
index 00000000..19966fe9
--- /dev/null
+++ b/gbp/scripts/config.py
@@ -0,0 +1,87 @@
+# vim: set fileencoding=utf-8 :
+#
+# (C) 2014 Guido Guenther <agx@sigxcpu.org>
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+#
+"""Query and display config file values"""
+
+import ConfigParser
+import sys
+import os, os.path
+from gbp.config import (GbpOptionParser, GbpOptionGroup)
+from gbp.errors import GbpError
+import gbp.log
+
+def parse_args(argv):
+ try:
+ parser = GbpOptionParser(command=os.path.basename(argv[0]), prefix='',
+ usage='%prog [options] - display configuration settings')
+ except ConfigParser.ParsingError as err:
+ gbp.log.err(err)
+ return None, None
+
+ 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", type='tristate')
+ parser.add_config_file_option(option_name="color-scheme",
+ dest="color_scheme")
+ return parser.parse_args(argv)
+
+
+def parse_config(command):
+ parser = GbpOptionParser(command)
+ parser.parse_config_files()
+ return parser
+
+
+def print_single_value(query, printer):
+ try:
+ cmd, option = query.split('.')
+ except ValueError:
+ return 2
+
+ parser = parse_config(cmd)
+ value = parser.get_config_file_value(option)
+ printer(value)
+ return 0 if value else 1
+
+
+def single_value_printer(value):
+ if (value):
+ print(value)
+
+
+def main(argv):
+ retval = 0
+
+ (options, args) = parse_args(argv)
+ gbp.log.setup(options.color, options.verbose, options.color_scheme)
+
+ if not args:
+ gbp.log.error("No command given")
+ return 2
+ elif len(args) != 2:
+ gbp.log.error("Can only print a single value")
+ return 2
+ else:
+ query = args[1]
+
+ retval = print_single_value(query, single_value_printer)
+ return retval
+
+if __name__ == '__main__':
+ sys.exit(main(sys.argv))
+
+# vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·:
diff --git a/tests/01_test_help.py b/tests/01_test_help.py
index 0e4cf1d2..673d8708 100644
--- a/tests/01_test_help.py
+++ b/tests/01_test_help.py
@@ -12,6 +12,7 @@ class TestHelp(unittest.TestCase):
def testHelp(self):
for script in ['buildpackage',
'clone',
+ 'config',
'create_remote_repo',
'dch',
'import_orig',
diff --git a/tests/18_test_Config.py b/tests/18_test_Config.py
index 701288b4..ae39b5a2 100644
--- a/tests/18_test_Config.py
+++ b/tests/18_test_Config.py
@@ -62,3 +62,12 @@ class TestConfigParser(unittest.TestCase):
actual = parser.config['new_overrides_git_option1']
expected = 'new_overrides_git_value1'
self.assertEqual(actual, expected, "%s != %s for %s" % (actual, expected, cmd))
+
+ def test_get_config_file_value(self):
+ """
+ Read a single value from the parse config
+ """
+ parser = GbpOptionParser('cmd4')
+ self.assertEqual(parser.get_config_file_value('new_overrides_git_option1'),
+ 'new_overrides_git_value1')
+ self.assertEqual(parser.get_config_file_value('doesnotexist'), None)
diff --git a/tests/19_test_gbp_scripts_config.py b/tests/19_test_gbp_scripts_config.py
new file mode 100644
index 00000000..1c3369bd
--- /dev/null
+++ b/tests/19_test_gbp_scripts_config.py
@@ -0,0 +1,56 @@
+# vim: set fileencoding=utf-8 :
+# (C) 2014 Guido Günther <agx@sigxcpu.org>
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+"""Test L{gbp} config"""
+
+import os
+import sys
+import unittest
+import gbp.scripts.config
+
+class TestGbpConfigCommand(unittest.TestCase):
+
+ def setUp(self):
+ self.conffiles_save = os.environ.get('GBP_CONF_FILES')
+ self.confname = 'tests/data/gbp_config.conf'
+ self.assertTrue(os.stat(self.confname))
+ os.environ['GBP_CONF_FILES'] = self.confname
+
+ def test_invocation_single_value(self):
+ """Test if we an invoke it without error"""
+ ret = gbp.scripts.config.main(['doesnotmatter', 'coolcommand.branchname'])
+ self.assertEqual(ret, 0)
+
+ def test_invocation_missing_value(self):
+ """Test if we an invoke it without error"""
+ ret = gbp.scripts.config.main(['doesnotmatter', 'coolcommand.doesnotexist'])
+ self.assertEqual(ret, 1)
+
+ def test_invocation_parse_error(self):
+ """Test if we an invoke it without error"""
+ ret = gbp.scripts.config.main(['doesnotmatter', 'mustcontaindot'])
+ self.assertEqual(ret, 2)
+
+ def test_print_single_value(self):
+ class Printstub(object):
+ result = None
+ def __call__(self, arg):
+ self.result = arg
+
+ printstub = Printstub()
+ ret = gbp.scripts.config.print_single_value('coolcommand.branchname', printstub)
+ self.assertEqual(printstub.result, 'abranch')
+ self.assertEqual(ret, 0)
+
diff --git a/tests/data/gbp_config.conf b/tests/data/gbp_config.conf
new file mode 100644
index 00000000..468e0ba4
--- /dev/null
+++ b/tests/data/gbp_config.conf
@@ -0,0 +1,4 @@
+# Data for TestGbpConfig
+
+[coolcommand]
+branchname = abranch \ No newline at end of file
diff --git a/tests/test_Config.py b/tests/test_Config.py
index 2bae7653..cfdefd8b 100644
--- a/tests/test_Config.py
+++ b/tests/test_Config.py
@@ -74,7 +74,7 @@ def test_parser_fallback():
>>> f = open(confname, 'w')
>>> f.write('[DEFAULT]\\nthere = was\\n[foo]\\nthere = is\\n[git-foo]\\nno = truth\\n')
>>> f.close()
- >>> parser._parse_config_files()
+ >>> parser.parse_config_files()
>>> parser.config['there']
'is'
>>> parser.config['no']
@@ -93,13 +93,13 @@ def test_filter():
>>> f = open(confname, 'w')
>>> f.write('[bar]\\nfilter = asdf\\n')
>>> f.close()
- >>> parser._parse_config_files()
+ >>> parser.parse_config_files()
>>> parser.config['filter']
['asdf']
>>> f = open(confname, 'w')
>>> f.write("[bar]\\nfilter = ['this', 'is', 'a', 'list']\\n")
>>> f.close()
- >>> parser._parse_config_files()
+ >>> parser.parse_config_files()
>>> parser.config['filter']
['this', 'is', 'a', 'list']
"""