Skip to content

Commit b3d5deb

Browse files
committed
Document PyInstall example
1 parent 7f9043f commit b3d5deb

File tree

3 files changed

+168
-34
lines changed

3 files changed

+168
-34
lines changed

src/examples/README-ZH.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ Pyarmor 的安装路径是 `/path/to/pyarmor`
152152
153153
```
154154

155-
## 实例 4: 使用第三方发布工具打包加密脚本
155+
## 实例 4: 使用 py2exe / py2app / cx_Freeze 打包加密脚本
156156

157157
从这个例子中,可以学习到
158158

@@ -184,3 +184,54 @@ Pyarmor 的安装路径是 `/path/to/pyarmor`
184184
的脚本
185185

186186
对于其他打包工具 `cx_Freeze`, `py2app`, 基本使用方法和 `py2exe` 很类似。
187+
188+
## 实例 5: 使用 PyInstaller 打包加密脚本
189+
190+
从这个例子中,可以学习到
191+
192+
* 如何使用第三方工具 `PyInstaller` 来打包加密的脚本
193+
194+
如果没有安装 `pyinstaller`, 运行下面的命令进行安装
195+
196+
pip install pyinstaller
197+
198+
这个例子会使用 `PyInstaller` 来发布加密脚本,并最终生成一个可执行文件。
199+
主脚本是位于 `examples/testmod`[hello.py](testmod/hello.py)
200+
201+
首先把目录 `testmod` 下面的所有 `.py` 文件加密,存放到 `dist/obf`
202+
203+
pyarmor obfuscate --src testmod --entry hello.py --no-restrict --output dist/obf
204+
205+
接着先运行 `pyinsntaller` 打包没有加密的脚本到目录 `dist/hello`,这条命
206+
令同时会生成相应的 `hello.spec`文件
207+
208+
cd /path/to/pyarmor/examples
209+
pyinstaller testmod/hello.py
210+
211+
编辑文件 `hello.spec`, 修改里面的 `Analysis` 对象 `a`,增加加密脚本和运
212+
行加密脚本需要的辅助文件到数据文件里面
213+
214+
a = Analysis(['testmod/hello.py', 'dist/obf/hello.py'],
215+
pathex=['/path/to/pyarmor/examples],
216+
datas=[('dist/obf/*.lic', '.'), ('dist/obf/*.key', '.'), ('dist/obf/_pytransform.*', '.')],
217+
218+
`Analysis` 之后,在插入一段代码,告诉 `PyInstaller` 把加密后的脚本打
219+
进包里面,不要使用原来的脚本
220+
221+
a.scripts[0] = 'hello', 'dist/obf/hello.py', 'PYSOURCE'
222+
for i in range(len(a.pure)):
223+
if a.pure[i][1].startswith(a.pathex[0]):
224+
a.pure[i] = a.pure[i][0], a.pure[i][1].replace('/testmod/', '/dist/obf/'), a.pure[i][2]
225+
226+
使用修正版 `hello.spec` 重新运行 `pyinstaller` 进行打包
227+
228+
pyinstaller hello.spec
229+
230+
最后运行打包好的可执行文件,把 `license.lic` 删除之后看看能否运行,确保
231+
打包的是加密脚本
232+
233+
dist/hello/hello
234+
235+
rm dist/hello/license.lic
236+
dist/hello/hello
237+

src/examples/README.md

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,3 +196,44 @@ obfuscated scripts are copied here, and the `library.zip` is updated, the
196196
original `queens.pyc` replaced with obfuscated one.
197197

198198
For cx_Freeze and py2app, it's almost same as py2exe.
199+
200+
## Example 5: Build for PyInstaller
201+
202+
Here is one of workaround to pack obfuscated scripts with `PyInstaller`, this
203+
example will distribute a script `hello.py` and moudle file `queens.py`
204+
205+
First obfuscate all the `.py` file in the path `testmod` to `dist/obf`
206+
207+
pyarmor obfuscate --src testmod --entry hello.py --no-restrict --output dist/obf
208+
209+
Then run pyinstaller to build the bundle to `dist/hello/`, this command will
210+
generate `hello.spec` either
211+
212+
cd /path/to/pyarmor/examples
213+
pyinstaller testmod/hello.py
214+
215+
Edit `hello.spec`, change `Analysis`, add obfuscated script and date files as
216+
the following way
217+
218+
a = Analysis(['testmod/hello.py', 'dist/obf/hello.py'],
219+
datas=[('dist/obf/*.lic', '.'), ('dist/obf/*.key', '.'), ('dist/obf/_pytransform.*', '.')],
220+
221+
After `Analysis`, insert code to replace original python scrips and moudles with
222+
obfuscated ones
223+
224+
a.scripts[0] = 'hello', 'dist/obf/hello.py', 'PYSOURCE'
225+
for i in range(len(a.pure)):
226+
if a.pure[i][1].startswith(a.pathex[0]):
227+
a.pure[i] = a.pure[i][0], a.pure[i][1].replace('/testmod/', '/dist/obf/'), a.pure[i][2]
228+
229+
Next run pyinstaller with this `hello.spec`
230+
231+
pyinstaller hello.spec
232+
233+
Finally run exetuable with obfuscated scripts
234+
235+
dist/hello/hello
236+
237+
# Check the scripts are obfuscated
238+
rm dist/hello/license.lic
239+
dist/hello/hello

src/user-guide.md

Lines changed: 75 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,11 @@ This is the documentation for Pyarmor 3.4 and later.
1111
- [Use Project to Manage Obfuscated Scripts](#use-project-to-manage-obfuscated-scripts)
1212
- [Standalone Package](#standalone-package)
1313
- [Package Used by Outer Scripts](#package-used-by-outer-scripts)
14-
- [Pack Obfuscated Scripts with py2exe and cx_Freeze](#pack-obfuscated-scripts-with-py2exe-and-cx_freeze)
1514
- [Many Obfuscated Package Within Same Python Interpreter](#many-obfuscated-package-within-same-python-interpreter)
1615
- [Change Default Project Configuration](#change-default-project-configuration)
1716
- [Distribute Obfuscated Scripts](#distribute-obfuscated-scripts)
17+
- [Pack Obfuscated Scripts with py2exe and cx_Freeze](#pack-obfuscated-scripts-with-py2exe-and-cx_freeze)
18+
- [Pack Obfuscated Scripts with PyInstaller](#pack-obfuscated-scripts-with-pyinstaller)
1819
- [License of Obfuscated Scripts](#license-of-obfuscated-scripts)
1920
- [Cross Platform](#cross-platform)
2021
- [Use runtime path](#use-runtime-path)
@@ -254,38 +255,6 @@ it used by clear script `examples/testpkg/main.py`
254255
python main.py
255256
```
256257

