Modularize Existing Project by Using Polyspace Python API
If you have an existing Polyspace® Platform project that you want to modularize, or separate into multiple files, you can use the Polyspace Python® API to convert configurations and tests to external files that are referenced by the project. If you have not set up the Polyspace Python API, follow the steps in Set Up Python API for Polyspace before running this script. For additional information about the Polyspace Python API, see Workflow Automation Using Python API.
In this example, the script converts the active configurations and all graphical tests in the Polyspace Test™ demo project to external configurations and test references.
from polyspace.project import Project
import polyspace
import polyspace.test
import os
from pathlib import Path
import shutil
import tempfile
# Find the demo project
root_path_readonly = (
Path(polyspace.__install_path__)
/ "polyspace"
/ "examples"
/ "pstest"
/ "Getting_Started_Example"
)
project_name = "Demo_C_PS_Test"
# Copy the demo project folder to a writable temporary location
temp_dir_base = tempfile.mkdtemp(prefix="polyspace_example_")
writable_root_path = Path(temp_dir_base) / root_path_readonly.name
shutil.copytree(root_path_readonly, writable_root_path, dirs_exist_ok=True)
project_path = writable_root_path / f"{project_name}.psprjx"
# Ensure output folders exist in the writable location
configs_dir = writable_root_path / "configs"
test_refs_root = writable_root_path / "test_refs"
configs_dir.mkdir(parents=True, exist_ok=True)
test_refs_root.mkdir(parents=True, exist_ok=True)
# Open the project from the writable location
proj = Project(str(project_path))
# Prepare paths for the configuration files
build_config_path = configs_dir / f"{project_name}_buildConfig.pscfg"
static_analysis_config_path = configs_dir / f"{project_name}_staticAnalysisConfig.pscfg"
test_config_path = configs_dir / f"{project_name}_testConfig.pscfg"
# Get owned configuration objects for the active configurations
owned_build_config = proj.ActiveBuildConfiguration
owned_static_config = proj.ActiveStaticAnalysisConfiguration
owned_test_config = proj.ActiveTestConfiguration
# Convert owned configurations to external configuration files referenced by the project
build_config_ref = proj.BuildConfigurationRefs.moveAsRef(
owned_build_config,
str(build_config_path)
)
static_config_ref = proj.StaticAnalysisConfigurationRefs.moveAsRef(
owned_static_config,
str(static_analysis_config_path)
)
test_config_ref = proj.TestConfigurationRefs.moveAsRef(
owned_test_config,
str(test_config_path)
)
# Convert graphical tests to files referenced by the project
for suite in proj.TestSuites:
if not suite.TestCases:
continue
suite_dir = test_refs_root / suite.Name
suite_dir.mkdir(parents=True, exist_ok=True)
for existing_test_case in list(suite.TestCases):
test_case_name = existing_test_case.Name
ref_filename = suite_dir / f"{test_case_name}.pstestd"
suite.TestCaseRefs.moveAsRef(existing_test_case, str(ref_filename), test_case_name)
# Build the project and save a copy
status = polyspace.test.build(proj)
proj.saveCopy(str(writable_root_path / "modular_project.psprjx"))
print("Writable project root:", writable_root_path)
print("External configuration files folder:", configs_dir)
print("Graphical test files root folder:", test_refs_root)
After converting the configurations and graphical tests to files that the project references, decide which files to submit to version control. If you want the .psprjx project file to be under version control, submit it along with your source code and the new .pscfg configuration files and .pstestd graphical test files. Alternatively, you can submit only the source code, configurations, and test files to version control, and
use the Polyspace
Python API to recreate the project on demand from its constituent parts. For more information, see Submit Polyspace Files to Version Control and Create Project Dynamically by Using Polyspace Python API.
When you are done with this example, run this code to remove the temporary folder that contains the example files:
try:
shutil.rmtree(temp_dir_base)
print(f"Temporary folder removed: {temp_dir_base}")
except PermissionError as e:
print(f"Warning: Could not remove temporary folder due to permissions: {e}")
except FileNotFoundError:
print("Temporary folder already removed")
except Exception as e:
print(f"Error removing temporary folder: {e}")See Also
polyspace.project.Project | polyspace.project.BuildConfigurationRef | polyspace.project.StaticAnalysisConfigurationRef | polyspace.project.TestConfigurationRef | polyspace.project.TestCaseRef | polyspace.test.build
Topics
- Set Up Python API for Polyspace
- Create Easily Shareable Projects for Version Control
- Modularize Project by Using External Configurations, Test References, and External Stub Files
- Create Project Dynamically by Using Polyspace Python API
- Submit Polyspace Files to Version Control
- Workflow Automation Using Python API