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

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

◆ lcg()

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