Source

check_os / check_os4.py

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 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

class Result(object):
    def __init__(self):
        self.result = ''
        self.msg = ''
        self.os_type_str = ''
        self.sdr_type_str = ''
        self.os_langs = []
        self.sdr_langs = []
        self.os_lang = ''
        self.po = ''

def main():
    ""
    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')
    options, args = opts.parse_args()

    # Result defs
    res = Result()

    try:
        os_type = ostype.GetWindowsVersion()
        res.os_lang = ostype.GetWindowsLanguage()
        res.os_langs = ostype.GetWindowsMUI()
    except Exception, e:
        res.result = 'NG'
        res.msg = 'e'

    res.os_type_str = 'Windows ' + os_type[0] + ' ' + os_type[1] + \
                  (' ' + os_type[2] + ', ' if os_type[2] else ', ') + os_type[3]

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

    try:
        sdr_type = sdrobj.os_type()
        res.sdr_lang = sdrobj.os_lang()
        res.po = sdrobj.get_po()
    except Exception, e:
        res.result = 'NG'
        res.msg = e
        display(res)
        sys.exit(1)
 
    res.sdr_type_str = 'Windows ' + sdr_type[0] + ' ' + sdr_type[1] + \
                  (' ' + sdr_type[2] + ', ' if sdr_type[2] else ', ') + \
                  sdr_type[3]

    if sdrobj.is_mui():
        res.msg = "This is MUI order."
        res.sdr_langs = sdrobj.sdr_mui()
        res.result = 'OK' if res.os_langs == res.sdr_langs else 'NG'
    else:
        if options.CHECK_TABLE and not sdrobj.has_valid_srv():
            res.msg = "Invalid SRV found in the SDR"
            res.result = 'NG'
            display(res)
            sys.exit(1)

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

def display(res):
    "Display results in a web browser."
    tstr = '''<html>
<head><title>OS Check Result</title></head>
<body>

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

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

<hr>

% if res.os_type_str:
    <b>Installed OS:</b> ${res.os_type_str}
% endif

<br>

% if res.sdr_type_str:
    <b>Required OS:</b> ${res.sdr_type_str}
% endif

<hr>

<b>Installed languages:</b></br>
<ul>
% if res.os_langs:
    % for lang in res.os_langs:
        <%
            os_lang_str = lang[0] + '(' + lang[1] + ')'
        %>
        <li>${os_lang_str}</li>
    % endfor
% else:
    <%
        os_lang_str = res.os_lang[0] + '(' + res.os_lang[1] + ')'
    %>
    <li>${os_lang_str}</li>
% endif
</ul>

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

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

if __name__ == '__main__':
    main()