test command read/write stdin/stdout by default

E.g.:

    jenkins-jobs test < config/jenkins.yml

This is convenient for demoing or implementing functional tests.

Change-Id: I4e3030e261d3b90f75e4a033ea074d18764d97bf
This commit is contained in:
Marc Abramowitz 2014-04-02 08:30:14 -07:00
parent ad43311203
commit 14ed8b12d0
3 changed files with 32 additions and 11 deletions
doc/source
jenkins_jobs

@ -86,6 +86,11 @@ Once you have a configuration defined, you can test the job builder by running::
which will write XML files to the output directory for all of the jobs
defined in the configuration directory.
If you want to run a simple test with just a single YAML file and see the XML
output on stdout::
jenkins-jobs test /path/to/config
Updating Jenkins
^^^^^^^^^^^^^^^^
When you're satisfied with the generated XML from the test, you can run::

@ -567,8 +567,8 @@ class Builder(object):
for job in jobs:
self.delete_job(job['name'])
def update_job(self, fn, names=None, output_dir=None):
self.load_files(fn)
def update_job(self, input_fn, names=None, output=None):
self.load_files(input_fn)
self.parser.generateXML(names)
self.parser.jobs.sort(lambda a, b: cmp(a.name, b.name))
@ -576,18 +576,24 @@ class Builder(object):
for job in self.parser.jobs:
if names and not matches(job.name, names):
continue
if output_dir:
if names:
print job.output()
if output:
if hasattr(output, 'write'):
# `output` is a file-like object
logger.debug("Writing XML to '{0}'".format(output))
output.write(job.output())
continue
output_dir = output
try:
os.makedirs(output_dir)
except OSError:
if not os.path.isdir(output_dir):
raise
fn = os.path.join(output_dir, job.name)
logger.debug("Writing XML to '{0}'".format(fn))
f = open(fn, 'w')
output_fn = os.path.join(output_dir, job.name)
logger.debug("Writing XML to '{0}'".format(output_fn))
f = open(output_fn, 'w')
f.write(job.output())
f.close()
continue

@ -17,6 +17,7 @@ import argparse
import ConfigParser
import logging
import os
import platform
import sys
@ -39,8 +40,9 @@ def main():
action='store_true',
dest='delete_old', default=False,)
parser_test = subparser.add_parser('test')
parser_test.add_argument('path', help='path to YAML file or directory')
parser_test.add_argument('-o', dest='output_dir', required=True,
parser_test.add_argument('path', help='path to YAML file or directory',
nargs='?', default=sys.stdin)
parser_test.add_argument('-o', dest='output_dir', default=sys.stdout,
help='path to output XML')
parser_test.add_argument('name', help='name(s) of job(s)', nargs='*')
parser_delete = subparser.add_parser('delete')
@ -125,6 +127,14 @@ def main():
ignore_cache=ignore_cache,
flush_cache=options.flush_cache)
if options.path == sys.stdin:
logger.debug("Input file is stdin")
if options.path.isatty():
key = 'CTRL+Z' if platform.system() == 'Windows' else 'CTRL+D'
logger.warn(
"Reading configuration from STDIN. Press %s to end input.",
key)
if options.command == 'delete':
for job in options.name:
logger.info("Deleting jobs in [{0}]".format(job))
@ -142,7 +152,7 @@ def main():
builder.delete_old_managed(keep=[x.name for x in jobs])
elif options.command == 'test':
builder.update_job(options.path, options.name,
output_dir=options.output_dir)
output=options.output_dir)
if __name__ == '__main__':
sys.path.insert(0, '.')