脚本加密python

LczPtr 2019-12-27

脚本加密python

from pathlib import Path
import python_minifier
import compileall
import sys

def get_save_path(from_dir, to_dir, p):
    ori_dir = str(from_dir.resolve())
    _p = str(p.resolve())
    return to_dir.joinpath(_p.split(ori_dir)[-1][1:])


def mix_pyminifier(from_dir, to_dir):
    for _p in from_dir.rglob(‘*.py‘):
        with _p.open(encoding=‘utf-8‘) as read_fp:
            res = python_minifier.minify(read_fp.read())

        _to_p = get_save_path(from_dir, to_dir, _p)
        Path(_to_p.parent).mkdir(exist_ok=True, parents=True)
        with _to_p.open(‘w‘, encoding=‘utf-8‘) as write_fp:
            write_fp.write(res)


def py2pyc(from_dir, to_dir):
    compileall.compile_dir(str(from_dir.resolve()), maxlevels=20, force=True)


def pyc2py(from_dir, to_dir):
    pass


if __name__ == ‘__main__‘:
    origin_dir = Path(sys.argv[1])
    target_dir = Path(sys.argv[2])
    pymini_dir = target_dir.joinpath(‘pymini‘)
    pyc_dir = target_dir.joinpath(‘pyc_dir‘)
    py_dir = target_dir.joinpath(‘py_dir‘)
    mix_pyminifier(origin_dir, target_dir)
    # py2pyc(origin_dir, pyc_dir)
    # pyc2py(pyc_dir, pyc_dir)

混淆项目代码步骤:

脚本加密python

相关推荐