Ejemplo n.º 1
0
def make_runtime(capsule, output, licfile=None, platforms=None, package=False):
    if package:
        output = os.path.join(output, 'pytransform')
        if not os.path.exists(output):
            os.makedirs(output)
    logging.info('Generating runtime files to %s', output)

    myzip = ZipFile(capsule, 'r')
    if 'pytransform.key' in myzip.namelist():
        logging.info('Extract pytransform.key')
        myzip.extract('pytransform.key', output)
    else:
        logging.info('Extract pyshield.key, pyshield.lic, product.key')
        myzip.extract('pyshield.key', output)
        myzip.extract('pyshield.lic', output)
        myzip.extract('product.key', output)

    if licfile is None:
        logging.info('Extract license.lic')
        myzip.extract('license.lic', output)
    else:
        logging.info('Copying %s as license file', licfile)
        shutil.copy2(licfile, os.path.join(output, 'license.lic'))

    if not platforms:
        libfile = pytransform._pytransform._name
        if not os.path.exists(libfile):
            libname = dll_name + dll_ext
            libfile = os.path.join(PYARMOR_PATH, libname)
            if not os.path.exists(libfile):
                pname = pytransform.format_platform()
                libpath = os.path.join(PYARMOR_PATH, 'platforms')
                libfile = os.path.join(libpath, pname, libname)
        logging.info('Copying %s', libfile)
        shutil.copy2(libfile, output)
    elif len(platforms) == 1:
        filename = _build_platforms(platforms)[0]
        logging.info('Copying %s', filename)
        shutil.copy2(filename, output)
    else:
        libpath = os.path.join(output, pytransform.plat_path)
        logging.info('Create library path to support multiple platforms: %s',
                     libpath)
        if not os.path.exists(libpath):
            os.mkdir(libpath)

        filenames = _build_platforms(platforms)
        for platid, filename in list(zip(platforms, filenames)):
            logging.info('Copying %s', filename)
            path = os.path.join(libpath, *platid.split('.'))
            logging.info('To %s', path)
            if not os.path.exists(path):
                os.makedirs(path)
            shutil.copy2(filename, path)

    filename = os.path.join(PYARMOR_PATH, 'pytransform.py')
    shutil.copy2(filename,
                 os.path.join(output, '__init__.py') if package else output)

    logging.info('Generate runtime files OK')
Ejemplo n.º 2
0
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)
Ejemplo n.º 3
0
def _format_platid(platid=None):
    if platid is None:
        platid = pytransform.format_platform()
    if os.path.isabs(platid):
        return os.path.normpath(platid)
    return platid.replace('\\', '/').replace('/', '.')
Ejemplo n.º 4
0
def make_runtime(capsule, output, licfile=None, platforms=None, package=False,
                 suffix='', restrict=True, supermode=False):
    if supermode:
        _make_super_runtime(capsule, output, licfile=licfile, suffix=suffix,
                            platforms=platforms, restrict=restrict)
        return

    if package:
        output = os.path.join(output, 'pytransform' + suffix)
        if not os.path.exists(output):
            os.makedirs(output)
    logging.info('Generating runtime files to %s', relpath(output))

    myzip = ZipFile(capsule, 'r')
    if 'pytransform.key' in myzip.namelist():
        logging.info('Extract pytransform.key')
        myzip.extract('pytransform.key', output)
    else:
        logging.info('Extract pyshield.key, pyshield.lic, product.key')
        myzip.extract('pyshield.key', output)
        myzip.extract('pyshield.lic', output)
        myzip.extract('product.key', output)

    if licfile is None:
        logging.info('Generate default license.lic')
        make_license_key(capsule, 'Dashingsoft-PyArmor',
                         output=os.path.join(output, 'license.lic'),
                         key=myzip.read('private.key'))
        # info = myzip.getinfo('license.lic')
        # if info.date_time[0] > 2020:
        #     logging.info('Extract license.lic')
        #     myzip.extract('license.lic', output)
    else:
        logging.info('Copying %s as license file', relpath(licfile))
        shutil.copy2(licfile, os.path.join(output, 'license.lic'))

    def copy3(src, dst):
        if suffix:
            x = os.path.basename(src).replace('.', ''.join([suffix, '.']))
            shutil.copy2(src, os.path.join(dst, x))
        else:
            shutil.copy2(src, dst)

    if not platforms:
        if supermode:
            platid = pytransform.format_platform()
            libfile = _build_platforms([platid], restrict)[0]
        else:
            libfile = pytransform._pytransform._name
            if not os.path.exists(libfile):
                libname = dll_name + dll_ext
                libfile = os.path.join(PYARMOR_PATH, libname)
                if not os.path.exists(libfile):
                    pname = pytransform.format_platform()
                    libpath = os.path.join(PYARMOR_PATH, 'platforms')
                    libfile = os.path.join(libpath, pname, libname)
        logging.info('Copying %s', libfile)
        copy3(libfile, output)

    elif len(platforms) == 1:
        filename = _build_platforms(platforms, restrict)[0]
        logging.info('Copying %s', filename)
        copy3(filename, output)
    else:
        libpath = os.path.join(output, pytransform.plat_path)
        logging.info('Create library path to support multiple platforms: %s',
                     libpath)
        if not os.path.exists(libpath):
            os.mkdir(libpath)

        filenames = _build_platforms(platforms, restrict)
        for platid, filename in list(zip(platforms, filenames)):
            logging.info('Copying %s', filename)
            path = os.path.join(libpath, *platid.split('.')[:2])
            logging.info('To %s', path)
            if not os.path.exists(path):
                os.makedirs(path)
            copy3(filename, path)

    filename = os.path.join(PYARMOR_PATH, 'pytransform.py')
    if package:
        shutil.copy2(filename, os.path.join(output, '__init__.py'))
    else:
        copy3(filename, output)

    logging.info('Generate runtime files OK')
