OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
seadas_info.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 
3 """
4 Print various items that may be of interest for SeaDAS and OCSSW
5 troubleshooting or forum posts.
6 """
7 
8 from __future__ import print_function
9 
10 import argparse
11 import os
12 import re
13 import subprocess
14 import sys
15 import xml.etree.ElementTree as ET
16 import platform
17 
18 
19 __version__ = '0.0.3.devel'
20 
22  """
23  Return the Python version, cleaned of extra text (especially relevant for
24  Anaconda installations. Return "Not found" if the Python version is not
25  found (although how that happens is a major mystery!).
26  """
27  py_ver = 'Not found'
28  py_ver = sys.version
29  if sys.version.upper().find('ANACONDA') != -1:
30  py_ver = ' '.join([sys.version.split(' ')[0], '(part of an Anaconda installation)'])
31  else:
32  py_ver = sys.version.split(' ')[0]
33  return py_ver
34 
35 def get_command_output(command):
36 
37  cmd_output = None
38  try:
39  process = subprocess.Popen(command, shell=False, stdout=subprocess.PIPE,
40  stderr=subprocess.PIPE)
41  stdout, stderr = process.communicate()
42  cmd_output = stdout.decode("utf-8") + stderr.decode("utf-8")
43  except:
44  cmd_output = None
45  return cmd_output
46 
47 def get_env():
48  """
49  Returns env output, which is obtained by running env
50  via subprocess.
51  """
52  env_output = None
53  cmd = ["env"]
54  output = get_command_output(cmd)
55  if output:
56  env_output = output.splitlines()
57  env_output.sort()
58  return env_output
59 
60 def get_l_program_version(l_program, ocssw_root):
61  """
62  Returns the version for various "l programs" (e.g. l2gen, l3bin)
63  """
64  lprog_ver = 'program not found'
65  exe_dirs = ['bin', 'run/bin', 'run/bin/linux_64']
66  ver_args = ['version', '-version']
67  exe_fnd = False
68  prog = os.path.join(ocssw_root, l_program)
69  if os.path.exists(prog):
70  exe_fnd = True
71  else:
72  for cand_dir in exe_dirs:
73  prog = os.path.join(ocssw_root, cand_dir, l_program)
74  if os.path.exists(prog):
75  exe_fnd = True
76  break
77  if exe_fnd:
78  for ver_arg in ver_args:
79  cmd_line = [prog, ver_arg]
80  out_txt = get_command_output(cmd_line)
81  if out_txt:
82  ver_lines = out_txt.split('\n')
83  ver_lines.remove('')
84  lprog_ver = ver_lines[-1].strip()
85  break
86  return lprog_ver
87 
88 def get_installed_mission(ocdata_root):
89  missionInstalled = []
90  missionListPath = os.path.join(ocdata_root, 'common/missionList.xml')
91  if os.path.exists(missionListPath):
92  tree = ET.parse(missionListPath)
93  root = tree.getroot()
94  for elem in root:
95  name = elem.get('name')
96  for subelem in elem:
97  missionDir = subelem.text
98  pathAbs = os.path.join(ocdata_root, missionDir)
99  if os.path.exists(pathAbs):
100  missionInstalled.append(name)
101  return missionInstalled
102 
104  """
105  Returns the java version, which is obtained by running java -version
106  via subprocess.
107  """
108  java_ver = 'Not installed'
109  cmd = ['java', '-version']
110  output = get_command_output(cmd)
111  if output and output.upper().find('RUNTIME') != -1:
112  lines = output.split('\n')
113  java_ver = re.sub('(^[^"]+|(?<=")[^"]+$)', '', lines[0]).strip('"')
114  return java_ver
115 
117  """
118  Returns the distribution of the operating system.
119  """
120  dist = ''
121  rel_lines = None
122  uname_info = os.uname()
123  if uname_info[0] == 'Linux':
124  with open('/etc/os-release', 'rt') as os_rel_file:
125  rel_lines = os_rel_file.readlines()
126  if rel_lines:
127  rel_info_dict = dict()
128  for line in rel_lines:
129  line_parts = line.split('=')
130  if len(line_parts) == 2:
131  if line_parts[0].strip():
132  rel_info_dict[line_parts[0].strip()] = line_parts[1].strip().strip('"')
133  if 'PRETTY_NAME' in rel_info_dict:
134  dist = rel_info_dict['PRETTY_NAME']
135  elif uname_info[0] == 'Darwin':
136  dist = 'MacOS ' + platform.mac_ver()[0]
137  else:
138  dist = ' '.join([uname_info[0], uname_info[2]])
139  return dist
140 
142  """
143  Returns the python3 path, which is obtained by running which python3
144  via subprocess.
145  """
146  python3_path = ''
147  cmd = ['which', 'python3']
148  output = get_command_output(cmd)
149  if output:
150  lines = output.split('\n')
151  lines.remove('')
152  python3_path = lines[-1].strip()
153  return python3_path
154 
155 def get_seadas_version(seadas_root):
156  """
157  Returns the SeaDAS version as held in the VERSION.txt file in the
158  directory pointed to by the SEADAS_ROOT environment variable.
159  """
160  seadas_ver = 'Not installed'
161  ver_path = os.path.join(seadas_root, 'VERSION.txt')
162  if os.path.exists(ver_path):
163  with open(ver_path, 'rt') as ver_file:
164  in_line = ver_file.readline()
165  if in_line.find('VERSION') != -1:
166  seadas_ver = in_line.split(' ', 1)[1].strip()
167  return seadas_ver
168 
170  """
171  Handle help or version being requested on the command line.
172  """
173  parser = argparse.ArgumentParser()
174  parser.add_argument("--AppDir", \
175  help="Application installation directory", \
176  type=str)
177  args = parser.parse_args()
178  return args
179 
180 def print_seadas_info(ocssw_root, ocdata_root):
181  """
182  Print out the items of interest for the SeaDAS installation specified
183  by the seadas_root and ocssw_root parameters.
184  """
185  print('NASA Science Processing (OCSSW):')
186  print('OCSSWROOT={0}'.format(ocssw_root))
187  print('OCDATAROOT={0}'.format(ocdata_root))
188  print('l2gen version: {0}'.format(get_l_program_version('l2gen', ocssw_root).strip()))
189  print('l2bin version: {0}'.format(get_l_program_version('l2bin', ocssw_root).strip()))
190  print('l3bin version: {0}'.format(get_l_program_version('l3bin', ocssw_root).strip()))
191  print('l3mapgen version: {0}'.format(get_l_program_version('l3mapgen', ocssw_root).strip()))
192  print('Installed Missions: {0}'.format(get_installed_mission(ocdata_root)))
193  print('')
194  return
195 
197  """
198  Print out information about the system (OS, Python version, Java version).
199  """
200  print('General System and Software:')
201  print('Operating system: {0}'.format(get_os_distribution()))
202  print('Java version: {0}'.format(get_java_version()))
203  print('Python3 version: {0}'.format(get_cleaned_python_version()))
204  print('Python3 Path: {0}'.format(get_python3_path()))
205  env_output = get_env()
206  printenvs = ['CC','CXX','FC','OCSSW_ARCH','OCSSW_BIN','OCSSW_DEBUG','OCSSW_MODIS',
207  'OCVARROOT','ELEMENTS','EOS_LIB_PREFIX','GCC_TUNE','HDFEOS_LIB',
208  'HRPT_STATION_IDENTIFICATION_FILE','L2GEN_ANC','LIB3_BIN','LIB3_CHECK',
209  'LIB3_DIR','LIB3_INC','LIB3_LIB','NAVCTL','NAVQC','ORBCTL','PGSINC','PGSLIB',
210  'OCTS_REGISTRATION_TABLES','PROJ_DATA','PROJ_LIB','SWFTBL','SWTBL']
211  if env_output:
212  print('Env:')
213  for line in env_output:
214  key, value = line.split('=', 1)
215  if key in printenvs:
216  print(' {0}={1}'.format(key,value))
217 
218  return
219 
220 def main():
221  """
222  The program's main function - gets and prints items of interest.
223  """
224  if 'OCSSWROOT' in os.environ:
225  ocssw_root = os.environ['OCSSWROOT']
226  else:
227  ocssw_root = 'Not defined'
228  if 'OCDATAROOT' in os.environ:
229  ocdata_root = os.environ['OCDATAROOT']
230  else:
231  ocdata_root = 'Not defined'
232  print_seadas_info(ocssw_root, ocdata_root)
234  return 0
235 
236 if __name__ == '__main__':
237  sys.exit(main())
def print_sys_info()
Definition: seadas_info.py:196
def get_python3_path()
Definition: seadas_info.py:141
def get_os_distribution()
Definition: seadas_info.py:116
def get_cleaned_python_version()
Definition: seadas_info.py:21
def print_seadas_info(ocssw_root, ocdata_root)
Definition: seadas_info.py:180
def get_java_version()
Definition: seadas_info.py:103
def get_command_output(command)
Definition: seadas_info.py:35
def get_l_program_version(l_program, ocssw_root)
Definition: seadas_info.py:60
def handle_command_line()
Definition: seadas_info.py:169
def get_installed_mission(ocdata_root)
Definition: seadas_info.py:88
def get_env()
Definition: seadas_info.py:47
def get_seadas_version(seadas_root)
Definition: seadas_info.py:155