Skip to content

Commit f3b9e3d

Browse files
authored
Merge pull request doxygen#11780 from artyom-fedosov/improvement/runtests.py
refactor(runtests): simplify regexes and functions, add docstring
2 parents 57005e5 + 3d7698a commit f3b9e3d

File tree

1 file changed

+46
-49
lines changed

1 file changed

+46
-49
lines changed

testing/runtests.py

Lines changed: 46 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -10,61 +10,58 @@
1010
import sys
1111
import subprocess
1212
import shlex
13+
import io
1314

1415
config_reg = re.compile(r'.*\/\/\s*(?P<name>\S+):\s*(?P<value>.*)$')
15-
bkmk_reg = re.compile(r'.*bkmkstart\s+([A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]).*')
16-
hyper_reg = re.compile(r'.*HYPERLINK\s+[\\l]*\s+"([A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z])".*')
17-
pageref_reg = re.compile(r'.*PAGEREF\s+([A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z][A-Z]).*')
16+
bkmk_reg = re.compile(r'.*bkmkstart\s+([A-Z]{10}).*')
17+
hyper_reg = re.compile(r'.*HYPERLINK\s+[\\l]*\s+"([A-Z]{10})".*')
18+
pageref_reg = re.compile(r'.*PAGEREF\s+([A-Z]{10}).*')
1819

1920

2021
def xopen(fname, mode='r', encoding='utf-8'):
21-
'''Unified file opening for Python 2 an Python 3.
22+
'''Unified file opening for Python 2 an Python 3.'''
2223

23-
Python 2 does not have the encoding argument. Python 3 has one.
24-
'''
24+
return io.open(fname, mode=mode, encoding=encoding)
25+
26+
27+
def xpopen(cmd, cmd1="", encoding='utf-8-sig', get_stderr=False):
28+
'''Unified file pipe opening for Python 2 an Python 3.'''
2529

2630
if sys.version_info[0] == 2:
27-
return open(fname, mode=mode) # Python 2 without encoding
28-
else:
29-
return open(fname, mode=mode, encoding=encoding) # Python 3 with encoding
31+
return os.popen(cmd).read() # Python 2 without encoding
32+
33+
proc = subprocess.Popen(shlex.split(cmd1 if get_stderr else cmd),
34+
stdout=subprocess.PIPE,
35+
stderr=subprocess.PIPE,
36+
encoding=encoding) # Python 3 with encoding
37+
return (proc.stderr if get_stderr else proc.stdout).read()
3038

