# Copyright (c) 2015-2016,2018 Software AG, Darmstadt, Germany and/or Software AG USA Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.
# Use, reproduction, transfer, publication or disclosure is prohibited except as specifically provided for in your License Agreement with Software AG
#
# $Revision: 335594 $
#
""" Contains PySys extensions for running ant builds from your testcases.
"""
import os
from pysys.constants import *
from pysys.utils.pycompat import openfile
from apama.common import _initProject, stringToUnicode
[docs]def antBuild(parent, buildfile, args=None, workingDir='build', timeout=60, environs=None):
"""
Run an ant build file with Apama environment properties set,
typically to generate a project artifact such as a Java plugin or adapter.
Runs in a 'build' subdirectory of the parent's output directory.
Be careful to ensure that the ant build file generates its output
under its working directory, or under an explicitly specified directory
that is located inside the test output directory.
@param parent: a ProcessUser object (such as a BaseTest)
@param buildfile: absolute path to the ant build.xml to run
@param args: an optional list of arguments to pass to ant, such as targets to build or -Dprop=value properties.
@param workingDir: the working directory for ant, which can be relative to the parent's output directory. Will be created if it doesn't exist.
@param timeout: The timeout to run ant
@param environs: a dictionary of environment variables (key, value) for ANT execution.
Throws an exception if the ant build does not complete successfully.
"""
displayName = 'ant'
buildfile = os.path.normpath(buildfile)
if not os.path.isabs(workingDir): workingDir = parent.output+'/'+workingDir
outdir = workingDir
if not os.path.exists(outdir): os.makedirs(outdir)
dstdout, dstderr = parent.allocateUniqueStdOutErr(displayName)
ant = os.path.normpath(os.getenv('ANT_HOME')+'/bin/ant'+('.bat' if PLATFORM=='win32' else ''))
if not os.path.exists(ant): raise Exception('Cannot find ant at: "%s"'%ant)
_initProject(parent.project)
antenvirons = {}
# don't copy parent process env (for safety), but some specific env vars
# should be copied if present
for varname in ['_JAVA_OPTIONS']:
if varname in os.environ:
antenvirons[varname] = stringToUnicode(os.environ[varname])
antenvirons.update({
'APAMA_HOME':parent.project.APAMA_HOME,
'APAMA_WORK':parent.project.APAMA_WORK,
'APAMA_LIBRARY_VERSION':getattr(parent.project, 'APAMA_LIBRARY_VERSION', ''),
'PATH':os.path.dirname(ant)+os.pathsep+os.getenv('PATH',''),
'JAVA_HOME':os.path.normpath(parent.project.APAMA_COMMON_JRE+'/..'),
'ANT_HOME':os.path.dirname(os.path.dirname(ant)), # needed so ant can locate its java classes
})
# Merge in supplied environs, this will overwrite to user can set their own JAVA_HOME for instance.
if environs:
antenvirons.update(environs)
p = parent.startProcess(ant,
['-buildfile', buildfile,
]+(args or []),
environs=antenvirons,
timeout=timeout, stdout=dstdout, stderr=dstderr, displayName='%s %s'%(displayName, buildfile),
workingDir=outdir)
# exit code is a bit unreliable for ant, so grep stdout instead to find out if it worked
openfileargs = {'encoding':parent.getDefaultFileEncoding(dstdout)}
if openfileargs['encoding']: openfileargs['errors'] = 'replace' # don't allow encoding mismatch to stop this succeeding
with openfile(dstdout, **openfileargs) as f:
out = f.read()
if not 'BUILD SUCCESSFUL' in out:
parent.log.error('Ant execution failed for %s: \n%s'%(buildfile, out.strip()))
with open(dstderr) as f:
out = f.read()
if out.strip(): parent.log.error('%s'%out.strip())
raise Exception('Ant execution failed for %s'%buildfile)