DAS  3.0
Das Analysis System
detect_environment Namespace Reference

Functions

def lcg ()
 
def conda_like ()
 
def detect_environment ()
 

Function Documentation

◆ conda_like()

def detect_environment.conda_like ( )
Generates the code to set up a conda/mamba/micromamba environment.

\note Only micromamba is officially supported.
22 def conda_like():
23  r"""
24  Generates the code to set up a conda/mamba/micromamba environment.
25 
26  \note Only micromamba is officially supported.
27  """
28 
29  env_name = os.environ['CONDA_DEFAULT_ENV']
30 
31  # Figure out which variant we're using
32  if 'MAMBA_EXE' in os.environ and 'MAMBA_ROOT_PREFIX' in os.environ:
33 
34  template = '''
35 # Make sure {mamba_exe_name} is set up
36 export MAMBA_EXE='{mamba_exe}';
37 export MAMBA_ROOT_PREFIX='{mamba_root}';
38 __mamba_setup="$("$MAMBA_EXE" shell hook --shell bash --root-prefix "$MAMBA_ROOT_PREFIX" 2> /dev/null)"
39 if [ $? -eq 0 ]; then
40  eval "$__mamba_setup"
41 else
42  alias {mamba_exe_name}="$MAMBA_EXE" # Fallback on help from mamba activate
43 fi
44 unset __mamba_setup
45 
46 # Enable the environment
47 {mamba_exe_name} activate '{env_name}'
48 '''
49  mamba_exe = os.environ['MAMBA_EXE']
50  mamba_root = os.environ['MAMBA_ROOT_PREFIX']
51  mamba_exe_name = 'micromamba' if mamba_exe.endswith('micromamba') else 'mamba'
52  return template.format(mamba_exe=mamba_exe,
53  mamba_root=mamba_root,
54  mamba_exe_name=mamba_exe_name,
55  env_name=env_name)
56  elif 'CONDA_EXE' in os.environ:
57  template = '''
58 # Make sure conda is set up
59 __conda_setup="$('{conda_exe}' 'shell.bash' 'hook' 2> /dev/null)"
60 if [ $? -eq 0 ]; then
61  eval "$__conda_setup"
62 else
63  if [ -f "{conda_base}/etc/profile.d/conda.sh" ]; then
64  . "{conda_base}/etc/profile.d/conda.sh"
65  else
66  export PATH="{conda_base}/bin:$PATH"
67  fi
68 fi
69 unset __conda_setup
70 
71 # Enable the environment
72 conda activate '{env_name}'
73 '''
74 
75  conda_exe = os.environ['CONDA_EXE']
76  conda_root = abspath(join(conda_exe, os.pardir, os.pardir))
77  return template.format(conda_exe=conda_exe,
78  conda_root=conda_root,
79  env_name=env_name)
80  else:
81  raise ValueError('\x1B[31mUnknown conda-like environment\x1B[0m')
82 
83 

◆ detect_environment()

def detect_environment.detect_environment ( )
Detect the environment in which the command is begin run, and returns code
to set it up again. Returns an empty string on failure.
84 def detect_environment():
85  """
86  Detect the environment in which the command is begin run, and returns code
87  to set it up again. Returns an empty string on failure.
88  """
89 
90  # List of environment variables used to detect which environment is active.
91  detect = {
92  'LCG_VERSION': lcg,
93  'CONDA_DEFAULT_ENV': conda_like,
94  }
95 
96  for env_var, function in detect.items():
97  if env_var in os.environ:
98  try:
99  return function()
100  except:
101  pass # Try other possibilities
102 
103  return ''
104 
105 

◆ lcg()

def detect_environment.lcg ( )
Generates the code to set up an LCG environment.
8 def lcg():
9  """
10  Generates the code to set up an LCG environment.
11  """
12 
13  # The CMAKE_PREFIX_PATH seems to point to the root of the environment.
14  location = os.environ.get('CMAKE_PREFIX_PATH')
15  setup = join(location, 'setup.sh')
16  if not isfile(setup):
17  raise ValueError('\x1B[31mCould not find LCG setup.sh\x1B[0m')
18 
19  return 'source ' + setup
20 
21 
detect_environment.detect_environment
def detect_environment()
Definition: detect_environment.py:84
detect_environment.lcg
def lcg()
Definition: detect_environment.py:8
detect_environment.conda_like
def conda_like()
Definition: detect_environment.py:22