Skip to content

Commit

Permalink
#99 fixed bv_env and added bv_unenv
Browse files Browse the repository at this point in the history
  • Loading branch information
sapetnioc committed Jun 30, 2023
1 parent ccd28f8 commit 56c661d
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 8 deletions.
5 changes: 4 additions & 1 deletion bin/bv_env
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ for casa in (
if (casa / "bv_maker.cfg").exists():
# This is a conda-based development environment
modified["BRAINVISA_BVMAKER_CFG"] = f"{casa}/bv_maker.cfg"
modified["CMAKE_LIBRARY_PATH"] = f"{os.environ['CONDA_PREFIX']}/lib:{os.environ['CONDA_PREFIX']}/x86_64-conda-linux-gnu/sysroot/usr/lib64"
elif (casa / "conf" / "bv_maker.cfg").exists():
# This is a Apptainer based development environment without Conda
modified["BRAINVISA_BVMAKER_CFG"] = f"{casa}/conf/bv_maker.cfg"
Expand All @@ -52,6 +53,7 @@ for casa in (
modified["CASA_BUILD"] = casa_build
modified["CASA_INSTALL"] = f"{casa}/{host}install"
modified["CASA_TEST"] = f"{casa}/{host}test"
modified["BRAINVISA_INSTALL_PREFIX"] = f"{modified['CASA_INSTALL']}"
break
else:
casa_build = os.environ.get("CASA_BUILD")
Expand Down Expand Up @@ -81,9 +83,10 @@ if brainvisa_cmake_src:
for n, v in modified.items():
if not isinstance(v, str):
v = ":".join(v)
os.environ[n] = v
if len(sys.argv) <= 1:
print(f'export BRAINVISA_UNENV_{n}="{os.environ.get(n, "")}"')
print(f'export {n}="{v}"')
os.environ[n] = v

if len(sys.argv) > 1:
sys.exit(subprocess.run(sys.argv[1:], env=os.environ).returncode)
31 changes: 31 additions & 0 deletions bin/bv_unenv
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/usr/bin/env python

import sys, os

if len(sys.argv) >= 2 and sys.argv[1] in ('-h', '--help'):
print('bv_unenv [command [command_arg [...]]]')
print('Reset original environment (that was active before using bv_env)')
print('Without arguments, print the runtime environment to be used '
'on the standard output')
print('With arguments, set the runtime environment, and run the '
'command passed in arguments in this environment.')
sys.exit(0)

unenv = [ i for i in os.environ if i.startswith( 'BRAINVISA_UNENV_' ) ]
if len( sys.argv ) > 1:
for n in unenv:
v = os.environ.get(n)
if v:
os.environ[n[16:]] = os.environ[n]
else:
del os.environ[n[16:]]
del os.environ[n]
os.execvpe( sys.argv[1], sys.argv[ 1: ], os.environ )
else:
for n in unenv:
v = os.environ.get(n)
if v:
print(f"export {n[16:]}='{v}'")
else:
print(f'unset {n[16:]}')
print('unset', n)
52 changes: 52 additions & 0 deletions bin/bv_unenv.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# bv_unenv.sh: unset the environment which was set up by bv_env.sh in a shell.
#
# Usage:
# . bv_unenv.sh

# Create a temporary file with mktemp if available
bv_env_tempfile=$(mktemp -t bv_env.XXXXXXXXXX 2>/dev/null)

# Fall back to making up a file name
if ! [ -w "$bv_env_tempfile" ]
then
bv_env_i=0
bv_env_tempfile=${TMPDIR:-/tmp}/bv_env-$$-$bv_env_i
while [ -e "$bv_env_tempfile" ]; do
bv_env_i=$(($bv_env_i+1))
bv_env_tempfile=${TMPDIR:-/tmp}/bv_env-$$-$bv_env_i
done
unset bv_env_i
# Create the temporary file, making sure not to overwrite an existing file
(umask 077 && set -C && : > "$bv_env_tempfile") || {
echo "bv_unenv.sh: error creating $bv_env_tempfile, aborting" >&2
unset bv_env_tempfile
return 1
}
fi

bv_env_cleanup() {
rm -f "$bv_env_tempfile"
unset bv_env_tempfile
}


# Contrary to bv_env.sh, we know that bv_unenv is in the PATH so we do not have
# to guess its location.
bv_unenv >| "$bv_env_tempfile" || {
echo "bv_unenv.sh: error while using bv_unenv, aborting" >&2
bv_env_cleanup
return 1
}

. "$bv_env_tempfile" || {
echo "bv_unenv.sh: error while sourcing the output of $bv_env" >&2
bv_env_cleanup
hash -r
return 1
}

bv_env_cleanup

# Empty the cache of known command locations, which is necessary to take
# changes of $PATH into account under some shells.
hash -r
11 changes: 4 additions & 7 deletions python/brainvisa_cmake/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,10 @@ def configure(self, options, args):

brainvisa_cmake_root = pathlib.Path(__file__).parent.parent.parent

path = bin / 'bv_env'
path.unlink(missing_ok=True)
path.symlink_to(os.path.relpath(brainvisa_cmake_root / 'bin' / 'bv_env', bin))

path = bin / 'bv_env.sh'
path.unlink(missing_ok=True)
path.symlink_to(os.path.relpath(brainvisa_cmake_root / 'bin' / 'bv_env.sh', bin))
for f in ('bv_env', 'bv_env.sh', 'bv_unenv', 'bv_unenv.sh'):
path = bin / f
path.unlink(missing_ok=True)
path.symlink_to(os.path.relpath(brainvisa_cmake_root / 'bin' / f, bin))

cross_compiling_directories = {}
for k, s in self.configuration.sourcesDirectories.items():
Expand Down

0 comments on commit 56c661d

Please sign in to comment.