示例#1
0
文件: base.py 项目: ITrust/pyconcrete
    def lib_gen_pye(self,
                    py_code,
                    pye_filename,
                    folder=None,
                    keep_py=False,
                    keep_pyc=False):
        """ folder = None -> use @_tmp_dir """
        if not folder:
            folder = self.tmp_dir
        self.assertTrue(pye_filename.endswith('.pye'))
        filename = os.path.splitext(pye_filename)[0]
        py_filepath = join(folder, filename + '.py')
        pyc_filepath = join(folder, filename + '.pyc')
        pye_filepath = join(folder, filename + '.pye')

        # create .py
        with open(py_filepath, 'wb') as f:
            f.write(py_code)

        # create .pyc
        py_compile.compile(py_filepath)

        # create .pye & remove .py & .pyc
        import pyconcrete
        pyconcrete.encrypt_file(pyc_filepath, pye_filepath)

        # remove files
        if not keep_py:
            os.remove(py_filepath)
        if not keep_pyc:
            os.remove(pyc_filepath)

        return pye_filepath
示例#2
0
    def _compile_pye_file(self, args, py_file):
        """
        if there is no .pyc file, compile .pyc first
        then compile .pye
        """
        import pyconcrete
        pyc_file = py_file + 'c'
        pye_file = py_file + 'e'
        pyc_exists = exists(pyc_file)
        if not pyc_exists or os.stat(py_file).st_mtime != os.stat(
                pyc_file).st_mtime:
            py_compile.compile(py_file, cfile=pyc_file)
        if not exists(pye_file) or os.stat(py_file).st_mtime != os.stat(
                pye_file).st_mtime:
            pyconcrete.encrypt_file(pyc_file, pye_file)
            if args.verbose:
                print('* create %s' % pye_file)
        else:
            if args.verbose:
                print('* skip %s' % pye_file)

        # .pyc doesn't exists at beginning, remove it after .pye created
        if not pyc_exists or args.remove_pyc:
            os.remove(pyc_file)
        if args.remove_py:
            os.remove(py_file)
示例#3
0
    def lib_gen_pye(self, py_code, pye_filename, folder=None, keep_py=False, keep_pyc=False):
        """ folder = None -> use @_tmp_dir """
        if not folder:
            folder = self.tmp_dir
        self.assertTrue(pye_filename.endswith('.pye'))
        filename = os.path.splitext(pye_filename)[0]
        py_filepath = join(folder, filename + '.py')
        pyc_filepath = join(folder, filename + '.pyc')
        pye_filepath = join(folder, filename + '.pye')

        # create .py
        with open(py_filepath, 'wb') as f:
            f.write(to_bytes(py_code))

        # create .pyc
        py_compile.compile(py_filepath, cfile=pyc_filepath)

        # create .pye & remove .py & .pyc
        import pyconcrete
        pyconcrete.encrypt_file(pyc_filepath, pye_filepath)

        # remove files
        if not keep_py:
            os.remove(py_filepath)
        if not keep_pyc:
            os.remove(pyc_filepath)

        return pye_filepath
示例#4
0
    def __do_encrypt_decrypt_file(self, py_code):
        py_filepath = self.lib_gen_py(py_code, 'test.py')
        pye_filepath = py_filepath + 'e'

        import pyconcrete
        pyconcrete.encrypt_file(py_filepath, pye_filepath)

        with open(pye_filepath, 'rb') as f:
            data = f.read()
        return pyconcrete.decrypt_buffer(data)
示例#5
0
 def __do_encrypt_decrypt_file(self, py_code):
     py_filepath = self.lib_gen_py(py_code, 'test.py')
     pye_filepath = py_filepath + 'e'
     
     import pyconcrete
     pyconcrete.encrypt_file(py_filepath, pye_filepath)
     
     with open(pye_filepath, 'rb') as f:
         data = f.read()
     return pyconcrete.decrypt_buffer(data)
示例#6
0
    def compile_pye_file(self, py_file):
        pyc_file = py_file + 'c'
        pye_file = py_file + 'e'
        pyc_exists = exists(pyc_file)
        if not pyc_exists or os.stat(py_file).st_mtime != os.stat(
                pyc_file).st_mtime:
            py_compile.compile(py_file)
        if not exists(pye_file) or os.stat(py_file).st_mtime != os.stat(
                pye_file).st_mtime:
            pyconcrete.encrypt_file(pyc_file, pye_file)
            if self.args.verbose:
                print '* create %s' % pye_file
        else:
            if self.args.verbose:
                print '* skip %s' % pye_file

        # .pyc doesn't exists at begining, remove it after .pye created
        if not pyc_exists or self.args.remove_pyc:
            os.remove(pyc_file)
        if self.args.remove_py:
            os.remove(py_file)