<commit_before>#!/usr/bin/env python3

import sys
import re
import subprocess
import getopt


class Options:
	''' test_report -o report.xml runner'''
	def __init__(self, args):
		self.args = args

	def runner(self):
		_, args = getopt.getopt(self.args, "o:")
		return args[0]

	def outfile(self):
		opts, _ = getopt.getopt(self.args, "o:")
		return next((a for o, a in opts if o == '-o'), 'test_report.xml')


class Main:
	def __init__(self, args):
		self.options = Options(args)
		self.returncode = 0

	def isTest(self, l):
		return re.match('^.* \(\d+us\): (SUCCESS|FAIL)$', l.decode('utf8'))

	def split(self, l):
		r = re.match('^(.*) \((\d+)us\): (SUCCESS|FAIL)$', l.decode('utf8'))
		return (r.group(1), int(r.group(2)), r.group(3))

	def log(self):
		try:
			out = subprocess.check_output([self.options.runner(), '-t'])
		except subprocess.CalledProcessError as e:
			self.returncode = e.returncode
			out = e.output
		return [self.split(l) for l in out.split(b'\n') if self.isTest(l)]

	# @todo #63 test_result hide detailes of test failure.
	#  This may be a problem if build machine have specific environment.
	#  Shippable should display failure details in log
	def text(self, log):
		return '\n'.join(sorted(('%s (%dus): %s' % l for l in log)))

	# @todo #63 Failures in xml file without details
	#  Details should be stored in <system-out/> tag
	def xml(self, log):
		result = '\n'.join((
			'<testcase name="%s" time="%f">%s%</testcase>' % (
				n.translate(str.maketrans("<>", "{}")),
				t / 1000000,
				"" if s == "SUCCESS" else "<failure/>"
			) for n, t, s in log
		))
		return '\n'.join((
			'<?xml version="1.0" encoding="UTF-8"?>',
			'<testsuites name="all">',
				'<testsuite name="goost">',
					result,
				'</testsuite>',
			'</testsuites>'
		))

	def run(self):
		log = self.log()
		open(self.options.outfile(), 'w').write(self.xml(log))
		print(self.text(log))
		return self.returncode


sys.exit(
	Main(sys.argv[1:]).run()
)
<commit_msg>#63: Fix formatting<commit_after>#!/usr/bin/env python3

import sys
import re
import subprocess
import getopt


class Options:
	''' test_report -o report.xml runner'''
	def __init__(self, args):
		self.args = args

	def runner(self):
		_, args = getopt.getopt(self.args, "o:")
		return args[0]

	def outfile(self):
		opts, _ = getopt.getopt(self.args, "o:")
		return next((a for o, a in opts if o == '-o'), 'test_report.xml')


class Main:
	def __init__(self, args):
		self.options = Options(args)
		self.returncode = 0

	def isTest(self, l):
		return re.match('^.* \(\d+us\): (SUCCESS|FAIL)$', l.decode('utf8'))

	def split(self, l):
		r = re.match('^(.*) \((\d+)us\): (SUCCESS|FAIL)$', l.decode('utf8'))
		return (r.group(1), int(r.group(2)), r.group(3))

	def log(self):
		try:
			out = subprocess.check_output([self.options.runner(), '-t'])
		except subprocess.CalledProcessError as e:
			self.returncode = e.returncode
			out = e.output
		return [self.split(l) for l in out.split(b'\n') if self.isTest(l)]

	# @todo #63 test_result hide detailes of test failure.
	#  This may be a problem if build machine have specific environment.
	#  Shippable should display failure details in log
	def text(self, log):
		return '\n'.join(sorted(('%s (%dus): %s' % l for l in log)))

	# @todo #63 Failures in xml file without details
	#  Details should be stored in <system-out/> tag
	def xml(self, log):
		result = '\n'.join((
			'<testcase name="%s" time="%f">%s</testcase>' % (
				n.translate(str.maketrans("<>", "{}")),
				t / 1000000,
				"" if s == "SUCCESS" else "<failure/>"
			) for n, t, s in log
		))
		return '\n'.join((
			'<?xml version="1.0" encoding="UTF-8"?>',
			'<testsuites name="all">',
				'<testsuite name="goost">',
					result,
				'</testsuite>',
			'</testsuites>'
		))

	def run(self):
		log = self.log()
		open(self.options.outfile(), 'w').write(self.xml(log))
		print(self.text(log))
		return self.returncode


sys.exit(
	Main(sys.argv[1:]).run()
)

{'fraction_non_alphanumeric': 0.19614311088556205, 'fraction_numerical': 0.012179649835067242, 'mean_word_length': 3.654073199527745, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 2, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 12}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5850706', 'n_tokens_mistral': 1642, 'n_tokens_neox': 1477, 'n_words': 388}