Skip to content

Commit

Permalink
Uberenv: Improved management of external commands outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
adrienbernede committed Mar 4, 2020
1 parent ee690da commit 203c25c
Showing 1 changed file with 18 additions and 10 deletions.
28 changes: 18 additions & 10 deletions scripts/uberenv/uberenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,30 @@
from os.path import join as pjoin


def sexe(cmd,ret_output=False,echo = False):
def sexe(cmd, ret_output=False, print_output=True, echo=False):
""" Helper for executing shell commands. """
if echo:
print("[exe: {}]".format(cmd))
if ret_output:
p = subprocess.Popen(cmd,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT)
res = p.communicate()[0]
res = res.decode('utf8')
return p.returncode,res
stderr=subprocess.STDOUT,
universal_newlines=True)
full_output = ""
if print_output:
while True:
output = p.stdout.readline()
full_output += output
if output == '' and p.poll() is not None:
break
if output:
print(output.strip('\n'))
rc = p.poll()
else:
full_output = p.communicate()[0]
rc = p.returncode
return rc, full_output
else:
return subprocess.call(cmd,shell=True)

Expand Down Expand Up @@ -341,7 +353,7 @@ def find_spack_pkg_path(self,pkg_name):
sys.exit(-1)

def read_spack_full_spec(self,pkg_name,spec):
rv, res = sexe("spack/bin/spack spec " + pkg_name + " " + spec, ret_output=True)
rv, res = sexe("spack/bin/spack spec " + pkg_name + " " + spec, ret_output=True, print_output=False)
for l in res.split("\n"):
if l.startswith(pkg_name) and l.count("@") > 0 and l.count("arch=") > 0:
return l.strip()
Expand Down Expand Up @@ -480,8 +492,6 @@ def install(self):
for line in out.split("\n"):
if line.startswith(error_key):
install_path=line.replace(error_key,"")
else:
print(line)

if install_path and os.path.isdir(install_path):
print("[Warning: {} has already been install in {}]".format(self.pkg_name,install_path))
Expand All @@ -492,8 +502,6 @@ def install(self):
else:
print("[ERROR: not a directory {}".format(install_path))
return res
else:
print(out)

if "spack_activate" in self.project_opts:
print("[activating dependent packages]")
Expand Down

0 comments on commit 203c25c

Please sign in to comment.