OB.DAAC Logo
NASA Logo
Ocean Color Science Software

ocssw V2022
install_ocssw.py
Go to the documentation of this file.
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3 """
4 Created on Tue Sep 18 11:08:27 2018
5 
6 @author: dshea
7 """
8 
9 from operator import indexOf
10 import sys
11 import re
12 import argparse
13 import os
14 import shutil
15 import subprocess
16 import urllib.parse
17 import json
18 import datetime
19 import tempfile
20 import tarfile
21 import smtplib
22 from email.message import EmailMessage
23 
24 #sys.path.insert(0, os.path.dirname(__file__))
25 import manifest as mf
26 
27 # global variables
28 MANIFEST_BASENAME = "manifest.json"
29 BUNDLELIST_BASENAME = "bundleList.json"
30 INSTALL_OCSSW_BASENAME = "install_ocssw.json"
31 versionString = "8.0"
32 baseUrl = "https://oceandata.sci.gsfc.nasa.gov/manifest/tags"
33 manifestCommand = os.path.dirname(__file__) + "/manifest.py"
34 currentThing = 1
35 totalNumThings = 0
36 
37 
41 bundleList = []
42 initialBundleList = [
43  {"name":"root", "dir":".", "help":"random files in the root dir", "extra":"--exclude . --include bundleList.json --include OCSSW_bash.env --include OCSSW.env", "commandLine":True},
44  {"name":"bin_linux_64", "dir":"bin_linux_64", "help":"executables for Linux", "commandLine":False},
45  {"name":"bin_macosx_intel", "dir":"bin_macosx_intel", "help":"executables for Mac", "commandLine":False},
46  {"name":"bin_odps", "dir":"bin_odps", "help":"executables for ODPS", "commandLine":False},
47  {"name":"lib_linux_64", "dir":"lib_linux_64", "help":"shared libraries for Linux", "commandLine":False},
48  {"name":"lib_macosx_intel", "dir":"lib_macosx_intel", "help":"shared libraries for Mac", "commandLine":False},
49  {"name":"lib_odps", "dir":"lib_odps", "help":"shared libraries for ODPS", "commandLine":False},
50  {"name":"opt_linux_64", "dir":"opt_linux_64", "help":"3rd party library for Linux", "extra": "--exclude src", "commandLine":False},
51  {"name":"opt_macosx_intel", "dir":"opt_macosx_intel", "help":"3rd party library for Mac", "extra": "--exclude src", "commandLine":False},
52  {"name":"opt_odps", "dir":"opt_odps", "help":"3rd party library for ODPS", "extra": "--exclude src", "commandLine":False},
53 
54  {"name":"ocssw_src", "dir":"ocssw_src", "help":"OCSSW source code", "commandLine":False},
55  {"name":"opt_src", "dir":"opt/src", "help":"3rd party library sources", "extra":"--exclude .buildit.db", "commandLine":True},
56 
57  {"name":"afrt", "dir":"share/afrt", "help":"Ahmad-Fraser RT data", "commandLine":True},
58  {"name":"aquaverse", "dir":"share/aquaverse", "help":"Algorithm based on Mixture Density Networks", "commandLine":True},
59  {"name":"avhrr", "dir":"share/avhrr", "help":"AVHRR", "commandLine":True},
60  {"name":"aviris", "dir":"share/aviris", "help":"AVIRIS", "commandLine":True},
61  {"name":"common", "dir":"share/common", "help":"common", "commandLine":True},
62  {"name":"czcs", "dir":"share/czcs", "help":"CZCS", "commandLine":True},
63  {"name":"eval", "dir":"share/eval", "help":"evaluation", "commandLine":True},
64  {"name":"goci", "dir":"share/goci", "help":"GOCI", "commandLine":True},
65  {"name":"hawkeye", "dir":"share/hawkeye", "help":"Hawkeye", "commandLine":True},
66  {"name":"hico", "dir":"share/hico", "help":"HICO", "commandLine":True},
67  {"name":"l5tm", "dir":"share/l5tm", "help":"l5tm", "commandLine":True},
68  {"name":"l7etmp", "dir":"share/l7etmp", "help":"l7etmp", "commandLine":True},
69  {"name":"meris", "dir":"share/meris", "help":"MERIS", "commandLine":True},
70  {"name":"misr", "dir":"share/misr", "help":"MISR", "commandLine":True},
71  {"name":"modis", "dir":"share/modis", "help":"MODIS common", "extra":"--exclude aqua --exclude terra", "commandLine":False},
72  {"name":"modisa", "dir":"share/modis/aqua", "help":"MODIS AQUA", "commandLine":True},
73  {"name":"modist", "dir":"share/modis/terra", "help":"MODIS TERRA", "commandLine":True},
74  {"name":"mos", "dir":"share/mos", "help":"MOS", "commandLine":True},
75  {"name":"msi", "dir":"share/msi", "help":"MSI Sentinel 2 common", "extra":"--exclude s2a --exclude s2b", "commandLine":False},
76  {"name":"msis2a", "dir":"share/msi/s2a", "help":"MSI Sentinel 2A", "commandLine":True},
77  {"name":"msis2b", "dir":"share/msi/s2b", "help":"MSI Sentinel 2B", "commandLine":True},
78  {"name":"oci", "dir":"share/oci", "help":"PACE OCI", "commandLine":True},
79  {"name":"ocia", "dir":"share/ocia", "help":"PACE OCI AVIRIS", "commandLine":True},
80  {"name":"ocip", "dir":"share/ocip", "help":"PACE OCI PRISM", "commandLine":True},
81  {"name":"ocis", "dir":"share/ocis", "help":"PACE OCI Simulated data", "commandLine":True},
82  {"name":"ocm1", "dir":"share/ocm1", "help":"OCM1", "commandLine":True},
83  {"name":"ocm2", "dir":"share/ocm2", "help":"OCM2", "commandLine":True},
84  {"name":"ocrvc", "dir":"share/ocrvc", "help":"OC Virtual Constellation", "commandLine":True},
85  {"name":"octs", "dir":"share/octs", "help":"OCTS", "commandLine":True},
86  {"name":"olci", "dir":"share/olci", "help":"OLCI Sentinel 3 common", "extra":"--exclude s3a --exclude s3b", "commandLine":False},
87  {"name":"olcis3a", "dir":"share/olci/s3a", "help":"OLCI Sentinel 3A", "commandLine":True},
88  {"name":"olcis3b", "dir":"share/olci/s3b", "help":"OLCI Sentinel 3B", "commandLine":True},
89  {"name":"oli", "dir":"share/oli", "help":"OLI Landsat", "extra":"--exclude l8 --exclude l9", "commandLine":False},
90  {"name":"olil8", "dir":"share/oli/l8", "help":"OLI Landsat 8", "commandLine":True},
91  {"name":"olil9", "dir":"share/oli/l9", "help":"OLI Landsat 9", "commandLine":True},
92  {"name":"osmi", "dir":"share/osmi", "help":"OSMI", "commandLine":True},
93  {"name":"prism", "dir":"share/prism", "help":"PRISM", "commandLine":True},
94  {"name":"sabiamar", "dir":"share/sabiamar", "help":"Sabiamar", "commandLine":True},
95  {"name":"seawifs", "dir":"share/seawifs", "help":"SeaWiFS", "commandLine":True},
96  {"name":"sgli", "dir":"share/sgli", "help":"SGLI", "commandLine":True},
97  {"name":"spexone_remotap", "dir":"share/spexone/remotap", "help":"SPEX One RemoTAP", "commandLine":True},
98  {"name":"viirs", "dir":"share/viirs", "extra":"--exclude dem --exclude j1 --exclude j2 --exclude npp", "help":"VIIRS common", "commandLine":False},
99  {"name":"viirsdem", "dir":"share/viirs/dem", "help":"VIIRS Digital Elevation", "commandLine":True},
100  {"name":"viirsj1", "dir":"share/viirs/j1", "help":"VIIRS JPSS1", "commandLine":True},
101  {"name":"viirsj2", "dir":"share/viirs/j2", "help":"VIIRS JPSS2", "commandLine":True},
102  {"name":"viirsn", "dir":"share/viirs/npp", "help":"VIIRS NPP", "commandLine":True},
103  {"name":"wv3", "dir":"share/wv3", "help":"WV3", "commandLine":True},
104  {"name":"aerosol", "dir":"share/aerosol", "help":"aerosol processing with dtdb", "commandLine":True},
105  {"name":"cloud", "dir":"share/cloud", "help":"cloud properties processing", "commandLine":True},
106  {"name":"benchmark", "dir":"benchmark", "help":"benchmark MOSIS Aqua, level0 -> level3 Mapped", "commandLine":True},
107  {"name":"viirs_l1_benchmark", "dir":"viirs_l1_benchmark", "help":"VIIRS benchmark data", "commandLine":True},
108  {"name":"viirs_l1_bin_macosx_intel", "dir":"viirs_l1_bin_macosx_intel", "help":"Subset of binary files for VIIRS", "commandLine":False},
109  {"name":"viirs_l1_bin_linux_64", "dir":"viirs_l1_bin_linux_64", "help":"Subset of binary files for VIIRS", "commandLine":False},
110  {"name":"viirs_l1_bin_odps", "dir":"viirs_l1_bin_odps", "help":"Subset of binary files for VIIRS", "commandLine":False}
111 ]
112 
113 # list of bundles that have luts
114 lutBundles = ["seawifs", "modisa", "modist", "viirsn", "viirsj1"]
115 
116 
117 def findBundleInfo(bundleName):
118  for bundleInfo in bundleList:
119  if bundleInfo["name"] == bundleName:
120  return bundleInfo
121  return None
122 
123 def getArch():
124  """
125  Return the system arch string.
126  """
127  (sysname, _, _, _, machine) = os.uname()
128  if sysname == 'Darwin':
129  if machine == 'x86_64' or machine == 'i386':
130  return 'macosx_intel'
131  print("unsupported Mac machine =", machine)
132  exit(1)
133  if sysname == 'Linux':
134  if machine == 'x86_64':
135  return 'linux_64'
136  print("Error: can only install OCSSW software on 64bit Linux")
137  exit(1)
138  if sysname == 'Windows':
139  print("Error: can not install OCSSW software on Windows")
140  exit(1)
141  print('***** unrecognized system =', sysname, ', machine =', machine)
142  print('***** defaulting to linux_64')
143  return 'linux_64'
144 
145 def runCommand(command, pipe=False, shellVal=False):
146  if pipe:
147  proc = subprocess.run(command, stdout=subprocess.PIPE, shell=shellVal)
148  else:
149  proc = subprocess.run(command, shell=shellVal)
150  if proc.returncode != 0:
151  print("Error: trying to run = ", command)
152  sys.exit(1)
153  if pipe:
154  return proc.stdout
155 
156 def listTags(options):
157  manifest_options = mf.create_default_options()
158  manifest_options.wget = options.wget
159  mf.list_tags(manifest_options, None)
160 
161 def checkTag(options):
162  manifest_options = mf.create_default_options()
163  manifest_options.tag = options.tag
164  manifest_options.wget = options.wget
165  if not mf.check_tag(manifest_options, None):
166  print("tag=%s," % (options.tag), "Does not exist.")
167  sys.exit(1)
168 
169 def recordInstalledTag(options):
170  installInfo = {}
171  installInfo["tag"] = options.tag
172  installInfo["version"] = versionString
173  installInfo["install_date"] = datetime.datetime.now().isoformat(timespec="seconds")
174 
175  # save as a JSON file
176  if not os.path.isdir(options.install_dir):
177  os.makedirs(options.install_dir)
178 
179  fileName = "%s/%s" % (options.install_dir, INSTALL_OCSSW_BASENAME)
180  with open(fileName, 'w') as outfile:
181  json.dump(installInfo, outfile, indent=4, sort_keys=True)
182 
183 def getInstalledTag(options):
184  installOcsswFilename = "%s/%s" % (options.install_dir, INSTALL_OCSSW_BASENAME)
185  try:
186  # try reading the install_ocssw.json file
187  with open(installOcsswFilename, 'rb') as installOcsswFile:
188  installInfo = json.load(installOcsswFile)
189  return installInfo["tag"]
190  except:
191  # if that does not work, grab the last tag out of the manifest.json file
192  try:
193  manifestFilename = "%s/%s" % (options.install_dir, MANIFEST_BASENAME)
194  with open(manifestFilename, 'rb') as manifestFile:
195  manifest = json.load(manifestFile)
196  return manifest["tags"][-1]
197  except:
198  # if nothing works, do this
199  return "Unknown"
200 
201 def installedTag(options):
202  print("installedTag =", getInstalledTag(options))
203 
204 def installBundle(options, bundleInfo):
205  global currentThing
206  if options.verbose:
207  print()
208  print("Installing (" + str(currentThing), "of", str(totalNumThings) + ") -", bundleInfo["name"], flush=True)
209  currentThing += 1
210 
211  manifest_options = mf.create_default_options()
212  manifest_options.verbose = options.verbose
213  manifest_options.name = bundleInfo["name"]
214  manifest_options.tag = options.tag
215  manifest_options.dest_dir = "%s/%s" % (options.install_dir, bundleInfo["dir"])
216  manifest_options.save_dir = options.save_dir
217  manifest_options.local_dir = options.local_dir
218  manifest_options.wget = options.wget
219 
220  mf.download(manifest_options, None)
221 
222  if options.clean:
223  command = "%s clean %s/%s" % (manifestCommand, options.install_dir, bundleInfo["dir"])
224  if "extra" in bundleInfo:
225  command += " " + bundleInfo["extra"]
226  runCommand(command, shellVal=True)
227 
228 def updateLuts(options, lut):
229  runner = options.install_dir + "/bin/ocssw_runner"
230  if not os.path.isfile(runner):
231  print("Error - bin directory needs to be installed.")
232  exit(1)
233  if options.verbose:
234  print()
235  print("Installing lut -", lut)
236  command = "%s --ocsswroot %s update_luts %s" % (runner, options.install_dir, lut)
237  runCommand(command, shellVal=True)
238 
239 def getBundleListTag(manifestFilename):
240  try:
241  with open(manifestFilename, 'rb') as manifestFile:
242  manifest = json.load(manifestFile)
243  return manifest['files'][BUNDLELIST_BASENAME]['tag']
244  except json.JSONDecodeError:
245  print(manifestFilename, "is not a manifest file")
246  sys.exit(1)
247  except FileNotFoundError:
248  print(manifestFilename, "Not found")
249  sys.exit(1)
250  except KeyError:
251  print(manifestFilename, "is corrupt")
252  sys.exit(1)
253  print("could not find bundeList tag in", manifestFilename)
254  sys.exit(1)
255 
256 def downloadBundleList(options):
257  global bundleList
258 
259  if not options.tag:
260  print("\nWARNING: --tag is required to get the proper bundle list.\n")
261  bundleList = initialBundleList
262  return
263  else:
264  checkTag(options)
265 
266  tempDir = tempfile.TemporaryDirectory(prefix="install_ocssw-")
267  tempBundleFilePath = dest = "%s/%s" % (tempDir.name, BUNDLELIST_BASENAME)
268  if options.local_dir:
269  manifestFilename = "%s/%s/root/%s" % (options.local_dir, options.tag, MANIFEST_BASENAME)
270  bundleFilename = "%s/%s/root/%s" % (options.local_dir, getBundleListTag(manifestFilename),BUNDLELIST_BASENAME)
271  if not os.path.isfile(bundleFilename):
272  print(bundleFilename, "file does not exist")
273  sys.exit(1)
274  shutil.copy(bundleFilename, tempBundleFilePath)
275  elif options.wget:
276  command = "cd %s; wget -q %s/%s/root/%s" % (tempDir.name, options.base_url, options.tag, MANIFEST_BASENAME)
277  runCommand(command, shellVal=True)
278  manifestFilename = "%s/%s" % (tempDir.name, MANIFEST_BASENAME)
279  bundleListUrl = "%s/%s/root/%s" % (options.base_url, getBundleListTag(manifestFilename), BUNDLELIST_BASENAME)
280  command = "cd %s; wget -q %s" % (tempDir.name, bundleListUrl)
281  runCommand(command, shellVal=True)
282  else:
283  manifestUrl = "%s/%s/root/%s" % (options.base_url, options.tag, MANIFEST_BASENAME)
284  parts = urllib.parse.urlparse(manifestUrl)
285  host = parts.netloc
286  request = parts.path
287  status = mf.httpdl(host, request, localpath=tempDir.name, outputfilename=MANIFEST_BASENAME, force_download=True)
288  if status != 0:
289  print("Error downloading", manifestUrl, ": return code =", status)
290  sys.exit(1)
291  manifestFilename = "%s/%s" % (tempDir.name, MANIFEST_BASENAME)
292  bundleListUrl = "%s/%s/root/%s" % (options.base_url, getBundleListTag(manifestFilename), BUNDLELIST_BASENAME)
293  parts = urllib.parse.urlparse(bundleListUrl)
294  host = parts.netloc
295  request = parts.path
296  status = mf.httpdl(host, request, localpath=tempDir.name, outputfilename=BUNDLELIST_BASENAME, force_download=True)
297  if status != 0:
298  print("Error downloading", bundleListUrl, ": return code =", status)
299  sys.exit(1)
300  with open(tempBundleFilePath, 'rb') as bundleListFile:
301  bundleList = json.load(bundleListFile)
302 
303 
304 def downloadFile(options, tag, bundleInfo, fileName, destDir):
305  dest = "%s/%s" % (destDir, fileName)
306  dest_dir = os.path.dirname(dest)
307  if not os.path.isdir(dest_dir):
308  os.makedirs(dest_dir)
309 
310  if options.local_dir:
311  src = "%s/%s/%s/%s" % (options.local_dir, options.tag, bundleInfo["name"], fileName)
312  if options.verbose:
313  print("Copying %s from %s" % (fileName, src))
314  shutil.copy(src, dest)
315  return True
316 
317  url = "%s/%s/%s/%s" % (baseUrl, tag, bundleInfo["name"], fileName)
318  if options.verbose:
319  print("Downloading %s from %s" % (fileName, url))
320  if options.wget:
321  if os.path.isfile(dest):
322  os.remove(dest)
323  command = "cd %s; wget -q %s" % (dest_dir, url)
324  runCommand(command, shellVal=True)
325  else:
326  parts = urllib.parse.urlparse(url)
327  host = parts.netloc
328  request = parts.path
329  status = mf.httpdl(host, request, localpath=dest_dir,
330  outputfilename=os.path.basename(dest),
331  verbose=options.verbose,
332  force_download=True)
333  if status != 0:
334  print("Error downloading", dest, ": return code =", status)
335  return False
336  return True
337 
338 def bundleStatus(options, bundleInfo, fileLst_chg, fileLst_relPath_chg, fileLst_del):
339  returnStatus = True
340  statusTag = "tempStatusTag"
341  #os.chdir(options.install_dir)
342 
343  currentManifest = {}
344  tempDir = tempfile.TemporaryDirectory(prefix="install_ocssw-")
345  currentManifestFilename = "%s/%s" % (tempDir.name, MANIFEST_BASENAME)
346  if downloadFile(options, options.tag, bundleInfo, MANIFEST_BASENAME, tempDir.name):
347  with open(currentManifestFilename, 'rb') as manifestFile:
348  currentManifest = json.load(manifestFile)
349 
350  command = "cd %s; " % (options.install_dir)
351  command += manifestCommand + " generate"
352  command += " -n " + bundleInfo["name"]
353  command += " -t " + statusTag
354  command += " -b " + currentManifestFilename
355  command += " " + bundleInfo["dir"]
356  if "extra" in bundleInfo:
357  command += " " + bundleInfo["extra"]
358 
359  # command = [manifestCommand, "generate",
360  # "-n", bundleInfo["name"],
361  # "-t", statusTag,
362  # "-b", currentManifestFilename,
363  # bundleInfo["dir"]]
364  # if "extra" in bundleInfo:
365  # command += bundleInfo["extra"].split()
366  statusManifest = json.loads(runCommand(command, pipe=True, shellVal=True))
367 
368  if "tags" in currentManifest:
369  tagList = currentManifest["tags"]
370  versionStr = "("
371  if len(tagList) > 1:
372  versionStr += tagList[0] + ".."
373  versionStr += tagList[-1] + ")"
374  print(bundleInfo["name"], versionStr)
375  else:
376  print(bundleInfo["name"])
377 
378 
379 
380  # list new files
381  fileList = []
382  for f, info in statusManifest["files"].items():
383  if ("files" not in currentManifest) or (f not in currentManifest["files"]):
384  fileList.append(f)
385  if options.create_change:
386  fileLst_chg.append("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], f))
387  fileLst_relPath_chg.append("%s/%s" % (bundleInfo["dir"], f))
388  if fileList:
389  returnStatus = False
390  print(" New Files:")
391  for f in fileList:
392  print(" %s/%s" % (bundleInfo["dir"], f))
393 
394  # list modified files
395  fileList = []
396  for f, info in statusManifest["files"].items():
397  if ("files" in currentManifest) and (f in currentManifest["files"]):
398  if info["tag"] != currentManifest["files"][f]["tag"]:
399  fileList.append(f)
400  if options.create_change:
401  fileLst_chg.append("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], f))
402  fileLst_relPath_chg.append("%s/%s" % (bundleInfo["dir"], f))
403  if fileList:
404  returnStatus = False
405  print(" Modified Files:")
406  for f in fileList:
407  print(" %s/%s" % (bundleInfo["dir"], f))
408  if options.diff:
409  try:
410  if re.search('root', bundleInfo["name"]):
411  localFilename = "%s/%s" % (options.install_dir, f)
412  else:
413  localFilename = "%s/%s/%s" % (options.install_dir, bundleInfo["dir"], f)
414  with open(localFilename) as file_1:
415  file_1_text = file_1.readlines()
416  remoteFilename = "%s/%s" % (tempDir.name, f)
417  file_tag = currentManifest["files"][f]["tag"]
418  if downloadFile(options, file_tag, bundleInfo, f, tempDir.name):
419  if options.difftool is not None:
420  command = "%s %s %s" % (options.difftool, remoteFilename, localFilename)
421  subprocess.run(command, shell=True)
422  else:
423  print("diff %s remote..local" % (f))
424  command = "diff %s %s" % (remoteFilename, localFilename)
425  subprocess.run(command, shell=True)
426 
427  except:
428  continue
429 
430  # deleted file
431  fileList = []
432  if "files" in currentManifest:
433  for f, info in currentManifest["files"].items():
434  if f not in statusManifest["files"]:
435  fileList.append(f)
436  if options.create_change:
437  # fileLst_del.append("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], f))
438  fileLst_del.append("%s/%s" % (bundleInfo["dir"], f))
439  if fileList:
440  returnStatus = False
441  print(" Deleted Files:")
442  for f in fileList:
443  print(" %s/%s" % (bundleInfo["dir"], f))
444  return returnStatus
445 
446 def status(options):
447  global bundleList
448 
449  # theInstalledTag = getInstalledTag(options)
450  # print("installedTag =", theInstalledTag)
451 
452  # if not options.tag:
453  # options.tag = theInstalledTag
454 
455  theStatus = True
456  bundle_found = False
457  theInstalledTag = ''
458  fileList_change = []
459  #relative path of the fileList_change
460  fileList_relPath_change = []
461  fileList_delete = []
462 
463 
464  #loop through provided bundles e.g. --hico
465  for bundleInfo in bundleList:
466  if hasattr(options, bundleInfo["name"]) and getattr(options, bundleInfo["name"]):
467  # if not re.search("root", bundleInfo["name"]):
468  if not theInstalledTag:
469  theInstalledTag = getInstalledTag(options)
470  print("installedTag =", theInstalledTag)
471 
472  if not options.tag:
473  options.tag = theInstalledTag
474  if os.path.exists("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], MANIFEST_BASENAME)):
475  bundle_found = True
476  if not bundleStatus(options, bundleInfo, fileList_change, fileList_relPath_change, fileList_delete):
477  theStatus = False
478 
479  if not bundle_found:
480  startingDir = os.path.abspath(os.getcwd())
481  for bundleInfo in bundleList:
482  bundleDir = os.path.abspath(os.path.join(options.install_dir, bundleInfo["dir"]))
483  if startingDir not in bundleDir:
484  continue
485  if not re.search("root", bundleInfo["name"]):
486  if not theInstalledTag:
487  theInstalledTag = getInstalledTag(options)
488  print("installedTag =", theInstalledTag)
489 
490  if not options.tag:
491  options.tag = theInstalledTag
492 
493  if os.path.exists("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], MANIFEST_BASENAME)):
494  bundle_found = True
495  if not bundleStatus(options, bundleInfo, fileList_change, fileList_relPath_change, fileList_delete):
496  theStatus = False
497 
498  #loop through all the bundles
499  if not bundle_found:
500  theInstalledTag = getInstalledTag(options)
501  print("installedTag =", theInstalledTag)
502 
503  if not options.tag:
504  options.tag = theInstalledTag
505 
506  for bundleInfo in bundleList:
507  if os.path.exists("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], MANIFEST_BASENAME)):
508  bundle_found = True
509  if not bundleStatus(options, bundleInfo, fileList_change, fileList_relPath_change, fileList_delete):
510  theStatus = False
511 
512  if options.create_change:
513  uid = runCommand("whoami", True)
514  date = datetime.date.today().strftime("%Y-%m-%d")
515  if len(fileList_change) != 0:
516  file_name = options.tag + "_" + date + "_" + str(uid, 'UTF-8').strip()
517  tar_filename = file_name + ".tar"
518  tar = tarfile.open(file_name + ".tar", "w")
519  print("Creating " + tar_filename)
520  for index, name in enumerate(fileList_change):
521  tar.add(name, fileList_relPath_change[index])
522  tar.close()
523 
524  if len(fileList_delete) != 0:
525  remove_filename = file_name + ".remove"
526  print("Creating " + remove_filename)
527  with open(file_name + ".remove", "w") as fp:
528  for name in fileList_delete:
529  # write each item on a new line
530  fp.write("%s\n" % name)
531 
532  if options.deliver_change or options.deliver_email_from is not None:
533  glusteruser_dir = "/glusteruser/analyst/share_delivery"
534 
535  if(os.path.exists(glusteruser_dir)):
536  if len(fileList_change) != 0:
537  print("Copying .tar to /glusteruser/analyst/share_delivery")
538  shutil.copyfile(tar_filename, os.path.join(glusteruser_dir, tar_filename))
539  if len(fileList_delete) != 0:
540  print("Copying .remove files to /glusteruser/analyst/share_delivery")
541  shutil.copyfile(remove_filename, os.path.join(glusteruser_dir, remove_filename))
542 
543  if len(fileList_change) != 0 or len(fileList_delete) != 0:
544  message = EmailMessage()
545  if len(fileList_delete) == 0:
546  print("Sending email to swdevels@oceancolor.gsfc.nasa.gov notifying the delivery of the .tar files")
547  message.set_content(tar_filename)
548  message['Subject'] = '.tar file copied to /glusteruser/analyst/share_delivery'
549  elif len(fileList_change) == 0:
550  print("Sending email to swdevels@oceancolor.gsfc.nasa.gov notifying the delivery of the .remove files")
551  message.set_content(remove_filename)
552  message['Subject'] = '.remove file copied to /glusteruser/analyst/share_delivery'
553  else:
554  print("Sending email to swdevels@oceancolor.gsfc.nasa.gov notifying the delivery of the .tar and .remove files")
555  message.set_content(tar_filename + "\n" + remove_filename)
556  message['Subject'] = '.tar and.remove file copied to /glusteruser/analyst/share_delivery'
557 
558  if options.deliver_email_from is not None:
559  message['From'] = options.deliver_email_from
560  elif options.deliver_change:
561  install_ocssw_config_filename = os.path.join(os.path.expanduser('~'), ".install_ocssw")
562  with open(install_ocssw_config_filename, "r") as file_email:
563  message['From'] = file_email.readlines()[0].strip().split('deliver_email_from=')[1]
564  message['To'] = 'swdevels@oceancolor.gsfc.nasa.gov'
565 
566  smtp_server = smtplib.SMTP('localhost')
567  smtp_server.send_message(message)
568  smtp_server.quit()
569  else:
570  print("You do not have access to /glusteruser/analyst/share_dir")
571 
572 
573  return theStatus
574 
575 def update(options):
576  global bundleList
577  global totalNumThings
578 
579  theInstalledTag = getInstalledTag(options)
580  print("installedTag =", theInstalledTag)
581 
582  # count the number of bundles to install
583  totalNumThings = 0
584  for bundleInfo in bundleList:
585  if os.path.exists("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], MANIFEST_BASENAME)):
586  totalNumThings += 1
587 
588  for bundleInfo in bundleList:
589  if os.path.exists("%s/%s/%s" % (options.install_dir, bundleInfo["dir"], MANIFEST_BASENAME)):
590  installBundle(options, bundleInfo)
591 
592  recordInstalledTag(options)
593 
594 def run():
595  global totalNumThings
596  global bundleList
597 
598  # first make a parser to download the bundleInfo file
599  parser = argparse.ArgumentParser(description="Install OCSSW bundles", add_help=False)
600  parser.add_argument("-t", "--tag", default=None,
601  help="tag that you want to install")
602  parser.add_argument("-b", "--base_url", default=baseUrl,
603  help="remote url for the bundle server")
604  parser.add_argument("-l", "--local_dir", default=None,
605  help="local directory to use for bundle source instead of the bundle server")
606  parser.add_argument("--wget", default=False, action="store_true",
607  help="use wget for file download")
608  parser.add_argument("--list_tags", default=False, action="store_true",
609  help="list the tags that exist on the server")
610  parser.add_argument("--version", default=False, action="store_true",
611  help="print this program's version")
612 
613  options1 = parser.parse_known_args()
614  if not options1[0].list_tags and not options1[0].version:
615  downloadBundleList(options1[0])
616 
617  epilog = """
618  The content of the config file ~/.install_ocssw --
619  deliver_email_from=a valid email address that is on the list of swdevels@oceancolor.gsfc.nasa.gov
620  """
621  # now make the real parser
622  parser = argparse.ArgumentParser(description="Install OCSSW bundles", epilog=epilog, formatter_class=argparse.RawDescriptionHelpFormatter,)
623  parser.add_argument("--version", default=False, action="store_true",
624  help="print this program's version")
625  parser.add_argument("--list_tags", default=False, action="store_true",
626  help="list the tags that exist on the server")
627  parser.add_argument("--installed_tag", default=False, action="store_true",
628  help="list the currently installed tag")
629  parser.add_argument("--status", default=False, action="store_true",
630  help="compare the main tag manifest to the files in the bundle directories")
631  parser.add_argument("--update", default=False, action="store_true",
632  help="update all installed bundles to the tag given")
633  parser.add_argument("--diff", default=False, action="store_true",
634  help="show the difference between the old and new text files")
635  parser.add_argument("--difftool", default=None,
636  help="show the difference between the old and new text files using diff tool such as meld")
637  parser.add_argument("--create_change", default=False, action="store_true",
638  help="create a tar file of new and modified files and a text file with a list of files to be deleted")
639  parser.add_argument("--deliver_change", default=False, action="store_true",
640  help="create and copy to /glusteruser/analyst/share_delivery/ a tar file of new and modified files and a text file with a list of files to be deleted, and email to swdevels@oceancolor.gsfc.nasa.gov from the email address stored in .install_ocssw")
641  parser.add_argument("--deliver_email_from", default=None,
642  help="create and copy to /glusteruser/analyst/share_delivery/ a tar file of new and modified files and a text file with a list of files to be deleted, and email to swdevels@oceancolor.gsfc.nasa.gov from the email address you provide")
643  parser.add_argument("-t", "--tag", default=None,
644  help="tag that you want to install")
645  parser.add_argument("-i", "--install_dir", default=os.environ.get("OCSSWROOT", None),
646  help="root directory for bundle installation (default=$OCSSWROOT)")
647  parser.add_argument("-b", "--base_url", default=baseUrl,
648  help="remote url for the bundle server")
649  parser.add_argument("-l", "--local_dir", default=None,
650  help="local directory to use for bundle source instead of the bundle server")
651  parser.add_argument("-s", "--save_dir", default=None,
652  help="local directory to save a copy of the downloaded bundles")
653  parser.add_argument("-c", "--clean", default=False, action="store_true",
654  help="delete extra files in the destination directory")
655  parser.add_argument("--wget", default=False, action="store_true",
656  help="use wget for file download")
657  parser.add_argument("-a", "--arch", default=None,
658  help="use this architecture instead of guessing the local machine (linux_64 macosx_intel odps)")
659  parser.add_argument("-v", "--verbose", action="count", default=0, help="increase output verbosity")
660 
661  # add weird bundle switches
662  parser.add_argument("--bin", default=False, action="store_true",
663  help="install binary executables")
664  parser.add_argument("--opt", default=False, action="store_true",
665  help="install 3rd party programs and libs")
666  parser.add_argument("--src", default=False, action="store_true",
667  help="install source files")
668  parser.add_argument("--luts", default=False, action="store_true",
669  help="install LUT files")
670  parser.add_argument("--viirs_l1_bin", default=False, action="store_true",
671  help="install VIIRS binary executables subset")
672 
673  # add bundles from the bundle list
674  for bundleInfo in bundleList:
675  if bundleInfo["commandLine"]:
676  parser.add_argument("--" + bundleInfo["name"], default=False, action="store_true",
677  help="install " + bundleInfo["help"] + " files")
678 
679  # add argument
680  parser.add_argument("--direct_broadcast", default=False, action="store_true",
681  help="toggle on bundles needed for MODIS direct broadcast")
682  parser.add_argument("--seadas", default=False, action="store_true",
683  help="toggle on the base set of bundles for SeaDAS")
684  parser.add_argument("--odps", default=False, action="store_true",
685  help="toggle on the base set of bundles for ODPS systems")
686  parser.add_argument("--viirs_l1", default=False, action="store_true",
687  help="install everything to run and test the VIIRS executables")
688  parser.add_argument("--all", default=False, action="store_true",
689  help="toggle on all satellite bundles")
690 
691  options = parser.parse_args()
692 
693  # print version
694  if options.version:
695  print(os.path.basename(sys.argv[0]), versionString)
696  sys.exit(0)
697 
698  if options.list_tags:
699  listTags(options)
700  sys.exit(0)
701 
702  if options.installed_tag:
703  installedTag(options)
704  sys.exit(0)
705 
706  # make sure arch is set
707  if not options.arch:
708  if options.odps:
709  options.arch = "odps"
710  else:
711  options.arch = getArch()
712 
713  # fix bin dir
714  bundleName = "bin_" + options.arch
715  bundleInfo = findBundleInfo(bundleName)
716  bundleInfo["dir"] = "bin" # fix the bin dest directory
717 
718  # fix lib dir
719  bundleName = "lib_" + options.arch
720  bundleInfo = findBundleInfo(bundleName)
721  bundleInfo["dir"] = "lib" # fix the lib dest directory
722 
723  # fix opt dir
724  bundleName = "opt_" + options.arch
725  bundleInfo = findBundleInfo(bundleName)
726  bundleInfo["dir"] = "opt" # fix the opt dest directory
727 
728  if options.status:
729  options.diff = False
730  options.create_change = False
731  if status(options):
732  sys.exit(0)
733  else:
734  sys.exit(1)
735 
736  if options.difftool is not None:
737  options.diff = True
738 
739  if options.diff:
740  if status(options):
741  sys.exit(0)
742  else:
743  sys.exit(1)
744 
745  if options.deliver_change or options.deliver_email_from is not None:
746  options.create_change = True
747 
748  if options.create_change:
749  if status(options):
750  sys.exit(0)
751  else:
752  sys.exit(1)
753 
754  if not options.tag:
755  print("--tag is required")
756  sys.exit(1)
757 
758  if not options.install_dir:
759  print("--install_dir is required if OCSSWROOT enviroment variable is not set")
760  sys.exit(1)
761 
762  if options.update:
763  update(options)
764  sys.exit(0)
765 
766  # add convience arguments
767  if options.benchmark:
768  options.seadas = True
769  options.modisa = True
770 
771  if options.viirs_l1:
772  options.root = True
773  options.viirs_l1_bin = True
774  options.viirs_l1_benchmark = True
775  options.opt = True
776  options.luts = True
777  options.common = True
778  options.ocrvc = True
779  options.viirsn = True
780  options.viirsj1 = True
781  options.viirsdem = True
782 
783  if options.direct_broadcast:
784  options.seadas = True
785  options.modisa = True
786  options.modist = True
787 
788  if options.seadas:
789  options.root = True
790  options.bin =True
791  options.opt = True
792  options.luts = True
793  options.common = True
794  options.ocrvc = True
795 
796  if options.odps:
797  options.root = True
798  options.bin =True
799  options.opt = True
800  options.common = True
801  options.ocrvc = True
802  options.all = True
803 
804  if options.all:
805  for bundleInfo in bundleList:
806  if bundleInfo["commandLine"]:
807  if "share/" in bundleInfo["dir"]:
808  setattr(options, bundleInfo["name"], True)
809 
810  # unset silly sensors for ODPS
811  if options.odps:
812  options.opt_src = False
813  if hasattr(options, "benchmark"):
814  options.benchmark = False
815  if hasattr(options, "afrt"):
816  options.afrt = False
817  if hasattr(options, "aquaverse"):
818  options.aquaverse = False
819  options.avhrr = False
820  options.aviris = False
821  options.l5tm = False
822  options.l7etmp = False
823  if hasattr(options, "misr"):
824  options.misr = False
825  options.mos = False
826  options.msi = False
827  if hasattr(options, "msis2a"):
828  options.msis2a = False
829  options.msis2b = False
830  options.ocm1 = False
831  options.ocia = False
832  options.ocip = False
833  options.ocm2 = False
834  options.oli = False
835  if hasattr(options, "olil8"):
836  options.olil8 = False
837  options.olil9 = False
838  options.osmi = False
839  options.prism = False
840  options.sabiamar = False
841  options.sgli = False
842  if hasattr(options, "spexone_remotap"):
843  options.spexone_remotap = False
844  options.wv3 = False
845  options.benchmark = False
846  if hasattr(options, "viirs_l1_benchmark"):
847  options.viirs_l1_benchmark = False
848 
849  # take care of the sub-sensors
850  if options.modisa or options.modist:
851  options.modis = True
852  if hasattr(options, "msis2a"):
853  if options.msis2a or options.msis2b:
854  options.msi = True
855  if options.viirsn or options.viirsj1:
856  options.viirs = True
857  if hasattr(options, "viirsj2"):
858  if options.viirsj2:
859  options.viirs = True
860  if hasattr(options, "olcis3a"):
861  if options.olcis3a or options.olcis3b:
862  options.olci = True
863  if hasattr(options, "olil8"):
864  if options.olil8 or options.olil9:
865  options.oli = True
866  options.olil8 = True # This is needed since L9 is a symbolic link to L8 LUTS
867 
868  # make sure bin and viirs_l1_bin are not both set
869  if options.bin and options.viirs_l1_bin:
870  print("Error: Can not install --bin and --viirs_l1_bin")
871  sys.exit(1)
872 
873  # count the things we are going to install
874  totalNumThings = 0
875  for bundleInfo in bundleList:
876  if hasattr(options, bundleInfo["name"]) and getattr(options, bundleInfo["name"]):
877  totalNumThings += 1
878 
879  # count bin and lib bundles
880  if options.bin:
881  totalNumThings += 2
882 
883  # count viirs_l1_bin and lib bundles
884  if options.viirs_l1_bin:
885  totalNumThings += 2
886 
887  # count the opt bundle
888  if options.opt:
889  totalNumThings += 1
890 
891  # count source bundles (ocssw-src and opt/src)
892  if options.src:
893  totalNumThings += 2
894 
895  # record the installed tag if any bundles are getting installed
896  if totalNumThings > 0:
897  recordInstalledTag(options)
898 
899  # now install the bundles
900 
901  # do the weird bundles
902  if options.bin:
903  bundleName = "bin_" + options.arch
904  bundleInfo = findBundleInfo(bundleName)
905  installBundle(options, bundleInfo)
906 
907  if options.viirs_l1_bin:
908  bundleName = "viirs_l1_bin_" + options.arch
909  bundleInfo = findBundleInfo(bundleName)
910  if not bundleInfo:
911  print("Error: tag does not contain the viirs_l1_bin bundles")
912  sys.exit(1)
913  installBundle(options, bundleInfo)
914 
915  if options.bin or options.viirs_l1_bin:
916  bundleName = "lib_" + options.arch
917  bundleInfo = findBundleInfo(bundleName)
918  installBundle(options, bundleInfo)
919 
920  if options.opt:
921  bundleName = "opt_" + options.arch
922  bundleInfo = findBundleInfo(bundleName)
923  installBundle(options, bundleInfo)
924 
925  if options.src:
926  bundleInfo = findBundleInfo("opt_src")
927  installBundle(options, bundleInfo)
928  bundleInfo = findBundleInfo("ocssw_src")
929  installBundle(options, bundleInfo)
930 
931  # do the normal bundles
932  for bundleInfo in bundleList:
933  if hasattr(options, bundleInfo["name"]) and getattr(options, bundleInfo["name"]):
934  installBundle(options, bundleInfo)
935 
936  # update luts
937  if options.luts:
938  commonUpdated = False
939  for bundleInfo in bundleList:
940  if hasattr(options, bundleInfo["name"]) and getattr(options, bundleInfo["name"]):
941  if bundleInfo["name"] in lutBundles:
942  updateLuts(options, bundleInfo["name"])
943  commonUpdated = True
944  # make sure var/common gets updated
945  if not commonUpdated:
946  updateLuts(options, "common")
947 
948  print("Done\n")
949 
950 if __name__ == "__main__":
951  sys.exit(run())
def installedTag(options)
def listTags(options)
def getBundleListTag(manifestFilename)
def recordInstalledTag(options)
def update(options)
def findBundleInfo(bundleName)
character(len=1000) if
Definition: names.f90:13
def downloadBundleList(options)
def status(options)
def bundleStatus(options, bundleInfo, fileLst_chg, fileLst_relPath_chg, fileLst_del)
const char * str
Definition: l1c_msi.cpp:35
def downloadFile(options, tag, bundleInfo, fileName, destDir)
def runCommand(command, pipe=False, shellVal=False)
def getInstalledTag(options)
def installBundle(options, bundleInfo)
def updateLuts(options, lut)
def checkTag(options)