# xpyBuild - eXtensible Python-based Build System
#
# teamcity format logging handler
#
# Copyright (c) 2013 - 2017 Software AG, Darmstadt, Germany and/or its licensors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# $Id: teamcity.py 301527 2017-02-06 15:31:43Z matj $
#

# ##teamcity[progressMessage '(%d/%d) %s']

import logging, re, os

from utils.consoleformatter import registerConsoleFormatter, ConsoleFormatter, publishArtifact

def _publishArtifact(path):
	"""
	@deprecated: Use L{utils.consoleformatter.publishArtifact} instead
	"""
	# legacy implementation for compatibility with xpybuild scripts before 1.12
	publishArtifact('<artifact>', path)

def _teamcityEscape(s):
	# to be on the safe side, remove all non-ascii chars and convert to bit string
	if isinstance(s, unicode):
		s = s.encode('ascii', errors='replace')
	else:
		s = s.decode('ascii', errors='ignore').encode('ascii')
	
	s = s.replace('\r','').strip()
	s = s.replace('|', '||')
	s = s.replace("'", "|'")
	s = s.replace('[', '|[')
	s = s.replace(']', '|]')
	s = s.replace('\n', '|n')
	return s


class TeamcityHandler(ConsoleFormatter):
	"""
	An alternative log handler than adds some teamcity-format output messages.
	"""
	output = None
	def __init__(self, output, buildOptions):
		ConsoleFormatter.__init__(self)
		self.output = output
		
		self.bufferingDisabled = True # useful to let teamcity know as soon as there is an error
		
	def handle(self, record):
		if record.getMessage().startswith("##teamcity"):
			self.output.write("%s\n" % record.getMessage())
		elif record.levelno == logging.ERROR:
			self.output.write("##teamcity[message text='%s' status='ERROR']\n" % _teamcityEscape(record.getMessage()))
		elif record.levelno == logging.CRITICAL and record.getMessage().startswith("***"):
			self.output.write("##teamcity[progressMessage '%s']\n" % _teamcityEscape(record.getMessage()))
		else:
			self.output.write("##teamcity[message text='%s']\n" % _teamcityEscape(record.getMessage()))
		self.output.flush()
	
	def publishArtifact(self, logger, displayName, path):
		# displayName is ignored for teamcity
		logger.critical("##teamcity[publishArtifacts '%s']" % _teamcityEscape(path))

	
registerConsoleFormatter("teamcity", TeamcityHandler)

# xpyBuild - eXtensible Python-based Build System
#
# teamcity format logging handler
#
# Copyright (c) 2013 - 2017 Software AG, Darmstadt, Germany and/or its licensors
#
#   Licensed under the Apache License, Version 2.0 (the "License");
#   you may not use this file except in compliance with the License.
#   You may obtain a copy of the License at
#
#       http://www.apache.org/licenses/LICENSE-2.0
#
#   Unless required by applicable law or agreed to in writing, software
#   distributed under the License is distributed on an "AS IS" BASIS,
#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#   See the License for the specific language governing permissions and
#   limitations under the License.
#
# $Id: teamcity.py 301527 2017-02-06 15:31:43Z matj $
#

# ##teamcity[progressMessage '(%d/%d) %s']

import logging, re, os

from utils.consoleformatter import registerConsoleFormatter, ConsoleFormatter, publishArtifact

def _publishArtifact(path):
	"""
	@deprecated: Use L{utils.consoleformatter.publishArtifact} instead
	"""
	# legacy implementation for compatibility with xpybuild scripts before 1.12
	publishArtifact('<artifact>', path)

def _teamcityEscape(s):
	# to be on the safe side, remove all non-ascii chars and convert to bit string
	if isinstance(s, unicode):
		s = s.encode('ascii', errors='replace')
	else:
		s = s.decode('ascii', errors='ignore').encode('ascii')
	
	s = s.replace('\r','').strip()
	s = s.replace('|', '||')
	s = s.replace("'", "|'")
	s = s.replace('[', '|[')
	s = s.replace(']', '|]')
	s = s.replace('\n', '|n')
	return s


class TeamcityHandler(ConsoleFormatter):
	"""
	An alternative log handler than adds some teamcity-format output messages.
	"""
	output = None
	def __init__(self, output, buildOptions):
		ConsoleFormatter.__init__(self)
		self.output = output
		
		self.bufferingDisabled = True # useful to let teamcity know as soon as there is an error
		
	def handle(self, record):
		if record.getMessage().startswith("##teamcity"):
			self.output.write("%s\n" % record.getMessage())
		elif record.levelno == logging.ERROR:
			self.output.write("##teamcity[message text='%s' status='ERROR']\n" % _teamcityEscape(record.getMessage()))
		elif record.levelno == logging.CRITICAL and record.getMessage().startswith("***"):
			self.output.write("##teamcity[progressMessage '%s']\n" % _teamcityEscape(record.getMessage()))
		else:
			self.output.write("##teamcity[message text='%s']\n" % _teamcityEscape(record.getMessage()))
		self.output.flush()
	
	def publishArtifact(self, logger, displayName, path):
		# displayName is ignored for teamcity
		logger.critical("##teamcity[publishArtifacts '%s']" % _teamcityEscape(path))

	
registerConsoleFormatter("teamcity", TeamcityHandler)

# xpyBuild - eXtensible Python-based Build System # # teamcity format logging handler # # Copyright (c) 2013 - 2017 Software AG, Darmstadt, Germany and/or its licensors # #   Licensed under the Apache License, Version 2.0 (the "License"); #   you may not use this file except in compliance with the License. #   You may obtain a copy of the License at # #       http://www.apache.org/licenses/LICENSE-2.0 # #   Unless required by applicable law or agreed to in writing, software #   distributed under the License is distributed on an "AS IS" BASIS, #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #   See the License for the specific language governing permissions and #   limitations under the License. # # $Id: teamcity.py 301527 2017-02-06 15:31:43Z matj $ # # ##teamcity[progressMessage '(%d/%d) %s'] @deprecated: Use L{utils.consoleformatter.publishArtifact} instead # legacy implementation for compatibility with xpybuild scripts before 1.12 # to be on the safe side, remove all non-ascii chars and convert to bit string An alternative log handler than adds some teamcity-format output messages. # useful to let teamcity know as soon as there is an error #teamcity"): #teamcity[message text='%s' status='ERROR']\n" % _teamcityEscape(record.getMessage())) #teamcity[progressMessage '%s']\n" % _teamcityEscape(record.getMessage())) #teamcity[message text='%s']\n" % _teamcityEscape(record.getMessage())) # displayName is ignored for teamcity #teamcity[publishArtifacts '%s']" % _teamcityEscape(path))