31-
def xpopen(cmd, cmd1="",encoding='utf-8-sig', getStderr=False):
32-
'''Unified file pipe opening for Python 2 an Python 3.
3339

34-
Python 2 does not have the encoding argument. Python 3 has one. and
40+
def clean_header(errmsg):
41+
''' Messages (due to the usage of more) have a content like:
42+
::::::::::::
43+
<file name>
44+
::::::::::::
45+
This function skips such headers and returns only the main content.
3546
'''
3647

37-
if sys.version_info[0] == 2:
38-
return os.popen(cmd).read() # Python 2 without encoding
39-
else:
40-
if (getStderr):
41-
proc = subprocess.Popen(shlex.split(cmd1),stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding=encoding) # Python 3 with encoding
42-
return proc.stderr.read()
43-
else:
44-
proc = subprocess.Popen(shlex.split(cmd),stdout=subprocess.PIPE,stderr=subprocess.PIPE,encoding=encoding) # Python 3 with encoding
45-
return proc.stdout.read()
48+
lines = errmsg.splitlines()
49+
rtn_msg = ""
50+
skip = 0
51+
for line in lines:
52+
if not line:
53+
continue
54+
if skip > 0:
55+
skip -= 1
56+
continue
57+
if line.startswith(":::::::"):
58+
skip = 3
59+
continue
60+
rtn_msg += line
61+
rtn_msg += "\n"
62+
return rtn_msg
63+
4664

47-
def clean_header(errmsg):
48-
# messages (due to the usage of more) have a contents like:
49-
# ::::::::::::
50-
# <file name>
51-
# ::::::::::::
52-
# we want to skip these
53-
msg = errmsg.split('\n')
54-
rtnmsg = ""
55-
cnt = -1
56-
for o in msg:
57-
if (o):
58-
if (cnt == -1):
59-
if o.startswith(":::::::"):
60-
cnt = 3
61-
if (cnt > 0):
62-
cnt-=1
63-
else:
64-
rtnmsg+=o
65-
rtnmsg+="\n"
66-
return rtnmsg
67-
6865
class Tester:
6966
def __init__(self,args,test):
7067
self.args = args
@@ -356,7 +353,7 @@ def perform_test(self,testmgr):
356353
exe_string += ' %s' % (redirx)
357354
exe_string += ' %s more "%s/temp"' % (separ,xmlxsd_output)
358355

359-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
356+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
360357
if xmllint_out:
361358
xmllint_out = re.sub(r'.*validates','',xmllint_out).rstrip('\n')
362359
else:
@@ -381,7 +378,7 @@ def perform_test(self,testmgr):
381378
exe_string += ' %s' % (redirx)
382379
exe_string += ' %s more "%s/temp"' % (separ,xmlxsd_output)
383380

384-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
381+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
385382
if xmllint_out:
386383
xmllint_out = re.sub(r'.*validates','',xmllint_out).rstrip('\n')
387384
else:
@@ -411,7 +408,7 @@ def perform_test(self,testmgr):
411408
exe_string += ' %s' % (redirx)
412409
exe_string += ' %s more "%s/temp"' % (separ,xmlxsd_output)
413410

414-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
411+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
415412
if xmllint_out:
416413
xmllint_out = re.sub(r'.*validates','',xmllint_out).rstrip('\n')
417414
else:
@@ -452,7 +449,7 @@ def perform_test(self,testmgr):
452449
exe_string += ' %s more "%s/temp"' % (separ,docbook_output)
453450

454451
failed_docbook=False
455-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
452+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
456453
xmllint_out = self.cleanup_xmllint_docbook(xmllint_out)
457454
if xmllint_out:
458455
xmllint_out = clean_header(xmllint_out)
@@ -481,7 +478,7 @@ def perform_test(self,testmgr):
481478
exe_string += ' %s' % (redirx)
482479
exe_string += ' %s more "%s/temp"' % (separ,html_output)
483480
failed_html=False
484-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
481+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
485482
xmllint_out = self.cleanup_xmllint(xmllint_out)
486483
if xmllint_out:
487484
xmllint_out = clean_header(xmllint_out)
@@ -496,7 +493,7 @@ def perform_test(self,testmgr):
496493
exe_string1 = exe_string
497494
exe_string += ' %s' % (redirx)
498495
exe_string += ' %s more "%s/temp"' % (separ,html_output)
499-
xmllint_out = xpopen(exe_string,exe_string1,getStderr=True)
496+
xmllint_out = xpopen(exe_string,exe_string1,get_stderr=True)
500497
xmllint_out = self.cleanup_xmllint(xmllint_out)
501498
if xmllint_out:
502499
msg += (xmllint_out,)
@@ -522,7 +519,7 @@ def perform_test(self,testmgr):
522519
exe_string += ' %s' % (redirl)
523520
if outType:
524521
exe_string += ' %s more temp' % (separ)
525-
latex_out = xpopen(exe_string,exe_string1,getStderr=outType)
522+
latex_out = xpopen(exe_string,exe_string1,get_stderr=outType)
526523
os.chdir(cur_directory)
527524
if (outType and latex_out.find("Error")!=-1):
528525
msg += ("PDF generation failed\n For a description of the problem see 'refman.log' in the latex directory of this test",)

0 commit comments

Comments
 (0)