pypy / pypy / translator / cli /

import os.path
import platform
import py

class AbstractSDK(object):
    def _check_helper(cls, helper):
        if py.path.local.sysfind(helper) is None:
            py.test.skip("%s is not on your path." % helper)
            return helper
    _check_helper = classmethod(_check_helper)

    def runtime(cls):
        for item in cls.RUNTIME:
        return cls.RUNTIME
    runtime = classmethod(runtime)

    def ilasm(cls):
        return cls._check_helper(cls.ILASM)
    ilasm = classmethod(ilasm)

    def csc(cls):
        return cls._check_helper(cls.CSC)
    csc = classmethod(csc)

    def peverify(cls):
        return cls._check_helper(cls.PEVERIFY)
    peverify = classmethod(peverify)

class MicrosoftSDK(AbstractSDK):
    RUNTIME = []
    ILASM = 'ilasm'    
    CSC = 'csc'
    PEVERIFY = 'peverify'

def get_mono_version():
    from commands import getoutput
    lines = getoutput('mono -V').splitlines()
    parts = lines[0].split()
    # something like ['Mono', 'JIT', 'compiler', 'version', '', ...]
    iversion = parts.index('version')
    ver = parts[iversion+1]     # ''
    ver = ver.split('.')        # ['2', '4', '2', '3']
    return tuple(map(int, ver)) # (2, 4, 2, 3)

class MonoSDK(AbstractSDK):
    RUNTIME = ['mono']
    ILASM = 'ilasm2'
    CSC = 'gmcs'
    PEVERIFY = 'peverify' # it's not part of mono, but we get a meaningful skip message

    # this is a workaround for this bug:
    # they promised that it
    # should be fixed in versions after 2.4.3.x, in the meanwhile pass
    # -O=-branch
    def runtime(cls):
        ver = get_mono_version()
        if (2, 1) < ver < (2, 4, 3):
            return ['mono', '-O=-branch']
        return ['mono']

def key_as_dict(handle):
    import _winreg
    i = 0
    res = {}
    while True:
            name, value, type_ = _winreg.EnumValue(handle, i)
            res[name] = value
            i += 1
        except WindowsError:
    return res

def find_mono_on_windows():
    if platform.system() != 'Windows':
        return None
    import _winreg
        hMono = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "Software\\Novell\\Mono")
    except WindowsError: # mono seems not installed
        return None

    mono = key_as_dict(hMono)
    mono_version = mono.get('DefaultCLR', None)
    if mono_version is None:
        return None

    hMono_data = _winreg.OpenKey(_winreg.HKEY_LOCAL_MACHINE, "Software\\Novell\\Mono\\%s" % mono_version)
    mono_data = key_as_dict(hMono_data)
    mono_dir = str(mono_data['SdkInstallRoot'])
    return os.path.join(mono_dir, 'bin')

def get_default_SDK():
    if platform.system() == 'Windows':
        SDK = MicrosoftSDK
        # if present, use mono ilasm2 instead of MS ilasm
        mono_bin = find_mono_on_windows()
        if mono_bin is not None:
            SDK.ILASM = os.path.join(mono_bin, 'ilasm2.bat')
        # XXX the failing tests are boring, and the SDK is usually installed
        # on windows. I do not care right now, because the Linux buildbots
        # don't test this at all...
        if platform.architecture()[0] == '64bit':
            py.test.skip('mono on 64bit is not well enough supported')
        SDK = MonoSDK
    return SDK

SDK = get_default_SDK()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.