Skip to content

Instantly share code, notes, and snippets.

@codewithmichael
Last active November 26, 2022 07:31
Show Gist options
  • Save codewithmichael/bad9050e631c3fec18565e29a6c8997c to your computer and use it in GitHub Desktop.
Save codewithmichael/bad9050e631c3fec18565e29a6c8997c to your computer and use it in GitHub Desktop.
Export a Godot-Python project in a Linux-distributable format.
#!/bin/bash
#
# FILE: export-godot-python.sh
# AUTH: Michael Spencer <code.with.michael@gmail.com>
# DATE: 2022.10.23
# COPY: MIT License, Copyright (c) 2022, Michael Spencer
#
# DESC:
# Export a Godot-Python project in a Linux-distributable format.
#
# DEPS:
# Linux -
# Godot Game Engine - v3.5.1 - https://godotengine.org/
# Godot-Python - v0.50.0 - https://github.com/touilleMan/godot-python
# GodotPckTool - v1.7 - https://github.com/hhyyrylainen/GodotPckTool
#
# REQS:
# Godot Project using the PythonScript addon.
# Project must be pre-configured for export using the "Linux/X11" template.
# All python scripts must be in a project-level "Scripts" directory.
# Godot must be available at "godot" in your $PATH.
# GodotPckTool must be available at "godotpcktool" in your $PATH.
set -e
USAGE="$(basename "$0") [-h] <project_directory> <export_directory>"
case "$1" in
-h|--help) echo "USAGE: $USAGE" && exit 0 ;;
esac
# Requires godot and godotpcktool be installed in $PATH
GD=godot
GDPCK=godotpcktool
# Parse Args
test -z "$1" -o -z "$2" && >&2 echo "USAGE: $USAGE" && exit 1
PROJECT_DIR=$(realpath "$1")
EXPORT_DIR=$(realpath "$2")
# Verify directories
grep -q "^$PROJECT_DIR/" <<< "$EXPORT_DIR/" && >&2 echo "Project and Export directories cannot overlap" && exit 1
test ! -d "$PROJECT_DIR" && >&2 echo "Project directory does not exist" && exit 1
test ! -d "$EXPORT_DIR" && mkdir -p "$EXPORT_DIR"
PROJECT_NAME=$(basename "$PROJECT_DIR")
# Clean built files
>/dev/null rm -rf \
"$EXPORT_DIR/libpythonscript.so" \
"$EXPORT_DIR/$PROJECT_NAME.x86_64" \
"$EXPORT_DIR/$PROJECT_NAME.pck" \
"$EXPORT_DIR/addons" \
"$EXPORT_DIR/Scripts"
# Export project
"$GD" --path "$PROJECT_DIR" --export "Linux/X11" "$EXPORT_DIR/$PROJECT_NAME.x86_64"
# Clean extra lib (if present)
>/dev/null rm -rf \
"$EXPORT_DIR/libpythonscript.so" \
# Copy PythonScript
mkdir -p "$EXPORT_DIR/addons/pythonscript"
mkdir -p "$EXPORT_DIR/Scripts"
cp -a "$PROJECT_DIR/addons/pythonscript/x11-64" "$EXPORT_DIR/addons/pythonscript"
cp "$PROJECT_DIR/addons/pythonscript/LICENSE.txt" "$EXPORT_DIR/addons/pythonscript"
# Create dummy Scripts
for f in "$PROJECT_DIR/Scripts/"*.py; do
touch "$EXPORT_DIR/Scripts/$(basename $f)"
done
# Repack PCK with dummy scripts
pushd "$EXPORT_DIR"
"$GDPCK" "$PROJECT_NAME.pck" -a r -e "/Scripts/.*.py" -a a "Scripts/"
popd
# Copy real Scripts
for f in "$PROJECT_DIR/Scripts/"*.py; do
cp "$f" "$EXPORT_DIR/Scripts/$(basename $f)"
done
# Compile Scripts
PYTHON="$EXPORT_DIR/addons/pythonscript/x11-64/bin/python3.8"
chmod 755 "$PYTHON"
pushd "$EXPORT_DIR/Scripts"
for f in *.py; do
"$PYTHON" -OO -m py_compile "$f"
mv "__pycache__/"*.pyc "${f}c"
rm "$f"
done
rm -rf "__pycache__"
popd
# Uncomment to extract PCK for examination
#"$GDPCK" "$EXPORT_DIR/$PROJECT_NAME.pck" -a e -o "$EXPORT_DIR/extracted"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment