예제 #1
0
def _build_platforms(platforms, restrict=True):
    results = []
    checksums = dict([(p['id'], p['sha256']) for p in get_platform_list()])
    n = len(platforms)
    if restrict:
        features = [7, 3] if (pytransform.version_info()[2] & 2) else [5, 4, 0]
    else:
        features = [7, 3, 5, 4, 0]
    for platid in platforms:
        if (n > 1) and os.path.isabs(platid):
            raise RuntimeError('Invalid platform `%s`, for multiple platforms '
                               'it must be `platform.machine`' % platid)
        if (n > 1) and platid.startswith('vs2015.'):
            raise RuntimeError('The platform `%s` does not work '
                               'in multiple platforms target' % platid)
        filename = _get_platform_library_filename(platid, features)
        if filename is None:
            logging.info('No dynamic library found for %s with features %s',
                         platid, features)
            download_pytransform(platid)
            filename = _get_platform_library_filename(platid, features)
            if filename is None:
                raise RuntimeError('No dynamic library found for %s '
                                   'with features %s' % (platid, features))

        if platid in checksums:
            with open(filename, 'rb') as f:
                data = f.read()
            if hashlib.sha256(data).hexdigest() != checksums[platid]:
                logging.info('The platform %s is out of date', platid)
                download_pytransform(platid)
        results.append(filename)

    logging.debug('Target dynamic library: %s', results)
    return results
예제 #2
0
def _build_platforms(platforms, restrict=True, supermode=False):
    results = []
    flist1 = [7, 3]
    flist2 = [5, 4, 0]

    checksums = dict([(p['id'], p['sha256']) for p in get_platform_list()])
    n = len(platforms)

    t = pytransform.version_info()[-1]
    if supermode:
        features = [11] if (t & FEATURE_JIT) else [8]
    elif restrict:
        features = flist1 if (t & FEATURE_JIT) else flist2
    else:
        features = flist1 + flist2

    for platid in platforms:
        if (n > 1) and os.path.isabs(platid):
            raise RuntimeError('Invalid platform `%s`, for multiple platforms '
                               'it must be `platform.machine`' % platid)
        if (n > 1) and platid.startswith('vs2015.'):
            raise RuntimeError('The platform `%s` does not work '
                               'in multiple platforms target' % platid)
        filename = _get_library_filename(platid, features, supermode)
        if filename is None:
            download_pytransform(platid)
            filename = _get_library_filename(platid, features, supermode)
            if filename is None:
                raise RuntimeError('No dynamic library found for %s with '
                                   'features %s' % (platid, features))

        if not supermode:
            if filename.startswith(PLATFORM_PATH):
                features = flist1
            else:
                t = int(os.path.basename(os.path.dirname(filename)))
                features[:] = flist1 if t & FEATURE_JIT else flist2

        if platid in checksums:
            with open(filename, 'rb') as f:
                data = f.read()
            if hashlib.sha256(data).hexdigest() != checksums[platid]:
                logging.info('The platform %s is out of date', platid)
                download_pytransform(platid)
        results.append(filename)

    logging.debug('Target dynamic library: %s', results)
    return results
예제 #3
0
파일: utils.py 프로젝트: kalufinnle/pyarmor
def _get_platform_library_filename(platid):
    if os.path.isabs(platid):
        plist = [platid]
    else:
        n = pytransform.version_info()[2]
        t = list(platid.split('.'))
        plist = [os.path.join(PLATFORM_PATH, *t)] if n & 2 else []
        if len(t) == 2:
            t.append(n)
            for k in ([3, 7] if n & 2 else [0, 4, 5]):
                t[-1] = str(k)
                plist.append(os.path.join(CROSS_PLATFORM_PATH, *t))
        else:
            plist.append(os.path.join(CROSS_PLATFORM_PATH, *t))

    for path in plist:
        if not os.path.exists(path):
            continue
        for x in os.listdir(path):
            if x.startswith('_pytransform.'):
                return os.path.join(path, x)
