validate.py 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. #!/usr/bin/python
  2. import sys
  3. import os
  4. import getopt
  5. import subprocess
  6. import json
  7. from xml.dom.minidom import parse
  8. global target_mode
  9. # XSD validator from http://www.ltg.ed.ac.uk/~ht/xsv-status.html.
  10. xsv_path="c:\\Program Files\\XSV\\xsv.exe"
  11. if not(os.path.exists(xsv_path)):
  12. print("Can not find \""+xsv_path+"\"");
  13. print("Please download the XSV validator from ")
  14. print("http://www.ltg.ed.ac.uk/~ht/xsv-status.html")
  15. print("and install it in "+os.path.split(xsv_path)[0])
  16. sys.exit(2)
  17. schema_root="http://subversion.assembla.com/svn/IS29500/trunk/"
  18. #schema_root="file:///C:/Projects/schema/trunk/"
  19. validation_matrix={
  20. "image/png": None,
  21. "image/gif": None,
  22. "image/x-wmf": None,
  23. "image/x-emf": None,
  24. "image/jpeg": None,
  25. "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/wml.xsd"},
  26. "application/vnd.openxmlformats-officedocument.extended-properties+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/shared-documentPropertiesExtended.xsd"},
  27. "application/vnd.openxmlformats-package.core-properties+xml": {"xsd": schema_root+"Part2/OpenPackagingConventions-XMLSchema/opc-coreProperties.xsd"},
  28. "application/vnd.openxmlformats-officedocument.drawingml.diagramColors+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/dml-diagram.xsd"},
  29. "application/vnd.openxmlformats-officedocument.drawingml.diagramData+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/dml-diagram.xsd"},
  30. "application/vnd.ms-office.drawingml.diagramDrawing+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/dml-diagram.xsd"},
  31. "application/vnd.openxmlformats-officedocument.wordprocessingml.fontTable+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/wml.xsd"},
  32. "application/vnd.openxmlformats-officedocument.drawingml.diagramLayout+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/dml-diagram.xsd"},
  33. "application/vnd.openxmlformats-officedocument.drawingml.diagramStyle+xml": {"xsd": schema_root+"Part4/OfficeOpenXML-XMLSchema-Transitional/dml-diagram.xsd"}
  34. }
  35. def validate(path):
  36. print("=== validate: "+path)
  37. tmp_dir=os.path.abspath(os.path.join("win32", target_mode))
  38. mce_extract_path=os.path.abspath(os.path.join("win32", target_mode, "mce_extract.exe"))
  39. if os.path.exists(mce_extract_path):
  40. _args = [ mce_extract_path ]
  41. _args.append(path)
  42. sys.stderr.write("invoking "+os.path.split(_args[0])[1]+" for "+path+"..."); sys.stderr.flush()
  43. p=subprocess.Popen(_args, stdout=subprocess.PIPE)
  44. parts=eval(p.communicate()[0])
  45. for part in parts:
  46. if part["type"] in validation_matrix:
  47. validation_hint=None
  48. validation_hint=validation_matrix[part["type"]]
  49. if not(validation_hint is None):
  50. print "validating "+part["name"]+" of type "+part["type"]
  51. validation_error=True
  52. validation_msg="Internal error invoking validator"
  53. _args = [ mce_extract_path ]
  54. _args.append(path)
  55. _args.append(part["name"])
  56. # _args.append("--understands"); _args.append("http://schemas.microsoft.com/office/word/2010/wordml")
  57. tmp_file=os.path.join(tmp_dir, "temp.xml")
  58. out_file=os.path.join(tmp_dir, "out.xml")
  59. if os.path.exists(tmp_file):
  60. os.remove(tmp_file)
  61. _args.append("--out"); _args.append(tmp_file)
  62. if True or not(os.path.exists(tmp_file)):
  63. sys.stderr.write("invoking "+os.path.split(_args[0])[1]+" for "+part["name"]+"..."); sys.stderr.flush()
  64. ret=subprocess.call(_args)
  65. if os.path.exists(tmp_file):
  66. if os.path.exists(out_file):
  67. os.remove(out_file)
  68. if not(os.path.exists(out_file)):
  69. _args = [ xsv_path ]
  70. _args.append("-o"); _args.append(os.path.relpath(out_file));
  71. _args.append(os.path.relpath(tmp_file).replace('\\', '/'))
  72. _args.append(validation_hint["xsd"])
  73. sys.stderr.write("invoking "+os.path.split(_args[0])[1]+" for "+part["name"]+"..."); sys.stderr.flush()
  74. returncode=subprocess.call(_args)
  75. sys.stderr.write("done\n"); sys.stderr.flush()
  76. if 0==returncode:
  77. result=parse(out_file)
  78. validation_error=not(result.documentElement.tagName == "xsv"
  79. and "true"==result.documentElement.getAttribute("instanceAssessed")
  80. and "0"==result.documentElement.getAttribute("instanceErrors")
  81. and "0"==result.documentElement.getAttribute("schemaErrors"))
  82. else:
  83. validation_msg="error executing "+os.path.split(_args[0])[1]
  84. if validation_error:
  85. validation_msg=result.toxml()
  86. if validation_error:
  87. print "**ERROR validating "+part["name"]
  88. print validation_msg
  89. else:
  90. print "ignoring "+part["name"]+" of type "+part["type"]
  91. else:
  92. print "skipping "+part["name"]+" of type "+part["type"]
  93. sys.stdout.flush()
  94. else:
  95. print "can not find mce_extract tool!"
  96. def usage():
  97. print("usage:")
  98. print(" validate [--target=mode \"debug|release\"] sample.docx")
  99. if __name__ == "__main__":
  100. sys.path.append(os.path.abspath("third_party"))
  101. target_mode=None
  102. input=[]
  103. try:
  104. opts, args = getopt.getopt(sys.argv[1:], "h", ["help", "target=", "generate=", "skip", "type="])
  105. except getopt.GetoptError, err:
  106. # print help information and exit:
  107. print str(err) # will print something like "option -a not recognized"
  108. usage()
  109. sys.exit(2)
  110. for o, a in opts:
  111. if o in ("-h", "--help"):
  112. usage()
  113. sys.exit(0)
  114. elif o in ("--target"):
  115. target_mode=a
  116. elif o in ("--type"):
  117. target_mode=os.path.join(target_mode, a)
  118. if None==target_mode:
  119. print "please specify target mode, e.g. --target=debug"
  120. sys.exit(2)
  121. for f in args:
  122. validate(f)