Skip to content

Commit

Permalink
Fix dependency test.
Browse files Browse the repository at this point in the history
  • Loading branch information
salome-eriksson committed Aug 2, 2023
1 parent bfcb5e8 commit bfc0110
Showing 1 changed file with 64 additions and 41 deletions.
105 changes: 64 additions & 41 deletions misc/tests/test-dependencies.py
Original file line number Diff line number Diff line change
@@ -1,63 +1,86 @@
#! /usr/bin/env python3

import os
import re
import shutil
import subprocess
import sys

DIR = os.path.dirname(os.path.abspath(__file__))
REPO = os.path.dirname(os.path.dirname(DIR))
DOWNWARD_FILES = os.path.join(REPO, "src", "search", "DownwardFiles.cmake")
TEST_BUILD_CONFIGS = os.path.join(REPO, "test_build_configs.py")
BUILD = os.path.join(REPO, "build.py")
BUILDS = os.path.join(REPO, "builds")
paths_to_clean = [TEST_BUILD_CONFIGS]


def clean_up(paths_to_clean):
print("\nCleaning up")
for path in paths_to_clean:
print("Removing {path}".format(**locals()))
if os.path.isfile(path):
os.remove(path)
if os.path.isdir(path):
shutil.rmtree(path)
print("Done cleaning")


with open(DOWNWARD_FILES) as d:
content = d.readlines()

content = [line for line in content if '#' not in line]
content = [line for line in content if 'NAME' in line or 'CORE_LIBRARY' in line or 'DEPENDENCY_ONLY' in line]

libraries_to_be_tested = []
for line in content:
if 'NAME' in line:
libraries_to_be_tested.append(line.replace("NAME", "").strip())
if 'CORE_LIBRARY' in line or 'DEPENDENCY_ONLY' in line:
libraries_to_be_tested.pop()

with open(TEST_BUILD_CONFIGS, "w") as f:
for library in libraries_to_be_tested:
lowercase = library.lower()
line = "{lowercase} = [\"-DCMAKE_BUILD_TYPE=Debug\", \"-DDISABLE_LIBRARIES_BY_DEFAULT=YES\"," \
" \"-DLIBRARY_{library}_ENABLED=True\"]\n".format(**locals())
f.write(line)
paths_to_clean.append(os.path.join(BUILDS, lowercase))
LIBRARY_DEFINITION_FILE = os.path.join(REPO, "src", "search", "DownwardFiles.cmake")
BUILDS = os.path.join(REPO, "builds/test-dependencies")


# Find the closing bracket matching the opening bracket that occurred before start
def find_corresponding_closing_bracket(content, start):
nested_level = 1
for index, character in enumerate(content[start:]):
if character == "(":
nested_level += 1
if character == ")":
nested_level -= 1
if nested_level == 0:
return index+start

# Extract all "create_fast_downward_library(...)" blocks and if the library is
# not a core or depencency library, add its name to the return list.
def get_library_definitions(content):
libraries = []
library_definition_string = "create_fast_downward_library\("
pattern = re.compile(library_definition_string)
for library_match in pattern.finditer(content):
start = library_match.start()+len(library_definition_string)
end = find_corresponding_closing_bracket(content, start)
library_definition = content[start:end]
# we cannot manually en-/disable core and dependency only libraries
if any(s in library_definition for s in ["CORE_LIBRARY", "DEPENDENCY_ONLY"]):
continue
name_match = re.search("NAME (\S+)", library_definition)
assert(name_match)
name = name_match.group(1)
libraries.append(name)
return libraries


# Try to get the number of CPUs
try:
# Number of usable CPUs (Unix only)
NUM_CPUS = len(os.sched_getaffinity(0))
except AttributeError:
# Number of available CPUs as a fall-back (may be None)
NUM_CPUS = os.cpu_count()

# Read in the file where libraries are defined and extract the library names.
with open(LIBRARY_DEFINITION_FILE) as d:
content = d.read()
content = re.sub('#(.*)','', content) #Remove all comments
libraries = get_library_definitions(content)

# Build each library and add it to libraries_failed_test if not successfull.
libraries_failed_test = []
for library in libraries_to_be_tested:
for library in libraries:
build_path = os.path.join(BUILDS, library.lower())
config_args = [
"cmake", "-S", os.path.join(REPO, "src"), "-B", build_path,
"-DCMAKE_BUILD_TYPE=Debug", "-DDISABLE_LIBRARIES_BY_DEFAULT=YES",
"-DLIBRARY_{library}_ENABLED=True\"]\n".format(**locals())
]
build_args = ["cmake", "--build", build_path]
if NUM_CPUS:
build_args += ["-j", f"{NUM_CPUS}"]

try:
subprocess.check_call([BUILD, library.lower()])
subprocess.check_call(config_args)
subprocess.check_call(build_args)
except subprocess.CalledProcessError:
libraries_failed_test.append(library)

# Report the result of the test.
if libraries_failed_test:
print("\nFailure:")
for library in libraries_failed_test:
print("{library} failed dependencies test".format(**locals()))
sys.exit(1)
else:
print("\nAll libraries have passed dependencies test")
clean_up(paths_to_clean)

0 comments on commit bfc0110

Please sign in to comment.