1. Wang Dingwei
  2. check_os

Source

check_os / sdrparse.py

import re
import srvdesc

lang_list = [('ARA', 'Arabic', []),
             ('BPOR', 'Portuguese', ['Brazil']),
             ('BULG', 'Bulgarian', []),
             ('CRO', 'Crotian', []),
             ('CZE', 'Czech', []),
             ('DAN', 'Danish', []),
             ('DUT', 'Dutch', []),
             ('ENG', 'English', []),
             ('FIN', 'Finnish', []),
             ('FR', 'French', []),
             ('GER', 'German', []),
             ('GRE', 'Greek', []),
             ('HEB', 'Hebrew', []),
             ('HUN', 'Hungarian', []),
             ('IPOR', 'Portuguese', ['Portugal']),
             ('ITL', 'Italian', []),
             ('JPN', 'Japanese', []),
             ('KOR', 'Korean', []),
             ('NOR', 'Norwegian', []),
             ('NORW', 'Norwegian', []),
             ('POL', 'Polish', []),
             ('ROMA', 'Romanian', []),
             ('RUS', 'Russian', []),
             ('SCHI', 'Chinese', ['PRC', 'Singapore']),
             ('SLK', 'Slovak', []),
             ('SLN', 'Slovenian', []),
             ('SPN', 'Spanish', []),
             ('SRB', 'Serbrian', []),
             ('SWE', 'Swedish', []),
             ('TCHI', 'Chinese', ['Taiwan']),
             ('TCHK', 'Chinese', ['Hong Kong SAR']),
             ('THAI', 'Thai', []),
             ('TUR', 'Turkish', []),
             #('MUI,CANADA', 'Canadian MUI', []),
             #('MUI,EMF,NORDIC', 'Nordic MUI', []),
             #('MUI,EMF,SWI', 'Swiss MUI', []),
             #('MUI,EMF,BNLX', 'Benelux MUL', []),
             #('MUI,EMF,EURC', 'EMEA MUI', [])
             ]

os_desc_dict = {'WXPPSP3': ('XP', 'Professional', 'SP3', '32-bit'),
                'WXPHSP3': ('XP', 'Home', 'SP3', '32-bit'),
                'VHB32SP2': ('Vista', 'Home Basic', 'SP2', '32-bit'),
                'VHP32SP2': ('Vista', 'Home Premium', 'SP2', '32-bit'),
                'VB32SP2': ('Vista', 'Business', 'SP2', '32-bit'),
                'VU32SP2': ('Vista', 'Ultimate', 'SP2', '32-bit'),
                'VHB64SP2': ('Vista', 'Home Basic', 'SP2', '64-bit'),
                'VHP64SP2': ('Vista', 'Home Premium', 'SP2', '64-bit'),
                'VB64SP2': ('Vista', 'Business', 'SP2', '64-bit'),
                'VU64SP2': ('Vista', 'Ultimate', 'SP2', '64-bit'),
                'W7S32': ('7', 'Starter', None, '32-bit'),
                'W7HB32': ('7', 'Home Basic', None, '32-bit'),
                'W7HP32': ('7', 'Home Premium', None, '32-bit'),
                'W7P32': ('7', 'Professional', None, '32-bit'),
                'W7U32': ('7', 'Ultimate', None, '32-bit'),
                'W7HB64': ('7', 'Home Basic', None, '64-bit'),
                'W7HP64': ('7', 'Home Premium', None, '64-bit'),
                'W7P64': ('7', 'Professional', None, '64-bit'),
                'W7U64': ('7', 'Ultimate', None, '64-bit'),
                }


class SDR:
    def __init__(self, sdr):
        f = open(sdr)
        self.sdr = [line.strip() for line in f.readlines()]
        f.close()

    def get_po(self):
        "Returns PO number of the SDR if exists."
        po_ = self.sdr[-1].split(' ')[2]
        if po_.isdigit(): return po_

    def is_mui(self):
        "Returns True if is MUI order."
        return True if len(self.mui_parts()) > 0 else False

    def mui_parts(self):
        "Returns a list of mui tuples (PN, LANG) of the sdr"
        muilist = []
        re_mui = re.compile(r'^INFO ([0-9A-Z]{5}) INFO,XTRA-LANG@V,([A-Z]+)')
        for s in self.sdr:
            if s.startswith('INFO'):
                matchobj = re_mui.match(s)
                if matchobj:
                    muilist.append((matchobj.group(1), matchobj.group(2)))
        return muilist

    def sdr_mui(self):
        s = [get_sdr_lang(x[1]) for x in self.mui_parts()]
        s.sort(key=lambda x: x[0])
        return s

    def os_srv(self, valid=False):
        "Returns first 'SI SW' part in the SDR, which should be the OS part."
        for item in self.sdr:
            if item.startswith('SI SW'):
                return item
        return None

    def has_valid_srv(self):
        "Checks the srv description, to see if it matches certain format."
        srvli = srvdesc.srv_desc    # !!! srv_desc may not be acurate
        for srv, desc in srvli:
            fulldesc = 'SI SW %s %s' % (srv, desc)
            if fulldesc in self.os_srv():   # !!! the two may not be equal
                return True
        return False

    def os_info(self):
        "os_info() -> (OS_SRV, OS_TYPE, OS_LANG)"
        re_xp_sp3 = re.compile(r'SI SW ([0-9A-Z]{5}) SRV,OS,(WXP[HP]SP3),128,([A-Z]+)')
        re_vista_sp2 = re.compile(r'SI SW ([0-9A-Z]{5}) SRV,OS,(V(?:HB|HP|B|U)(?:32|64)SP2),([A-Z]+)')
        re_win7 = re.compile(r'SI SW ([0-9A-Z]{5}) SRV,OS,(W7(?:S|HB|HP|P|U)(?:32|64)),([A-Z]+)')

        for r in (re_win7, re_vista_sp2, re_xp_sp3):
            matchobj = r.match(self.os_srv())
            if matchobj:
                return matchobj.groups()
        raise ValueError("Unable to get OS info from SDR")

    def os_pn(self):
        return get_sdr_os(self.os_info()[0])

    def os_type(self):
        return get_sdr_os(self.os_info()[1])

    def os_lang(self):
        "Returns (os, region) tuple for desc"
        if self.is_mui():
            return ('MUI', ['Multi-Language'])
        else:
            get_sdr_os(self.os_info()[2])


def get_sdr_os(desc):
    "Returns a tuple of OS information according to the SDR"
    try:
        return os_desc_dict[desc]
    except:
        raise IndexError("Unable to get OS info from SDR")

def get_sdr_lang(desc):
    for (s, d, r) in lang_list:
        if desc == s:
            return (d, r)
    raise IndexError("Unable to find %s in language list" % desc)

if __name__ == '__main__':
    for v in os_desc_dict.itervalues():
        assert(len(v) == 4)
    for i in lang_list:
        assert(len(i) == 3)
        assert(isinstance(i[0], str))
        assert(isinstance(i[1], str))
        assert(isinstance(i[2], list))
        for j in i[2]:
            assert(isinstance(j, str))