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