|
| 1 | +############################################################################### |
| 2 | +# Copyright (c) 2015-2019, Lawrence Livermore National Security, LLC. |
| 3 | +# |
| 4 | +# Produced at the Lawrence Livermore National Laboratory |
| 5 | +# |
| 6 | +# LLNL-CODE-716457 |
| 7 | +# |
| 8 | +# All rights reserved. |
| 9 | +# |
| 10 | +# This file is part of Ascent. |
| 11 | +# |
| 12 | +# For details, see: http://ascent.readthedocs.io/. |
| 13 | +# |
| 14 | +# Please also read ascent/LICENSE |
| 15 | +# |
| 16 | +# Redistribution and use in source and binary forms, with or without |
| 17 | +# modification, are permitted provided that the following conditions are met: |
| 18 | +# |
| 19 | +# * Redistributions of source code must retain the above copyright notice, |
| 20 | +# this list of conditions and the disclaimer below. |
| 21 | +# |
| 22 | +# * Redistributions in binary form must reproduce the above copyright notice, |
| 23 | +# this list of conditions and the disclaimer (as noted below) in the |
| 24 | +# documentation and/or other materials provided with the distribution. |
| 25 | +# |
| 26 | +# * Neither the name of the LLNS/LLNL nor the names of its contributors may |
| 27 | +# be used to endorse or promote products derived from this software without |
| 28 | +# specific prior written permission. |
| 29 | +# |
| 30 | +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 31 | +# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 32 | +# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 33 | +# ARE DISCLAIMED. IN NO EVENT SHALL LAWRENCE LIVERMORE NATIONAL SECURITY, |
| 34 | +# LLC, THE U.S. DEPARTMENT OF ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY |
| 35 | +# DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 36 | +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 37 | +# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 38 | +# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 39 | +# STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 40 | +# IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 41 | +# POSSIBILITY OF SUCH DAMAGE. |
| 42 | +# |
| 43 | +############################################################################### |
| 44 | +import os |
| 45 | +import sys |
| 46 | +import subprocess |
| 47 | + |
| 48 | +from os.path import join as pjoin |
| 49 | + |
| 50 | +# if you have bad luck with spack load, this |
| 51 | +# script is for you! |
| 52 | +# |
| 53 | +# Looks for subdir: spack or uberenv_libs/spack |
| 54 | +# queries spack for given package names and |
| 55 | +# creates a bash script that adds those to your path |
| 56 | +# |
| 57 | +# |
| 58 | +# usage: |
| 59 | +# python gen_spack_env_script.py [spack_pkg_1 spack_pkg_2 ...] |
| 60 | +# |
| 61 | + |
| 62 | +def sexe(cmd,ret_output=False,echo = True): |
| 63 | + """ Helper for executing shell commands. """ |
| 64 | + if echo: |
| 65 | + print("[exe: {}]".format(cmd)) |
| 66 | + if ret_output: |
| 67 | + p = subprocess.Popen(cmd, |
| 68 | + shell=True, |
| 69 | + stdout=subprocess.PIPE, |
| 70 | + stderr=subprocess.STDOUT) |
| 71 | + res = p.communicate()[0] |
| 72 | + res = res.decode('utf8') |
| 73 | + return p.returncode,res |
| 74 | + else: |
| 75 | + return subprocess.call(cmd,shell=True) |
| 76 | + |
| 77 | + |
| 78 | +def spack_exe(spath=None): |
| 79 | + if spath is None: |
| 80 | + to_try = [pjoin("uberenv_libs","spack"), "spack"] |
| 81 | + for p in to_try: |
| 82 | + abs_p = os.path.abspath(p) |
| 83 | + print("[looking for spack directory at: {}]".format(abs_p)) |
| 84 | + if os.path.isdir(abs_p): |
| 85 | + print("[FOUND spack directory at: {}]".format(abs_p)) |
| 86 | + return os.path.abspath(pjoin(abs_p,"bin","spack")) |
| 87 | + print("[ERROR: failed to find spack directory!]") |
| 88 | + sys.exit(-1) |
| 89 | + else: |
| 90 | + spack_exe = os.path.abspath(spath,"bin","spack") |
| 91 | + if not os.path.isfile(spack_exec): |
| 92 | + print("[ERROR: failed to find spack directory at spath={}]").format(spath) |
| 93 | + sys.exit(-1) |
| 94 | + return spack_exe |
| 95 | + |
| 96 | +def find_pkg(pkg_name): |
| 97 | + r,rout = sexe(spack_exe() + " find -p " + pkg_name,ret_output = True) |
| 98 | + print(rout) |
| 99 | + for l in rout.split("\n"): |
| 100 | + print(l) |
| 101 | + lstrip = l.strip() |
| 102 | + if not lstrip == "" and \ |
| 103 | + not lstrip.startswith("==>") and \ |
| 104 | + not lstrip.startswith("--"): |
| 105 | + return {"name": pkg_name, "path": l.split()[-1]} |
| 106 | + print("[ERROR: failed to find package named '{}']".format(pkg_name)) |
| 107 | + sys.exit(-1) |
| 108 | + |
| 109 | +def path_cmd(pkg): |
| 110 | + return('export PATH={}:$PATH\n'.format((pjoin(pkg["path"],"bin")))) |
| 111 | + |
| 112 | +def write_env_script(pkgs): |
| 113 | + ofile = open("s_env.sh","w") |
| 114 | + for p in pkgs: |
| 115 | + print("[found {} at {}]".format(p["name"],p["path"])) |
| 116 | + ofile.write("# {}\n".format(p["name"])) |
| 117 | + ofile.write(path_cmd(p)) |
| 118 | + print("[created {}]".format(os.path.abspath("s_env.sh"))) |
| 119 | + |
| 120 | +def main(): |
| 121 | + pkgs = [find_pkg(pkg) for pkg in sys.argv[1:]] |
| 122 | + if len(pkgs) > 0: |
| 123 | + write_env_script(pkgs) |
| 124 | + else: |
| 125 | + print("usage: python gen_spack_env_script.py spack_pkg_1 spack_pkg_2 ...") |
| 126 | + |
| 127 | +if __name__ == "__main__": |
| 128 | + main() |
0 commit comments