【YOLOV5从开发到部署实战】八、YOLO项目代码加密

关于代码加密

在源代码开发中,如何保护好自己开发的产品,维护好自主知识产权,是开发过程中必要了解的,对于经常做开发的来讲对源代码加密也多种方法。今天这里介绍一种不能反编译的YOLO项目代码的加密方法。

Python 语法简单,使用方便,我们可以使用它快速地编写程序和构建应用。 在编写好程序之后,我们必然要进行程序的分发。如果我们写的是图形界面程序,可能会打包成相应操作系统平台的二进制运行文件 (当然也可能直接发 Python 代码给别人运行)。

如果我们写的是 Web 应用程序,则需要部署在指定的服务器上 。而这,就涉及到了 源码保护的问题 。我们不需要程序的使用者能够看到程序的源码。但是,Python 作为一门动态语言和脚本语言,运行通过它编写的程序,并不需要进行静态编译和打包的过程, 对其代码进行加密是一件很麻烦、复杂和困难的事情 。

如果构建好的 Python 应用程序只是我们内部使用,或者部署在服务器上以 SaaS 化的形式供使用者使用,那么也根本无需考虑 Python 代码加密和源码泄露的问题。但是,如果我们编写的程序是要进行商业授权的呢?

源码的保护则是必须要做的一件事情。

安装环境

# python安装Cython
pip install Cython
# linux安装python-devel,gcc
yum install python-devel
yum install gcc

单文件加密

#!/usr/bin/env python
# -*- coding: utf-8 -*-

from distutils.core import setup
from Cython.Build import cythonize

setup(ext_modules = cythonize(['【代码文件】.py']))
python setup.py build_ext

文件夹加密

#-* -coding: UTF-8 -* -
__author__ = 'WangRongsheng'

"""
执行前提:
    系统安装python-devel 和 gcc
    Python安装cython
编译某个文件夹:
    python setup.py BigoModel
生成结果:
    目录 build 下
生成完成后:
    启动文件还需要py/pyc担当,须将启动的py/pyc拷贝到编译目录并删除so文件
"""

import sys, os, shutil, time
from distutils.core import setup
from Cython.Build import cythonize

starttime = time.time()
setupfile= os.path.join(os.path.abspath('.'), __file__)

def getpy(basepath=os.path.abspath('.'), parentpath='', name='', build_dir="build", 
          excepts=(), copyOther=False, delC=False):
    """
    获取py文件的路径
    :param basepath: 根路径
    :param parentpath: 父路径
    :param name: 文件/夹
    :param excepts: 排除文件
    :param copy: 是否copy其他文件
    :return: py文件的迭代器
    """
    fullpath = os.path.join(basepath, parentpath, name)
    for fname in os.listdir(fullpath):
        ffile = os.path.join(fullpath, fname)
        if os.path.isdir(ffile) and ffile != os.path.join(basepath, build_dir) and not fname.startswith('.'):
            for f in getpy(basepath, os.path.join(parentpath, name), fname, build_dir, excepts, copyOther, delC):
                yield f
        elif os.path.isfile(ffile):
            # print("\t", basepath, parentpath, name, ffile)
            ext = os.path.splitext(fname)[1]
            if ext == ".c":
                if delC and os.stat(ffile).st_mtime > starttime:
                    os.remove(ffile)
            elif ffile not in excepts and ext not in('.pyc', '.pyx'):
                # print("\t\t", basepath, parentpath, name, ffile)
                if ext in('.py', '.pyx') and not fname.startswith('__'):
                    yield os.path.join(parentpath, name, fname)
                elif copyOther:
                        dstdir = os.path.join(basepath, build_dir, parentpath, name)
                        if not os.path.isdir(dstdir): os.makedirs(dstdir)
                        shutil.copyfile(ffile, os.path.join(dstdir, fname))
        else:
            pass

if __name__ == "__main__":
    currdir = os.path.abspath('.')
    parentpath = sys.argv[1] if len(sys.argv)>1 else "."

    currdir, parentpath = os.path.split(currdir if parentpath == "." else os.path.abspath(parentpath))
    build_dir = os.path.join(parentpath, "build")
    build_tmp_dir = os.path.join(build_dir, "temp")
    print("start:", currdir, parentpath, build_dir)
    os.chdir(currdir)
    try:
        #获取py列表
        module_list = list(getpy(basepath=currdir,parentpath=parentpath, build_dir=build_dir, excepts=(setupfile)))
        print(module_list)
        setup(ext_modules = cythonize(module_list),script_args=["build_ext", "-b", build_dir, "-t", build_tmp_dir])
        module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), copyOther=True))
    except Exception as ex:
        print("error! ", ex)
    finally:
        print("cleaning...")
        module_list = list(getpy(basepath=currdir, parentpath=parentpath, build_dir=build_dir, excepts=(setupfile), delC=True))
        if os.path.exists(build_tmp_dir): shutil.rmtree(build_tmp_dir)

    print("complate! time:", time.time()-starttime, 's')
python setup.py 【文件夹名称】
  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

落难Coder

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值