PageRenderTime 24ms CodeModel.GetById 4ms RepoModel.GetById 1ms app.codeStats 0ms

/indra/lib/python/indra/util/test_win32_manifest.py

https://bitbucket.org/lindenlab/viewer-beta/
Python | 146 lines | 92 code | 27 blank | 27 comment | 13 complexity | 670ac8040e740db6202fe8d1825f1f14 MD5 | raw file
Possible License(s): LGPL-2.1
  1. #!/usr/bin/env python
  2. """\
  3. @file test_win32_manifest.py
  4. @brief Test an assembly binding version and uniqueness in a windows dll or exe.
  5. $LicenseInfo:firstyear=2009&license=viewerlgpl$
  6. Second Life Viewer Source Code
  7. Copyright (C) 2009-2011, Linden Research, Inc.
  8. This library is free software; you can redistribute it and/or
  9. modify it under the terms of the GNU Lesser General Public
  10. License as published by the Free Software Foundation;
  11. version 2.1 of the License only.
  12. This library is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. Lesser General Public License for more details.
  16. You should have received a copy of the GNU Lesser General Public
  17. License along with this library; if not, write to the Free Software
  18. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  20. $/LicenseInfo$
  21. """
  22. import sys, os
  23. import tempfile
  24. from xml.dom.minidom import parse
  25. class AssemblyTestException(Exception):
  26. pass
  27. class NoManifestException(AssemblyTestException):
  28. pass
  29. class MultipleBindingsException(AssemblyTestException):
  30. pass
  31. class UnexpectedVersionException(AssemblyTestException):
  32. pass
  33. class NoMatchingAssemblyException(AssemblyTestException):
  34. pass
  35. def get_HKLM_registry_value(key_str, value_str):
  36. import _winreg
  37. reg = _winreg.ConnectRegistry(None, _winreg.HKEY_LOCAL_MACHINE)
  38. key = _winreg.OpenKey(reg, key_str)
  39. value = _winreg.QueryValueEx(key, value_str)[0]
  40. #print 'Found: %s' % value
  41. return value
  42. def find_vc_dir():
  43. supported_versions = (r'8.0', r'9.0')
  44. supported_products = (r'VisualStudio', r'VCExpress')
  45. value_str = (r'ProductDir')
  46. for product in supported_products:
  47. for version in supported_versions:
  48. key_str = (r'SOFTWARE\Microsoft\%s\%s\Setup\VC' %
  49. (product, version))
  50. try:
  51. return get_HKLM_registry_value(key_str, value_str)
  52. except WindowsError, err:
  53. x64_key_str = (r'SOFTWARE\Wow6432Node\Microsoft\VisualStudio\%s\Setup\VS' %
  54. version)
  55. try:
  56. return get_HKLM_registry_value(x64_key_str, value_str)
  57. except:
  58. print >> sys.stderr, "Didn't find MS %s version %s " % (product,version)
  59. raise
  60. def find_mt_path():
  61. vc_dir = find_vc_dir()
  62. mt_path = '\"%sbin\\mt.exe\"' % vc_dir
  63. return mt_path
  64. def test_assembly_binding(src_filename, assembly_name, assembly_ver):
  65. print "checking %s dependency %s..." % (src_filename, assembly_name)
  66. (tmp_file_fd, tmp_file_name) = tempfile.mkstemp(suffix='.xml')
  67. tmp_file = os.fdopen(tmp_file_fd)
  68. tmp_file.close()
  69. mt_path = find_mt_path()
  70. resource_id = ""
  71. if os.path.splitext(src_filename)[1].lower() == ".dll":
  72. resource_id = ";#2"
  73. system_call = '%s -nologo -inputresource:%s%s -out:%s > NUL' % (mt_path, src_filename, resource_id, tmp_file_name)
  74. print "Executing: %s" % system_call
  75. mt_result = os.system(system_call)
  76. if mt_result == 31:
  77. print "No manifest found in %s" % src_filename
  78. raise NoManifestException()
  79. manifest_dom = parse(tmp_file_name)
  80. nodes = manifest_dom.getElementsByTagName('assemblyIdentity')
  81. versions = list()
  82. for node in nodes:
  83. if node.getAttribute('name') == assembly_name:
  84. versions.append(node.getAttribute('version'))
  85. if len(versions) == 0:
  86. print "No matching assemblies found in %s" % src_filename
  87. raise NoMatchingAssemblyException()
  88. elif len(versions) > 1:
  89. print "Multiple bindings to %s found:" % assembly_name
  90. print versions
  91. print
  92. raise MultipleBindingsException(versions)
  93. elif versions[0] != assembly_ver:
  94. print "Unexpected version found for %s:" % assembly_name
  95. print "Wanted %s, found %s" % (assembly_ver, versions[0])
  96. print
  97. raise UnexpectedVersionException(assembly_ver, versions[0])
  98. os.remove(tmp_file_name)
  99. print "SUCCESS: %s OK!" % src_filename
  100. print
  101. if __name__ == '__main__':
  102. print
  103. print "Running test_win32_manifest.py..."
  104. usage = 'test_win32_manfest <srcFileName> <assemblyName> <assemblyVersion>'
  105. try:
  106. src_filename = sys.argv[1]
  107. assembly_name = sys.argv[2]
  108. assembly_ver = sys.argv[3]
  109. except:
  110. print "Usage:"
  111. print usage
  112. print
  113. raise
  114. test_assembly_binding(src_filename, assembly_name, assembly_ver)