Merge pull request #170 from robertbaldyga/convert-ut-to-python3

Convert UT scripts to python3
This commit is contained in:
Michał Mielewczyk 2019-05-27 09:58:26 +02:00 committed by GitHub
commit b1321edf69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 100 additions and 87 deletions

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright(c) 2012-2018 Intel Corporation # Copyright(c) 2012-2018 Intel Corporation
@ -125,8 +125,8 @@ class TestGenerator(object):
self.tested_file_path = path self.tested_file_path = path
return return
print self.get_main_tested_dir() + path print(os.path.join(self.get_main_tested_dir(), path))
print "Given path not exists!" print("Given path not exists!")
exit(1) exit(1)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright(c) 2012-2018 Intel Corporation # Copyright(c) 2012-2018 Intel Corporation
@ -8,9 +8,17 @@
import shutil import shutil
import sys import sys
import re import re
import commands
import os.path import os.path
from collections import defaultdict from collections import defaultdict
import subprocess
def run_command(args):
result = subprocess.run(" ".join(args), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result.stdout = result.stdout.decode("ASCII")
result.stderr = result.stderr.decode("ASCII")
print(result.stderr)
return result
import tests_config import tests_config
# #
@ -97,26 +105,25 @@ class UnitTestsSourcesGenerator(object):
gcc_command = gcc_command_template +\ gcc_command = gcc_command_template +\
path + " > " + preprocessing_dst path + " > " + preprocessing_dst
status, output = commands.getstatusoutput(gcc_command) result = run_command([gcc_command])
if status != 0: if result.returncode != 0:
print "Generating preprocessing for " + self.get_main_tested_dir() + path \ print(f"Generating preprocessing for {self.get_main_tested_dir() + path} failed!")
+ " failed!" print(result.output)
print output run_command(["rm", "-f", preprocessing_dst])
commands.getoutput("rm -f " + preprocessing_dst)
continue continue
self.remove_hashes(preprocessing_dst) self.remove_hashes(preprocessing_dst)
print "Preprocessed file " + path + " saved to " + preprocessing_dst print(f"Preprocessed file {path} saved to {preprocessing_dst}")
def copy_includes(self): def copy_includes(self):
includes_dict = self.get_includes_to_copy_dict() includes_dict = self.get_includes_to_copy_dict()
for dst, src in includes_dict.iteritems(): for dst, src in includes_dict.items():
src_path = os.path.normpath(self.get_main_tested_dir() + src) src_path = os.path.normpath(self.get_main_tested_dir() + src)
if not os.path.isdir(src_path): if not os.path.isdir(src_path):
print "Directory " + src_path + " given to include does not exists!" print(f"Directory {src_path} given to include does not exists!")
continue continue
dst_path = os.path.normpath(self.get_main_UT_dir() + dst) dst_path = os.path.normpath(self.get_main_UT_dir() + dst)
@ -131,7 +138,7 @@ class UnitTestsSourcesGenerator(object):
preprocessed_tested_path = self.get_preprocessing_repo() + path preprocessed_tested_path = self.get_preprocessing_repo() + path
if not os.path.isfile(preprocessed_tested_path): if not os.path.isfile(preprocessed_tested_path):
print "No preprocessed path for " + test_path + " test file." print(f"No preprocessed path for {test_path} test file.")
continue continue
tested_src = self.get_src_to_test(test_path, preprocessed_tested_path) tested_src = self.get_src_to_test(test_path, preprocessed_tested_path)
@ -140,8 +147,7 @@ class UnitTestsSourcesGenerator(object):
with open(self.get_sources_to_test_repo() + test_path, "w") as f: with open(self.get_sources_to_test_repo() + test_path, "w") as f:
f.writelines(tested_src) f.writelines(tested_src)
print "Sources for " + test_path + " saved in " +\ print(f"Sources for {test_path} saved in {self.get_sources_to_test_repo() + test_path}")
self.get_sources_to_test_repo() + test_path
def create_main_cmake_lists(self): def create_main_cmake_lists(self):
buf = "cmake_minimum_required(VERSION 2.6.0)\n\n" buf = "cmake_minimum_required(VERSION 2.6.0)\n\n"
@ -173,7 +179,7 @@ class UnitTestsSourcesGenerator(object):
with open(self.get_main_UT_dir() + "CMakeLists.txt", "w") as f: with open(self.get_main_UT_dir() + "CMakeLists.txt", "w") as f:
f.writelines(buf) f.writelines(buf)
print "Main CMakeLists.txt generated written to " + self.get_main_UT_dir() + "CMakeLists.txt" print(f"Main CMakeLists.txt generated written to {self.get_main_UT_dir()} CMakeLists.txt")
def generate_cmakes_for_tests(self): def generate_cmakes_for_tests(self):
test_files_paths = self.get_files_with_tests_list() test_files_paths = self.get_files_with_tests_list()
@ -181,7 +187,7 @@ class UnitTestsSourcesGenerator(object):
for test_path in test_files_paths: for test_path in test_files_paths:
tested_file_path = self.get_sources_to_test_repo() + test_path tested_file_path = self.get_sources_to_test_repo() + test_path
if not os.path.isfile(tested_file_path): if not os.path.isfile(tested_file_path):
print "No source to test for " + test_path + " test" print(f"No source to test for {test_path} test")
continue continue
test_file_path = self.get_main_UT_dir() + test_path test_file_path = self.get_main_UT_dir() + test_path
@ -192,7 +198,7 @@ class UnitTestsSourcesGenerator(object):
cmake_path = os.path.splitext(cmake_path)[0] + ".cmake" cmake_path = os.path.splitext(cmake_path)[0] + ".cmake"
with open(cmake_path, "w") as f: with open(cmake_path, "w") as f:
f.writelines(cmake_buf) f.writelines(cmake_buf)
print "cmake file for " + test_path + " written to " + cmake_path print(f"cmake file for {test_path} written to {cmake_path}")
cmake_lists_path = os.path.dirname(cmake_path) + os.sep cmake_lists_path = os.path.dirname(cmake_path) + os.sep
self.update_cmakelists(cmake_lists_path, cmake_path) self.update_cmakelists(cmake_lists_path, cmake_path)
@ -269,11 +275,11 @@ class UnitTestsSourcesGenerator(object):
# find all functions' definitions | put tabs instead of spaces | # find all functions' definitions | put tabs instead of spaces |
# take only columns with function name and line number | sort in descending order # take only columns with function name and line number | sort in descending order
status, output = commands.getstatusoutput(ctags_path + "-x --c-types=f " + file_path + " --language-force=c | \ result = run_command([ctags_path, "-x", "--c-types=f", file_path,
sed \"s/ \\+/\t/g\" | cut -f 1,3 | sort -nsr -k 2") "--language-force=c | sed \"s/ \\+/\t/g\" | cut -f 1,3 | sort -nsr -k 2"])
# 'output' is string, but it has to be changed to list # 'output' is string, but it has to be changed to list
output = output.split("\n") output = list(filter(None, result.stdout.split("\n")))
return output return output
def remove_functions_from_list(self, functions_list, to_remove_list): def remove_functions_from_list(self, functions_list, to_remove_list):
@ -332,14 +338,14 @@ class UnitTestsSourcesGenerator(object):
function_name = self.get_tested_function_name(path) function_name = self.get_tested_function_name(path)
if file_path is None: if file_path is None:
print path + " file has no tested_file tag!" print(f"{path} file has no tested_file tag!")
return None return None
elif not os.path.isfile(self.get_main_tested_dir() + file_path): elif not os.path.isfile(self.get_main_tested_dir() + file_path):
print "Tested file given in " + path + " not exist!" print(f"Tested file given in {path} does not exist!")
return None return None
if function_name is None: if function_name is None:
print path + " file has no tested_function_name tag!" print(f"{path} file has no tested_function_name tag!")
return None return None
return True return True
@ -472,23 +478,23 @@ class UnitTestsSourcesGenerator(object):
del code_lines_list[line_id + 1: last_line_id + 1] del code_lines_list[line_id + 1: last_line_id + 1]
def set_ctags_path(self): def set_ctags_path(self):
status, output = commands.getstatusoutput("/usr/bin/ctags --version &> /dev/null") result = run_command(["/usr/bin/ctags --version &> /dev/null"])
if status == 0: if result.returncode == 0:
path = "/usr/bin/ctags " path = "/usr/bin/ctags "
status, output = commands.getstatusoutput(path + "--c-types=f") result = run_command([path, "--c-types=f"])
if not re.search("unrecognized option", output, re.IGNORECASE): if not re.search("unrecognized option", result.stdout, re.IGNORECASE):
self.ctags_path = path self.ctags_path = path
return return
status, output = commands.getstatusoutput("/usr/local/bin/ctags --version &> /dev/null") result = run_command(["/usr/local/bin/ctags --version &> /dev/null"])
if status == 0: if result.returncode == 0:
path = "/usr/local/bin/ctags " path = "/usr/local/bin/ctags "
status, output = commands.getstatusoutput(path + "--c-types=f") result = run_command(["path", "--c-types=f"])
if not re.search("unrecognized option", output, re.IGNORECASE): if not re.search("unrecognized option", result.stdout, re.IGNORECASE):
self.ctags_path = path self.ctags_path = path
return return
print "ERROR: Current ctags version don't support \"--c-types=f\" parameter!" print("ERROR: Current ctags version don't support \"--c-types=f\" parameter!")
exit(1) exit(1)
def get_ctags_path(self): def get_ctags_path(self):
@ -556,7 +562,7 @@ class UnitTestsSourcesGenerator(object):
main_UT_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\ main_UT_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\
+ os.sep + tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS)) + os.sep + tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS))
if not os.path.isdir(main_UT_dir): if not os.path.isdir(main_UT_dir):
print "Given path to main UT directory is wrong!" print("Given path to main UT directory is wrong!")
sys.exit(1) sys.exit(1)
self.main_UT_dir = main_UT_dir self.main_UT_dir = main_UT_dir
@ -565,7 +571,7 @@ class UnitTestsSourcesGenerator(object):
main_tested_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\ main_tested_dir = os.path.normpath(os.path.normpath(self.get_script_dir_path()\
+ os.sep + tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT)) + os.sep + tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT))
if not os.path.isdir(main_tested_dir): if not os.path.isdir(main_tested_dir):
print "Given path to main tested directory is wrong!" print("Given path to main tested directory is wrong!")
sys.exit(1) sys.exit(1)
self.main_tested_dir = main_tested_dir self.main_tested_dir = main_tested_dir
@ -579,7 +585,7 @@ def __main__():
generator.create_main_cmake_lists() generator.create_main_cmake_lists()
generator.generate_cmakes_for_tests() generator.generate_cmakes_for_tests()
print "Files for testing generated!" print("Files for testing generated!")
if __name__ == "__main__": if __name__ == "__main__":
__main__() __main__()

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright(c) 2012-2018 Intel Corporation # Copyright(c) 2012-2018 Intel Corporation
@ -7,68 +7,75 @@
import tests_config import tests_config
import os import os
import commands
import sys import sys
import subprocess
def run_command(args):
result = subprocess.run(" ".join(args), shell=True,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
result.stdout = result.stdout.decode("ASCII")
result.stderr = result.stderr.decode("ASCII")
return result
script_path = os.path.dirname(os.path.realpath(__file__)) script_path = os.path.dirname(os.path.realpath(__file__))
main_UT_dir = os.path.normpath(script_path + os.sep\ main_UT_dir = os.path.join(script_path, tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS)
+ tests_config.MAIN_DIRECTORY_OF_UNIT_TESTS) + os.sep
main_tested_dir = os.path.normpath(script_path + os.sep\ main_tested_dir = os.path.join(script_path, tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT)
+ tests_config.MAIN_DIRECTORY_OF_TESTED_PROJECT) + os.sep
if not os.path.isdir(os.path.join(main_UT_dir, "ocf_env", "ocf")):
if not os.path.isdir(main_UT_dir + "ocf_env" + os.sep + "ocf"):
try: try:
os.makedirs(main_UT_dir + "ocf_env" + os.sep + "ocf") os.makedirs(os.path.join(main_UT_dir, "ocf_env", "ocf"))
except Exception: except Exception:
print "Cannot create ocf_env/ocf directory!" raise Exception("Cannot create ocf_env/ocf directory!")
status, output = commands.getstatusoutput("cp " + main_tested_dir +\ result = run_command([ "cp", "-r",
"inc" + os.sep + "*" + " " + main_UT_dir + "ocf_env" + os.sep + "ocf") os.path.join(main_tested_dir, "inc", "*"),
os.path.join(main_UT_dir, "ocf_env", "ocf") ])
if result.returncode != 0:
raise Exception("Preparing sources for testing failed!")
result = run_command([ os.path.join(script_path, "prepare_sources_for_testing.py") ])
if result.returncode != 0:
raise Exception("Preparing sources for testing failed!")
if os.system(script_path + os.sep + "prepare_sources_for_testing.py") != 0: build_dir = os.path.join(main_UT_dir, "build")
print "Preparing sources for testing failed!" logs_dir = os.path.join(main_UT_dir, "logs")
exit()
try:
build_dir = main_UT_dir + "build" + os.sep if not os.path.isdir(build_dir):
logs_dir = main_UT_dir + "logs" + os.sep
if not os.path.isdir(build_dir):
try:
os.makedirs(build_dir) os.makedirs(build_dir)
except Exception: if not os.path.isdir(logs_dir):
print "Cannot create build directory!"
if not os.path.isdir(logs_dir):
try:
os.makedirs(logs_dir) os.makedirs(logs_dir)
except Exception: except Exception:
print "Cannot create logs directory!" raise Exception("Cannot create logs directory!")
cmake_status, cmake_output = commands.getstatusoutput("cd " + build_dir + " && cmake ..") os.chdir(build_dir)
print cmake_output
with open(logs_dir + 'cmake.output', 'w') as f:
f.write(cmake_output)
if cmake_status != 0: cmake_result = run_command([ "cmake", ".." ])
with open(logs_dir + 'tests.output', 'w') as f:
print(cmake_result.stdout)
with open(os.path.join(logs_dir, "cmake.output"), "w") as f:
f.write(cmake_result.stdout)
if cmake_result.returncode != 0:
with open(os.path.join(logs_dir, "tests.output"), "w") as f:
f.write("Cmake step failed! More details in cmake.output.") f.write("Cmake step failed! More details in cmake.output.")
sys.exit(1) sys.exit(1)
make_status, make_output = commands.getstatusoutput("cd " + build_dir + " && make") make_result = run_command([ "make", "-j" ])
print make_output
with open(logs_dir + 'make.output', 'w') as f:
f.write(make_output)
if make_status != 0: print(make_result.stdout)
with open(logs_dir + 'tests.output', 'w') as f: with open(os.path.join(logs_dir, "make.output"), "w") as f:
f.write(make_result.stdout)
if make_result.returncode != 0:
with open(os.path.join(logs_dir, "tests.output"), "w") as f:
f.write("Make step failed! More details in make.output.") f.write("Make step failed! More details in make.output.")
sys.exit(1) sys.exit(1)
test_status, test_output = commands.getstatusoutput("cd " + build_dir + " && make test") test_result = run_command([ "make", "test" ])
print test_output
with open(logs_dir + 'tests.output', 'w') as f: print(test_result.stdout)
f.write(test_output) with open(os.path.join(logs_dir , "tests.output"), "w") as f:
f.write(test_result.stdout)

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright(c) 2012-2018 Intel Corporation # Copyright(c) 2012-2018 Intel Corporation

View File

@ -1,4 +1,4 @@
#!/usr/bin/env python2 #!/usr/bin/env python3
# #
# Copyright(c) 2012-2018 Intel Corporation # Copyright(c) 2012-2018 Intel Corporation
@ -11,7 +11,7 @@ import os
args = ' '.join(sys.argv[1:]) args = ' '.join(sys.argv[1:])
script_path = os.path.dirname(os.path.realpath(__file__)) script_path = os.path.dirname(os.path.realpath(__file__))
framework_script_path = script_path + os.sep + "../framework/add_new_test_file.py" framework_script_path = os.path.join(script_path, "../framework/add_new_test_file.py")
framework_script_path = os.path.normpath(framework_script_path) framework_script_path = os.path.normpath(framework_script_path)
status, output = commands.getstatusoutput(framework_script_path + " " + args) status, output = commands.getstatusoutput(framework_script_path + " " + args)