OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
setupenv.py
Go to the documentation of this file.
1 import os
2 from pathlib import Path
3 import logging
4 import sys
5 
6 
7 def env(self):
8  """
9  A simple module to populate some important environment variables
10  """
11  if os.getenv("OCSSWROOT") is None:
12  scriptdir = Path(__file__).resolve().parent
13  if 'src' in scriptdir.parts:
14  self.dirs['packageroot'] = scriptdir.parents[2]
15  else:
16  self.dirs['packageroot'] = scriptdir.parents[1]
17  else:
18  self.dirs['packageroot'] = Path(os.getenv("OCSSWROOT"))
19 
20  self.dirs['root'] = self.dirs['packageroot'] / "share"
21  if os.getenv("OCDATAROOT"):
22  self.dirs['root'] = Path(os.getenv("OCDATAROOT"))
23 
24  if not self.dirs['root'].exists():
25  print("ERROR: The OCDATAROOT {} directory does not exist.".format(self.dirs['root']))
26  print("Please make sure you have downloaded and installed OCSSW and sourced the OCSSW environment")
27  sys.exit(1)
28 
29  self.dirs['scripts'] = self.dirs['packageroot'] / "scripts"
30  if os.getenv("OCSSW_SCRIPTS"):
31  self.dirs['scripts'] = Path(os.getenv("OCSSW_SCRIPTS"))
32 
33  self.dirs['var'] = self.dirs['packageroot'] / "var"
34  if os.getenv("OCVARROOT"):
35  self.dirs['var'] = Path(os.getenv("OCVARROOT"))
36 
37  self.dirs['bin'] = self.dirs['packageroot'] / "bin"
38  if os.getenv("OCSSW_BIN"):
39  self.dirs['bin'] = Path(os.getenv("OCSSW_BIN"))
40 
41  self.dirs['bin3'] = self.dirs['packageroot'] / "opt" / "bin"
42  if os.getenv("LIB3_BIN"):
43  self.dirs['bin3'] = Path(os.getenv("LIB3_BIN"))
44 
45  self.dirs['log'] = self.dirs['var'] / "log"
46  Path(self.dirs['log']).mkdir(parents=True, exist_ok=True)
47 
48  if os.getenv("OCSSW_DEBUG") is not None and int(os.getenv("OCSSW_DEBUG")) > 0:
49  if not os.path.exists(self.dirs['bin']):
50  print("Error: OCSSW_DEBUG set, but...\n\t%s\ndoes not exist!" % self.dirs['bin'])
51  sys.exit(1)
52  else:
53  print("Running debug binaries...\n\t%s" % self.dirs['bin'])
54 
55  self.dirs['run'] = Path.cwd()
56  if self.curdir:
57  self.dirs['anc'] = self.dirs['run']
58  else:
59  if self.ancdir is None:
60  if os.getenv("L2GEN_ANC") is None and os.getenv("USER_L2GEN_ANC") is None:
61  if self.verbose:
62  print("Neither the L2GEN_ANC nor USER_L2GEN_ANC environment variables are set.")
63  print("...using the current working directory for ancillary file download.")
64  self.curdir = True
65  self.dirs['anc'] = self.dirs['run']
66  else:
67  if os.getenv("L2GEN_ANC") is not None:
68  self.dirs['anc'] = os.getenv("L2GEN_ANC")
69  else:
70  self.dirs['anc'] = os.getenv("USER_L2GEN_ANC")
71  else:
72  self.dirs['anc'] = Path(self.ancdir)
73 
74 def build_executable_path(prog_name):
75  """
76  Returns the path to the program named in prog_name as a pathlib Path object.
77  None is returned if the program is not found.
78  """
79  packageroot = None
80  if os.getenv("OCSSWROOT"):
81  packageroot = Path(os.getenv("OCSSWROOT"))
82 
83  prog_path = packageroot / 'bin' / prog_name
84  if not Path.exists(prog_path):
85  err_msg = "Cannot find program %s" % prog_name
86  logging.error(err_msg)
87  sys.exit(err_msg)
88 
89  return prog_path
def env(self)
Definition: setupenv.py:7
def build_executable_path(prog_name)
Definition: setupenv.py:74