NASA Logo
Ocean Color Science Software

ocssw V2022
cdata-log.py
Go to the documentation of this file.
1 #! /usr/bin/env python3
2 
3 import os
4 import sys
5 import argparse
6 from pathlib import Path
7 
8 version = "2.0"
9 seperatorStr = "=============================================="
10 local = False
11 
13  line = ""
14 
15  # init the output dictionary
16  out = {
17  "valid": False, # did we fill up the record
18  "failed": False, # did the test Pass of Fail
19  "name": "", # name of the test
20  "log": [], # all of the lines of the test output
21  "command": "", # test command line
22  "dir": "", # dir the test was run from
23  "cmpDir": "", # dir nccmp was run from
24  "cdataDir": "", # dir to run cdata from
25  "newFile": [], # new files just created
26  "oldFile": [] # old test files use as comparison
27  }
28 
29  # find first line of test
30  while True:
31  line = fp.readline()
32  if not line:
33  return False
34  if " Testing: " in line:
35  parts = line.split()
36  if len(parts) == 3:
37  out["name"] = parts[2]
38  break
39 
40  # add log header
41  out["log"].append(seperatorStr)
42  out["log"].append(seperatorStr)
43  out["log"].append(seperatorStr)
44 
45  # search for command
46  while True:
47  line = fp.readline()
48  if not line:
49  return False
50  out["log"].append(line)
51  if line.startswith("Command: "):
52  parts = line.split(' ', 1)
53  out["command"] = parts[1]
54  break;
55 
56  # search for dir
57  while True:
58  line = fp.readline()
59  if not line:
60  return False
61  out["log"].append(line)
62  if line.startswith("Directory: "):
63  parts = line.split()
64  out["dir"] = parts[1]
65  break;
66 
67  # find new and old filenames
68  found = False
69  out["cmpDir"] = out["dir"]
70  parts = out["command"].split('&&')
71  for cmd in parts:
72  cmd = cmd.strip()
73  if cmd.startswith("cd "):
74  cmdParts = cmd.split()
75  newDir = cmdParts[1]
76  if newDir[0] != '/':
77  newPath = Path(out["cmpDir"]) / newDir
78  newDir = str(newPath.resolve())
79  out["cmpDir"] = newDir
80  elif cmd.startswith("nccmp ") or cmd.startswith("compare_hdf4 ") or cmd.startswith("diff ") or cmd.startswith("tiffcmp ") :
81  cmdParts = cmd.split()
82  out["newFile"].append(out["cmpDir"] + "/" + cmdParts[-1].strip().rstrip('"'))
83  out["oldFile"].append(out["cmpDir"] + "/" + cmdParts[-2].strip())
84  found = True
85  elif cmd.startswith("compare "):
86  cmdParts = cmd.split()
87  out["newFile"].append(out["cmpDir"] + "/" + cmdParts[-2].strip())
88  out["oldFile"].append(out["cmpDir"] + "/" + cmdParts[-3].strip())
89  found = True
90  if not found:
91  return out
92 
93  # figure out the cdata dir
94  line = out["cmpDir"]
95  parts = line.split("/")
96  if parts[-2] == "testdata":
97  out["cdataDir"] = line
98  elif parts[-3] == "testdata":
99  parts.pop(-1)
100  out["cdataDir"] = "/".join(parts)
101  else:
102  print("ERROR: can't find cdataDir")
103  print(" name =", out["name"])
104  print(" cmpDir =", out["cmpDir"])
105  print()
106  return out
107 
108  # delete cdataDir from old and new filenames
109  s = out["cdataDir"] + "/"
110  for i in range(len(out["newFile"])):
111  out["newFile"][i] = out["newFile"][i].replace(s, "")
112  out["oldFile"][i] = out["oldFile"][i].replace(s, "")
113 
114  # search for passed or failed
115  lastLine = ""
116  while True:
117  line = fp.readline()
118  if not line:
119  return False
120  out["log"].append(line)
121  if lastLine.startswith("-----------------------------------------"):
122  if line == "Test Passed.\n":
123  out["failed"] = False
124  break;
125  if line == "Test Failed.\n":
126  out["failed"] = True
127  break;
128  lastLine = line
129 
130  out["valid"] = True
131  return out
132 
133 
134 def runCmd(cmd):
135  print("+" + cmd)
136  os.system(cmd)
137 
138 
139 def update(fp):
140  cdataSet = set()
141  while(True):
142  # read the next test record
143  rec = read_next_test(fp)
144  if not rec:
145  break
146 
147  if rec["valid"] and rec["failed"]:
148 
149  cmdList = []
150  cmdList.append("cd " + rec["cdataDir"])
151  for i in range(len(rec["newFile"])):
152  cmdList.append("cp " + rec["newFile"][i] + " " + rec["oldFile"][i])
153  if not local:
154  cmdList.append("cdata add " + rec["oldFile"][i])
155 
156  for line in rec["log"]:
157  print(line.rstrip())
158  for line in cmdList:
159  print("+" + line)
160  print(seperatorStr)
161 
162  val = input("update(u)/ignore(i) (defaut=u): ")
163  if val == '' or val == 'u' or val == 'U':
164  print("updating")
165  runCmd("; ".join(cmdList))
166  cdataSet.add(rec["cdataDir"])
167  else:
168  print("ignoring")
169 
170  print()
171  if not local:
172  for line in list(cdataSet):
173  runCmd("cd " + line + "; cdata push")
174  return 0
175 
176 
177 def listFailed(fp):
178  while(True):
179  # read the next test record
180  rec = read_next_test(fp)
181  if not rec:
182  break
183 
184  if rec["valid"] and rec["failed"]:
185  for line in rec["log"]:
186  print(line.rstrip())
187  return 0
188 
189 
190 def main():
191  global local
192 
193  parser = argparse.ArgumentParser(prog="cdata-log")
194  parser.add_argument('--version', action='version', version='%(prog)s ' + version)
195  parser.add_argument('--list-failed', action='store_true', default=False,
196  help="list log of failed tests")
197  parser.add_argument('--local', action='store_true', default=False,
198  help="only copy updated test files to local dir")
199 
200  args = parser.parse_args()
201 
202  filename = "Testing/Temporary/LastTest.log"
203 
204  if not os.path.exists(filename):
205  print("ERROR:", filename, "does not exist")
206  print("ERROR: cdata-log must be run from the same directory as ctest")
207  return 1
208 
209  if args.local:
210  local = True
211 
212  with open(filename) as fp:
213  if args.list_failed:
214  return listFailed(fp)
215  else:
216  return update(fp)
217 
218 if __name__ == "__main__":
219  sys.exit(main())
def runCmd(cmd)
Definition: cdata-log.py:134
list(APPEND LIBS ${NETCDF_LIBRARIES}) find_package(GSL REQUIRED) include_directories($
Definition: CMakeLists.txt:8
void print(std::ostream &stream, const char *format)
Definition: PrintDebug.hpp:38
def update(fp)
Definition: cdata-log.py:139
def main()
Definition: cdata-log.py:190
def listFailed(fp)
Definition: cdata-log.py:177
while(++r<=NROOTS)
Definition: decode_rs.h:169
Definition: aerosol.c:136
def read_next_test(fp)
Definition: cdata-log.py:12