Skip to content

Commit

Permalink
Fix psi4 windows version detection (#426)
Browse files Browse the repository at this point in the history
* print

* split version

* collapse

* reset timeout

* fix again

* invert for consistency

* Update docs/source/changelog.rst
  • Loading branch information
loriab committed Aug 18, 2023
1 parent 1a36ef7 commit cbec059
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
8 changes: 8 additions & 0 deletions docs/source/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ Changelog
.. - UNSOLVED (:issue:`397`) extras failed
v0.28.1 / 2023-08-18
--------------------

Bug Fixes
+++++++++
- (:pr:`426`) Psi4 - fix ``get_version`` on Windows where whole path and command were getting passed to version parser. @loriab


v0.28.0 / 2023-08-15
--------------------

Expand Down
21 changes: 12 additions & 9 deletions qcengine/programs/psi4.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,24 +63,24 @@ def found(raise_error: bool = False) -> bool:
if "module does not exist" in exc["stderr"]:
psiapi = True # --module argument only in Psi4 DDD branch (or >=1.6) so grandfathered in
else:
so, se = exc["stdout"], exc["stderr"]
so, se, rc = exc["stdout"].strip(), exc["stderr"], exc["proc"].returncode
error_msg = f" In particular, psi4 command found but unable to load psi4 module into sys.path. stdout: {so}, stderr: {se}"
error_which = which_import
if (so) and (not se) and (exc["proc"].returncode == 0):
psimod = Path(so.rstrip()) # stdout is string & Path is tolerant, so safe op
if (so) and (not se) and (rc == 0):
psimod = Path(so)
if psimod.exists():
sys.path.append(str(psimod))
psiapi = which_import("psi4", return_bool=True)

if psiapi and not psithon:
with popen(["python", "-c", "import psi4; print(psi4.executable)"]) as exc:
exc["proc"].wait(timeout=30)
so, se = exc["stdout"], exc["stderr"]
so, se, rc = exc["stdout"].strip(), exc["stderr"], exc["proc"].returncode
error_msg = f" In particular, psi4 module found but unable to load psi4 command into PATH. stdout: {so}, stderr: {se}"
# yes, everthing up to here could be got from `import psi4; psiexe = psi4.executable`. but, we try not to
# load programs/modules in the `def found` fns.
if (so) and (not se) and (exc["proc"].returncode == 0):
psiexe = Path(so.rstrip()) # stdout is string & Path is tolerant, so safe op
if (so) and (not se) and (rc == 0):
psiexe = Path(so)
if psiexe.exists():
os.environ["PATH"] += os.pathsep + str(psiexe.parent)
psithon = which("psi4", return_bool=True)
Expand All @@ -103,9 +103,12 @@ def get_version(self) -> str:
if which_prog not in self.version_cache:
with popen([which_prog, "--version"]) as exc:
exc["proc"].wait(timeout=30)
if (exc["proc"].returncode != 0) or exc["stderr"]:
raise TypeError(f"Error {exc['proc'].returncode} retrieving Psi4 version: " + exc["stderr"])
self.version_cache[which_prog] = safe_version(exc["stdout"].rstrip())
so, se, rc = exc["stdout"].strip(), exc["stderr"], exc["proc"].returncode
if (so) and (not se) and (rc == 0):
# Windows echos the command, so split stdout to collect response
self.version_cache[which_prog] = safe_version(so.split()[-1])
else:
raise TypeError(f"Error {rc} retrieving Psi4 version: stdout: {so}, stderr: {se}")

candidate_version = self.version_cache[which_prog]

Expand Down

0 comments on commit cbec059

Please sign in to comment.