Ejemplo n.º 5
0
def _make_super_runtime(capsule, output, licfile=None, platforms=None,
                        restrict=True, suffix=''):
    supermode = True
    logging.info('Generating super runtime library to %s', relpath(output))
    if not os.path.exists(output):
        os.makedirs(output)

    def copy3(src, dst):
        if suffix:
            x = os.path.basename(src).replace('.', ''.join([suffix, '.']))
            shutil.copy2(src, os.path.join(dst, x))
        else:
            shutil.copy2(src, dst)

    if not platforms:
        platid = pytransform.format_platform()
        filelist = _build_platforms([platid], restrict, supermode)[:1]
    elif len(platforms) == 1:
        filelist = _build_platforms(platforms, restrict, supermode)[:1]
    else:
        filelist = _build_platforms(platforms, restrict, supermode)

    myzip = ZipFile(capsule, 'r')
    if 'pytransform.key' not in myzip.namelist():
        raise RuntimeError('No pytransform.key found in capsule')

    logging.info('Extract pytransform.key')
    keydata = myzip.read('pytransform.key')
    size1 = keydata[0] + keydata[1] * 8
    size2 = keydata[2] + keydata[2] * 8
    if licfile is None:
        logging.info('Generate default license.lic')
        lickey = make_license_key(capsule, 'Dashingsoft-PyArmor',
                                  key=myzip.read('private.key'))
    else:
        logging.info('Use license file %s', relpath(licfile))
        with open(licfile, 'rb') as f:
            lickey = f.read()

    def patch_library(filename):
        patkey = b'\x0f\x00\x07\x06'
        patlen = len(patkey)
        with open(filename, 'rb') as f:
            data = bytearray(f.read())

        n = len(data)
        for i in range(n):
            if data[i:i+patlen] == patkey:
                fmt = 'I' * 8
                header = struct.unpack(fmt, data[i:i+32])
                if sum(header[2:]) != 912:
                    continue
                max_size = header[1]
                if size1 + size2 + len(lickey) > max_size:
                    raise RuntimeError('Too much license data')
                offset = 16
                data[i:i+size1+size2] = keydata[offset:]
                data[i:i+len(lickey)] = lickey
        else:
            raise RuntimeError('Invalid dynamic library')

        with open(filename, 'wb') as f:
            f.write(data)

    for filename in filelist:
        logging.info('Copying %s', filename)
        copy3(filename, output)
        logging.info('Write license data')
        patch_library(filename)

    logging.info('Generate runtime files OK')
