Source

check_os / check_os4

Full commit
#!python
#
# Author: Joel Wang <Joel_Wang@wistron.com>
#
# History:
# 2010-09-17, V4.0, output as html, opens in browser instead of cmd
# 2010-08-25, V3.0, colorful output
# 2010-06-19, V2.1, fixed MUI logic. Added py2exe support.
# 2010-05-24, V2.0, code refactored.
# 2010-05-20, V1.4, better mui support.
# 2010-05-20, V1.3, fixed spanish OS description, more error check code added.
# 2010-05-19, V1.2, read system language from registry instead of locale
# settings.
# 2010-05-10, V1.1, minor tweaks, now it uses optparse module.
# 2010-05-06, V1.0, first release.
#

"""
Compares Installed OS information with SDR information. Reports failure if they
are not matched.
"""

import os
import sys
import optparse
import msvcrt
import ostype
import sdrparse
import tempfile
import webbrowser
from mako.template import Template

version = '4.0'

def lang_matched(sdr_lang, os_lang):
    if sdr_lang[0] == os_lang[0]:
        if not sdr_lang[1] or os_lang[1] in sdr_lang[1]:
            return True
        else:
            return False
    else:
        return False

def mui_matched(os_mui, sdr_mui):
    "Compares all mui parts with registry info, return True if OK."
    if len(os_mui) == len(sdr_mui):
        zipped = zip(sdr_mui, os_mui)
        for (sdr_lang, os_lang) in zipped:
            if not lang_matched(sdr_lang, os_lang):
                return False
        return True
    else:
        return False

def main():
    global VERBOSE, DEBUG

    VERBOSE = False
    USAGE = 'usage: %prog [options]'

    # Parsing arguments
    opts = optparse.OptionParser(usage=USAGE, version=version)
    opts.add_option('-c', '--check-table', action="store_true",
            dest='CHECK_TABLE')
    opts.add_option('-v', '--verbose', action="store_true", dest='VERBOSE')
    opts.add_option('-d', '--debug', action="store_true", dest='DEBUG')
    options, args = opts.parse_args()
    VERBOSE = options.VERBOSE
    DEBUG = options.DEBUG

    # Result defs
    global result, msg, os_type, sdr_type, os_langs, sdr_langs, os_lang, po
    result = ''
    msg = ''

    # Read SDR information
    sdr_path = 'C:\\dell.sdr'
    try:
        sdrobj = sdrparse.SDR(sdr_path)
    except Exception, e:
        result = 'NG'
        msg = e
        display()
        sys.exit(1)

    os_type = ostype.GetWindowsVersion()
    os_lang = ostype.GetWindowsLanguage()
    os_mui = ostype.GetWindowsMUI()
    
    sdr_type = sdrobj.os_type()
    sdr_lang = sdrobj.os_lang()
    po = sdrobj.get_po()
    
    if sdrobj.is_mui():
        msg = "This is MUI order."
        if os_mui == sdrobj.sdr_mui():
            result = 'OK'
        else:
            result = 'NG'    
        sdr_langs = sdrobj.sdr_mui()
        os_langs = os_mui
    else:
        if options.CHECK_TABLE and not sdrobj.has_valid_srv():
            msg = "Invalid SRV found in the SDR"
            result = 'NG'
            display()
            sys.exit(1)

        if (os_type == sdr_type and 
            lang_matched(sdr_lang, os_lang)):
            result = 'OK'
        else:
            result = 'NG'
        sdr_langs = [sdr_lang]
        os_langs = [os_lang]
        

    display()

def display():

    tstr = '''<html>
<head><title>OS Check Result</title></head>
<body>

% if result == 'OK':
    <b style="font-size:96px; color:green">${result}</b>
% elif result == 'NG':
    <b style="font-size:96px; color:red">${result}</b>
% else:
    <b style="font-size:96px; color:black">${result}</b>
% endif
<br>
% if msg:
    <b>${msg}</b><br>
% endif

% if po:
    <b>PO:</b> ${po}<br>
% endif

<hr>

<%
    os_type_str = 'Windows ' + os_type[0] + ' ' + os_type[1] + \
                  (' ' + os_type[2] + ', ' if os_type[2] else ', ') + os_type[3]
%>

<%
    sdr_type_str = 'Windows ' + sdr_type[0] + ' ' + sdr_type[1] + \
                  (' ' + sdr_type[2] + ', ' if sdr_type[2] else ', ') + \
                  sdr_type[3]
%>

<b>Installed OS:</b> ${os_type_str}
<br>
<b>Required OS:</b> ${sdr_type_str}
<hr>

<b>Installed languages:</b></br>
<ul>
% if os_langs:
    % for lang in os_langs:
        <%
            os_lang_str = lang[0] + '(' + lang[1] + ')'
        %>
        <li>${os_lang_str}</li>
    % endfor
% else:
    <%
        os_lang_str = os_lang[0] + '(' + os_lang[1] + ')'
    %>
    <li>${os_lang_str}</li>
% endif
</ul>
<b>Required languages:</b></br>
<ul>
% for lang in sdr_langs:
    <%
        region = ' ' + '(' + ','.join(lang[1]) + ')' if lang[1] else ''
        lang_str = lang[0] + region
    %>
    <li>${lang_str}</li>
% endfor
</ul>
</body></html>'''

    tmpdir = tempfile.mkdtemp()
    html = os.path.join(tmpdir, 'osinfo.htm')
    templ = Template(tstr)
    
    with open(html, 'w') as f:
        f.write(templ.render(**globals()))
    webbrowser.open(html)

if __name__ == '__main__':
    main()