257-
#### Pack Obfuscated Scripts with py2exe and cx_Freeze
258-
259-
From v4.4, introduces a command `pack` to pack obfuscated scripts with py2exe,
260-
cx_Freeze etc.
261-
262-
First install py2xe
263-
264-
pip install py2exe
265-
266-
Then write `setup.py` for py2exe, here is an example script
267-
`examples/py2exe/setup.py`. There are 2 scripts in this example, entry script
268-
`hello.py` and `queens.py`. To be sure it works
269-
270-
cd examples/py2exe
271-
python setup.py py2exe
272-
273-
After that, run command `pack` to pack obfuscated scripts
274-
275-
python pyarmor.py pack --type py2exe examples/py2exe/hello.py
276-
277-
Check the output path of `examples/py2exe/dist`, the runtime files required by
278-
obfuscated scripts are copied here, and the `library.zip` is updated, the
279-
original `queens.pyc` replaced with obfuscated one.
280-
281-
For cx_Freeze, py2app, it's almost same as py2exe. Learn more options
282-
about command [pack](#pack)
283-
284-
Quick start from the following template scripts
285-
286-
* [pack-obfuscated-scripts.bat](examples/pack-obfuscated-scripts.bat) for Windows
287-
* [pack-obfuscated-scripts.sh](examples/pack-obfuscated-scripts.sh) for most of others
288-
289258
#### Many Obfuscated Package Within Same Python Interpreter
290259

291260
Suppose there are 3 odoo modules `web-login1`, `web-login2`,
@@ -444,6 +413,79 @@ For package which used by other scripts:
444413
machine. To be exact, the magic string value used to recognize
445414
byte-compiled code files (.pyc files) must be same.
446415

416+
#### Pack Obfuscated Scripts with py2exe and cx_Freeze
417+
418+
From v4.4, introduces a command `pack` to pack obfuscated scripts with py2exe,
419+
cx_Freeze etc.
420+
421+
First install py2xe
422+
423+
pip install py2exe
424+
425+
Then write `setup.py` for py2exe, here is an example script
426+
`examples/py2exe/setup.py`. There are 2 scripts in this example, entry script
427+
`hello.py` and `queens.py`. To be sure it works
428+
429+
cd examples/py2exe
430+
python setup.py py2exe
431+
432+
After that, run command `pack` to pack obfuscated scripts
433+
434+
python pyarmor.py pack --type py2exe examples/py2exe/hello.py
435+
436+
Check the output path of `examples/py2exe/dist`, the runtime files required by
437+
obfuscated scripts are copied here, and the `library.zip` is updated, the
438+
original `queens.pyc` replaced with obfuscated one.
439+
440+
For cx_Freeze, py2app, it's almost same as py2exe. Learn more options
441+
about command [pack](#pack)
442+
443+
Quick start from the following template scripts
444+
445+
* [pack-obfuscated-scripts.bat](examples/pack-obfuscated-scripts.bat) for Windows
446+
* [pack-obfuscated-scripts.sh](examples/pack-obfuscated-scripts.sh) for most of others
447+
448+
#### Pack Obfuscated Scripts with PyInstaller
449+
450+
Here is one of workaround to pack obfuscated scripts with `PyInstaller`, this
451+
example will distribute a script `hello.py` and moudle file `queens.py`
452+
453+
First obfuscate all the `.py` file in the path `testmod` to `dist/obf`
454+
455+
pyarmor obfuscate --src testmod --entry hello.py --no-restrict --output dist/obf
456+
457+
Then run pyinstaller to build the bundle to `dist/hello/`, this command will
458+
generate `hello.spec` either
459+
460+
cd /path/to/pyarmor/examples
461+
pyinstaller testmod/hello.py
462+
463+
Edit `hello.spec`, change `Analysis`, add obfuscated script and date files as
464+
the following way
465+
466+
a = Analysis(['testmod/hello.py', 'dist/obf/hello.py'],
467+
datas=[('dist/obf/*.lic', '.'), ('dist/obf/*.key', '.'), ('dist/obf/_pytransform.*', '.')],
468+
469+
After `Analysis`, insert code to replace original python scrips and moudles with
470+
obfuscated ones
471+
472+
a.scripts[0] = 'hello', 'dist/obf/hello.py', 'PYSOURCE'
473+
for i in range(len(a.pure)):
474+
if a.pure[i][1].startswith(a.pathex[0]):
475+
a.pure[i] = a.pure[i][0], a.pure[i][1].replace('/testmod/', '/dist/obf/'), a.pure[i][2]
476+
477+
Next run pyinstaller with this `hello.spec`
478+
479+
pyinstaller hello.spec
480+
481+
Finally run exetuable with obfuscated scripts
482+
483+
dist/hello/hello
484+
485+
# Check the scripts are obfuscated
486+
rm dist/hello/license.lic
487+
dist/hello/hello
488+
447489
### License of Obfuscated Scripts
448490

449491
There is a file `license.lic` in the output path "dist", the default

0 commit comments

Comments
 (0)