Use JJBConfig for arg/config initialization.

* Create jenkins_jobs.config module with JJBConfig class.
* Move DEFAULT_CONF from jenkins_jobs.cmd into jenkins_jobs.config
* Move configuration initialization into JJBConfig
* Create method, "do_magical_things" to handle arbitration between
  config file and arguments as well as setting default values for
  config file settings if it doesn't contain the expected keys.
* Move JenkinsJobs.create_parser into its own module,
  jenkins_jobs.cli.parser, it can be used to provide default settings
  in the JJBConfig class when an argparse namespace object is not
  provided; this is primarily necessary because most of the original
  configuration initialization code relies on this being a namespace
  object (simple descendant of the object class).

At this point JJBConfig isn't much more than an object-oriented
version of the way configuration handling happened previously. Its
current form, however, is more amenable to the ultimate goal of the
2.0 refactorings--namely, being able to pass a single config object
around rather than breaking it up into apparently arbitrary settings
necessary to instantiate the Builder class and its delegate objects.

Change-Id: Ic0147e1dccbe620aaaba039a434e7cea6c670054
This commit is contained in:
Wayne Warren
2015-12-23 14:58:45 -08:00
committed by Darragh Bailey
parent 2bf92c3ed7
commit 2cf11f014c
10 changed files with 449 additions and 298 deletions

View File

@@ -13,21 +13,20 @@
# License for the specific language governing permissions and limitations
# under the License.
import argparse
import logging
import sys
from stevedore import extension
import jenkins_jobs.version
from jenkins_jobs import cmd
from jenkins_jobs import version
from jenkins_jobs.cli.parser import create_parser
from jenkins_jobs.config import JJBConfig
logging.basicConfig(level=logging.INFO)
def __version__():
return "Jenkins Job Builder version: %s" % \
jenkins_jobs.version.version_info.version_string()
version.version_info.version_string()
class JenkinsJobs(object):
@@ -47,90 +46,27 @@ class JenkinsJobs(object):
various command line parameters.
"""
def __init__(self, args=None):
def __init__(self, args=None, **kwargs):
if args is None:
args = []
parser = self._create_parser()
self._options = parser.parse_args(args)
parser = create_parser()
options = parser.parse_args(args)
if not self._options.command:
self.jjb_config = JJBConfig(arguments=options, **kwargs)
self.jjb_config.do_magical_things()
if not options.command:
parser.error("Must specify a 'command' to be performed")
logger = logging.getLogger()
if (self._options.log_level is not None):
self._options.log_level = getattr(logging,
self._options.log_level.upper(),
logger.getEffectiveLevel())
logger.setLevel(self._options.log_level)
self._config_file_values = cmd.setup_config_settings(self._options)
def _create_parser(self):
parser = argparse.ArgumentParser()
parser.add_argument(
'--conf',
dest='conf',
help='''configuration file''')
parser.add_argument(
'-l',
'--log_level',
dest='log_level',
default='info',
help='''log level (default: %(default)s)''')
parser.add_argument(
'--ignore-cache',
action='store_true',
dest='ignore_cache',
default=False,
help='''ignore the cache and update the jobs anyhow (that will only
flush the specified jobs cache)''')
parser.add_argument(
'--flush-cache',
action='store_true',
dest='flush_cache',
default=False,
help='''flush all the cache entries before updating''')
parser.add_argument(
'--version',
dest='version',
action='version',
version=__version__(),
help='''show version''')
parser.add_argument(
'--allow-empty-variables',
action='store_true',
dest='allow_empty_variables',
default=None,
help='''Don\'t fail if any of the variables inside any string are
not defined, replace with empty string instead.''')
parser.add_argument(
'--user', '-u',
help='''The Jenkins user to use for authentication. This overrides
the user specified in the configuration file.''')
parser.add_argument(
'--password', '-p',
help='''Password or API token to use for authenticating towards
Jenkins. This overrides the password specified in the configuration
file.''')
subparser = parser.add_subparsers(
dest='command',
help='''update, test or delete job''')
extension_manager = extension.ExtensionManager(
namespace='jjb.cli.subcommands',
invoke_on_load=True,
)
def parse_subcommand_args(ext, subparser):
ext.obj.parse_args(subparser)
extension_manager.map(parse_subcommand_args, subparser)
return parser
if (options.log_level is not None):
options.log_level = getattr(logging,
options.log_level.upper(),
logger.getEffectiveLevel())
logger.setLevel(options.log_level)
def execute(self):
jenkins_jobs.cmd.execute(self._options, self._config_file_values)
cmd.execute(self.jjb_config)
def main():