genmoduleidfzphash.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # usage:
  2. # genmoduleidfzphash.py -d [directory of fzp files] -o [output file name]
  3. # loads the each fzp file, extracts the moduleID, and saves the id-filename pair to the output file
  4. import getopt, sys, os, os.path, re, xml.dom.minidom, xml.dom
  5. def usage():
  6. print """
  7. usage:
  8. genmoduleidfzphash.py -d [directory of fzp files] -o [output file name]
  9. loads the each fzp file, extracts the moduleID, and saves the id-filename pair to the output file
  10. """
  11. def main():
  12. try:
  13. opts, args = getopt.getopt(sys.argv[1:], "hd:o:", ["help", "directory", "output"])
  14. except getopt.GetoptError, err:
  15. # print help information and exit:
  16. print str(err) # will print something like "option -a not recognized"
  17. usage()
  18. sys.exit(2)
  19. inputDir = None
  20. output = None
  21. for o, a in opts:
  22. #print o
  23. #print a
  24. if o in ("-d", "--directory"):
  25. inputDir = a
  26. elif o in ("-o", "--output"):
  27. output = a
  28. elif o in ("-h", "--help"):
  29. usage()
  30. sys.exit(2)
  31. else:
  32. assert False, "unhandled option"
  33. if(not(inputDir)):
  34. usage()
  35. sys.exit(2)
  36. if(not(output)):
  37. usage()
  38. sys.exit(2)
  39. outfile = open(output, 'wb')
  40. for fname in os.listdir(inputDir):
  41. if not fname.endswith(".fzp"):
  42. continue
  43. dom = xml.dom.minidom.parse(os.path.join(inputDir, fname))
  44. root = dom.documentElement
  45. id = root.getAttribute("moduleId")
  46. if (id):
  47. outfile.write(id)
  48. outfile.write(",")
  49. outfile.write(fname.replace(".fzp", ""))
  50. outfile.write("\n")
  51. outfile.flush()
  52. outfile.close()
  53. if __name__ == "__main__":
  54. main()