-
Notifications
You must be signed in to change notification settings - Fork 18
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
Python handling Bash/CMake compilation errors #102
Conversation
fixed: cpp 11 code generation
…s the script goes through. If the script sctops itself, the last line < stderr: end > will not show up. Therefore, Python can check if this message exists in the file. If not, he throws a RunTime error, and indicates which step did not worked (last line in the file)
…all the steps that the run.sh performs with CMake. Two cases: if everything goes well, the error file contains the string <stderr: end> and is removed; if somethng goes wrong, python throws an error, and tell which cmake operation is causing the prolem
Codecov Report
@@ Coverage Diff @@
## master #102 +/- ##
=========================================
- Coverage 82.54% 82.34% -0.2%
=========================================
Files 162 162
Lines 7540 7558 +18
=========================================
Hits 6224 6224
- Misses 1316 1334 +18
Continue to review full report at Codecov.
|
Hi all, import os
import subprocess as sp
project_name = 'dampedosc'
kwsp = {
"cwd": os.path.join(project_name, project_name),
"stderr": sp.PIPE,
"check": True,
"shell": True,
"universal_newlines": True
}
with open(os.path.join(project_name, project_name, "build.log"), 'w') as fid:
kwsp['stdout'] = fid
p = sp.run('cmake . -Bbuild', **kwsp)
p = sp.run('cmake --build build -- -j3', **kwsp)
kwsp['stdout'] = sp.PIPE
p = sp.run('./bin/%s' % project_name, **kwsp)
print(p.stdout) raise an error if an error occurs during the build (cmake returning a non zero error code) or the simulation. In this code, the output (stdout and stderr) of the build are logged in the build.log file while the output of the simulation appears in the python interpreter. Many possibilities are available and they greatly simplifies the workflow with respect to this pull request. |
Hi, This implementation seems to be a more robust and flexible solution, that takes advantage of everything the It will also, in my own opinion, simplify the code inside |
Hi everyone, I took @FabricioS example code, and implemented it in the It needs to be tested on Windows. It seems that the option if sys.platform.startswith('win'):
sp_config["shell"] = True
else:
sp_config["shell"] = False Thoughts
What are your insights on this? Best regards, |
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.
This Pull Request seems to integrate things for the other pull request (#100).
Is this intended ?
See also inline comments about how to explicitly report the build errors to users
|
||
# build simu.cpp | ||
# build simu.cpp |
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.
Be careful with indentation. This two-lines change can be reverted
os.remove('stderr') | ||
# Commands | ||
cmd = { | ||
'Bbuild': '%s . -Bbuild' % self.config['cmake'], |
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.
You can even be more explicit in the keys of the cmd dictionary. For example:
Bbuild -> build_tree
build -> compile_src
# Perform build | ||
with open(os.path.join(sp_config["cwd"], "build.log"), 'w') as fid: | ||
sp_config['stdout'] = fid | ||
p = subprocess.run(cmd['Bbuild'], **sp_config) |
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.
You may wrap these two build steps to catch errors, and add a mention to the log in the build.log file. Something like:
with open(os.path.join(sp_config["cwd"], "build.log"), 'w') as fid:
sp_config['stdout'] = fid
try:
p = subprocess.run(cmd['Bbuild'], **sp_config)
p = subprocess.run(cmd['build'], **sp_config)
except subprocess.CalledProcessError as err:
print('Error while building simulation executable. See %s for details' % fid.name)
print(p.stdout)
print(p.stderr)
raise subprocess.CalledProcessError(err)
Regarding this comment:
Can you point where the difference is explained in the subprocess docs ? I only see a discussion about shell here : https://docs.python.org/3.5/library/subprocess.html#frequently-used-arguments. Some points of interest:
|
Regarding
We may print the build.log for high values of the verbose variables (set in config.py). In relation with this question and the verbose variable, the logging module would also be useful.
If build errors are now catched correctly, I don't see the point of removing old executables. |
Hi there, Regarding old changes from PR #100I've cleaned this PR from any other modifications. The only file concerned now is the About the
|
In fact it is a lot of commits for a final pretty simple change. |
Merge PR #102 Python handling Bash/CMake compilation errors
This PR is a first fix designed to catch compilation errors when using the C++ feature on PyPHS.
Principle
It relies on a temporary file,
stderr
, created in the same folder asrun.sh
.When running a
C++
based simulation, PyPHS execute a shell script that is the interface between Python and the CMake compiler. Every step is logged instderr
. After the compilation step, Python check for the log:if this line doesn't appear in the
stderr
file, python raise a RuntimeError, and the last line of the file, which contains the last step that caused the error.Implementation
Shell script
pyphs/numerics/cpp/simu2cpp.py
set -e
command affects the behavior of the script: if an error is encountered, bash stops the execution of the script.stderr
file is removed (if fi
control structure)echo "stderr: step n - doing stuff" > stderr
stderr: end
at the end of the fileError checking
pyphs/numerics/simulations/simulation.py
stderr: end
is in the listDrawbacks
This implementation is not the best, but it was the one that required the least amount of time. Some ideas:
RuntimeError
the correct exception to raise?stdout
orstderr
created by the execution of bash file. I tried to work with that, but couldn't figure it out.