-
Notifications
You must be signed in to change notification settings - Fork 41
Changes to FmuBuilder.build_FMU. 1. Unload the script module after th… #247
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -189,7 +189,6 @@ def get_model_description(filepath: Path, module_name: str, class_name: str) -> | |
# Produce the xml | ||
return instance.modelName, instance.to_xml() | ||
|
||
|
||
class FmuBuilder: | ||
|
||
@staticmethod | ||
|
@@ -201,13 +200,31 @@ def build_FMU( | |
newargs: dict | None = None, | ||
**options, | ||
) -> Path: | ||
""" Build the FMU from the Python script, additional project files and documentatiion. | ||
|
||
Args: | ||
script_file (FilePath): The main Python script containing the Python model class | ||
dest (FilePath)='.': Optional destination path. | ||
If this is a full file name with '.fmu' extension, it is used as FMU file name. | ||
Otherwise the FMU file name is constructed automatically from the script file name. | ||
project_files (Iterable[FilePath]): Optional list/tuple of additional project files needed to run model | ||
documentation_folder (FilePath): Optional additional documentation (beyond modelDescription) | ||
newargs (dict): Optional dict of replacements of model class __init__() arguments. | ||
""" | ||
script_file = Path(script_file) | ||
if not script_file.exists(): | ||
raise ValueError(f"No such file {script_file!s}") | ||
if not script_file.suffix.endswith(".py"): | ||
raise ValueError(f"File {script_file!s} must have extension '.py'!") | ||
|
||
dest = Path(dest) | ||
if ( dest.suffix == '.fmu' and # explicit FMU file name shall always have suffix '.fmu' | ||
( dest.is_file() or # Note that .is_file() returns False if the file does not yet exist | ||
not dest.is_dir())): # if dest represents an (existing) directory we cannot interpret as file! | ||
dest_file = dest | ||
dest = dest.parent | ||
else: | ||
dest_file = "" # FMU file name is automatically generated below | ||
if not dest.exists(): | ||
dest.mkdir(parents=True) | ||
project_files = set(map(Path, project_files)) | ||
|
@@ -261,13 +278,14 @@ def build_FMU( | |
temp_dest = temp_dir / file_.name | ||
shutil.copytree(file_, temp_dest) | ||
else: | ||
assert file_.name != script_file.name, ( # avoid the inclusion of the script in project files | ||
"It seems that the script file is included a second time in the project_files") | ||
shutil.copy2(file_, temp_dir) | ||
|
||
model_identifier, xml = get_model_description( | ||
temp_dir.absolute() / script_file.name, module_name, model_class.__name__ | ||
) | ||
|
||
dest_file = dest / f"{model_identifier}.fmu" | ||
dest_file = dest / f"{model_identifier}.fmu" if dest_file == "" else dest_file | ||
|
||
type_node = xml.find("CoSimulation") | ||
option_names = [opt.name for opt in FMI2_MODEL_OPTIONS] | ||
|
@@ -317,6 +335,8 @@ def build_FMU( | |
zip_fmu.writestr( | ||
"modelDescription.xml", xml_str.toprettyxml(encoding="UTF-8") | ||
) | ||
if newargs is not None: | ||
sys.modules.pop(Path(script_file).stem) # otherwise old script may be active when loading the FMU! | ||
Comment on lines
+338
to
+339
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. module syntax can be |
||
|
||
return dest_file | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if we want to only check the name of the file for deciding the assertion. The file name be same in different python modules.