Coder Social home page Coder Social logo

py2sec's Introduction

py2sec

English Readme

py2sec 一款轻量的脚本工具,基于 Cython 将 .py 编译成 run-time libraries 文件:.so (Linux && Mac),或 .pyd (Win)。一定程度上实现了“加密”保护源代码的需求。

系统支持

Linux && MacOS && Windows

注意:Windows用户运行时可能会遇到如下错误

error: command 'cl.exe' failed: No such file or directory

请安装对应Win系统版本的Visual C++ Build Tools

py2sec 特性

  1. .so / .pyd 文件可以像 .py 模块一样正常调用。例如:importfrom module import * "
  2. py2sec 可以指定编译单个 .py 文件,也可以指定一个 python 项目目录
  3. py2sec 还可以提升代码运行速度,至多提升30倍
  4. py2sec 自动识别项目中的 .py 文件,且只编译 .py 类型文件
  5. 可以指定不需要编译的文件或子目录
  6. py2sec 不影响源文件,加密后的文件或项目将被存放在 result 文件夹
  7. 兼容多平台:macOS、Linux、Windows、
  8. 兼容 Python 版本:python2 and python3, 可使用 -p(--py) 参数来指定版本
  9. 支持多线程(待改进)

环境配置

pip install requirements.txt

使用说明

使用

请将要加密的目录或文件存放在py2sec根目录下

python py2sec.py [选项] ...

选项

-v,  --version    显示 py2sec 版本
-h,  --help       显示帮助菜单
-p,  --pyth       Python的版本, 默认为 你的 "python" 命令绑定的python版本
                  例: -p 3  (比如你使用python3)
-d,  --directory  Python项目路径 (如果使用-d参数, 将编译整个Python项目)
-f,  --file       Python文件 (如果使用-f, 将编译单个Python文件)
-m,  --maintain   标记你不想编译的文件或文件夹路径
                  注意: 文件夹需要以路径分隔符号(`/`或`\\`,依据系统而定)结尾,并且需要和-d参数一起使用 
                  例: -m setup.py,mod/__init__.py,exclude_dir/
-x  --nthread     编译启用的线程数
-q  --quiet       静默模式,默认False
-r  --release     Release 模式,清除所有中间文件,只保留加密结果文件,默认False
python py2sec.py -f test.py
python py2sec.py -f example/test1.py -r
python py2sec.py -d example/ -m test1.py,bbb/

# 一些操作系统使用 "python3" 命令来执行python3,如Ubuntu,这里可以使用 -p 参数来运行
python3 py2sec.py -p 3 -d example/

项目结构

  • build/ 临时文件夹, .o, .so/.pyd 文件
  • tmp_build/ 临时文件夹, .c 文件
  • result/ 编译最终结果存放目录
  • result/log.txt 编译过程日志
  • py2sec.py 主函数文件
  • py2sec_build.py.template 用于生成 py2sec_build.py 的模板文件
  • requirements.txt 依赖库的清单

协议

本项目遵循MIT协议

示例

整个Python项目编译前:

demo1

py2sec 编译后效果:

demo2

py2sec's People

Contributors

cckuailong avatar nodewee avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

py2sec's Issues

error: command 'cl.exe' failed: None

Environment: win10 + python3.9
Visual studio build tools:
(1) MSVC v143 - VS 2002 C++ x64/x86 build tools(lastest)
(2) Windows 10 SDK (10.0.20348.0)

I put py2sec.py in f:\py and all py files in f:\py\main, then run command

f:\py > python py2sec.py -d main -r

and Error reports

(spd39) F:\py>python py2sec.py -d main\ -r
Active code page: 65001
Compiling main\sa\init.py because it changed.
[1/1] Cythonizing main\sa\init.py
running build_ext
building 'main.sa.init' extension
creating build\temp.win-amd64-cpython-39
creating build\temp.win-amd64-cpython-39\Release
creating build\temp.win-amd64-cpython-39\Release\tmp_build
creating build\temp.win-amd64-cpython-39\Release\tmp_build\main
creating build\temp.win-amd64-cpython-39\Release\tmp_build\main\sa
cl.exe /c /nologo /O2 /W3 /GL /DNDEBUG /MD -Id:\miniconda3\envs\spd39\include -Id:\miniconda3\envs\spd39\Include /Tctmp_build\main\sa\init.c /Fobuild\temp.win-amd64-cpython-39\Release\tmp_build\main\sa\init.obj
error: command 'cl.exe' failed: None

Py2Sec Encrypt Encounter Error

I don't know what's wrong , please give some suggestions.

Py2Sec Encrypt Encounter Error

Environment:
Python==3.11.4
Cython==3.0.5

Command:python py2sec.py -f example/test1.py -r
Error message:

Compiling example/test1.py because it changed.
[1/1] Cythonizing example/test1.py

Error compiling Cython file:
------------------------------------------------------------
...
print(1)
^
------------------------------------------------------------

example\test1.py:1:0: 'example/test1' is not a valid module name
Traceback (most recent call last):
  File "E:\DevProject\PythonProject\LIBRARY\py2sec\tmp_py2sec_build.py", line 28, in <module>
    ext_modules = cythonize(extentions, nthreads=1, build_dir="tmp_build", quiet=False, compiler_directives=compiler_directives)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "D:\MiniConda\Lib\site-packages\Cython\Build\Dependencies.py", line 1154, in cythonize
    cythonize_one(*args)
  File "D:\MiniConda\Lib\site-packages\Cython\Build\Dependencies.py", line 1321, in cythonize_one
    raise CompileError(None, pyx_file)
Cython.Compiler.Errors.CompileError: example/test1.py

Py2Sec Encrypt Encounter Error

one single .so?

Hi,

This is definitely a great tool. Is it possible to package everything into one dynamic library file ".so"? Currently a package contains quite a few ".so"s.

Thanks.

cannot ignore directory

when I use -m [directory/] to maintain a directory, it will appear error -- "dir_list is a list cannot be split". I go to see the source code. there are some problems.

  1. in line 76 and 77 temp[0],temp[1:-1] will cut off the last one and file just can be chosen the first one.
    My suggestion is change the rule, right the directory first then file like [a/,b/],file1,file2. Then use temp[0] to take directory, temp[1:] to take file_list
  2. in line 87, dir_list is a list so cannot use .split

It is wonderful to transform project and maintain file. But maintain directory still have some issues those issue also in your last project "py2o". Thank you!

cleanup

Hi,

Could you please add clean-ups for package builds? Ideally after package building there will only be a "result/package" directory. There can be a flag to retain intermediate steps for debugging purpose if user is interested.

Thanks.

跨平台

作者你好,是不支持在mac上编译成.so后在linux上运行吗?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.