Build Pyarmored Wheel

Modern Python packages can contain apyproject.toml file,first introduced in PEP 518 and later expanded in PEP 517, PEP 621 andPEP 660. This file contains build system requirements and information, which areused by pip to build the package.

Since v7.2.0, pyarmor could be as PEP 517 backend to build a pyarmored wheelbased onsetuptools.build_meta.

Here an example package structure:

mypkg/setup.pypyproject.tomlsrc/__init__.py...

Thepyproject.toml may like this:

[build-system]requires=["setuptools","wheel"]build-backend="setuptools.build_meta"

First make sure backendsetuptools.build_meta works by running the followingcommands to build wheel. If it doesn’t work, please learn the related knowledgespip wheel and make it works:

cdmypkg/pipwheel.

Now editpyproject.toml, change build backend topyarmor.build_meta:

[build-system]requires=["setuptools","wheel","pyarmor>7.2.0"]build-backend="pyarmor.build_meta"

Build a pyarmored wheel by same commands:

cdmypkg/pipwheel.

Or build a pyarmored wheel withSuper Mode by setting extra obfuscationoptions in environment variablePIP_PYARMOR_OPTIONS:

cdmypkg/# In WindowssetPIP_PYARMOR_OPTIONS=--advanced2pipwheel.# In Linux or MacOsPIP_PYARMOR_OPTIONS="--advanced 2"pipwheel.

Since v7.2.4 pip configurationpyarmor.advanced also could be used to builda pyarmored wheel withSuper Mode.

First runpip config

pipconfigsetpyarmor.advanced2

Then run the building command:

cdmypkg/pipwheel.

Removing this configuration by this way:

pipconfigunsetpyarmor.advanced

How does it work

The Python scripts obfuscated by pyarmor are same as normal Python scripts withan extra dynamic library or extension. Sopyarmor.build_meta just does

  1. Callsetuptools.build_meta to build wheel
  2. Unpack wheel
  3. Obfuscate all the .py files in the unpacking path
  4. Append the pyarmor runtime files to wheel fileRECORD
  5. Repack the patched wheel

About the details, please refer to functionbdist_wheel in thepyarmor/build_meta.py

It’s a simple script, and only implements basic functions, if something is wrongwith the script, do it manully or write a shell script as the following steps:

  1. Build wheel with original package
  2. Unpack this wheel to temporary path by this command::
    python3 -m wheel unpack –dest /path/to/temp xxx.whl
  3. Obfuscate the scripts bypyarmor obfuscate, then overrite all the.pyfiles in the unpack path with the obfuscated ones.
  4. Append the pyarmor runtime files to wheel fileRECORD, search it in theunpack path, the format please refer to the existing items
  5. Repack the patched wheel::
    python3 -m wheel pack /path/to/temp

Pull request for this feature is welcomed if there is any further requirement.

Important

Build pyarmored wheel is a helper function, there is no more support forthis.

If you don’t know how to build a wheel from a package which includes binaryfile, please learn it by yourself, then take the obfuscated scripts as thenormal scripts, refer toKey Points to Use Obfuscated Scripts.