예제 #4
0
파일: utils.py 프로젝트: kalufinnle/pyarmor
def pytransform_bootstrap(capsule=None):
    logging.debug('PyArmor installation path: %s', PYARMOR_PATH)
    logging.debug('PyArmor data path: %s', DATA_PATH)
    path = PYARMOR_PATH
    licfile = os.path.join(path, 'license.lic')
    if not os.path.exists(licfile):
        if not os.getenv('HOME', os.getenv('USERPROFILE')):
            logging.info('Create trial license file: %s', licfile)
            shutil.copy(os.path.join(path, 'license.tri'), licfile)
        else:
            licfile = os.path.join(DATA_PATH, 'license.lic')
            if not os.path.exists(licfile):
                if not os.path.exists(DATA_PATH):
                    logging.info('Create pyarmor data path: %s', DATA_PATH)
                    os.makedirs(DATA_PATH)
                logging.info('Create trial license file: %s', licfile)
                shutil.copy(os.path.join(path, 'license.tri'), licfile)

    libname = dll_name + dll_ext
    platid = pytransform.format_platform()
    logging.debug('Native platform is %s', _format_platid(platid))

    if os.getenv('PYARMOR_PLATFORM'):
        p = os.getenv('PYARMOR_PLATFORM')
        logging.info('PYARMOR_PLATFORM is set to %s', p)
        platid = os.path.join(*os.path.normpath(p).split('.'))
        logging.debug('Build platform is %s', _format_platid(platid))

    if os.path.isabs(platid):
        if not os.path.exists(os.path.join(platid, dll_name)):
            raise RuntimeError('No dynamic library found at %s', platid)
    else:
        libpath = PLATFORM_PATH
        logging.debug('Search dynamic library in the path: %s', libpath)
        if not os.path.exists(os.path.join(libpath, platid, libname)):
            libpath = CROSS_PLATFORM_PATH
            logging.debug('Search dynamic library in the path: %s', libpath)
            if not os.path.exists(os.path.join(libpath, platid, libname)):
                found = _search_downloaded_files(libpath, platid, libname)
                if found:
                    logging.debug('Found available dynamic library %s', found)
                    platid = found
                else:
                    if not os.path.exists(libpath):
                        logging.info('Create cross platform libraries path %s',
                                     libpath)
                        os.makedirs(libpath)
                    rid = download_pytransform(platid, libpath, index=0)[0]
                    platid = os.path.join(*rid.split('.'))
        if libpath == CROSS_PLATFORM_PATH:
            platid = os.path.abspath(os.path.join(libpath, platid))

    pytransform.pyarmor_init(platid=platid)
    logging.debug('Loaded dynamic library: %s', pytransform._pytransform._name)

    ver = pytransform.version_info()
    logging.debug('The version of core library is %s', ver)
    if ver[0] < 8:
        raise RuntimeError('PyArmor does not work with this core library '
                           '(r%d), which reversion < 8' % ver[0])

    if capsule is not None and not os.path.exists(capsule):
        logging.info('Generating public capsule ...')
        make_capsule(capsule)
예제 #5
0
def check_cross_platform(platforms, supermode=False, vmode=False):
    if not platforms:
        platforms = []
    fn1 = pytransform.version_info()[2]

    features = None
    if vmode:
        features = ['25' if supermode else '21']
        if sys.platform not in ('win32', ):
            raise RuntimeError('VM Protect mode only works for Windows')
        for platid in platforms:
            if not platid.starswith('windows'):
                raise RuntimeError('VM Protect mode only works for Windows')
            nlist = platid.split('.')
            if len(nlist) > 2:
                raise RuntimeError('Invalid platform name for VM mode')
        if not len(platforms):
            platforms = [_format_platid()]
    elif supermode:
        features = ['11' if (fn1 & FEATURE_JIT) else '8']
        if not len(platforms):
            v = 'py%d%d' % sys.version_info[:2]
            platforms = ['.'.join([_format_platid(), features[0], v])]

    result = []
    for name in platforms:
        platid = _get_preferred_platid(name, features=features)
        if platid is None:
            raise RuntimeError('No available dynamic library for %s '
                               'with features %s' % (name, features))
        result.append(platid)

    reboot = None
    if result:
        platid = result[0]
        nlist = platid.split('.')
        fn2 = int(nlist[2])
        if fn2 in (21, 25):
            n = 21
        elif fn2 in (0, 8):
            n = 0
        else:
            n = 7
        if (n != fn1) and not (n & fn1 & 0x12):
            reboot = '.'.join([_format_platid(), str(n)])
            os.environ['PYARMOR_PLATFORM'] = reboot

        logging.info('Update target platforms to: %s', result)
        for p in result[1:]:
            fn3 = int(p.split('.')[2])
            if (n != fn3) and not (n & fn3):
                raise RuntimeError('Multi platforms conflict, platform %s'
                                   ' could not mixed with %s' % (p, platid))

    if reboot:
        logging.info('====================================================')
        logging.info('Reload PyArmor with platform: %s', reboot)
        logging.info('====================================================')
        pytransform_bootstrap(force=True)
        # _reboot_pytransform(reboot)
        # return False

    return result