Ejemplo n.º 6
0
def make_runtime(capsule,
                 output,
                 licfile=None,
                 platforms=None,
                 package=False,
                 suffix='',
                 restrict=True,
                 supermode=False):
    if supermode:
        return _make_super_runtime(capsule,
                                   output,
                                   licfile=licfile,
                                   suffix=suffix,
                                   platforms=platforms,
                                   restrict=restrict)

    if package:
        output = os.path.join(output, 'pytransform' + suffix)
        if not os.path.exists(output):
            os.makedirs(output)
    logging.info('Generating runtime files to %s', relpath(output))

    myzip = ZipFile(capsule, 'r')
    if 'pytransform.key' in myzip.namelist():
        logging.info('Extract pytransform.key')
        myzip.extract('pytransform.key', output)
    else:
        logging.info('Extract pyshield.key, pyshield.lic, product.key')
        myzip.extract('pyshield.key', output)
        myzip.extract('pyshield.lic', output)
        myzip.extract('product.key', output)
    myzip.close()

    _build_license_file(capsule,
                        licfile,
                        output=os.path.join(output, 'license.lic'))

    def copy3(src, dst):
        if suffix:
            x = os.path.basename(src).replace('.', ''.join([suffix, '.']))
            shutil.copy2(src, os.path.join(dst, x))
        else:
            shutil.copy2(src, dst)
        checklist.append(_get_checksum(src))

    checklist = []
    if not platforms:
        libfile = pytransform._pytransform._name
        if not os.path.exists(libfile):
            libname = dll_name + dll_ext
            libfile = os.path.join(PYARMOR_PATH, libname)
            if not os.path.exists(libfile):
                pname = pytransform.format_platform()
                libpath = os.path.join(PYARMOR_PATH, 'platforms')
                libfile = os.path.join(libpath, pname, libname)
        logging.info('Copying %s', libfile)
        copy3(libfile, output)

    elif len(platforms) == 1:
        filename = _build_platforms(platforms, restrict)[0]
        logging.info('Copying %s', filename)
        copy3(filename, output)
    else:
        libpath = os.path.join(output, pytransform.plat_path)
        logging.info('Create library path to support multiple platforms: %s',
                     libpath)
        if not os.path.exists(libpath):
            os.mkdir(libpath)

        filenames = _build_platforms(platforms, restrict)
        for platid, filename in list(zip(platforms, filenames)):
            logging.info('Copying %s', filename)
            path = os.path.join(libpath, *platid.split('.')[:2])
            logging.info('To %s', path)
            if not os.path.exists(path):
                os.makedirs(path)
            copy3(filename, path)

    filename = os.path.join(PYARMOR_PATH, 'pytransform.py')
    if package:
        shutil.copy2(filename, os.path.join(output, '__init__.py'))
    else:
        copy3(filename, output)

    logging.info('Generate runtime files OK')
    return checklist
Ejemplo n.º 7
0
def make_runtime(capsule,
                 output,
                 licfile=None,
                 platforms=None,
                 package=False,
                 suffix='',
                 restrict=True,
                 supermode=False):
    if supermode:
        return _make_super_runtime(capsule,
                                   output,
                                   licfile=licfile,
                                   suffix=suffix,
                                   platforms=platforms,
                                   restrict=restrict)

    if package:
        output = os.path.join(output, 'pytransform' + suffix)
    if not os.path.exists(output):
        os.makedirs(output)
    logging.info('Generating runtime files to %s', relpath(output))

    checklist = []
    keylist = _build_keylist(capsule, licfile)

    def copy3(src, dst, onlycopy=False):
        x = os.path.basename(src)
        if suffix:
            x = x.replace('.', ''.join([suffix, '.']))
        target = os.path.join(dst, x)
        shutil.copy2(src, target)

        if onlycopy:
            return

        logging.info('Patch library %s', target)
        data = _patch_extension(target, keylist, suffix)
        with open(target, 'wb') as f:
            f.write(data)
        checklist.append(sum(bytearray(data)))

    if not platforms:
        libfile = pytransform._pytransform._name
        if not os.path.exists(libfile):
            libname = dll_name + dll_ext
            libfile = os.path.join(PYARMOR_PATH, libname)
            if not os.path.exists(libfile):
                pname = pytransform.format_platform()
                libpath = os.path.join(PYARMOR_PATH, 'platforms')
                libfile = os.path.join(libpath, pname, libname)
        logging.info('Copying %s', libfile)
        copy3(libfile, output)

    elif len(platforms) == 1:
        filename = _build_platforms(platforms, restrict)[0]
        logging.info('Copying %s', filename)
        copy3(filename, output)
    else:
        libpath = os.path.join(output, pytransform.plat_path)
        logging.info('Create library path to support multiple platforms: %s',
                     libpath)
        if not os.path.exists(libpath):
            os.mkdir(libpath)

        filenames = _build_platforms(platforms, restrict)
        for platid, filename in list(zip(platforms, filenames)):
            logging.info('Copying %s', filename)
            path = os.path.join(libpath, *platid.split('.')[:2])
            logging.info('To %s', path)
            if not os.path.exists(path):
                os.makedirs(path)
            copy3(filename, path)

    filename = os.path.join(PYARMOR_PATH, 'pytransform.py')
    if package:
        logging.info('Copying %s', filename)
        logging.info('Rename it to %s/__init__.py', os.path.basename(output))
        shutil.copy2(filename, os.path.join(output, '__init__.py'))
    else:
        logging.info('Copying %s', filename)
        copy3(filename, output, onlycopy=True)

    logging.info('Generate runtime files OK')
    return checklist