distutils util miscellaneous utility functions anything that doesn t fit into one of the other util py modules return a string that identifies the current platform this is used mainly to distinguish platformspecific build directories and platformspecific built distributions typically includes the os name and version and the architecture as supplied by os uname although the exact information included depends on the os eg on linux the kernel version isn t particularly important examples of returned values linuxi586 linuxalpha solaris2 6sun4u windows will return one of winamd64 64bit windows on amd64 aka x8664 intel64 em64t etc win32 all others specifically sys platform is returned for other nonposix platforms currently just returns sys platform set for cross builds explicitly xxx what about the architecture nt is intel or alpha mac os is m68k or ppc etc try to distinguish various flavours of unix convert the os name to lowercase remove characters and translate spaces for power macintosh at least on linuxintel machine is the processor i386 etc xxx what about alpha sparc etc we can t use platform architecture0 because a bootstrap problem we use a dict to get an error if some suspicious happens fall through to standard osnamereleasemachine representation needed by splitquoted split a string up according to unix shelllike rules for quotes and backslashes in short words are delimited by spaces as long as those spaces are not escaped by a backslash or inside a quoted string single and double quotes are equivalent and the quote characters can be backslashescaped the backslash is stripped from any twocharacter escape sequence leaving only the escaped character the quote characters are stripped from any quoted string returns a list of words this is a nice algorithm for splitting up a single string since it doesn t require characterbycharacter examination it was a little bit of a brainbender to get it working right though will become part of the current word splitquoted return a string that identifies the current platform this is used mainly to distinguish platform specific build directories and platform specific built distributions typically includes the os name and version and the architecture as supplied by os uname although the exact information included depends on the os eg on linux the kernel version isn t particularly important examples of returned values linux i586 linux alpha solaris 2 6 sun4u windows will return one of win amd64 64bit windows on amd64 aka x86_64 intel64 em64t etc win32 all others specifically sys platform is returned for other non posix platforms currently just returns sys platform set for cross builds explicitly xxx what about the architecture nt is intel or alpha mac os is m68k or ppc etc try to distinguish various flavours of unix convert the os name to lowercase remove characters and translate spaces for power macintosh at least on linux intel machine is the processor i386 etc xxx what about alpha sparc etc sunos 5 solaris 2 we can t use platform architecture 0 because a bootstrap problem we use a dict to get an error if some suspicious happens fall through to standard osname release machine representation needed by split_quoted split a string up according to unix shell like rules for quotes and backslashes in short words are delimited by spaces as long as those spaces are not escaped by a backslash or inside a quoted string single and double quotes are equivalent and the quote characters can be backslash escaped the backslash is stripped from any two character escape sequence leaving only the escaped character the quote characters are stripped from any quoted string returns a list of words this is a nice algorithm for splitting up a single string since it doesn t require character by character examination it was a little bit of a brain bender to get it working right though unescaped unquoted whitespace now we definitely have a word delimiter preserve whatever is being escaped will become part of the current word slurp singly quoted string slurp doubly quoted string split_quoted
import os
import re
import string
import sys
from distutils.errors import DistutilsPlatformError

def get_host_platform():
    
    if os.name == 'nt':
        if 'amd64' in sys.version.lower():
            return 'win-amd64'
        if '(arm)' in sys.version.lower():
            return 'win-arm32'
        if '(arm64)' in sys.version.lower():
            return 'win-arm64'
        return sys.platform

    
    if "_PYTHON_HOST_PLATFORM" in os.environ:
        return os.environ["_PYTHON_HOST_PLATFORM"]

    if os.name != "posix" or not hasattr(os, 'uname'):
        
        
        return sys.platform

    

    (osname, host, release, version, machine) = os.uname()

    
    
    osname = osname.lower().replace('/', '')
    machine = machine.replace(' ', '_')
    machine = machine.replace('/', '-')

    if osname[:5] == "linux":
        
        
        
        return  "%s-%s" % (osname, machine)
    elif osname[:5] == "sunos":
        if release[0] >= "5":           
            osname = "solaris"
            release = "%d.%s" % (int(release[0]) - 3, release[2:])
            
            
            
            bitness = {2147483647:"32bit", 9223372036854775807:"64bit"}
            machine += ".%s" % bitness[sys.maxsize]
        
    elif osname[:3] == "aix":
        from _aix_support import aix_platform
        return aix_platform()
    elif osname[:6] == "cygwin":
        osname = "cygwin"
        rel_re = re.compile (r'[\d.]+', re.ASCII)
        m = rel_re.match(release)
        if m:
            release = m.group()
    elif osname[:6] == "darwin":
        import _osx_support, sysconfig
        osname, release, machine = _osx_support.get_platform_osx(
                                        sysconfig.get_config_vars(),
                                        osname, release, machine)

    return "%s-%s-%s" % (osname, release, machine)

def get_platform():
    if os.name == 'nt':
        TARGET_TO_PLAT = {
            'x86' : 'win32',
            'x64' : 'win-amd64',
            'arm' : 'win-arm32',
        }
        return TARGET_TO_PLAT.get(os.environ.get('VSCMD_ARG_TGT_ARCH')) or get_host_platform()
    else:
        return get_host_platform()



_wordchars_re = _squote_re = _dquote_re = None
def _init_regex():
    global _wordchars_re, _squote_re, _dquote_re
    _wordchars_re = re.compile(r'[^\\\'\"%s ]*' % string.whitespace)
    _squote_re = re.compile(r"'(?:[^'\\]|\\.)*'")
    _dquote_re = re.compile(r'"(?:[^"\\]|\\.)*"')

def split_quoted (s):
    

    
    
    
    if _wordchars_re is None: _init_regex()

    s = s.strip()
    words = []
    pos = 0

    while s:
        m = _wordchars_re.match(s, pos)
        end = m.end()
        if end == len(s):
            words.append(s[:end])
            break

        if s[end] in string.whitespace: 
            words.append(s[:end])       
            s = s[end:].lstrip()
            pos = 0

        elif s[end] == '\\':            
                                        
            s = s[:end] + s[end+1:]
            pos = end+1

        else:
            if s[end] == "'":           
                m = _squote_re.match(s, end)
            elif s[end] == '"':         
                m = _dquote_re.match(s, end)
            else:
                raise RuntimeError("this can't happen (bad char '%c')" % s[end])

            if m is None:
                raise ValueError("bad string (mismatched %s quotes?)" % s[end])

            (beg, end) = m.span()
            s = s[:beg] + s[beg+1:end-1] + s[end:]
            pos = m.end() - 2

        if pos >= len(s):
            words.append(s)
            break

    return words