| 1 | #!/usr/bin/env bash |
| 2 | |
| 3 | set -euo pipefail |
| 4 | |
| 5 | PROJECT_ROOT="$(realpath "$(dirname "$0")/..")" |
| 6 | |
| 7 | # Which requirements.txt to pass to pip |
| 8 | REQUIREMENTS_FILE="${PROJECT_ROOT}/requirements.txt" |
| 9 | |
| 10 | # Where to put the final output in |
| 11 | PYLIBS_DIR="${PROJECT_ROOT}/pylibs" |
| 12 | |
| 13 | # Distinguish Ubuntu Touch versions |
| 14 | UT_FRAMEWORK="${SDK_FRAMEWORK:-''}" |
| 15 | |
| 16 | if [[ -n "$UT_FRAMEWORK" ]]; then |
| 17 | PYLIBS_DIR="${PROJECT_ROOT}/pylibs-${UT_FRAMEWORK}" |
| 18 | REQUIREMENTS_FILE="${PROJECT_ROOT}/requirements-${UT_FRAMEWORK}.txt" |
| 19 | fi |
| 20 | |
| 21 | echo "Using $REQUIREMENTS_FILE for resolving dependencies" |
| 22 | |
| 23 | # List the glob patterns of files/directories you want in the qml plugin |
| 24 | # The idea here is to only declare what's _required_ to reduce our size a bit |
| 25 | PYTHON_INCLUDED_PATTERNS=( |
| 26 | "soupsieve" |
| 27 | "markdown" |
| 28 | "jinja2" |
| 29 | "markupsafe" |
| 30 | "bs4" |
| 31 | "cssutils" |
| 32 | "encutils" |
| 33 | # "pynliner" # we include our own instead |
| 34 | "pygments" |
| 35 | "html2text" |
| 36 | "importlib_metadata" |
| 37 | "zipp" |
| 38 | "typing_extensions*" |
| 39 | ) |
| 40 | |
| 41 | # Remove something from the copied-over modules, to improve final size |
| 42 | PYTHON_EXCLUDED_PATHS=( |
| 43 | # cssutils comes with a large test suite |
| 44 | # which we don't really need in the snap |
| 45 | # it saves us about 1MB |
| 46 | "cssutils/tests" |
| 47 | ) |
| 48 | |
| 49 | if [ ! -f "${REQUIREMENTS_FILE}" ]; then |
| 50 | echo "${REQUIREMENTS_FILE} does not exist!" >&2 |
| 51 | exit 1 |
| 52 | fi |
| 53 | |
| 54 | if ! command -v pip3; then |
| 55 | echo "pip3 binary not found!" >&2 |
| 56 | exit 1 |
| 57 | fi |
| 58 | |
| 59 | # Clear our output dir |
| 60 | rm -vfr "${PYLIBS_DIR}" |
| 61 | mkdir -vp "${PYLIBS_DIR}" |
| 62 | |
| 63 | PYTHONUSERBASE="$(mktemp -d)" |
| 64 | echo "Set temporary PYTHONUSERBASE to: ${PYTHONUSERBASE}" |
| 65 | export PYTHONUSERBASE |
| 66 | |
| 67 | echo "Installing required modules via pip..." |
| 68 | pip3 install --upgrade -r "${REQUIREMENTS_FILE}" --user |
| 69 | PYTHON_PACKAGES_DIR="$(python3 -m site --user-site)" |
| 70 | echo "Python packages were installed to: ${PYTHON_PACKAGES_DIR}" |
| 71 | echo "Installation done." |
| 72 | |
| 73 | echo "Copying over requested python modules..." |
| 74 | for pattern in "${PYTHON_INCLUDED_PATTERNS[@]}"; do |
| 75 | cp -vr "${PYTHON_PACKAGES_DIR}/"${pattern} "${PYLIBS_DIR}/" |
| 76 | done |
| 77 | echo "Copying done." |
| 78 | |
| 79 | echo "Trimming unneeded things from the modules..." |
| 80 | for excludedPath in "${PYTHON_EXCLUDED_PATHS[@]}"; do |
| 81 | rm -vr "${PYLIBS_DIR}/${excludedPath:?entry in PYTHON_EXCLUDED_PATHS is null or unset}" |
| 82 | done |
| 83 | echo "Trimming done." |
| 84 | |
| 85 | echo "Cleaning up pip data..." |
| 86 | rm -vr "${PYTHONUSERBASE}" |
| 87 | echo "Cleaning done." |
| 88 | |
| 89 | echo "Done! Data has been installed to: ${PYLIBS_DIR}" |