diff --git a/src/examples/README.md b/src/examples/README.md index 965cb1f2..db6c5603 100644 --- a/src/examples/README.md +++ b/src/examples/README.md @@ -1,5 +1,28 @@ # Exmaples +Content + +* [README-ZH.md](README-ZH.md) 中文版 + +## Sample Shell Scripts + +Here are some rich comment shell scripts used to obfuscate Python scripts in +different cases, `.bat` for Windows, `.sh` for Linux and MacOS. Download one of +them, edit the variables in it according to actual enviroments, then run it to +obfuscate your python scripts quickly. + +* [obfuscate-app.bat](obfuscate-app.bat) / [obfuscate-app.sh](obfuscate-app.sh) + +* [obfuscate-pkg.bat](obfuscate-pkg.bat) / [obfuscate-pkg.sh](obfuscate-pkg.sh) + +* [build-with-project.bat](build-with-project.bat) / [build-with-project.sh](build-with-project.sh) + + Increment build, only updated scripts are obfuscated since last build + Advanced filters python scripts in the src path + More convenient to manage obfuscated scripts + +* [build-for-exe.bat](build-for-exe.bat) / [build-for-exe.sh](build-for-exe.sh) + A good example is the best teacher. Here are several typical scenarios * [Obfuscate script](#obfuscate-script) @@ -7,6 +30,15 @@ A good example is the best teacher. Here are several typical scenarios * [Bind to fixed machines](#bind-to-fixed-machines) * [Work with py2exe](#work-with-py2exe) +How to run these examples, + +``` + pip install pyarmor + cd /path/to/pyarmor + cd examples + +``` + In the following sections, assumes that Python is installed, it can be called diff --git a/src/examples/build-with-project.sh b/src/examples/build-with-project.sh new file mode 100755 index 00000000..25144ede --- /dev/null +++ b/src/examples/build-with-project.sh @@ -0,0 +1,98 @@ +# +# Sample script used to obfuscate python source files with prroject +# +# There are several advantages to manage obfuscated scripts by project: +# +# Increment build, only updated scripts are obfuscated since last build +# Filter scripts, for example, exclude all the test scripts +# More convenient command to manage obfuscated scripts +# + +# TODO: python interpreter +PYTHON=python + +# TODO: Absolute path of pyarmor installed, where to find pyarmor.py +PYARMOR_PATH=/usr/local/lib/ + +# TODO: Absolute path in which all python scripts will be obfuscated +SOURCE=/home/jondy/workspace/project/src + +# TODO: Entry script filename, must be relative to $SOURCE +# For package, set to __init__.py +ENTRY_SCRIPT=__init__.py + +# TODO: output path for saving project config file, and obfuscated scripts +PROJECT=/home/jondy/workspace/project/pyarmor-dist + +# TODO: Filter the source files +#PROJECT_FILTER="global-include *.py, prune: test" + +# TODO: If generate special license for obfuscated scripts, uncomment next line +#LICENSE_CODE=special-user + +# Extra information for special license, uncomment the corresponding lines as your demand +# They're useness if LICENSE_CODE is not set + +#LICENSE_EXPIRED_DATE=2019-01-01 +#LICENSE_HARDDISK_SERIAL_NUMBER=SF210283KN +#LICENSE_MAC_ADDR=70:38:2a:4d:6f +#LICENSE_IPV4_ADDR=192.168.121.101 + +# TODO: If try to test obfuscated files, uncomment next line +#TEST_OBFUSCATED_FILES=1 + +# Set PACKAGE_NAME if it's a package +if [[ "${ENTRY_SCRIPT}" == "__init__.py" ]] ; then + PACKAGE_NAME=$(basename $SOURCE) +fi + +# Create a project +cd ${PYARMOR_PATH} +$PYTHON pyarmor.py init --src=$SOURCE --entry=${ENTRY_SCRIPT} $PROJECT + +# Change to project path, there is a convenient script pyarmor.bat +cd $PROJECT + +# Filter source files by config project filter +if [[ -n "${PROJECT_FILTER}" ]] ; then + ./pyarmor.sh config --manifest "${PROJECT_FILTER}" || exit 1 +fi + + +# Obfuscate scripts by command build +./pyarmor.sh build || exit 1 + +# Generate special license if any +if [[ -n "${LICENSE_CODE}" ]] ; then + LICENSE_OPTIONS="" + [[ -n "${LICENSE_EXPIRED_DATE SET}" ]] && LICENSE_OPTIONS="${LICENSE_OPTIONS} --expired %LICENSE_EXPIRED_DATE%" + [[ -n "${LICENSE_HARDDISK_SERIAL_NUMBER}" ]] && LICENSE_OPTIONS="${LICENSE_OPTIONS} --bind-disk ${LICENSE_HARDDISK_SERIAL_NUMBER}" + [[ -n "${LICENSE_MAC_ADDR}" ]] && LICENSE_OPTIONS="${LICENSE_OPTIONS} --bind-mac ${LICENSE_MAC_ADDR}" + [[ -n "${LICENSE_IPV4_ADDR}" ]] && LICENSE_OPTIONS="${LICENSE_OPTIONS} --bind-ipv4 ${LICENSE_IPV4_ADDR}" + + ./pyarmor.sh licenses ${LICENSE_OPTIONS} ${LICENSE_CODE} || exit 1 + + # Overwrite default license with this license + echo Replace default license with "licenses/${LICENSE_CODE}/license.lic" + if [[ -n "${PACKAGE_NAME}" ]] ; then + cp licenses/${LICENSE_CODE}/license.lic $PROJECT/dist + else + cp licenses/${LICENSE_CODE}/license.lic $PROJECT/dist/${PACKAGE_NAME} + fi +fi + +# Run obfuscated scripts if +if [[ "${TEST_OBFUSCATED_FILES}" == "1" && -n "${ENTRY_SCRIPT}" ]] ; then + + # Test package + if [[ -n "${PACKAGE_NAME}" ]] ; then + cd $PROJECT/dist + $PYTHON -c "import ${PACKAGE_NAME}" + + # Test script + else + cd $PROJECT/dist + $PYTHON ${ENTRY_SCRIPT} + fi + +fi diff --git a/src/examples/obfuscate-app.sh b/src/examples/obfuscate-app.sh new file mode 100755 index 00000000..872343f6 --- /dev/null +++ b/src/examples/obfuscate-app.sh @@ -0,0 +1,45 @@ +# +# Sample script used to obfuscate python scripts. +# +# Before run it, all TODO variables need to set correctly. +# + +# TODO: python interpreter +PYTHON=python + +# TODO: Absolute path of pyarmor installed, where to find pyarmor.py +PYARMOR_PATH=/usr/local/lib/ + +# TODO: Absolute path in which all python scripts will be obfuscated +SOURCE=/home/jondy/workspace/project/src + +# TODO: Entry script filename, must be relative to $SOURCE +ENTRY_SCRIPT=main.py + +# TODO: Output path for obfuscated scripts and runtime files +OUTPUT=/home/jondy/workspace/project/dist + +# TODO: Let obfuscated scripts expired on some day, uncomment next line +# LICENSE_EXPIRED_DATE=2019-01-01 + +# TODO: If try to run obfuscated scripts, uncomment next line +# TEST_OBFUSCATED_SCRIPTS=1 + +# Obfuscate scripts +cd ${PYARMOR_PATH} +$PYTHON pyarmor.py obfuscate --recursive --src "$SOURCE" --entry "${ENTRY_SCRIPT}" --output $OUTPUT || exit 1 + +# Generate an expired license if any +if ! [[ "${LICENSE_EXPIRED_DATE}" == "" ]] ; then + LICENSE_CODE="expired-${LICENSE_EXPIRED_DATE}" + $PYTHON pyarmor.py licenses --expired ${LICENSE_EXPIRED_DATE} ${LICENSE_CODE} || exit 1 + + # Overwrite default license with this expired license + echo The obfuscated scripts will be expired on ${LICENSE_EXPIRED_DATE} + cp licenses\${LICENSE_CODE}\license.lic $OUTPUT + +# Run obfuscated scripts +if [[ "${TEST_OBFUSCATED_SCRIPTS}" == "1" ]] ; then + cd $OUTPUT + $PYTHON ${ENTRY_SCRIPT} +fi diff --git a/src/examples/obfuscate-pkg.sh b/src/examples/obfuscate-pkg.sh new file mode 100755 index 00000000..1c88613c --- /dev/null +++ b/src/examples/obfuscate-pkg.sh @@ -0,0 +1,51 @@ +# +# Sample script used to obfuscate a python package. +# +# Before run it, all TODO variables need to set correctly. +# + +# TODO: python interpreter +PYTHON=python + +# TODO: Absolute path of pyarmor installed, where to find pyarmor.py +PYARMOR_PATH=/usr/local/lib/ + +# TODO: Absolute path in which all python scripts will be obfuscated +SOURCE=/home/jondy/workspace/project/src + +# TODO: Package name, __init__.py shoule be in $SOURCE/$PKGNAME +PKGNAME=foo + +# TODO: Output path for obfuscated package and runtime files +OUTPUT=/home/jondy/workspace/project/dist + +# TODO: Let obfuscated package expired on some day, uncomment next line +#LICENSE_EXPIRED_DATE=2019-01-01 + +# TODO: If try to test obfuscated package, uncomment next line +#TEST_OBFUSCATED_PACKAGE=1 + +# Check package +PKGPATH=$SOURCE/$PKGNAME +[[ -f "$PKGPATH/__init__.py" ]] || ( echo "No __init__.py found in package path $PKGPATH" && exit 1 ) + + +# Obfuscate scripts +cd ${PYARMOR_PATH} +$PYTHON pyarmor.py obfuscate --recursive --src "$PKGPATH" --entry "__init__.py" --output "${OUTPUT}/$PKGNAME" || exit 1 + +# Generate an expired license if any +if ! [[ "${LICENSE_EXPIRED_DATE}" == "" ]] ; then + RCODE="expired-${LICENSE_EXPIRED_DATE}" + $PYTHON pyarmor.py licenses --expired ${LICENSE_EXPIRED_DATE} $RCODE || exit 1 + + # Overwrite default license with this expired license + echo "The obfuscated scripts will be expired on ${LICENSE_EXPIRED_DATE}" + cp licenses/$RCODE/license.lic ${OUTPUT}/$PKGNAME +) + +# Run obfuscated scripts if +if [[ "${TEST_OBFUSCATED_PACKAGE}" == "1" ]] ; then + cd ${OUTPUT} + $PYTHON -c "import $PKGNAME" +fi