preCICE v3.1.1
Loading...
Searching...
No Matches
compileAndTest.py
Go to the documentation of this file.
1#!/usr/bin/env python3
2"""
3Compiles and tests preCICE. Can be used with git bisect run or alike.
4Return 0 on success, 1 on failure and 125 on compilation failure which tells git bisect to skip that commit (neither mark it as good or bad)
5
6To ensure a clean test it can delete and recreate the ./build/TestOutput/ directories.
7"""
8import argparse
9import multiprocessing
10import os
11import shutil
12import subprocess
13import sys
14
15parser = argparse.ArgumentParser(
16 formatter_class=argparse.ArgumentDefaultsHelpFormatter,
17 description="All additional arguments are passed to the testprecice binary.",
18)
19
20parser.add_argument(
21 "-s",
22 "--source-dir",
23 help="Source directory of preCICE",
24 dest="source_dir",
25 default=os.getcwd(),
26)
27parser.add_argument(
28 "-b",
29 "--build-dir",
30 help="Build directory of preCICE",
31 dest="build_dir",
32 default=os.path.join(os.getcwd(), "build"),
33)
34
35parser.add_argument(
36 "-c", "--compile", help="Compile preCICE", dest="compile", action="store_true"
37)
38parser.add_argument(
39 "-r",
40 "--removebuild",
41 help="Remove build/ and .scon* files before compiling",
42 dest="remove_build",
43 action="store_true",
44)
45parser.add_argument(
46 "-k",
47 "--keeptest",
48 help="Do not remove test directory for each test run",
49 dest="keep_test",
50 action="store_true",
51)
52parser.add_argument("-t", help="Run tests.", dest="run_tests", action="store_true")
53parser.add_argument(
54 "-j",
55 help="Number of CPUs to compile on",
56 dest="compile_cpus",
57 default=multiprocessing.cpu_count(),
58)
59parser.add_argument("--logconfig", "-l", help="Log config file", default="")
60
61
62def wipedir(dir):
63 try:
64 shutil.rmtree(dir)
65 os.makedirs(dir)
66 except FileNotFoundError:
67 pass
68
69
70if len(sys.argv) < 2:
71 parser.print_help()
72 sys.exit(1)
73
74args, remainder = parser.parse_known_args()
75
76
77if args.compile:
78 if args.remove_build:
79 print("Wiping build directory ...")
80 wipedir(args.build_dir)
81 else:
82 os.makedirs(args.build_dir, exist_ok=True)
83
84 CONFIGURE_CMD = "cmake -DPRECICE_MPICommunication=ON -DPRECICE_PETScMapping=ON -DCMAKE_BUILD_TYPE=Debug {src}".format(
85 src=args.source_dir
86 )
87 if subprocess.call(CONFIGURE_CMD, shell=True, cwd=args.build_dir) != 0:
88 sys.exit(125) # Cannot compile, 125 means to skip that revision
89
90 COMPILE_CMD = "cmake --build {dir} -- -j {cpus}".format(
91 dir=args.build_dir, cpus=args.compile_cpus
92 )
93 if subprocess.call(COMPILE_CMD, shell=True) != 0:
94 sys.exit(125) # Cannot compile, 125 means to skip that revision
95
96if args.run_tests:
97 testbase = os.path.join(args.build_dir, "TestOutput")
98 testdirs = [os.path.join(testbase, dir) for dir in next(os.walk(testbase))[1]]
99 if not args.keep_test:
100 print("Wiping test directories ...")
101 for dir in testdirs:
102 print("Wiping directory: {}".format(dir))
103 wipedir(dir)
104
105 if args.logconfig:
106 print("Copy ", args.logconfig, " to test directories")
107 for dst in testdirs:
108 shutil.copyfile(args.logconfig, os.path.join(dir, "log.conf"))
109
110 print(
111 "Running Tests:",
112 )
113 ret_code = subprocess.call(
114 "ctest -VV --output-on-failure", shell=True, cwd=args.build_dir
115 )
116
117 if not ret_code == 0:
118 sys.exit(1)