get_ece_config_param.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. #! /usr/bin/env python
  2. """This script retrieves the value of a PARAMETER tag from either a MODEL or a
  3. PLATFORM in one XML file.
  4. Usage:
  5. get_ece_config_param.py XML MODEL|PLATFORM PARAMETER
  6. Example #1, retrieve the number of processor for IFS:
  7. get_ece_config_param.py config-run.xml IFS NUMPROC
  8. Example #2, retrieve the 'run dir' on the 'bi' platform, defined in the config-run.xml:
  9. get_ece_config_param.py config-run.xml bi RUN_DIR
  10. If not found or error, returns nothing and exits with return code 1.
  11. """
  12. from xml.etree import ElementTree, ElementInclude
  13. import xml.sax
  14. from io import StringIO
  15. import sys
  16. import os
  17. if __name__ == "__main__":
  18. if len(sys.argv) != 4:
  19. sys.stderr.write( "Must pass THREE arguments! Usage:")
  20. sys.stderr.write( " get_ece_config_param.py XMLFILE MODEL|PLATFORM PARAMETER")
  21. sys.exit(1)
  22. f = sys.argv[1]
  23. modelname = sys.argv[2]
  24. paramname = sys.argv[3]
  25. # -- PARSE FILE
  26. parser = xml.sax.make_parser()
  27. try:
  28. tree = ElementTree.parse(f)
  29. except IOError:
  30. sys.stderr.write( "Could not open file: %s\n" % f)
  31. sys.exit(1)
  32. except ElementTree.ParseError, v:
  33. row, column = v.position
  34. print "error on row", row, "column", column, ":", v
  35. sys.exit(1)
  36. except:
  37. sys.stderr.write( "Could not parse XML file: %s\n"+f)
  38. sys.exit(1)
  39. root = tree.getroot()
  40. ElementInclude.include(root)
  41. s = unicode(ElementTree.tostring(root))
  42. io = StringIO(s)
  43. parser.parse(io)
  44. # -- Get list of PARAMETER, first assuming it is a MODEL, if not it is a PLATFORM
  45. Params=[tag.findall('Parameter') for tag in root.findall('Model') if tag.get('name')==modelname]
  46. if not Params:
  47. Params=[tag.findall('Parameter') for tag in root.findall('Platform') if tag.get('name')==modelname]
  48. # -- Return value
  49. if Params:
  50. val=[k.find('Value').text for k in Params[0] if k.get('name') == paramname ]
  51. else:
  52. val=None
  53. if val:
  54. sys.stdout.write( val[0] )
  55. else:
  56. sys.stderr.write("Parameter '%s' or Model/Platform '%s' not found.\n" % (paramname,modelname))
  57. sys.exit(1)