Files
IfcOpenShell/src/ifcbimtester/bimtester/run.py
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

344 lines
9.9 KiB
Python
Raw Normal View History

2020-11-23 09:34:13 +01:00
import behave.formatter.pretty # Needed for pyinstaller to package it
import os
import shutil
2020-11-25 07:28:10 +01:00
import sys
2020-11-23 09:34:13 +01:00
import tempfile
2020-11-25 07:28:10 +01:00
import webbrowser
2020-11-23 09:34:13 +01:00
# TODO: if the ifc file name or path contains special character
# like German Umlaute behave gives an error
2020-11-23 09:34:13 +01:00
# get bimtester source code module path
bimtester_path = os.path.dirname(os.path.realpath(__file__))
# print(bimtester_path)
locale_path = os.path.join(bimtester_path, "locale")
2020-11-23 09:34:13 +01:00
try:
# PyInstaller creates a temp folder and stores path in _MEIPASS
base_path = sys._MEIPASS
except Exception:
base_path = os.path.dirname(os.path.realpath(__file__))
def get_resource_path(relative_path):
return os.path.join(base_path, relative_path)
def run_tests(args):
print("# Run tests.")
report_file = os.path.join("report", "report.json")
if "copyintemprun" in args and args["copyintemprun"] is True:
print("copyintemprun")
is_copyintemprun = True
args, copy_base_path = copy_intmp_tests(args)
features_path = os.path.join(copy_base_path, "features")
report_file = os.path.join(copy_base_path, report_file)
else:
print("No copyintemprun")
is_copyintemprun = False
if not get_features(args):
print("No features could be found to check.")
return False
features_path = get_resource_path("features")
2021-01-14 16:06:26 +01:00
# get behave args
behave_args = get_behave_args(args, features_path, report_file)
2021-01-14 16:06:26 +01:00
# run tests
if behave_args != []:
run_behave(behave_args)
else:
print("Error, not able to run behave because of empty behave args.")
return False
if is_copyintemprun is True:
return report_file
else:
return True
2021-01-14 16:06:26 +01:00
def get_behave_args(args, features_path, report_file):
if os.path.isdir(features_path):
behave_args = [features_path]
else:
return []
if os.path.isdir(locale_path):
behave_args.extend([
# path for translation files
# next two lines are one arg
"--define",
"localedir={}".format(locale_path)
])
else:
print(
"Error, translation locals path '{}' does not exist."
.format(locale_path)
)
if args["advanced_arguments"]:
behave_args.extend(args["advanced_arguments"].split())
if args["ifcfile"]:
behave_args.extend([
# next two lines are one arg
"--define",
"ifcfile={}".format(args["ifcfile"])
])
if args["path"]:
behave_args.extend([
# next two lines are one arg
"--define",
"path={}".format(args["path"])
])
if not args["console"]:
behave_args.extend([
# redirect prints in step methods
# if step fails some output is catched, thus might not be printed
# https://github.com/behave/behave/issues/346
"--no-capture",
# next two lines are one arg
"--format",
"json.pretty",
# report file, if relative, than relative to current shell path
# next two lines are one arg
"--outfile",
report_file,
])
return behave_args
2021-01-14 16:06:26 +01:00
def run_behave(behave_args):
from json import dumps
print(dumps(behave_args, indent=4))
from behave.__main__ import main as behave_main
2020-11-23 09:34:13 +01:00
behave_main(behave_args)
print("# All tests are finished.")
2021-01-14 16:06:26 +01:00
2020-11-23 09:34:13 +01:00
return True
def get_features(args):
# current_path = os.path.abspath(".")
features_dir = get_resource_path("features")
for f in os.listdir(features_dir):
if f.endswith(".feature"):
os.remove(os.path.join(features_dir, f))
if args["feature"]:
2020-12-18 07:06:14 +01:00
shutil.copyfile(
args["feature"],
os.path.join(
get_resource_path("features"),
os.path.basename(args["feature"])
)
)
2020-11-23 09:34:13 +01:00
return True
if os.path.exists("features"):
shutil.copytree("features", get_resource_path("features"))
return True
has_features = False
for f in os.listdir("."):
if not f.endswith(".feature"):
continue
if args["feature"] and args["feature"] != f:
continue
has_features = True
2020-12-18 07:06:14 +01:00
shutil.copyfile(
f,
os.path.join(get_resource_path("features"), os.path.basename(f))
)
2020-11-23 09:34:13 +01:00
return has_features
"""
# clean logs to be able to run tests
# once again but on another building model and in another directory
# somehow does not work, thus test will be run in the same directory
# on each new run, directory will be deleted before each new run
# https://github.com/behave/behave/issues/871
# run bimtester
2020-11-25 07:28:10 +01:00
# copy manually this code, run bimtester again,
# does not work on two directories
2020-11-23 09:34:13 +01:00
from behave.runner_util import reset_runtime
reset_runtime()
"""
def copy_intmp_tests(args={}):
2020-11-23 09:34:13 +01:00
print("# Copy features and steps to temp.")
2020-11-23 09:34:13 +01:00
from behave import __version__ as behave_version
# https://github.com/behave/behave/issues/871
if behave_version == "1.2.5":
2020-12-18 07:06:14 +01:00
print(
"At least behave version 1.2.6 is needed, but version {} found."
.format(behave_version)
)
2020-11-23 09:34:13 +01:00
return False
# print(args)
# get the features_path, the dir where the feature files to test are in
if ("featuresdir" in args and args["featuresdir"] != ""):
is_features = True
the_features_path = os.path.join(args["featuresdir"], "features")
if not os.path.isdir(the_features_path):
2020-12-18 07:06:14 +01:00
print(
"Error, the features directory '{}' does not exist."
.format(the_features_path)
2020-12-18 07:06:14 +01:00
)
return False
else:
is_features = False
2020-11-23 09:34:13 +01:00
# get ifc path and ifc filename
if ("ifcfile" in args and args["ifcfile"] != ""):
is_ifcfile = True
ifcfile = args["ifcfile"]
if os.path.isfile(ifcfile) is not True:
print("Error, the ifc file '{}' does not exist.".format(ifcfile))
return False
ifc_path = os.path.dirname(os.path.realpath(ifcfile))
if os.path.isdir(ifc_path) is False:
print("ifc path does not exist.")
return False
else:
is_ifcfile = False
# print(is_features)
# print(is_ifcfile)
if is_features is True and is_ifcfile is True:
print("features given, ifcfile given.")
elif is_features is False and is_ifcfile is True:
print("features given, ifcfile NOT given.")
# features = ifcfile directory
the_features_path = os.path.join(ifc_path, "features")
elif is_features is True and is_ifcfile is False:
print("features given, ifcfile NOT given.")
# the ifcfile provided in the feature files is used
# TODO What will be passed as ifcfile arg?
print("Not yet implemented.")
return False
elif is_features is False and is_ifcfile is False:
print("features NOT given, ifcfile NOT given.")
# the current directory = features
# the ifcfile provided in the feature files is used
# TODO What will be passed as ifcfile arg?
print("Not yet implemented.")
return False
2020-11-23 09:34:13 +01:00
else:
print("Error: this should never happen, please debug.")
return False
2020-11-23 09:34:13 +01:00
# set up paths
# a unique temp path should not be used
# behave raises an ambiguous step exception
2021-01-12 13:44:11 +01:00
# copy_base_path = tempfile.mkdtemp()
2020-11-23 09:34:13 +01:00
# thus use the same path on every run
# but delete it if exists
2021-01-12 13:44:11 +01:00
copy_base_path = os.path.join(tempfile.gettempdir(), "bimtesterfc")
if os.path.isdir(copy_base_path):
2020-11-23 09:34:13 +01:00
from shutil import rmtree
2021-01-12 13:44:11 +01:00
rmtree(copy_base_path) # fails on read only files
if os.path.isdir(copy_base_path):
print(
"Delete former beimtester run dir '{}' failed"
.format(copy_base_path)
)
2020-11-23 09:34:13 +01:00
return False
2021-01-12 13:44:11 +01:00
os.mkdir(copy_base_path)
copy_features_path = os.path.join(copy_base_path, "features")
# copy features path from bimtester source code
srccode_features_path = os.path.join(
bimtester_path,
"features",
)
# print(srccode_features_path)
if os.path.exists(srccode_features_path):
shutil.copytree(srccode_features_path, copy_features_path)
else:
print(
"Bimtester source code features directory {} not found."
.format(srccode_features_path)
)
return False
2020-11-23 09:34:13 +01:00
# copy features files
# print(the_features_path)
2020-11-23 09:34:13 +01:00
# print(copy_features_path)
# parameter dirs_exist_ok=True, from py 3.8
# if os.path.exists(the_features_path):
# shutil.copytree(
# the_features_path,
# copy_features_path,
# dirs_exist_ok=True
# )
# copy feature files
feature_files = os.listdir(the_features_path)
2020-11-23 09:34:13 +01:00
# print(feature_files)
for feature_file in feature_files:
cp_feature_file = os.path.join(copy_features_path, feature_file)
2020-11-23 09:34:13 +01:00
# print(feature_file)
# copy file
shutil.copyfile(
os.path.join(the_features_path, feature_file),
cp_feature_file
)
2020-11-23 09:34:13 +01:00
return args, copy_base_path
2020-11-23 09:34:13 +01:00
def run_all(args):
2020-11-23 09:34:13 +01:00
print("# Run all.")
2020-11-23 09:34:13 +01:00
# run bimtester
report_file = run_tests(args)
print(report_file)
# check if it worked out well
if report_file is False:
print("BIMTester behave tests returned False.")
return False
if not os.path.isfile(report_file):
print("Report directory does not exist. This should not happen. Debug")
return False
2020-11-23 09:34:13 +01:00
# create html report and open in webbrowser
2020-11-23 09:34:13 +01:00
from .reports import generate_report
generate_report(report_file=report_file)
# get the feature files
2020-12-18 07:06:14 +01:00
feature_files = os.listdir(
os.path.join(args["featuresdir"], "features")
2020-12-18 07:06:14 +01:00
)
# print(feature_files)
2020-11-23 09:34:13 +01:00
for ff in feature_files:
2020-12-18 07:06:14 +01:00
webbrowser.open(os.path.join(
os.path.dirname(report_file),
2020-12-18 07:06:14 +01:00
ff + ".html"
))
2020-11-23 09:34:13 +01:00
return True