Last active
March 4, 2019 10:58
-
-
Save larpon/96d3b0da520a69c6d061f8f562953667 to your computer and use it in GitHub Desktop.
Patch a Krita appimage with a pip package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
# Add pip packages to a Krita .appimage | |
# - as descriped here https://gist.github.com/Larpon/e81ed68895bdba35098d9d84688ac01e | |
# | |
# The script is working on a copy of the appimage to ensure any failed | |
# runs doesn't make your krita unusable | |
# You can patch an appimage multiple times | |
# TODO support installing multiple packages in one go | |
SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | |
SCRIPT_NAME="$(basename "$0")" | |
export PID=$$ | |
_PWD=$(pwd) | |
VERSION=0.1 | |
_PIP_PACKAGE="" | |
col() { | |
if [[ $# -eq 0 ]] ; then return; fi | |
local c=$1 | |
local nocolor="\033[0m" | |
local color="$nocolor" | |
# More colors at https://misc.flogisoft.com/bash/tip_colors_and_formatting | |
if [ $c == "black" ]; then color="\e[30m"; fi | |
if [ $c == "red" ]; then color="\e[31m"; fi | |
if [ $c == "green" ]; then color="\e[32m"; fi | |
if [ $c == "yellow" ]; then color="\e[33m"; fi | |
if [ $c == "blue" ]; then color="\e[34m"; fi | |
if [ $c == "magenta" ]; then color="\e[35m"; fi | |
if [ $c == "cyan" ]; then color="\e[36m"; fi | |
if [ $c == "light-grey" ] || [ $c == "grey" ]; then color="\e[37m"; fi | |
if [ $c == "dark-grey" ]; then color="\e[90m"; fi | |
if [ $c == "white" ]; then color="\e[97m"; fi | |
echo -e "${color}${2}${nocolor}" | |
} | |
info() { | |
echo -e "[$(col blue "INFO")] "$@ | |
} | |
error() { | |
>&2 echo -e "[$(col red "ERROR")] "$@ | |
} | |
strip_slashes() { | |
local stripped="$1" | |
case $stripped in | |
*[!/]*/) stripped=${stripped%"${stripped##*[!/]}"};; | |
esac | |
echo "$stripped" | |
} | |
ensure_path() { | |
if [ ! -d "$1" ]; then | |
mkdir -p "$1" | |
fi | |
} | |
temp_path() | |
{ | |
local _tmp_path="/tmp/krita-pip/$1" | |
ensure_path "$_tmp_path" | |
echo "$(strip_slashes "$_tmp_path")" | |
} | |
cmdavail () { | |
command -v "$1" >/dev/null 2>&1 | |
} | |
check() { | |
cmdavail $1 || { error >&2 "$2"; exit 1; } | |
} | |
trap "exit 1" TERM | |
die() { | |
error $@ | |
kill -s TERM $PID | |
} | |
rms() { | |
local _dir="$1" | |
if [ -e "$_dir" ] && [ "$_dir" != "" ] && [ "$_dir" != "/" ]; then | |
info "Removing \"$1\"" | |
cd "$1" && rm -rf * && cd "$_PWD" | |
fi | |
} | |
usage() { | |
cat << EOF | |
VERSION: $VERSION | |
USAGE: | |
$SCRIPT_NAME [options] | |
DESCRIPTION: | |
Patch a Krita appimage | |
AUTHOR: | |
Lars Pontoppidan <dev.larpon@gmail.com> | |
LICENSE: | |
MIT license | |
OPTIONS: | |
-h | |
Print this message and exit | |
-p <package_name> | |
Name of pip package to add to the Krita image | |
EXAMPLES: | |
* Patch a Krita .appimage with PIL (Pillow) | |
# $SCRIPT_NAME -p pillow ./krita-4.1.7-x86_64.appimage | |
EOF | |
} | |
check appimagetool "This tool requires \"appimagetool\" (https://github.com/AppImage/AppImageKit)" | |
check pyenv "This tool requires \"pyenv\" (https://github.com/pyenv/pyenv-installer)" | |
while getopts ":hp:" OPTION | |
do | |
case $OPTION in | |
h ) usage && exit 0 ;; | |
p ) _PIP_PACKAGE=$OPTARG ;; | |
* ) usage; die "One or more options not recognized..." ;; # DEFAULT | |
esac | |
done | |
shift "$(($OPTIND - 1))" # Move argument pointer to next argument. | |
_KRITA_APPIMAGE="$1" | |
rms "$(temp_path)" | |
if [ -f "$1" ]; then | |
info "Copying \"$_KRITA_APPIMAGE\" to $(temp_path) for patching" | |
cp "$1" "$(temp_path)/" | |
else | |
die "Please provide a valid path to a Krita appimage (\"$_KRITA_APPIMAGE\" does not exist)" | |
fi | |
cd "$(temp_path)" | |
_KRITA_APPIMAGE="$(temp_path)/$( basename "$_KRITA_APPIMAGE" )" | |
info "Extracting \"$_KRITA_APPIMAGE\" to $(temp_path) for patching" | |
$_KRITA_APPIMAGE --appimage-extract | |
_KRITA_EXTRACTED_PATH="$(temp_path)/squashfs-root" | |
_PYTHON_ENV="$(temp_path "py")" | |
info "Setting up python env in \"$_PYTHON_ENV\"" | |
cd "$_PYTHON_ENV" | |
# TODO auto-detect krita python version | |
pyenv install 3.5.2 | |
pyenv local 3.5.2 | |
info "Installing \"$_PIP_PACKAGE\"" | |
pip install $_PIP_PACKAGE | |
info "Copying to Krita work directory" | |
cp -r "$PYENV_ROOT/versions/3.5.2/lib/python3.5/site-packages" "$_KRITA_EXTRACTED_PATH/usr/lib/python3.5/" | |
info "Removing Krita copy $_KRITA_APPIMAGE" | |
rm "$_KRITA_APPIMAGE" | |
info "Re-packaging patched Krita" | |
appimagetool "$_KRITA_EXTRACTED_PATH" "$_KRITA_APPIMAGE" | |
chmod +x "$_KRITA_APPIMAGE" | |
info "Patched Krita image written to $_KRITA_APPIMAGE" | |
info "Have a productive time!" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment