import sys
import os
import os.path
from optparse import OptionParser

#-------------------------------------------------------------------------------
# the main function
def main():
    # usage, parse parameters
    usage = "usage: %prog [options] arg"
    parser = OptionParser( usage )

    # option to debug and verbose
    parser.add_option( "-v", "--verbose",
        action="store_true", dest="verbose" )

    # options to control files
    parser.add_option( "-l", "--list", type="string", dest="directoryList", help="list of elastix output directories" )
    #parser.add_option( "-o", "--output", dest="output", help="output text file" )

    (options, args) = parser.parse_args()

    # Convert string -l to list
    dirList = options.directoryList.split( ' ' )
    # Add everything not processed
    dirList.extend( args );
    #print "dirList: %s" % dirList

    print "directory checksum"

    for directory in dirList:
      # Equivalent to: fileName = options.directory + "/" + "elastix.log"
      fileName = os.path.join( directory, "elastix.log" );

      # Read elastix.log and find last line with checksum
      try:
        f = open( fileName )
      except IOError as e:
        print "%s No elastix.log found" % directory
        continue

      for line in f:
        if "Registration result checksum:" in line:
          checksumline = line

      # Extract checksum
      checksum = checksumline.split(': ')[1].rstrip( "\n" );

      # Print result
      print "%s %s" % (directory, checksum)
      f.close();

    return 0

#-------------------------------------------------------------------------------
if __name__ == '__main__':
    sys.exit(main())

@@ -1293,24 +1293,54 @@
 continue%0D%0A%0D%0A
+      checksumFound = False;%0D%0A
       for li
@@ -1431,16 +1431,50 @@
 e = line
+;%0D%0A          checksumFound = True;
 %0D%0A%0D%0A    
@@ -1491,24 +1491,51 @@
 t checksum%0D%0A
+      if checksumFound:%0D%0A  
       checks
@@ -1592,16 +1592,18 @@
 %0D%0A      
+  
 # Print 
@@ -1610,16 +1610,18 @@
 result%0D%0A
+  
       pr
@@ -1657,16 +1657,65 @@
 cksum)%0D%0A
+      else:%0D%0A        print %22%25s -%22 %25 directory%0D%0A%0D%0A
       f.

check for existence of checksum before printing