1#!/usr/bin/env python
2# vim:fileencoding=utf-8:noet
3from __future__ import (unicode_literals, division, absolute_import, print_function)
4
5import os
6import socket
7import sys
8import codecs
9import platform
10import re
11
12
13test_root = os.environ['TEST_ROOT']
14test_type = sys.argv[1]
15test_client = sys.argv[2]
16shell = sys.argv[3]
17fname = os.path.join(test_root, '.'.join((shell, test_type, test_client, 'full.log')))
18new_fname = os.path.join(test_root, '.'.join((shell, test_type, test_client, 'log')))
19pid_fname = os.path.join(test_root, '3rd', 'pid')
20
21is_pypy = platform.python_implementation() == 'PyPy'
22
23
24try:
25	with open(pid_fname, 'r') as P:
26		pid = P.read().strip()
27except IOError:
28	pid = None
29hostname = socket.gethostname()
30user = os.environ['USER']
31
32REFS_RE = re.compile(r'^\[\d+ refs\]\n')
33IPYPY_DEANSI_RE = re.compile(r'\033(?:\[(?:\?\d+[lh]|[^a-zA-Z]+[a-ln-zA-Z])|[=>])')
34ZSH_HL_RE = re.compile(r'\033\[\?\d+[hl]')
35
36start_str = 'cd "$TEST_ROOT"/3rd'
37if shell == 'pdb':
38	start_str = 'class Foo(object):'
39
40with codecs.open(fname, 'r', encoding='utf-8') as R:
41	with codecs.open(new_fname, 'w', encoding='utf-8') as W:
42		found_cd = False
43		i = -1
44		for line in (R if shell != 'fish' else R.read().split('\n')):
45			i += 1
46			if not found_cd:
47				found_cd = (start_str in line)
48				continue
49			if 'true is the last line' in line:
50				break
51			line = line.translate({
52				ord('\r'): None
53			})
54			if REFS_RE.match(line):
55				continue
56			line = line.replace(hostname, 'HOSTNAME')
57			line = line.replace(user, 'USER')
58			if pid is not None:
59				line = line.replace(pid, 'PID')
60			if shell == 'zsh':
61				line = line.replace('\033[0m\033[23m\033[24m\033[J', '')
62				line = ZSH_HL_RE.subn('', line)[0]
63			elif shell == 'fish':
64				res = ''
65				try:
66					while line.index('\033[0;'):
67						start = line.index('\033[0;')
68						end = line.index('\033[0m', start)
69						res += line[start:end + 4] + '\n'
70						line = line[end + 4:]
71				except ValueError:
72					pass
73				line = res
74			elif shell == 'tcsh':
75				try:
76					start = line.index('\033[0;')
77					end = line.index(' ', start)
78					line = line[start:end] + '\n'
79				except ValueError:
80					line = ''
81			elif shell == 'mksh':
82				# Output is different in travis: on my machine I see full
83				# command, in travis it is truncated just after `true`.
84				if line.startswith('[1] + Terminated'):
85					line = '[1] + Terminated bash -c ...\n'
86			elif shell == 'dash':
87				# Position of this line is not stable: it may go both before and
88				# after the next line
89				if line.startswith('[1] + Terminated'):
90					continue
91			elif shell == 'ipython' and is_pypy:
92				try:
93					end_idx = line.rindex('\033[0m')
94					try:
95						idx = line[:end_idx].rindex('\033[1;1H')
96					except ValueError:
97						idx = line[:end_idx].rindex('\033[?25h')
98					line = line[idx + len('\033[1;1H'):]
99				except ValueError:
100					pass
101				try:
102					data_end_idx = line.rindex('\033[1;1H')
103					line = line[:data_end_idx] + '\n'
104				except ValueError:
105					pass
106				if line == '\033[1;1H\n':
107					continue
108				was_empty = line == '\n'
109				line = IPYPY_DEANSI_RE.subn('', line)[0]
110				if line == '\n' and not was_empty:
111					line = ''
112			elif shell == 'rc':
113				if line == 'read() failed: Connection reset by peer\n':
114					line = ''
115			elif shell == 'pdb':
116				if is_pypy:
117					if line == '\033[?1h\033=\033[?25l\033[1A\n':
118						line = ''
119					line = IPYPY_DEANSI_RE.subn('', line)[0]
120					if line == '\n':
121						line = ''
122				if line.startswith(('>',)):
123					line = ''
124				elif line == '-> self.quitting = 1\n':
125					line = '-> self.quitting = True\n'
126				elif line == '\n':
127					line = ''
128				if line == '-> self.quitting = True\n':
129					break
130			W.write(line)
131