Compare commits

..

No commits in common. "master" and "master" have entirely different histories.

1382 changed files with 14215 additions and 45303 deletions

File diff suppressed because it is too large Load diff

View file

@ -1,46 +0,0 @@
#!/bin/sh
export SRCDIR="$PWD"
export WEBAPP_DIR="$SRCDIR/platypush/backend/http/webapp"
export SKIPCI="$PWD/.skipci"
rm -rf "$SKIPCI"
. .drone/macros/configure-git.sh
cd "$WEBAPP_DIR"
if [ $(git log --pretty=oneline $DRONE_COMMIT_AFTER...$DRONE_COMMIT_BEFORE . | wc -l) -eq 0 ]; then
echo "No UI changes detected, skipping build"
exit 0
fi
if [ "$(git log --pretty=format:%s HEAD...HEAD~1 | head -1)" == "[Automatic] Updated UI files" ]; then
echo "UI changes have already been committed, skipping build"
exit 0
fi
rm -rf dist node_modules
npm install
npm run build
if [ $(git status --porcelain dist | wc -l) -eq 0 ]; then
echo "No build files have been changed"
exit 0
fi
# Create a .skipci file to mark the fact that the next steps should be skipped
# (we're going to do another push anyway, so another pipeline will be triggered)
touch "$SKIPCI"
cd "$SRCDIR"
. .drone/macros/configure-ssh.sh
. .drone/macros/configure-gpg.sh
git add "${WEBAPP_DIR}/dist"
git commit "${WEBAPP_DIR}/dist" -S -m "[Automatic] Updated UI files" --no-verify
git remote rm origin
git remote add origin git@git.platypush.tech:platypush/platypush.git
git push -f origin master
# Restore the original git configuration
mv "$TMP_GIT_CONF" "$GIT_CONF"

View file

@ -1,23 +0,0 @@
#!/bin/sh
. .drone/macros/configure-git.sh
. .drone/macros/configure-ssh.sh
ssh-keyscan github.com >> ~/.ssh/known_hosts 2>/dev/null
# Clone the repository
branch=$(git rev-parse --abbrev-ref HEAD)
if [ -z "${branch}" ]; then
echo "No branch checked out"
exit 1
fi
git remote add github git@github.com:/blacklight/platypush.git
if [[ "$branch" == "master" ]]; then
git pull --rebase github "${branch}" || echo "No such branch on Github"
fi
# Push the changes to the GitHub mirror
git push -f --all -v github
git push --tags -v github

View file

@ -1,29 +0,0 @@
#!/bin/sh
# Install git
if [ -z "$(which git)" ]; then
if [ -n "$(which apt-get)" ]; then
apt-get update
apt-get install -y git
elif [ -n "$(which apk)" ]; then
apk add --update --no-cache git
elif [ -n "$(which yum)" ]; then
yum install -y git
elif [ -n "$(which dnf)" ]; then
dnf install -y git
elif [ -n "$(which pacman)" ]; then
pacman -Sy --noconfirm git
else
echo "Could not find a package manager to install git"
exit 1
fi
fi
# Backup the original git configuration before changing attributes
export GIT_CONF="$PWD/.git/config"
export TMP_GIT_CONF=/tmp/git.config.orig
cp "$GIT_CONF" "$TMP_GIT_CONF"
git config --global --add safe.directory "$PWD"
git config user.name "Platypush CI/CD Automation"
git config user.email "admin@platypush.tech"

View file

@ -1,30 +0,0 @@
#!/bin/sh
[ -z "$PGP_KEY" ] && echo "PGP_KEY is not set" && exit 1
[ -z "$PGP_KEY_ID" ] && echo "PGP_KEY_ID is not set" && exit 1
# Install gpg
if [ -z "$(which gpg)" ]; then
if [ -n "$(which apt-get)" ]; then
apt-get update
apt-get install -y gnupg
elif [ -n "$(which apk)" ]; then
apk add --update --no-cache bash gnupg
elif [ -n "$(which yum)" ]; then
yum install -y gnupg
elif [ -n "$(which dnf)" ]; then
dnf install -y gnupg
elif [ -n "$(which pacman)" ]; then
pacman -Sy --noconfirm gnupg
else
echo "Could not find a package manager to install gnupg"
exit 1
fi
fi
cat <<EOF | gpg --import --armor
$PGP_KEY
EOF
git config commit.gpgsign true
git config user.signingkey "$PGP_KEY_ID"

View file

@ -1,35 +0,0 @@
#!/bin/sh
if [ -z "$SSH_PUBKEY" ] || [ -z "$SSH_PRIVKEY" ]; then
echo "SSH_PUBKEY and SSH_PRIVKEY environment variables must be set"
exit 1
fi
# Install ssh
if [ -z "$(which ssh)" ]; then
if [ -n "$(which apt-get)" ]; then
apt-get update
apt-get install -y openssh
elif [ -n "$(which apk)" ]; then
apk add --update --no-cache openssh
elif [ -n "$(which yum)" ]; then
yum install -y openssh
elif [ -n "$(which dnf)" ]; then
dnf install -y openssh
elif [ -n "$(which pacman)" ]; then
pacman -Sy --noconfirm openssh
else
echo "Could not find a package manager to install openssh"
exit 1
fi
fi
mkdir -p ~/.ssh
echo $SSH_PUBKEY > ~/.ssh/id_rsa.pub
cat <<EOF > ~/.ssh/id_rsa
$SSH_PRIVKEY
EOF
chmod 0600 ~/.ssh/id_rsa
ssh-keyscan git.platypush.tech >> ~/.ssh/known_hosts 2>/dev/null

View file

@ -1,27 +0,0 @@
#!/bin/sh
echo "Installing required build dependencies"
apk add --update --no-cache git make py3-sphinx py3-myst-parser py3-pip $(cat platypush/install/requirements/alpine.txt)
pip install -U sphinx-rtd-theme sphinx-book-theme --break-system-packages
pip install . --break-system-packages
mkdir -p /docs/current
export APPDIR="$PWD"
rm -rf "$APPDIR/docs/build"
echo "Building the updated documentation"
cd "$APPDIR/docs/source"
git clone 'https://git.platypush.tech/platypush/platypush.wiki.git' wiki
echo "Linking the wiki to the Sphinx index"
cd wiki
cd "$APPDIR/docs"
make html
rm -f config*.yaml
cd "$APPDIR"
echo "Copying the new documentation files to the target folder"
mv -v "$APPDIR/docs/build" /docs/new
cd /docs
mv current old
mv new current
rm -rf old

View file

@ -1,6 +0,0 @@
#!/bin/sh
apk add --update --no-cache $(cat platypush/install/requirements/alpine.txt)
pip install . --break-system-packages
pip install -r requirements-tests.txt --break-system-packages
pytest tests

View file

@ -1,169 +0,0 @@
#!/bin/sh
[ -f .skipci ] && exit 0
echo "-- Installing dependencies"
apt update
apt install -y dpkg-dev gpg
echo "-- Creating a new apt root folder"
export TMP_APT_ROOT="/tmp/apt"
cp -r "$APT_ROOT" "$TMP_APT_ROOT"
echo "-- Cleaning up older apt releases"
find "$TMP_APT_ROOT/pool" -mindepth 2 -maxdepth 2 -type d | while read reldir; do
pkg_to_remove=$(( $(ls "$reldir"/*.deb | wc -l) - 1 ))
[ $pkg_to_remove -le 0 ] && continue
ls "$reldir"/*.deb | sort -V | head -n$pkg_to_remove | xargs rm -f
done
echo "-- Updating Packages files"
echo "stable\noldstable\nubuntu" | while read distro; do
echo "main\ndev" | while read branch; do
branch_dir="$TMP_APT_ROOT/pool/$distro/$branch"
echo "Checking pool folder: $branch_dir"
[ -d "$branch_dir" ] || mkdir -p "$branch_dir"
dist_dir="$TMP_APT_ROOT/dists/$distro/$branch/all"
mkdir -p "$dist_dir"
pkg_file="$dist_dir/Packages"
dpkg-scanpackages --arch all "$branch_dir" > "$pkg_file"
sed -i "$pkg_file" -re "s|^Filename: $TMP_APT_ROOT/|Filename: |"
cat "$pkg_file" | gzip -9 > "$pkg_file.gz"
echo "Generated Packages file: $pkg_file"
cat "$pkg_file"
done
done
echo "-- Updating Release files"
add_hashes() {
dist_dir=$1
hash_cmd=$2
hash_label=$3
echo "$hash_label:"
find "$dist_dir" -name 'Packages*' | while read file; do
basename="$(echo "$file" | sed -r -e "s|^$dist_dir/||")"
hash="$($hash_cmd "$file" | cut -d" " -f1)"
size="$(wc -c < $file)"
echo " $hash $size $basename"
echo " $hash $size $(echo $basename | sed -re 's|/all/|/binary-i386/|')"
echo " $hash $size $(echo $basename | sed -re 's|/all/|/binary-amd64/|')"
echo " $hash $size $(echo $basename | sed -re 's|/all/|/binary-armel/|')"
echo " $hash $size $(echo $basename | sed -re 's|/all/|/binary-armhf/|')"
echo " $hash $size $(echo $basename | sed -re 's|/all/|/binary-arm64/|')"
done
}
echo "stable\noldstable\nubuntu" | while read distro; do
dist_dir="$TMP_APT_ROOT/dists/$distro"
components=$(find "$dist_dir" -name Packages | awk -F '/' '{print $(NF-2)}' | uniq | tr '\n' ' ')
release_file="$dist_dir/Release"
cat <<EOF > "$release_file"
Origin: Platypush repository
Label: Platypush
Suite: $distro
Codename: $distro
Architectures: i386 amd64 armel armhf arm64
Components: $components
Description: The official APT repository for Platypush
Date: $(date -Ru)
EOF
add_hashes "$dist_dir" "md5sum" "MD5Sum" >> "$release_file"
add_hashes "$dist_dir" "sha1sum" "SHA1" >> "$release_file"
add_hashes "$dist_dir" "sha256sum" "SHA256" >> "$release_file"
done
echo "-- Generating list files"
mkdir -p "$TMP_APT_ROOT/lists"
for distro in stable oldstable ubuntu; do
for branch in main dev; do
echo "deb https://apt.platypush.tech/ $distro $branch" > "$TMP_APT_ROOT/lists/platypush-$distro-$branch.list"
done
done
echo "-- Updating index file"
cat <<EOF > "$TMP_APT_ROOT/index.txt"
Welcome to the Platypush APT repository!
Project homepage: https://platypush.tech
Source code: https://git.platypush.tech/platypush/platypush
Documentation / API reference: https://docs.platypush.tech
You can use this APT repository to install Platypush on Debian, Ubuntu or any
Debian-based distro.
Steps:
1. Add this repository's PGP key to your apt keyring
====================================================
# wget -q -O \\\
/etc/apt/trusted.gpg.d/platypush.asc \\\
https://apt.platypush.tech/pubkey.txt
2. Add the repository to your sources
=====================================
# wget -q -O \\\
/etc/apt/sources.list.d/platypush.list \\\
https://apt.platypush.tech/lists/platypush-<deb_version>-<branch>.list
Where:
- deb_version can be:
- *stable* - current Debian stable version
- *oldstable* - previous Debian stable version
- *ubuntu* - latest Ubuntu version
- branch can be either:
- *main* - latest stable release
- *dev* a package always in sync with the git version
For example, to install the latest stable tags on Debian stable:
# wget -q -O \\\
/etc/apt/sources.list.d/platypush.list \\\
https://apt.platypush.tech/lists/platypush-stable-main.list
3. Update your repos
====================
# apt update
4. Install Platypush
====================
# apt install platypush
EOF
echo "-- Importing and refreshing PGP key"
echo "$PGP_PUBKEY" > "$TMP_APT_ROOT/pubkey.txt"
export PGP_KEYID=$(echo "$PGP_PUBKEY" | gpg --with-colons --import-options show-only --import --fingerprint | grep -e '^fpr:' | head -1 | awk -F ':' '{print $(NF - 1)}')
cat <<EOF | gpg --import --armor
$PGP_PRIVKEY
EOF
echo "-- Signing Release files"
find "$TMP_APT_ROOT/dists" -type f -name Release | while read file; do
dirname="$(dirname "$file")"
cat "$file" | gpg -q --default-key "$PGP_KEYID" -abs > "$file.gpg"
cat "$file" | gpg -q --default-key "$PGP_KEYID" -abs --clearsign > "$dirname/InRelease"
done
echo "-- Updating the apt repo root"
export OLD_APT_ROOT="$REPOS_ROOT/oldapt"
rm -rf "$OLD_APT_ROOT"
mv "$APT_ROOT" "$OLD_APT_ROOT"
mv "$TMP_APT_ROOT" "$APT_ROOT"
chmod -R a+r "$APT_ROOT"
chmod a+x "$APT_ROOT"

View file

@ -1,38 +0,0 @@
#!/bin/sh
[ -f .skipci ] && exit 0
apk add --update --no-cache curl pacman sudo
. .drone/macros/configure-ssh.sh
. .drone/macros/configure-git.sh
git pull --rebase origin master --tags
export VERSION=$(python setup.py --version)
export HEAD=$(git log --pretty=format:%h HEAD...HEAD~1 | head -1)
export GIT_VERSION="$VERSION.r$(git log --pretty=oneline HEAD...v$VERSION | wc -l).g${HEAD}"
ssh-keyscan aur.archlinux.org >> ~/.ssh/known_hosts 2>/dev/null
adduser -u 1000 -D build
mkdir -p "$WORKDIR"
echo "--- Updating Arch git version"
export PKGDIR=$WORKDIR/git
git clone ssh://aur@aur.archlinux.org/platypush-git.git "$PKGDIR"
git config --global --add safe.directory "$PKGDIR"
chown -R build "$PKGDIR"
cd "$PKGDIR"
sed -i 'PKGBUILD' -r \
-e "s/^pkgver=.*/pkgver=$GIT_VERSION/" \
-e "s/^pkgrel=.*/pkgrel=1/" \
sudo -u build makepkg --printsrcinfo > .SRCINFO
export FILES_CHANGED=$(git status --porcelain --untracked-files=no | wc -l)
if [ $FILES_CHANGED -gt 0 ]; then
echo "--- Pushing git package version $GIT_VERSION"
git commit -a -m '[Automatic] Package update'
git push origin master
fi

View file

@ -1,46 +0,0 @@
#!/bin/sh
[ -f .skipci ] && exit 0
apk add --update --no-cache curl pacman sudo
. .drone/macros/configure-ssh.sh
. .drone/macros/configure-git.sh
git pull --rebase origin master --tags
export VERSION=$(python setup.py --version)
export TAG_URL="https://git.platypush.tech/platypush/platypush/archive/v$VERSION.tar.gz"
ssh-keyscan aur.archlinux.org >> ~/.ssh/known_hosts 2>/dev/null
adduser -u 1000 -D build
mkdir -p "$WORKDIR"
echo "--- Updating Arch stable version"
export PKGDIR="$WORKDIR/stable"
git clone ssh://aur@aur.archlinux.org/platypush.git "$PKGDIR"
git config --global --add safe.directory "$PKGDIR"
chown -R build "$PKGDIR"
cd "$PKGDIR"
export RELEASED_VERSION=$(grep -e '^pkgver=' PKGBUILD | sed -r -e 's/^pkgver=(.*)\s*/\1/')
if [ "$RELEASED_VERSION" == "$VERSION" ]; then
echo "--- No changes in the stable package version"
exit 0
fi
export TAG_CHECKSUM=$(curl --silent "$TAG_URL" | sha512sum | awk '{print $1}')
sed -i 'PKGBUILD' -r \
-e "s/^pkgver=.*/pkgver=$VERSION/" \
-e "s/^pkgrel=.*/pkgrel=1/" \
-e "s/^sha512sums=.*/sha512sums=('$TAG_CHECKSUM')/"
sudo -u build makepkg --printsrcinfo > .SRCINFO
export FILES_CHANGED=$(git status --porcelain --untracked-files=no | wc -l)
if [ $FILES_CHANGED -gt 0 ]; then
echo "--- Pushing stable package version $VERSION"
git commit -a -m '[Automatic] Package update'
git push origin master
fi

View file

@ -1,46 +0,0 @@
#!/bin/sh
export SKIPCI="$PWD/.skipci"
export CACHEFILE="$PWD/platypush/components.json.gz"
[ -f "$SKIPCI" ] && exit 0
# Backup the original git configuration before changing attributes
export GIT_CONF="$PWD/.git/config"
export TMP_GIT_CONF="/tmp/git.config.orig"
cp "$GIT_CONF" "$TMP_GIT_CONF"
. .drone/macros/configure-git.sh
# Only regenerate the components cache if either the plugins, backends,
# events or schemas folders have some changes (excluding the webapp files).
if [ -z "$(git log --pretty=oneline $DRONE_COMMIT_AFTER...$DRONE_COMMIT_BEFORE -- platypush/backend platypush/plugins platypush/schemas platypush/message/event ':(exclude)platypush/backend/http/webapp')" ]; then
echo 'No changes to the components file'
exit 0
fi
. .drone/macros/configure-ssh.sh
. .drone/macros/configure-gpg.sh
echo 'Updating components cache'
apk add --update --no-cache $(cat platypush/install/requirements/alpine.txt)
pip install . --break-system-packages
python - <<EOF
from platypush import get_plugin
get_plugin('inspect').refresh_cache(force=True)
EOF
# Create a .skipci file to mark the fact that the next steps should be skipped
# (we're going to do another push anyway, so another pipeline will be triggered)
touch "$SKIPCI"
git add "$CACHEFILE"
git commit "$CACHEFILE" -S -m "[Automatic] Updated components cache" --no-verify
git remote rm origin
git remote add origin git@git.platypush.tech:platypush/platypush.git
git push -f origin master
# Restore the original git configuration
mv "$TMP_GIT_CONF" "$GIT_CONF"

View file

@ -1,103 +0,0 @@
#!/bin/sh
[ -f .skipci ] && exit 0
echo "-- Copying source directory"
mkdir -p "$WORKDIR/src"
export SRCDIR="$WORKDIR/src/$DEB_VERSION"
cp -r "$PWD" "$SRCDIR"
cd "$SRCDIR"
echo "-- Installing dependencies"
export DEBIAN_FRONTEND=noninteractive
apt update
apt install -y curl dpkg-dev gpg git python3 python3-pip python3-setuptools
echo "--- Parsing metadata"
git config --global --add safe.directory "$PWD"
git pull --rebase origin master --tags
export VERSION=$(grep -e '^__version__' "${SRCDIR}/version.py" | sed -r -e 's/^__version__\s*=\s*"([^"]+)"$/\1/')
export GIT_VERSION="$VERSION-$(git log --pretty=oneline HEAD...v$VERSION | wc -l)"
export GIT_BUILD_DIR="$WORKDIR/${PKG_NAME}_${GIT_VERSION}_all"
export GIT_DEB="$WORKDIR/${PKG_NAME}_${GIT_VERSION}_all.deb"
export POOL_PATH="$APT_ROOT/pool/$DEB_VERSION/dev"
echo "--- Building git package"
pip install --prefix="$GIT_BUILD_DIR/usr" --no-cache --no-deps .
find "$GIT_BUILD_DIR" -name "site-packages" | while read dir; do
base="$(dirname "$dir")"
mv "$dir" "$base/dist-packages"
done
install -m755 -d "${GIT_BUILD_DIR}/usr/lib/systemd/system"
install -m755 -d "${GIT_BUILD_DIR}/usr/lib/systemd/user"
install -m750 -d "${GIT_BUILD_DIR}/var/lib/platypush"
install -m750 -d "${GIT_BUILD_DIR}/etc/platypush/scripts"
install -m644 "${SRCDIR}/platypush/config/config.yaml" "${GIT_BUILD_DIR}/etc/platypush/config.yaml"
install -m644 "${SRCDIR}/platypush/config/systemd/platypush.service" "${GIT_BUILD_DIR}/usr/lib/systemd/user/platypush.service"
install -m644 "${SRCDIR}/platypush/config/systemd/platypush.service" "${GIT_BUILD_DIR}/usr/lib/systemd/system/platypush.service"
sed -i "${GIT_BUILD_DIR}/usr/lib/systemd/system/platypush.service" -r \
-e 's/^#\s*Requires=(.*)/Requires=\1/' \
-e 's/^\[Service\]$/\[Service\]\
User=platypush\
Group=platypush\
WorkingDirectory=\/var\/lib\/platypush\
Environment="PLATYPUSH_CONFIG=\/etc\/platypush\/config.yaml"\
Environment="PLATYPUSH_WORKDIR=\/var\/lib\/platypush"/'
mkdir -p "$GIT_BUILD_DIR/DEBIAN"
cat <<EOF > "$GIT_BUILD_DIR/DEBIAN/control"
Package: $PKG_NAME
Version: $GIT_VERSION
Maintainer: Fabio Manganiello <fabio@platypush.tech>
Depends: $(cat platypush/install/requirements/debian.txt | tr '\n' ',' | sed -re 's/,$//' -e 's/,/, /g')
Architecture: all
Homepage: https://platypush.tech
Description: Universal command executor and automation hub.
EOF
cat <<EOF > "$GIT_BUILD_DIR/DEBIAN/postinst" && chmod +x "$GIT_BUILD_DIR/DEBIAN/postinst"
#!/bin/sh
set -e
if [ "\$1" = "configure" ]; then
grep -e '^platypush:' /etc/passwd 2>/dev/null || useradd -U -r -s /bin/false -d /var/lib/platypush platypush
mkdir -p /var/lib/platypush
chown -R platypush:platypush /var/lib/platypush
chown -R platypush:platypush /etc/platypush
if which systemctl; then systemctl daemon-reload; fi
fi
EOF
mkdir -p "$POOL_PATH"
rm -f "$POOL_PATH/"*.deb
dpkg --build "$GIT_BUILD_DIR"
echo "--- Copying $GIT_DEB to $POOL_PATH"
cp "$GIT_DEB" "$POOL_PATH"
# If main/all/Packages doesn't exist, then we should create the first main release
[ $(ls "$APT_ROOT/pool/$DEB_VERSION/main/${PKG_NAME}_${VERSION}-"*"_all.deb" 2>/dev/null | wc -l) -eq 0 ] && export UPDATE_STABLE_PKG=1
export PKGURL="https://apt.platypush.tech/dists/$DEB_VERSION/main/all/Packages"
[ -z "$UPDATE_STABLE_PKG" ] &&
curl -ILs -o /dev/null -w "%{http_code}" "$PKGURL" |
grep -e '^4' >/dev/null && export UPDATE_STABLE_PKG=1
# If the published release version differs from the current one, then we should publish a new main release
if [ -z "$UPDATE_STABLE_PKG" ]; then
RELEASED_VERSION=$(curl -s "$PKGURL" | grep -e '^Version: ' | head -1 | awk '{print $2}' | cut -d- -f 1)
[ "$RELEASED_VERSION" != "$VERSION" ] && export UPDATE_STABLE_PKG=1
fi
# Proceed and update the main release if the version number has changed
if [ -n "$UPDATE_STABLE_PKG" ]; then
echo "--- Updating main package"
mkdir -p "$APT_ROOT/pool/$DEB_VERSION/main"
cp "$GIT_DEB" "$APT_ROOT/pool/$DEB_VERSION/main/${PKG_NAME}_${VERSION}-1_all.deb"
fi

View file

@ -1,26 +0,0 @@
#!/bin/sh
[ -z "$DOCKER_USER" ] && echo "Please set the DOCKER_USER environment variable" && exit 1
[ -z "$DOCKER_PASS" ] && echo "Please set the DOCKER_PASS environment variable" && exit 1
export VERSION=$(grep current_version pyproject.toml | sed -r -e "s/.*=\s*['\"]?([^'\"]+)['\"]?\s*$/\1/")
export REGISTRY_ENDPOINT="${REGISTRY_ENDPOINT:-quay.io}"
export IMAGE_NAME="$REGISTRY_ENDPOINT/$DOCKER_USER/platypush"
# Log in to the registry
docker login "$REGISTRY_ENDPOINT" -u "$DOCKER_USER" -p "$DOCKER_PASS"
# Required for multi-platform builds
docker buildx create --name=multiarch --driver=docker-container
# Build and publish the images
docker buildx build \
-f Dockerfile.alpine \
-t "$IMAGE_NAME:$VERSION" \
-t "$IMAGE_NAME:latest" \
--platform linux/amd64,linux/arm64,linux/arm/v7 \
--builder multiarch \
--push .
# Clean up
docker buildx rm multiarch

View file

@ -1,5 +0,0 @@
#!/bin/sh
apk add --update --no-cache py3-twine py3-setuptools py3-wheel py3-pip
python setup.py sdist bdist_wheel
twine upload dist/platypush-$(python setup.py --version).tar.gz

View file

@ -1,261 +0,0 @@
#!/bin/sh
[ -f .skipci ] && exit 0
echo "-- Installing dependencies"
yum install -y \
createrepo \
git \
gpg \
python \
python-pip \
python-setuptools \
rpm-build \
rpm-sign \
systemd-rpm-macros \
wget \
yum-utils \
echo "-- Copying source directory"
mkdir -p "$WORKDIR"
export SRCDIR="$WORKDIR/src"
cp -r "$PWD" "$SRCDIR"
cd "$SRCDIR"
mkdir -p "$RPM_ROOT"
echo "--- Parsing metadata"
git config --global --add safe.directory $PWD
git pull --rebase origin master --tags
export VERSION=$(grep -e '^__version__' "${SRCDIR}/version.py" | sed -r -e 's/^__version__\s*=\s*"([^"]+)"$/\1/')
export RELNUM="$(git log --pretty=oneline HEAD...v$VERSION | wc -l)"
export SPECFILE="$WORKDIR/$PKG_NAME.spec"
export BUILD_DIR="$WORKDIR/build"
export TMP_RPM_ROOT="$WORKDIR/repo"
export SRC_URL="https://git.platypush.tech/platypush/platypush/archive/master.tar.gz"
echo "--- Creating git package spec"
cat <<EOF > $SPECFILE
Summary: Universal command executor and automation hub.
Name: $PKG_NAME-git
Version: $VERSION
Release: $RELNUM
URL: https://platypush.tech
Group: System
License: MIT
Packager: Fabio Manganiello <fabio@platypush.tech>
Source: $SRC_URL
Requires: $(cat platypush/install/requirements/fedora.txt | tr '\n' ' ')
Conflicts: $PKG_NAME
Prefix: %{_prefix}
BuildRoot: %{_tmppath}/%{name}-root
BuildRequires: systemd-rpm-macros
%{?sysusers_requires_compat}
%description
Universal command executor and automation hub.
%install
mkdir -p %{buildroot}/
cp -r "$BUILD_DIR"/* %{buildroot}/
install -p -Dm0644 "${BUILD_DIR}/usr/lib/sysusers.d/platypush.conf" %{buildroot}%{_sysusersdir}/platypush.conf
%pre
%sysusers_create_compat "${BUILD_DIR}/usr/lib/sysusers.d/platypush.conf"
%clean
%files
%defattr(750,platypush,platypush,750)
%dir /etc/platypush
/etc/platypush/*
/usr/bin/*
/usr/lib/python$(python3 --version | awk '{print $2}' | cut -d. -f 1,2)/site-packages/platypush
/usr/lib/python$(python3 --version | awk '{print $2}' | cut -d. -f 1,2)/site-packages/platypush-$VERSION.dist-info
/usr/lib/systemd/system/*
/usr/lib/systemd/user/*
%defattr(750,platypush,platypush,750)
%dir /var/lib/platypush
%{_sysusersdir}/platypush.conf
%changelog
* $(date +'%a %b %d %Y') admin <admin@platypush.tech>
- [Automatic] Release $VERSION-$RELNUM
EOF
echo "--- Building git package"
mkdir -p "$BUILD_DIR"
pip install --prefix="$BUILD_DIR/usr" --no-cache --no-deps .
install -m755 -d "${BUILD_DIR}/usr/lib/systemd/system"
install -m755 -d "${BUILD_DIR}/usr/lib/systemd/user"
install -m755 -d "${BUILD_DIR}/usr/lib/sysusers.d"
install -m750 -d "${BUILD_DIR}/var/lib/platypush"
install -m750 -d "${BUILD_DIR}/etc/platypush/scripts"
install -m644 "${SRCDIR}/platypush/config/config.yaml" "${BUILD_DIR}/etc/platypush/config.yaml"
install -Dm644 "${SRCDIR}/platypush/config/systemd/platypush-sysusers.conf" "${BUILD_DIR}/usr/lib/sysusers.d/platypush.conf"
install -m644 "${SRCDIR}/platypush/config/systemd/platypush.service" "${BUILD_DIR}/usr/lib/systemd/user/platypush.service"
install -m644 "${SRCDIR}/platypush/config/systemd/platypush.service" "${BUILD_DIR}/usr/lib/systemd/system/platypush.service"
sed -i "${BUILD_DIR}/usr/lib/systemd/system/platypush.service" -r \
-e 's/^#\s*Requires=(.*)/Requires=\1/' \
-e 's/^\[Service\]$/\[Service\]\
User=platypush\
Group=platypush\
WorkingDirectory=\/var\/lib\/platypush\
Environment="PLATYPUSH_CONFIG=\/etc\/platypush\/config.yaml"\
Environment="PLATYPUSH_WORKDIR=\/var\/lib\/platypush"/'
rpmbuild --target "noarch" -bb "$SPECFILE"
echo "--- Copying the new RPM package"
mkdir -p "$TMP_RPM_ROOT"
cp "$HOME/rpmbuild/RPMS/noarch/$PKG_NAME-git-$VERSION-$RELNUM.noarch.rpm" "$TMP_RPM_ROOT"
echo "--- Checking the latest released stable version"
export LATEST_STABLE_PKG=$(ls -rt "$RPM_ROOT/$PKG_NAME"*.rpm 2>/dev/null | grep -v "$PKG_NAME-git" | tail -1)
if [ -z "$LATEST_STABLE_PKG" ]; then
# If not stable release is available, then create one
export UPDATE_STABLE_PKG=1
else
# Otherwise, create a new release if the reported version on the repo is different
# from the latest released version.
export LATEST_STABLE_VERSION=$(basename $LATEST_STABLE_PKG | cut -d- -f 2)
if [ "$VERSION" != "$LATEST_STABLE_VERSION" ]; then
export UPDATE_STABLE_PKG=1
else
# If the version has remained the same, then simply copy the existing RPM to the
# new repository directory.
echo "Copying the existing release $LATEST_STABLE_VERSION to the new repository"
cp "$LATEST_STABLE_PKG" "$TMP_RPM_ROOT"
fi
fi
# If a new stable release is required, build another RPM
if [ -n "$UPDATE_STABLE_PKG" ]; then
export RELNUM=1
export SRC_URL="https://git.platypush.tech/platypush/platypush/archive/v$VERSION.tar.gz"
cat <<EOF > $SPECFILE
Summary: Universal command executor and automation hub.
Name: $PKG_NAME
Version: $VERSION
Release: $RELNUM
URL: https://platypush.tech
Group: System
License: MIT
Packager: Fabio Manganiello <fabio@platypush.tech>
Source: $SRC_URL
Requires: $(cat platypush/install/requirements/fedora.txt | tr '\n' ' ')
Conflicts: $PKG_NAME-git
Prefix: %{_prefix}
BuildRoot: %{_tmppath}/%{name}-root
BuildRequires: systemd-rpm-macros
%{?sysusers_requires_compat}
%description
Universal command executor and automation hub.
%install
mkdir -p %{buildroot}/
cp -r "$BUILD_DIR"/* %{buildroot}/
install -p -Dm0644 "${BUILD_DIR}/usr/lib/sysusers.d/platypush.conf" %{buildroot}%{_sysusersdir}/platypush.conf
%pre
%sysusers_create_compat "${BUILD_DIR}/usr/lib/sysusers.d/platypush.conf"
%clean
%files
%defattr(750,platypush,platypush,750)
%dir /etc/platypush
/etc/platypush/*
/usr/bin/*
/usr/lib/python$(python3 --version | awk '{print $2}' | cut -d. -f 1,2)/site-packages/platypush
/usr/lib/python$(python3 --version | awk '{print $2}' | cut -d. -f 1,2)/site-packages/platypush-$VERSION.dist-info
/usr/lib/systemd/system/*
/usr/lib/systemd/user/*
%defattr(750,platypush,platypush,750)
%dir /var/lib/platypush
%{_sysusersdir}/platypush.conf
%changelog
* $(date +'%a %b %d %Y') admin <admin@platypush.tech>
- [Automatic] Release $VERSION-$RELNUM
EOF
echo "--- Building package for stable release $VERSION"
rpmbuild --target "noarch" -bb "$SPECFILE"
cp "$HOME/rpmbuild/RPMS/noarch/$PKG_NAME-$VERSION-$RELNUM.noarch.rpm" "$TMP_RPM_ROOT"
fi
echo "--- Importing the repository keys"
cat <<EOF | gpg --import --armor
$PGP_PRIVKEY
EOF
export PGP_KEYID=$(echo "$PGP_PUBKEY" | gpg --with-colons --import-options show-only --import --fingerprint | grep -e '^fpr:' | head -1 | awk -F ':' '{print $(NF - 1)}')
cat <<EOF > $HOME/.rpmmacros
%signature gpg
%_gpg_name $PGP_KEYID
EOF
echo "--- Signing the new RPM packages"
rpm --addsign "$TMP_RPM_ROOT"/*.rpm
echo "--- Creating a new copy of the RPM repository"
createrepo "$TMP_RPM_ROOT"
gpg --detach-sign --armor "$TMP_RPM_ROOT/repodata/repomd.xml"
cat <<EOF > "$TMP_RPM_ROOT/platypush.repo"
[platypush]
name=Platypush repository
baseurl=https://rpm.platypush.tech
enabled=1
type=rpm
gpgcheck=1
gpgkey=https://rpm.platypush.tech/pubkey.txt
EOF
cat <<EOF > "$TMP_RPM_ROOT/index.txt"
Welcome to the Platypush RPM repository!
Project homepage: https://platypush.tech
Source code: https://git.platypush.tech/platypush/platypush
Documentation / API reference: https://docs.platypush.tech
You can use this RPM repository to install Platypush on Fedora or other
RPM-based distros - as long as they are compatible with the latest Fedora
release.
Steps:
1. Add the repository to your sources
=====================================
# yum config-manager --add-repo https://rpm.platypush.tech/platypush.repo
2. Install Platypush
====================
# yum install platypush
Or, if you want to install a version always up-to-date with the git repo:
# yum install platypush-git
EOF
cat <<EOF > "$TMP_RPM_ROOT/pubkey.txt"
$PGP_PUBKEY
EOF
echo "--- Updating the repository"
export NEW_RPM_ROOT="$REPOS_ROOT/rpm_new"
export OLD_RPM_ROOT="$REPOS_ROOT/rpm_old"
cp -r "$TMP_RPM_ROOT" "$NEW_RPM_ROOT"
rm -rf "$TMP_RPM_ROOT"
mv "$RPM_ROOT" "$OLD_RPM_ROOT"
mv "$NEW_RPM_ROOT" "$RPM_ROOT"
rm -rf "$OLD_RPM_ROOT"

View file

@ -1,22 +0,0 @@
# The device ID is the unique identifier for the device that runs Platypush.
# You should make sure that it's unique at least within your local network,
# as it is used to identify the device in the MQTT topics, on the HTTP API
# and on the published ZeroConf services.
PLATYPUSH_DEVICE_ID=platypush
# Use an external Redis server for the message queue. By default, the Platypush
# container will run a Redis server on the same container. Also remove the
# `--start-redis` option from the `docker run` command if you want to use an
# external Redis server.
# PLATYPUSH_REDIS_HOST=localhost
# PLATYPUSH_REDIS_PORT=6379
# Custom location for the Platypush configuration file.
# PLATYPUSH_CONFIG=/etc/platypush/config.yaml
# Custom location for the Platypush working directory.
# PLATYPUSH_WORKDIR=/var/lib/platypush
# SQLAlchemy database URL. By default, the Platypush container will run on a
# SQLite database installed under <WORKDIR>/main.db. If you want
# PLATYPUSH_DB=sqlite:////var/lib/platypush/main.db

1
.gitignore vendored
View file

@ -28,4 +28,3 @@ Session.vim
/docs/source/wiki
/.skipci
dump.rdb
.env

View file

@ -1 +0,0 @@
dist/

View file

@ -2,7 +2,7 @@
# See https://pre-commit.com/hooks.html for more hooks
repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v4.1.0
hooks:
# - id: trailing-whitespace
# - id: end-of-file-fixer
@ -11,15 +11,15 @@ repos:
- id: check-xml
- id: check-symlinks
- id: check-added-large-files
args: ['--maxkb=3000']
args: ['--maxkb=1500']
- repo: https://github.com/Lucas-C/pre-commit-hooks-nodejs
rev: v1.1.2
hooks:
- id: markdown-toc
- repo: https://github.com/PyCQA/flake8
rev: 7.1.0
- repo: https://github.com/pycqa/flake8
rev: 4.0.1
hooks:
- id: flake8
additional_dependencies:

View file

@ -1,358 +1,8 @@
# Changelog
## [1.3.4]
- [Bug]: Fixed installation bug in `pip install platypush` introduced by the
`pyproject.toml` migration.
## [1.3.3]
- [`3e02304a`](https://git.platypush.tech/platypush/platypush/commit/3e02304ac203625650ab4b03f9d4146a40839f2f)
[Auth]: Fixed generation of API tokens when 2FA is enabled.
## [1.3.2]
- [[#414](https://git.platypush.tech/platypush/platypush/issues/414)]: added
Jellyfin UI for all media plugins. This makes it possible to browse and play
media items from the Jellyfin library on multiple devices.
- [[#434](https://git.platypush.tech/platypush/platypush/issues/434)]: added
official Docker base image - [see
documentation](https://docs.platypush.tech/wiki/Installation.html#docker).
- [_Other Docker improvements_]
* Reduced size for the base images.
* Better self-documented [`docker-compose.yml`
file](https://git.platypush.tech/platypush/platypush/src/branch/master/docker-compose.yml).
- [[`db`](https://git.platypush.tech/platypush/platypush/commit/dd02be12)]:
Fixed compatibility of transaction scopes with SQLAlchemy 2.0.
- [`media.vlc`]: Improved state management.
- [`media.mpv`]: Enhanced compatibility. mpv 1.0 introduced many breaking
changes in the event management API, but it may not yet be available on all
distros. Platypush has now introduced a compatibility layer to seamlessly
interact with any version of mpv.
- [_Media UI_] Added support for embedded players for Jellyfin, YouTube and
local media.
- [[`http.webpage`](https://git.platypush.tech/platypush/platypush/commit/09413bc0)]:
Added support for custom headers to the scraping functions.
## [1.3.1]
- [[#344](https://git.platypush.tech/platypush/platypush/issues/344)]: removed
`marshmallow_dataclass` dependency. That package isn't included in the
package managers of any supported distros and requires to be installed via
pip. Making the Platypush' system packages depend on a pip-only package is
not a good idea. Plus, the library seems to be still in heavy development and
it has already broken compatibility with at least the `system` package.
## [1.3.0]
- [[#333](https://git.platypush.tech/platypush/platypush/issues/333)]: new file
browser UI/component. It includes custom MIME type support, a file editor
with syntax highlight, file download and file upload.
- [[#341](https://git.platypush.tech/platypush/platypush/issues/341)]:
procedures are now native entities that can be managed from the entities panel.
A new versatile procedure editor has also been added, with support for nested
blocks, conditions, loops, variables, context autocomplete, and more.
- [`procedure`]: Added the following features to YAML/structured procedures:
- `set`: to set variables whose scope is limited to the procedure / code
block where they are created. `variable.set` is useful to permanently
store variables on the db, `variable.mset` is useful to set temporary
global variables in memory through Redis, but sometimes you may just want
to assign a value to a variable that only needs to live within a procedure,
event hook or cron.
```yaml
- set:
foo: bar
temperature: ${output.get('temperature')}
```
- `return` can now return values too when invoked within a procedure:
```yaml
- return: something
# Or
- return: "Result: ${output.get('response')}"
```
- The default logging format is now much more compact. The full body of events
and requests is no longer included by default in `info` mode - instead, a
summary with the message type, ID and response time is logged. The full
payloads can still be logged by enabling `debug` logs through e.g. `-v`.
## [1.2.3]
- [[#422](https://git.platypush.tech/platypush/platypush/issues/422)]: adapted
media plugins to support streaming from the yt-dlp process. This allows
videos to have merged audio+video even if they had separate tracks upstream.
- [`media.*`] Many improvements on the media UI.
- [`zigbee.mqtt`] Removed synchronous logic from `zigbee.mqtt.device_set`. It
was prone to timeouts as well as pointless - the updated device state will
anyway be received as an event.
## [1.2.2]
- Fixed regression on older version of Python that don't fully support
`pyproject.toml` and can't install data files the new way.
## [1.2.1]
- Added static `/login` and `/register` Flask fallback routes to prevent 404 if
the client doesn't have JavaScript enabled.
- Fixed `apt` packages for Debian oldstable after the `setup.py` to
`pyproject.toml` migration.
- Fixed license string in the `pyproject.toml`.
## [1.2.0]
- [#419](https://git.platypush.tech/platypush/platypush/issues/419): added
support for randomly generated API tokens alongside JWT.
- [#339](https://git.platypush.tech/platypush/platypush/issues/339): added
support for 2FA with OTP codes.
- [#393](https://git.platypush.tech/platypush/platypush/issues/393): added
`bind_socket` parameter to `backend.http`, so Platypush can listen on (or
exclusively if `listen_port` is null) on a local UNIX socket as well.
- [#401](https://git.platypush.tech/platypush/platypush/issues/401): added
`--redis-bin` option / `PLATYPUSH_REDIS_BIN` environment variable to support
custom Redis (or drop-in replacements) executables when `--start-redis` is
specified.
- [#413](https://git.platypush.tech/platypush/platypush/issues/401): added
support for page-specific PWAs. If you navigate to `/plugin/<plugin-name>`,
and you install it as a PWA, you'll install a PWA only for that plugin - not
for the whole Platypush UI.
- Migrated project setup from `setup.py` to `pyproject.toml`.
- [`70db33b4e`](https://git.platypush.tech/platypush/platypush/commit/70db33b4e):
more application resilience in case Redis goes down.
- [`ee27b2c4`](https://git.platypush.tech/platypush/platypush/commit/ee27b2c4):
Refactor of all the authentication endpoints into a single `/auth` endpoint:
- `POST /register``POST /auth?type=register`
- `POST /login``POST /auth?type=login`
- `POST /auth``POST /auth?type=token`
- `POST /auth``POST /auth?type=jwt`
- [`2ccf0050`](https://git.platypush.tech/platypush/platypush/commit/2ccf0050):
Added support for binary content to `qrcode.generate`.
- [`b69e9500`](https://git.platypush.tech/platypush/platypush/commit/b69e9500):
Support for fullscreen mode on the `camera` plugins UI.
## [1.1.3] - 2024-07-16
- [`core`]: New architecture for the Redis bus - now leveraging pub/sub with a
connection pool instead of a single-connection queue. It makes the
application much faster and less prone to Redis deadlocks.
- [`youtube`]:
[#391](https://git.platypush.tech/platypush/platypush/issues/391): added
support for:
- Add/remove playlists (UI)
- Add to/remove from playlist (UI)
- Subscribe/unsubscribe from channels (UI)
- Browse channels and playlists directly in the UI
- Download media and audio
## [1.1.1] - 2024-06-24
- [`torrent`]: [[#263](https://git.platypush.tech/platypush/platypush/issues/263)], [[#375](https://git.platypush.tech/platypush/platypush/issues/375)],
[[#407](https://git.platypush.tech/platypush/platypush/issues/407)] and
[[#408](https://git.platypush.tech/platypush/platypush/issues/408)]: added
torrents.csv search provider and rewritten torrents UI.
- [[#281](https://git.platypush.tech/platypush/platypush/issues/281)]
replaced `warnings.warn` with `logging.warning`, as there is no easy and
reliable way of routing `warnings.warn` to `logging`.
## [1.1.0] - 2024-06-06
- [[#405](https://git.platypush.tech/platypush/platypush/issues/405)] Fixed
timezone/timestamp rendering issues for `calendar.ical` events.
- [[#403](https://git.platypush.tech/platypush/platypush/issues/403)]
Included inherited actions in plugins docs.
## [1.0.7] - 2024-06-02
- [[#384](https://git.platypush.tech/platypush/platypush/issues/384)] Added
`assistant.openai` and `tts.openai` plugins.
## [1.0.6] - 2024-06-01
- 🐛 Bug fix on one of the entities modules that prevented the application from
loading when `.` is part of `PYTHONPATH`.
## [1.0.5] - 2024-06-01
- A proper solution for the `utcnow()` issue.
It was a bit trickier than expected to solve, but now Platypush uses a
`utcnow()` facade that always returns a UTC datetime in a timezone-aware
representation.
The code should however also handle the case of timestamps stored on the db in
the old format.
## [1.0.4] - 2024-05-31
- Fixed regression introduced by
[c18768e61fef62924f4c1fac3089ecfb83666dab](https://git.platypush.tech/platypush/platypush/commit/c18768e61fef62924f4c1fac3089ecfb83666dab).
Python seems to have introduced a breaking change from the version 3.12 -
`datetime.utcnow()` is not deprecated, but `datetime.UTC`, the suggested
alternative, isn't available on older versions of Python. Added a workaround
that makes Platypush compatible with both the implementations.
## [1.0.3] - 2024-05-31
- [[#368](https://git.platypush.tech/platypush/platypush/issues/368)] Added
Ubuntu packages.
- Fixed bug that didn't get hooks to match events imported through the new
`platypush.events` symlinked module.
## [1.0.2] - 2024-05-26
- Fixed regression introduced by the support of custom names through the
`@procedure` decorator.
## [1.0.0] - 2024-05-26
Many, many changes for the first major release of Platypush after so many
years.
- [!3](https://git.platypush.tech/platypush/platypush/milestone/3) All
backends, except for `http`, `nodered`, `redis` and `tcp`, are gone. Many
were already deprecated a while ago, but the change now applies to all of
them. Backends should only be components that actively listen for application
messages to process, not generic daemon threads for integrations. This had
been a source of confusion for a long time. Backends and plugins are now
merged, meaning that you won't need to configure two different sections
instead of one for many integrations (one for the stateless plugin, and one
for the background state listener). Please check the
[documentation](https://docs.platypush.tech) to verify the configuration
changes required by your integrations. This has been a long process that has
involved the rewrite of most of the integrations, and many bugs have been
fixed.
- Improved Docker support - now with a default `docker-compose.yml`, multiple
Dockerfiles for
[Alpine](https://git.platypush.tech/platypush/platypush/src/branch/master/platypush/install/docker/alpine.Dockerfile),
[Debian](https://git.platypush.tech/platypush/platypush/src/branch/master/platypush/install/docker/debian.Dockerfile),
[Ubuntu](https://git.platypush.tech/platypush/platypush/src/branch/master/platypush/install/docker/ubuntu.Dockerfile)
and
[Fedora](https://git.platypush.tech/platypush/platypush/src/branch/master/platypush/install/docker/fedora.Dockerfile)
base images. Many improvements on the `platydock` and `platyvenv` scripts
too, with better automated installation processes for optional dependencies.
- Added [official
packages](https://git.platypush.tech/platypush/platypush#system-package-manager-installation)
for
[Debian](https://git.platypush.tech/platypush/platypush#debian-ubuntu)
and [Fedora](https://git.platypush.tech/platypush/platypush#fedora).
- Added `--device-id`, `--workdir`, `--logsdir`, `--cachedir`, `--main-db`,
`--redis-host`, `--redis-port` and `--redis-queue` CLI arguments, along the
`PLATYPUSH_DEVICE_ID`, `PLATYPUSH_WORKDIR`, `PLATYPUSH_LOGSDIR`,
`PLATYPUSH_CACHEDIR`, `PLATYPUSH_DB`, `PLATYPUSH_REDIS_HOST`,
`PLATYPUSH_REDIS_PORT` and `PLATYPUSH_REDIS_QUEUE` environment variables.
- Added an _Extensions_ panel to the UI to dynamically:
- Install new dependencies directly from the Web view.
- Explore the documentation as well as the supported actions and events for
each plugin.
- Get ready-to-paste configuration snippets/templates.
- New, completely rewritten [documentation](https://docs.platypush.tech), which
now integrates the wiki, dynamically includes plugins configuration snippets
and dependencies, and adds a global filter bar for the integrations.
- [[#394](https://git.platypush.tech/platypush/platypush/issues/394)] A more
intuitive way of installing extra dependencies via `pip`. Instead of a static
list that the user should check inside of `setup.py`, the syntax `pip install
'platypush[plugin1,plugin2,...]'` is now supported.
- No more need to manually create `__init__.py` in each of the `scripts`
folders that you want to use to store your custom scripts. Automatic
discovery of scripts and creation of module files has been implemented. You
can now just drop a `.py` script with your procedures, hooks or crons in the
scripts folder and it should be picked up by the application.
- The _Execute_ Web panel now supports procedures too, as well as curl snippets.
- Removed all `Response` objects outside of the root type. They are now all
replaced by Marshmallow schemas with the structure automatically generated in
the documentation.
- [`alarm`] [[#340](https://git.platypush.tech/platypush/platypush/issues/340)]
Rewritten integration. It now includes a powerful UI panel to set up alarms
with custom procedures too.
- [`assistant.picovoice`]
[[#304](https://git.platypush.tech/platypush/platypush/issues/304)] New
all-in-one Picovoice integration that replaces the previous `stt.picovoice.*`
integrations.
- [`youtube`]
[[#337](https://git.platypush.tech/platypush/platypush/issues/337)] Full
rewrite of the plugin. It now supports Piped instances instead of the
official YouTube API. A new UI has also been designed to explore
subscriptions, playlists and channels.
- [`weather.*`]
[[#308](https://git.platypush.tech/platypush/platypush/issues/308)] Removed
the `weather.darksky` integration (it's now owned by Apple and the API is
basically dead) and enhanced the `weather.openweathermap` plugin instead.
- [`camera.pi*`] The old `camera.pi` integration based on the deprecated
`picamera` module has been moved to `camera.pi.legacy`. `camera.pi` is now a
new plugin which uses the new `picamera2` API (and it's so far only
compatible with recent versions on the Raspberry Pi OS).
- Dynamically auto-generate plugins documentation in the UI from the RST
docstrings.
- New design for the configuration panel.
- Better synchronization between processes on threads on application stop -
greatly reduced the risk of hanging processes on shutdown.
- Migrated all CI/CD pipelines to [Drone
CI](https://ci-cd.platypush.tech/platypush/platypush).
- Removed `google.fit` integration, as Google has deprecated the API.
- Removed `wiimote` integration: the `cwiid` library hasn't been updated in
years, it doesn't even work well with Python 3, and I'm not in the mood of
bringing it back from the dead.
- Removed `stt.deepspeech` integration. That project has been basically
abandoned by Mozilla, the libraries are very buggy and I don't think it's
going to see new developments any time soon.
- [[#297](https://git.platypush.tech/platypush/platypush/issues/297)] Removed
`spotify` backend integration based on Librespot. The project has gone
through a lot of changes, and I no longer have a Spotify premium account to
work on a new implementation. Open to contributions if somebody still wants
it.
All notable changes to this project will be documented in this file.
Given the high speed of development in the first phase, changes are being
reported only starting from v0.20.2.
## [0.50.3] - 2023-07-22

View file

@ -27,9 +27,13 @@ Guidelines:
you are changing some of the core entities (e.g. requests, events, procedures, hooks, crons
or the bus) then make sure to add tests and not to break the existing tests.
- If the feature requires an optional dependency then make sure to document it
in the `manifest.json` - refer to the Wiki (how to write
[plugins](https://git.platypush.tech/platypush/platypush/wiki/Writing-your-own-plugins)
and
[backends](https://git.platypush.tech/platypush/platypush/wiki/Writing-your-own-backends))
for examples on how to write an extension manifest file.
- If the feature requires an optional dependency then make sure to document it:
- In the class docstring (see other plugins and backends for examples).
- In [`setup.py`](https://git.platypush.tech/platypush/platypush/-/blob/master/setup.py#L72) as
an `extras_require` entry.
- In the plugin/backend class pydoc string.
- In the `manifest.yaml` - refer to the Wiki (how to write
[plugins](https://git.platypush.tech/platypush/platypush/wiki/Writing-your-own-plugins)
and [backends](https://git.platypush.tech/platypush/platypush/wiki/Writing-your-own-backends))
for examples on how to write an extension manifest file.

View file

@ -1 +0,0 @@
platypush/install/docker/alpine.Dockerfile

View file

@ -1,6 +1,6 @@
MIT License
Copyright (c) 2017, 2024 Fabio Manganiello
Copyright (c) 2017, 2020 Fabio Manganiello
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal

View file

@ -2,16 +2,5 @@ recursive-include platypush/backend/http/webapp/dist *
recursive-include platypush/install *
include platypush/plugins/http/webpage/mercury-parser.js
include platypush/config/*.yaml
include platypush/config/systemd/*
global-include manifest.json
global-include manifest.yaml
global-include components.json.gz
global-exclude __pycache__/*
global-exclude *.pyc
# Exclude symlinks to avoid issues with setuptools
exclude platypush/backend/http/webapp/public/icons/openweathermap/black
exclude platypush/backend/http/webapp/public/icons/openweathermap/white
exclude platypush/backend/http/webapp/src/**
exclude platypush/events
exclude platypush/install/scripts/ubuntu

1819
README.md

File diff suppressed because it is too large Load diff

View file

@ -1,61 +0,0 @@
services:
platypush:
# Replace the build section with the next line if instead of building the
# image from a local checkout you want to pull the latest base
# (Alpine-based) image from the remote registry
# image: "quay.io/platypush/platypush:latest"
build:
context: .
# Alpine base image
dockerfile: ./platypush/install/docker/alpine.Dockerfile
# Debian base image
# dockerfile: ./platypush/install/docker/debian.Dockerfile
# Ubuntu base image
# dockerfile: ./platypush/install/docker/ubuntu.Dockerfile
# Fedora base image
# dockerfile: ./platypush/install/docker/fedora.Dockerfile
restart: "always"
command:
- platypush
- --redis-host
- redis
# Or, if you want to run Redis from the same container as Platypush,
# replace --redis-host redis with the line below
# - --start-redis
# Custom list of host devices that should be accessible to the container -
# e.g. an Arduino, an ESP-compatible microcontroller, a joystick etc.
# devices:
# - /dev/ttyUSB0
# Uncomment if you need plugins that require access to low-level hardware
# (e.g. Bluetooth BLE or GPIO/SPI/I2C) if access to individual devices is
# not enough or isn't practical
# privileged: true
# Copy .env.example to .env and modify as needed
# env_file:
# - .env
ports:
# Comment if you don't have the HTTP backend enable or you don't want to
# expose it
- "8008:8008"
# volumes:
# Replace with a path that contains/will contain your config.yaml file
# - /path/to/your/config:/etc/platypush
# Replace with a path that contains/will contain your working directory
# - /path/to/a/workdir:/var/lib/platypush
# Optionally, use an external volume for the cache
# - /path/to/a/cachedir:/var/cache/platypush
# This may be required, together with privileged mode, if you want the
# container to access the USB bus on the host (required for example if
# you have USB audio devices that you want to access from your plugins,
# or Bluetooth dongles, or other USB adapters).
# - /dev/bus/usb:/dev/bus/usb
redis:
image: redis

View file

@ -159,7 +159,7 @@ class IntegrationEnricher:
base_path,
*doc.split(os.sep)[:-1],
*doc.split(os.sep)[-1].split('.'),
'manifest.json',
'manifest.yaml',
)
if not os.path.isfile(manifest_file):

View file

@ -152,7 +152,7 @@ const generateComponentsGrid = () => {
return
}
if (window.location.pathname === '/' || window.location.pathname.endsWith('/index.html')) {
if (window.location.pathname.endsWith('/index.html')) {
if (tocWrappers.length < 2) {
return
}
@ -188,62 +188,9 @@ const renderActionsList = () => {
})
}
const createFilterBar = () => {
const input = document.createElement('input')
const referenceSection = document.getElementById('reference')
input.type = 'text'
input.placeholder = 'Filter'
input.classList.add('filter-bar')
input.addEventListener('input', (event) => {
const filter = event.target.value.toLowerCase()
referenceSection.querySelectorAll('ul.grid li').forEach((li) => {
if (li.innerText.toLowerCase().includes(filter)) {
li.style.display = 'flex'
} else {
li.style.display = 'none'
}
})
})
return input
}
const addFilterBar = () => {
const container = document.querySelector('.bd-main')
if (!container)
return
const referenceSection = document.getElementById('reference')
if (!referenceSection)
return
const header = referenceSection.querySelector('h2')
if (!header)
return
const origInnerHTML = header.innerHTML
header.innerHTML = '<span class="header-content">' + origInnerHTML + '</span>'
const input = createFilterBar()
header.appendChild(input)
const headerOffsetTop = header.offsetTop
// Apply the fixed class if the header is above the viewport
document.addEventListener('scroll', () => {
if (headerOffsetTop < window.scrollY) {
header.classList.add('fixed')
} else {
header.classList.remove('fixed')
}
})
}
document.addEventListener("DOMContentLoaded", function() {
generateComponentsGrid()
convertDepsToTabs()
addClipboardToCodeBlocks()
renderActionsList()
addFilterBar()
})

View file

@ -29,18 +29,15 @@ a.grid-title {
ul.grid li {
display: flex;
background: linear-gradient(0deg, #fff, #f9f9f9);
align-items: center;
justify-content: space-between;
margin: 0 10px 10px 0;
padding: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 15px;
flex-direction: column;
}
ul.grid img {
width: 48px;
width: 32px;
margin-right: 5px;
}
@ -55,20 +52,13 @@ ul.grid li code .pre {
}
ul.grid li:hover {
background: linear-gradient(0deg, #157765, #cbffd8) !important;
background: linear-gradient(0deg, #e0ffe8, #e3ffff);
}
ul.grid li a {
width: 100%;
width: calc(100% - 35px);
display: flex;
justify-content: center;
text-align: center;
margin-top: 0.5em;
}
ul.grid li:hover a,
ul.grid li:hover a > code {
color: white !important;
}
ul.grid li a code {
@ -138,51 +128,3 @@ ul.grid .icon {
border-radius: 0 0 0.75em 0.75em;
}
.bd-article-container {
position: relative;
}
.filter-bar {
width: 100%;
display: block;
font-size: 0.6em;
border: 1px solid #ccc;
border-radius: 0.75em;
margin: 0.5em 0;
padding: 0.25em;
}
#reference h2.fixed {
position: fixed;
top: 0;
background: white;
z-index: 1;
}
#reference h2.fixed .header-content {
display: none;
}
@media screen and (max-width: 768px) {
#reference h2.fixed {
width: 100%;
margin-left: -0.5em;
padding: 0.5em 0.5em 0 0.5em;
}
}
@media screen and (max-width: 959px) {
#reference h2.fixed {
width: 100%;
margin-left: -1em;
padding: 0.5em 0.5em 0 0.5em;
}
}
@media screen and (min-width: 960px) {
#reference h2.fixed {
width: 75%;
max-width: 800px;
padding-top: 0.5em;
}
}

View file

@ -7,6 +7,9 @@ Backends
:caption: Backends:
platypush/backend/http.rst
platypush/backend/midi.rst
platypush/backend/nodered.rst
platypush/backend/redis.rst
platypush/backend/stt.picovoice.hotword.rst
platypush/backend/stt.picovoice.speech.rst
platypush/backend/tcp.rst

View file

@ -21,7 +21,7 @@ sys.path.insert(0, os.path.abspath("./_ext"))
# -- Project information -----------------------------------------------------
project = 'Platypush'
copyright = '2017-2024, Fabio Manganiello'
copyright = '2017-2023, Fabio Manganiello'
author = 'Fabio Manganiello <fabio@manganiello.tech>'
# The short X.Y version
@ -199,8 +199,6 @@ intersphinx_mapping = {'python': ('https://docs.python.org/3', None)}
autodoc_default_options = {
'members': True,
'show-inheritance': True,
# Skip these classes from the documented inheritance tree
'inherited-members': 'threading.Thread',
}
sys.path.insert(0, os.path.abspath('../..'))

View file

@ -24,6 +24,7 @@ Events
platypush/events/geo.rst
platypush/events/github.rst
platypush/events/google.rst
platypush/events/google.fit.rst
platypush/events/google.pubsub.rst
platypush/events/gotify.rst
platypush/events/gpio.rst
@ -73,6 +74,7 @@ Events
platypush/events/web.rst
platypush/events/web.widget.rst
platypush/events/websocket.rst
platypush/events/wiimote.rst
platypush/events/xmpp.rst
platypush/events/zeroborg.rst
platypush/events/zeroconf.rst

View file

@ -24,8 +24,9 @@ Useful links
============
* The `main page <https://platypush.tech>`_ of the project.
* The `source code <https://git.platypush.tech/platypush/platypush>`_.
* The `blog <https://blog.platypush.tech>`_.
* The `Gitea page <https://git.platypush.tech/platypush/platypush>`_.
* The `blog <https://blog.platypush.tech>`_, for articles showing how to use
Platypush in real-world scenarios.
Wiki
====
@ -33,15 +34,11 @@ Wiki
.. toctree::
:maxdepth: 3
wiki/Home
wiki/Quickstart
wiki/index
wiki/Installation
wiki/Plugins-installation
wiki/APIs
wiki/Variables
wiki/Entities
wiki/Configuration
wiki/A-full-configuration-example
wiki/Installing-extensions
wiki/A-configuration-example
wiki/The-Web-interface
Reference
@ -53,6 +50,7 @@ Reference
backends
plugins
events
responses
Indices and tables
==================

View file

@ -0,0 +1,6 @@
``midi``
==========================
.. automodule:: platypush.backend.midi
:members:

View file

@ -0,0 +1,5 @@
``stt.picovoice.hotword``
===========================================
.. automodule:: platypush.backend.stt.picovoice.hotword
:members:

View file

@ -0,0 +1,5 @@
``stt.picovoice.speech``
==========================================
.. automodule:: platypush.backend.stt.picovoice.speech
:members:

View file

@ -0,0 +1,6 @@
``google.fit``
======================================
.. automodule:: platypush.message.event.google.fit
:members:

View file

@ -0,0 +1,6 @@
``wiimote``
===================================
.. automodule:: platypush.message.event.wiimote
:members:

View file

@ -1,5 +0,0 @@
``assistant.openai``
====================
.. automodule:: platypush.plugins.assistant.openai
:members:

View file

@ -1,5 +0,0 @@
``assistant.picovoice``
=======================
.. automodule:: platypush.plugins.assistant.picovoice
:members:

View file

@ -1,5 +0,0 @@
``cups``
========
.. automodule:: platypush.plugins.cups
:members:

View file

@ -0,0 +1,6 @@
``google.fit``
================================
.. automodule:: platypush.plugins.google.fit
:members:

View file

@ -0,0 +1,6 @@
``media.omxplayer``
=====================================
.. automodule:: platypush.plugins.media.omxplayer
:members:

View file

@ -1,5 +0,0 @@
``openai``
==========
.. automodule:: platypush.plugins.openai
:members:

View file

@ -0,0 +1,5 @@
``printer.cups``
==================================
.. automodule:: platypush.plugins.printer.cups
:members:

View file

@ -1,5 +0,0 @@
``procedures``
==============
.. automodule:: platypush.plugins.procedures
:members:

View file

@ -0,0 +1,5 @@
``stt.picovoice.hotword``
===========================================
.. automodule:: platypush.plugins.stt.picovoice.hotword
:members:

View file

@ -0,0 +1,5 @@
``stt.picovoice.speech``
==========================================
.. automodule:: platypush.plugins.stt.picovoice.speech
:members:

View file

@ -1,5 +0,0 @@
``tts.openai``
==============
.. automodule:: platypush.plugins.tts.openai
:members:

View file

@ -1,5 +0,0 @@
``tts.picovoice``
=================
.. automodule:: platypush.plugins.tts.picovoice
:members:

View file

@ -0,0 +1,5 @@
``camera.android``
=============================================
.. automodule:: platypush.message.response.camera.android
:members:

View file

@ -0,0 +1,5 @@
``camera``
=====================================
.. automodule:: platypush.message.response.camera
:members:

View file

@ -0,0 +1,5 @@
``google.drive``
===========================================
.. automodule:: platypush.message.response.google.drive
:members:

View file

@ -0,0 +1,5 @@
``pihole``
=====================================
.. automodule:: platypush.message.response.pihole
:members:

View file

@ -0,0 +1,5 @@
``printer.cups``
===========================================
.. automodule:: platypush.message.response.printer.cups
:members:

View file

@ -0,0 +1,5 @@
``qrcode``
=====================================
.. automodule:: platypush.message.response.qrcode
:members:

View file

@ -0,0 +1,5 @@
``ssh``
==================================
.. automodule:: platypush.message.response.ssh
:members:

View file

@ -0,0 +1,5 @@
``stt``
==================================
.. automodule:: platypush.message.response.stt
:members:

View file

@ -0,0 +1,5 @@
``tensorflow``
=========================================
.. automodule:: platypush.message.response.tensorflow
:members:

View file

@ -0,0 +1,5 @@
``translate``
========================================
.. automodule:: platypush.message.response.translate
:members:

View file

@ -11,8 +11,6 @@ Plugins
platypush/plugins/application.rst
platypush/plugins/arduino.rst
platypush/plugins/assistant.google.rst
platypush/plugins/assistant.openai.rst
platypush/plugins/assistant.picovoice.rst
platypush/plugins/autoremote.rst
platypush/plugins/bluetooth.rst
platypush/plugins/calendar.rst
@ -27,7 +25,6 @@ Plugins
platypush/plugins/clipboard.rst
platypush/plugins/config.rst
platypush/plugins/csv.rst
platypush/plugins/cups.rst
platypush/plugins/db.rst
platypush/plugins/dbus.rst
platypush/plugins/dropbox.rst
@ -41,6 +38,7 @@ Plugins
platypush/plugins/github.rst
platypush/plugins/google.calendar.rst
platypush/plugins/google.drive.rst
platypush/plugins/google.fit.rst
platypush/plugins/google.mail.rst
platypush/plugins/google.maps.rst
platypush/plugins/google.pubsub.rst
@ -77,6 +75,7 @@ Plugins
platypush/plugins/media.kodi.rst
platypush/plugins/media.mplayer.rst
platypush/plugins/media.mpv.rst
platypush/plugins/media.omxplayer.rst
platypush/plugins/media.plex.rst
platypush/plugins/media.subtitles.rst
platypush/plugins/media.vlc.rst
@ -95,11 +94,10 @@ Plugins
platypush/plugins/ngrok.rst
platypush/plugins/nmap.rst
platypush/plugins/ntfy.rst
platypush/plugins/openai.rst
platypush/plugins/otp.rst
platypush/plugins/pihole.rst
platypush/plugins/ping.rst
platypush/plugins/procedures.rst
platypush/plugins/printer.cups.rst
platypush/plugins/pushbullet.rst
platypush/plugins/pwm.pca9685.rst
platypush/plugins/qrcode.rst
@ -121,6 +119,8 @@ Plugins
platypush/plugins/smartthings.rst
platypush/plugins/sound.rst
platypush/plugins/ssh.rst
platypush/plugins/stt.picovoice.hotword.rst
platypush/plugins/stt.picovoice.speech.rst
platypush/plugins/sun.rst
platypush/plugins/switch.tplink.rst
platypush/plugins/switch.wemo.rst
@ -135,8 +135,6 @@ Plugins
platypush/plugins/tts.rst
platypush/plugins/tts.google.rst
platypush/plugins/tts.mimic3.rst
platypush/plugins/tts.openai.rst
platypush/plugins/tts.picovoice.rst
platypush/plugins/tv.samsung.ws.rst
platypush/plugins/twilio.rst
platypush/plugins/udp.rst

18
docs/source/responses.rst Normal file
View file

@ -0,0 +1,18 @@
Responses
=========
.. toctree::
:maxdepth: 1
:caption: Responses:
platypush/responses/camera.rst
platypush/responses/camera.android.rst
platypush/responses/google.drive.rst
platypush/responses/pihole.rst
platypush/responses/printer.cups.rst
platypush/responses/qrcode.rst
platypush/responses/ssh.rst
platypush/responses/stt.rst
platypush/responses/tensorflow.rst
platypush/responses/translate.rst

View file

@ -1,16 +1,15 @@
# A more versatile way to define event hooks than the YAML format of
# `config.yaml` is through native Python scripts. You can define hooks as simple
# Python functions that use the `platypush.event.hook.hook` decorator to specify
# on which event type they should be called, and optionally on which event
# attribute values.
# A more versatile way to define event hooks than the YAML format of `config.yaml` is through native Python scripts.
# You can define hooks as simple Python functions that use the `platypush.event.hook.hook` decorator to specify on
# which event type they should be called, and optionally on which event attribute values.
#
# Event hooks should be stored in Python files under
# `~/.config/platypush/scripts`. All the functions that use the @when decorator
# will automatically be discovered and imported as event hooks into the platform
# at runtime.
# Event hooks should be stored in Python files under `~/.config/platypush/scripts`. All the functions that use the
# @hook decorator will automatically be discovered and imported as event hooks into the platform at runtime.
# `run` is a utility function that runs a request by name (e.g. `light.hue.on`).
from platypush import when, run
from platypush.utils import run
# @hook decorator
from platypush.event.hook import hook
# Event types that you want to react to
from platypush.message.event.assistant import (
@ -19,15 +18,13 @@ from platypush.message.event.assistant import (
)
@when(SpeechRecognizedEvent, phrase='play ${title} by ${artist}')
def on_music_play_command(event, title=None, artist=None):
@hook(SpeechRecognizedEvent, phrase='play ${title} by ${artist}')
def on_music_play_command(event, title=None, artist=None, **context):
"""
This function will be executed when a SpeechRecognizedEvent with
`phrase="play the music"` is triggered. `event` contains the event object
and `context` any key-value info from the running context. Note that in this
specific case we can leverage the token-extraction feature of
SpeechRecognizedEvent through ${} that operates on regex-like principles to
extract any text that matches the pattern into context variables.
This function will be executed when a SpeechRecognizedEvent with `phrase="play the music"` is triggered.
`event` contains the event object and `context` any key-value info from the running context.
Note that in this specific case we can leverage the token-extraction feature of SpeechRecognizedEvent through
${} that operates on regex-like principles to extract any text that matches the pattern into context variables.
"""
results = run(
'music.mpd.search',
@ -37,17 +34,16 @@ def on_music_play_command(event, title=None, artist=None):
},
)
if results and results[0]:
if results:
run('music.mpd.play', results[0]['file'])
else:
run('tts.say', "I can't find any music matching your query")
@when(ConversationStartEvent)
def on_conversation_start():
@hook(ConversationStartEvent)
def on_conversation_start(event, **context):
"""
A simple hook that gets invoked when a new conversation starts with a voice
assistant and simply pauses the music to make sure that your speech is
properly detected.
A simple hook that gets invoked when a new conversation starts with a voice assistant and simply pauses the music
to make sure that your speech is properly detected.
"""
run('music.mpd.pause_if_playing')

View file

@ -1 +0,0 @@
../../platypush/config/systemd/platypush.service

View file

@ -0,0 +1,22 @@
# platypush systemd service example.
# Edit and copy this file to your systemd folder. It's usually
# /usr/lib/systemd/user for global installation or
# ~/.config/systemd/user for user installation. You can
# then control and monitor the service through
# systemd [--user] [start|stop|restart|status] platypush.service
[Unit]
Description=Platypush daemon
After=network.target redis.service
[Service]
# platypush installation path
ExecStart=/usr/bin/platypush
Restart=always
# How long should be waited before restarting the service
# in case of failure.
RestartSec=10
[Install]
WantedBy=default.target

View file

@ -8,6 +8,7 @@ import pkgutil
from platypush.backend import Backend
from platypush.message.event import Event
from platypush.message.response import Response
from platypush.plugins import Plugin
from platypush.utils.manifest import Manifests
from platypush.utils.mock import auto_mocks
@ -25,6 +26,10 @@ def get_all_events():
return _get_modules(Event)
def get_all_responses():
return _get_modules(Response)
def _get_modules(base_type: type):
ret = set()
base_dir = os.path.dirname(inspect.getfile(base_type))
@ -146,11 +151,20 @@ def generate_events_doc():
)
def generate_responses_doc():
_generate_components_doc(
index_name='responses',
package_name='message.response',
components=sorted(response for response in get_all_responses() if response),
)
def main():
with auto_mocks():
generate_plugins_doc()
generate_backends_doc()
generate_events_doc()
generate_responses_doc()
if __name__ == '__main__':

View file

@ -5,42 +5,29 @@ Platypush
.. license: MIT
"""
from .app import Application, app
from .app import Application
from .config import Config
from .context import Variable, get_backend, get_bus, get_plugin
from .cron import cron
from .event.hook import hook
from .context import get_backend, get_bus, get_plugin
from .message.event import Event
from .message.request import Request
from .message.response import Response
from .procedure import procedure
from .runner import main
from .utils import run
# Alias for platypush.event.hook.hook,
# see https://git.platypush.tech/platypush/platypush/issues/399
when = hook
__version__ = '1.3.4'
__author__ = 'Fabio Manganiello <fabio@manganiello.tech>'
__version__ = '0.50.3'
__all__ = [
'Application',
'Variable',
'Config',
'Event',
'Request',
'Response',
'app',
'cron',
'get_backend',
'get_bus',
'get_plugin',
'hook',
'main',
'procedure',
'run',
'when',
'__version__',
]

View file

@ -1,4 +1,4 @@
from ._app import Application, app, main
from ._app import Application, main
__all__ = ["Application", "app", "main"]
__all__ = ["Application", "main"]

View file

@ -5,7 +5,6 @@ import os
import signal
import subprocess
import sys
from textwrap import dedent
from typing import Optional, Sequence
from platypush.bus import Bus
@ -19,6 +18,7 @@ from platypush.entities import init_entities_engine, EntitiesEngine
from platypush.event.processor import EventProcessor
from platypush.logger import Logger
from platypush.message.event import Event
from platypush.message.event.application import ApplicationStartedEvent
from platypush.message.request import Request
from platypush.message.response import Response
from platypush.utils import get_enabled_plugins, get_redis_conf
@ -32,9 +32,6 @@ class Application:
# Default Redis port
_default_redis_port = 6379
# Default Redis binary, if --start-redis is set
_default_redis_bin = 'redis-server'
# backend_name => backend_obj map
backends = None
@ -58,7 +55,6 @@ class Application:
start_redis: bool = False,
redis_host: Optional[str] = None,
redis_port: Optional[int] = None,
redis_bin: Optional[str] = None,
ctrl_sock: Optional[str] = None,
):
"""
@ -146,11 +142,10 @@ class Application:
:param verbose: Enable debug/verbose logging, overriding the stored
configuration (default: False).
:param start_redis: If set, it starts a managed Redis instance upon
boot (it requires Redis installed on the server, see
``redis_bin``). This is particularly useful when running the
application inside of Docker containers, without relying on
``docker-compose`` to start multiple containers, and in tests
(default: False).
boot (it requires the ``redis-server`` executable installed on the
server). This is particularly useful when running the application
inside of Docker containers, without relying on ``docker-compose``
to start multiple containers, and in tests (default: False).
:param redis_host: Host of the Redis server to be used. The order of
precedence is:
@ -173,16 +168,6 @@ class Application:
the configuration file.
- ``6379``
:param redis_bin: Path to the Redis server executable, if ``start_redis``
is set. Alternative drop-in Redis implementations such as
``keydb-server``, ``valkey``, ``redict`` can be used. The order of
precedence is:
- The ``redis_bin`` parameter (or the ``--redis-bin`` command
line argument).
- The ``PLATYPUSH_REDIS_BIN`` environment variable.
- ``redis-server``
:param ctrl_sock: If set, it identifies a path to a UNIX domain socket
that the application can use to send control messages (e.g. STOP
and RESTART) to its parent.
@ -195,8 +180,6 @@ class Application:
or os.environ.get('PLATYPUSH_REDIS_QUEUE')
or RedisBus.DEFAULT_REDIS_QUEUE
)
os.environ['PLATYPUSH_REDIS_QUEUE'] = self.redis_queue
self.config_file = config_file or os.environ.get('PLATYPUSH_CONFIG')
self.verbose = verbose
self.db_engine = db or os.environ.get('PLATYPUSH_DB')
@ -226,11 +209,6 @@ class Application:
self.start_redis = start_redis
self.redis_host = redis_host or os.environ.get('PLATYPUSH_REDIS_HOST')
self.redis_port = redis_port or os.environ.get('PLATYPUSH_REDIS_PORT')
self.redis_bin = (
redis_bin
or os.environ.get('PLATYPUSH_REDIS_BIN')
or self._default_redis_bin
)
self._redis_conf = {
'host': self.redis_host or 'localhost',
'port': self.redis_port or self._default_redis_port,
@ -282,7 +260,7 @@ class Application:
port = self._redis_conf['port']
log.info('Starting local Redis instance on %s', port)
redis_cmd_args = [
self.redis_bin,
'redis-server',
'--bind',
'localhost',
'--port',
@ -365,13 +343,7 @@ class Application:
elif isinstance(msg, Response):
msg.log()
elif isinstance(msg, Event):
log.info(
'Received event: %s.%s[id=%s]',
msg.__class__.__module__,
msg.__class__.__name__,
msg.id,
)
msg.log(level=logging.DEBUG)
msg.log()
self.event_processor.process_event(msg)
return _f
@ -448,21 +420,7 @@ class Application:
if not self.no_capture_stderr:
sys.stderr = Logger(log.warning)
log.info(
dedent(
r'''
_____ _ _ _
| __ \| | | | | |
| |__) | | __ _| |_ _ _ _ __ _ _ ___| |__
| ___/| |/ _` | __| | | | '_ \| | | / __| '_ \
| | | | (_| | |_| |_| | |_) | |_| \__ \ | | |
|_| |_|\__,_|\__|\__, | .__/ \__,_|___/_| |_|
__/ | |
|___/|_|
'''
)
)
log.info('---- Starting Platypush v.%s', __version__)
log.info('---- Starting platypush v.%s', __version__)
# Start the local Redis service if required
if self.start_redis:
@ -487,6 +445,7 @@ class Application:
self.cron_scheduler.start()
assert self.bus, 'The bus is not running'
self.bus.post(ApplicationStartedEvent())
# Poll for messages on the bus
try:
@ -505,15 +464,10 @@ class Application:
self._run()
app: Optional[Application] = None
def main(*args: str):
"""
Application entry point.
"""
global app
app = Application.from_cmdline(args)
try:

View file

@ -52,10 +52,11 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
):
"""
:param bus: Reference to the bus object to be used in the backend
:param poll_seconds: If the backend implements a ``loop`` method, this
parameter expresses how often the loop should run in seconds.
:param poll_seconds: If the backend implements a ``loop`` method, this parameter expresses how often the
loop should run in seconds.
:param kwargs: Key-value configuration for the backend
"""
self._thread_name = self.__class__.__name__
EventGenerator.__init__(self)
ExtensionWithManifest.__init__(self)
@ -90,11 +91,8 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
It should be called by the derived classes whenever
a new message should be processed.
:param msg: Received message. It can be either a key-value dictionary,
a :class:`platypush.message.Message` object, or a string/byte UTF-8
encoded string.
:meta private:
:param msg: Received message. It can be either a key-value dictionary, a platypush.message.Message object,
or a string/byte UTF-8 encoded string
"""
msg = Message.build(msg)
@ -294,18 +292,10 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
self.logger.info('Terminated backend %s', self.__class__.__name__)
def on_stop(self):
"""
Callback invoked when the process stops.
:meta private:
"""
"""Callback invoked when the process stops"""
def stop(self):
"""
Stops the backend thread by sending a STOP event on its bus.
:meta private:
"""
"""Stops the backend thread by sending a STOP event on its bus"""
def _async_stop():
self._stop_event.set()
@ -320,7 +310,6 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
def should_stop(self):
"""
:return: True if the backend thread should be stopped, False otherwise.
:meta private:
"""
return self._stop_event.is_set()
@ -330,7 +319,6 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
:param timeout: The maximum time to wait for the backend thread to stop (default: None)
:return: True if the backend thread has stopped, False otherwise.
:meta private:
"""
start = time.time()
@ -404,7 +392,6 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
"version": "{platypush_version}"
}
:meta private:
"""
try:
from zeroconf import ServiceInfo, Zeroconf
@ -415,13 +402,6 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
)
return
if self.zeroconf:
self.logger.info(
'Zeroconf service already registered for %s, removing the previous instance',
self.__class__.__name__,
)
self.unregister_service()
self.zeroconf = Zeroconf()
srv_desc = {
'name': 'Platypush',
@ -460,8 +440,6 @@ class Backend(Thread, EventGenerator, ExtensionWithManifest):
def unregister_service(self):
"""
Unregister the Zeroconf service configuration if available.
:meta private:
"""
from redis import exceptions

View file

@ -8,10 +8,12 @@ import threading
from functools import partial
from multiprocessing import Process
from time import time
from typing import Mapping, Optional, Union
from typing import Mapping, Optional
import psutil
from tornado.httpserver import HTTPServer
from tornado.netutil import bind_sockets, bind_unix_socket
from tornado.netutil import bind_sockets
from tornado.process import cpu_count, fork_processes
from tornado.wsgi import WSGIContainer
from tornado.web import Application, FallbackHandler
@ -151,13 +153,14 @@ class HttpBackend(Backend):
.. code-block:: python
from platypush import get_plugin, when
from platypush.context import get_plugin
from platypush.event.hook import hook
from platypush.message.event.http.hook import WebhookEvent
hook_token = 'abcdefabcdef'
# Expose the hook under the /hook/lights_toggle endpoint
@when(WebhookEvent, hook='lights_toggle')
@hook(WebhookEvent, hook='lights_toggle')
def lights_toggle(event, **context):
# Do any checks on the request
assert event.headers.get('X-Token') == hook_token, 'Unauthorized'
@ -198,8 +201,7 @@ class HttpBackend(Backend):
def __init__(
self,
port: int = DEFAULT_HTTP_PORT,
bind_address: Optional[str] = '0.0.0.0',
bind_socket: Optional[Union[str, bool]] = None,
bind_address: str = '0.0.0.0',
resource_dirs: Optional[Mapping[str, str]] = None,
secret_key_file: Optional[str] = None,
num_workers: Optional[int] = None,
@ -208,16 +210,7 @@ class HttpBackend(Backend):
):
"""
:param port: Listen port for the web server (default: 8008)
:param bind_address: Address/interface to bind to (default: 0.0.0.0,
accept connection from any IP). You can set it to null to disable
the network interface binding, but then you must set ``bind_socket``
as an alternative.
:param bind_socket: Path to the Unix socket to bind to. If set, the
server will bind to the path of the specified Unix socket. If set to
``true``, then a socket will be automatically initialized on
``<workdir>/platypush-<device_id>.sock``. If not set, the server will
only listen on the specified bind address and port. Note that either
``bind_socket`` or ``socket_path`` must be set.
:param bind_address: Address/interface to bind to (default: 0.0.0.0, accept connection from any IP)
:param resource_dirs: Static resources directories that will be
accessible through ``/resources/<path>``. It is expressed as a map
where the key is the relative path under ``/resources`` to expose and
@ -236,25 +229,14 @@ class HttpBackend(Backend):
example, you are running the application on a small embedded
device that doesn't support Tornado.
"""
super().__init__(**kwargs)
assert (
bind_address or bind_socket
), 'Either bind_address or bind_socket must be set'
self.port = port
self._server_proc: Optional[Process] = None
self._service_registry_thread = None
self.bind_address = bind_address
if bind_socket is True:
bind_socket = os.path.join(
Config.get_workdir(), f'platypush-{Config.get_device_id()}.sock'
)
self.socket_path = None
if bind_socket:
self.socket_path = os.path.expanduser(bind_socket)
if resource_dirs:
self.resource_dirs = {
name: os.path.abspath(os.path.expanduser(d))
@ -279,8 +261,8 @@ class HttpBackend(Backend):
super().on_stop()
self.logger.info('Received STOP event on HttpBackend')
start = time()
remaining_time: partial[float] = partial(
get_remaining_timeout, timeout=self._STOP_TIMEOUT, start=start # type: ignore
remaining_time: partial[float] = partial( # type: ignore
get_remaining_timeout, timeout=self._STOP_TIMEOUT, start=start
)
if self._server_proc:
@ -310,11 +292,7 @@ class HttpBackend(Backend):
self.logger.info('HTTP server terminated')
def notify_web_clients(self, event):
"""
Notify all the connected web clients (over websocket) of a new event.
:meta private:
"""
"""Notify all the connected web clients (over websocket) of a new event"""
WSEventProxy.publish(event) # noqa: E1120
def _get_secret_key(self, _create=False):
@ -387,7 +365,6 @@ class HttpBackend(Backend):
)
if self.use_werkzeug_server:
assert self.bind_address, 'bind_address must be set when using Werkzeug'
application.config['redis_queue'] = self.bus.redis_queue # type: ignore
application.run(
host=self.bind_address,
@ -396,13 +373,9 @@ class HttpBackend(Backend):
debug=True,
)
else:
sockets = []
if self.bind_address:
sockets.extend(bind_sockets(self.port, address=self.bind_address))
if self.socket_path:
sockets.append(bind_unix_socket(self.socket_path))
sockets = bind_sockets(
self.port, address=self.bind_address, reuse_port=True
)
try:
fork_processes(self.num_workers)
@ -422,14 +395,6 @@ class HttpBackend(Backend):
workers when the server terminates:
https://github.com/tornadoweb/tornado/issues/1912.
"""
try:
import psutil
except ImportError:
self.logger.warning(
'Could not import psutil, hanging worker processes might remain active'
)
return
parent_pid = (
self._server_proc.pid
if self._server_proc and self._server_proc.pid
@ -457,8 +422,8 @@ class HttpBackend(Backend):
# Initialize the timeout
start = time()
remaining_time: partial[int] = partial(
get_remaining_timeout, timeout=self._STOP_TIMEOUT, start=start, cls=int # type: ignore
remaining_time: partial[int] = partial( # type: ignore
get_remaining_timeout, timeout=self._STOP_TIMEOUT, start=start, cls=int
)
# Wait for all children to terminate (with timeout)

View file

@ -4,15 +4,8 @@ import logging
from flask import Blueprint, request, abort, jsonify
from platypush.backend.http.app.utils import authenticate
from platypush.backend.http.app.utils.auth import (
UserAuthStatus,
current_user,
get_current_user_or_auth_status,
)
from platypush.exceptions.user import UserException
from platypush.user import User, UserManager
from platypush.utils import utcnow
from platypush.user import UserManager
auth = Blueprint('auth', __name__)
log = logging.getLogger(__name__)
@ -23,24 +16,39 @@ __routes__ = [
]
def _dump_session(session, redirect_page='/'):
return jsonify(
{
'status': 'ok',
'user_id': session.user_id,
'session_token': session.session_token,
'expires_at': session.expires_at,
'redirect': redirect_page,
}
)
@auth.route('/auth', methods=['POST'])
def auth_endpoint():
"""
Authentication endpoint. It validates the user credentials provided over a JSON payload with the following
structure:
.. code-block:: json
def _jwt_auth():
{
"username": "USERNAME",
"password": "PASSWORD",
"expiry_days": "The generated token should be valid for these many days"
}
``expiry_days`` is optional, and if omitted or set to zero the token will be valid indefinitely.
Upon successful validation, a new JWT token will be generated using the service's self-generated RSA key-pair and it
will be returned to the user. The token can then be used to authenticate API calls to ``/execute`` by setting the
``Authorization: Bearer <TOKEN_HERE>`` header upon HTTP calls.
:return: Return structure:
.. code-block:: json
{
"token": "<generated token here>"
}
"""
try:
payload = json.loads(request.get_data(as_text=True))
username, password = payload['username'], payload['password']
except Exception:
log.warning('Invalid payload passed to the auth endpoint')
except Exception as e:
log.warning('Invalid payload passed to the auth endpoint: ' + str(e))
abort(400)
expiry_days = payload.get('expiry_days')
@ -51,365 +59,8 @@ def _jwt_auth():
user_manager = UserManager()
try:
return jsonify(
{
'token': user_manager.generate_jwt_token(
username=username, password=password, expires_at=expires_at
),
}
)
return jsonify({
'token': user_manager.generate_jwt_token(username=username, password=password, expires_at=expires_at),
})
except UserException as e:
abort(401, str(e))
def _session_auth():
user_manager = UserManager()
session_token = request.cookies.get('session_token')
redirect_page = request.args.get('redirect') or '/'
if session_token:
user, session = user_manager.authenticate_user_session(session_token)[:2]
if user and session:
return _dump_session(session, redirect_page)
if request.form:
username = request.form.get('username')
password = request.form.get('password')
code = request.form.get('code')
remember = request.form.get('remember')
expires = utcnow() + datetime.timedelta(days=365) if remember else None
session, status = user_manager.create_user_session( # type: ignore
username=username,
password=password,
code=code,
expires_at=expires,
with_status=True,
)
if session:
return _dump_session(session, redirect_page)
if status:
auth_status = UserAuthStatus.by_status(status)
assert auth_status
return auth_status.to_response()
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
def _create_token():
payload = {}
try:
payload = json.loads(request.get_data(as_text=True))
except json.JSONDecodeError:
pass
user = None
username = payload.get('username')
password = payload.get('password')
name = payload.get('name')
expiry_days = payload.get('expiry_days')
user_manager = UserManager()
response = get_current_user_or_auth_status(request)
# Try and authenticate with the credentials passed in the JSON payload
if username and password:
user = user_manager.authenticate_user(username, password, skip_2fa=True)
if not isinstance(user, User):
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
if not user:
if not (response and isinstance(response, User)):
return response.to_response()
user = response
expires_at = None
if expiry_days:
expires_at = datetime.datetime.now() + datetime.timedelta(days=expiry_days)
try:
token = UserManager().generate_api_token(
username=str(user.username), name=name, expires_at=expires_at
)
return jsonify({'token': token})
except UserException:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
def _delete_token():
try:
payload = json.loads(request.get_data(as_text=True))
token = payload.get('token')
assert token
except (AssertionError, json.JSONDecodeError):
return UserAuthStatus.INVALID_TOKEN.to_response()
user_manager = UserManager()
try:
token = payload.get('token')
if not token:
return UserAuthStatus.INVALID_TOKEN.to_response()
ret = user_manager.delete_api_token(token)
if not ret:
return UserAuthStatus.INVALID_TOKEN.to_response()
return jsonify({'status': 'ok'})
except UserException:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
def _register_route():
"""Registration endpoint"""
user_manager = UserManager()
session_token = request.cookies.get('session_token')
redirect_page = request.args.get('redirect') or '/'
if session_token:
user, session = user_manager.authenticate_user_session(session_token)[:2]
if user and session:
return _dump_session(session, redirect_page)
if user_manager.get_user_count() > 0:
return UserAuthStatus.REGISTRATION_DISABLED.to_response()
if not request.form:
return UserAuthStatus.MISSING_USERNAME.to_response()
username = request.form.get('username')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
remember = request.form.get('remember')
if not username:
return UserAuthStatus.MISSING_USERNAME.to_response()
if not password:
return UserAuthStatus.MISSING_PASSWORD.to_response()
if password != confirm_password:
return UserAuthStatus.PASSWORD_MISMATCH.to_response()
user_manager.create_user(username=username, password=password)
session, status = user_manager.create_user_session( # type: ignore
username=username,
password=password,
expires_at=(utcnow() + datetime.timedelta(days=365) if remember else None),
with_status=True,
)
if session:
return _dump_session(session, redirect_page)
if status:
return status.to_response() # type: ignore
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
def _auth_get():
"""
Get the current authentication status of the user session.
"""
user_manager = UserManager()
session_token = request.cookies.get('session_token')
redirect_page = request.args.get('redirect') or '/'
user, session, status = user_manager.authenticate_user_session( # type: ignore
session_token, with_status=True
)
if user and session:
return _dump_session(session, redirect_page)
response = get_current_user_or_auth_status(request)
if isinstance(response, User):
user = response
return jsonify(
{'status': 'ok', 'user_id': user.user_id, 'username': user.username}
)
if response:
status = response
if status:
if not isinstance(status, UserAuthStatus):
status = UserAuthStatus.by_status(status)
if not status:
status = UserAuthStatus.INVALID_CREDENTIALS
return status.to_response()
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
def _auth_post():
"""
Authenticate the user session.
"""
auth_type = request.args.get('type') or 'token'
if auth_type == 'token':
return _create_token()
if auth_type == 'jwt':
return _jwt_auth()
if auth_type == 'register':
return _register_route()
if auth_type == 'login':
return _session_auth()
return UserAuthStatus.INVALID_AUTH_TYPE.to_response()
def _auth_delete():
"""
Logout/invalidate a token or the current user session.
"""
# Delete the specified API token if it's passed on the JSON payload
token = None
try:
payload = json.loads(request.get_data(as_text=True))
token = payload.get('token')
except json.JSONDecodeError:
pass
if token:
return _delete_token()
user_manager = UserManager()
session_token = request.cookies.get('session_token')
redirect_page = request.args.get('redirect') or '/'
if session_token:
user, session = user_manager.authenticate_user_session(session_token)[:2]
if user and session:
user_manager.delete_user_session(session_token)
return jsonify({'status': 'ok', 'redirect': redirect_page})
return UserAuthStatus.INVALID_SESSION.to_response()
def _tokens_get():
user = current_user()
if not user:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
tokens = UserManager().get_api_tokens(username=str(user.username))
return jsonify(
{
'tokens': [
{
'id': t.id,
'name': t.name,
'created_at': t.created_at,
'expires_at': t.expires_at,
}
for t in tokens
]
}
)
def _tokens_delete():
args = {}
try:
payload = json.loads(request.get_data(as_text=True))
token = payload.get('token')
if token:
args['token'] = token
else:
token_id = payload.get('token_id')
if token_id:
args['token_id'] = token_id
assert args, 'No token or token_id specified'
except (AssertionError, json.JSONDecodeError):
return UserAuthStatus.INVALID_TOKEN.to_response()
user_manager = UserManager()
user = current_user()
if not user:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
args['username'] = str(user.username)
try:
user_manager.delete_api_token(**args)
return jsonify({'status': 'ok'})
except AssertionError as e:
return (
jsonify({'status': 'error', 'error': 'bad_request', 'message': str(e)}),
400,
)
except UserException:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
except Exception as e:
log.error('Token deletion error', exc_info=e)
return UserAuthStatus.UNKNOWN_ERROR.to_response()
@auth.route('/auth', methods=['GET', 'POST', 'DELETE'])
def auth_endpoint():
"""
Authentication endpoint. It validates the user credentials provided over a
JSON payload with the following structure:
.. code-block:: json
{
"username": "USERNAME",
"password": "PASSWORD",
"code": "2FA_CODE",
"expiry_days": "The generated token should be valid for these many days"
}
``expiry_days`` is optional, and if omitted or set to zero the token will
be valid indefinitely.
Upon successful validation, a new JWT token will be generated using the
service's self-generated RSA key-pair and it will be returned to the user.
The token can then be used to authenticate API calls to ``/execute`` by
setting the ``Authorization: Bearer <TOKEN_HERE>`` header upon HTTP calls.
:return: Return structure:
.. code-block:: json
{
"token": "<generated token here>"
}
"""
if request.method == 'GET':
return _auth_get()
if request.method == 'POST':
return _auth_post()
if request.method == 'DELETE':
return _auth_delete()
return UserAuthStatus.INVALID_METHOD.to_response()
@auth.route('/tokens', methods=['GET', 'DELETE'])
@authenticate()
def tokens_route():
"""
:return: The list of API tokens created by the logged in user.
Note that this endpoint is only accessible by authenticated users
and it won't return the clear-text token values, as those aren't
stored in the database anyway.
"""
if request.method == 'GET':
return _tokens_get()
if request.method == 'DELETE':
return _tokens_delete()
return UserAuthStatus.INVALID_METHOD.to_response()
# vim:sw=4:ts=4:et:

View file

@ -14,26 +14,8 @@ __routes__ = [
@index.route('/')
@authenticate()
def index_route():
"""Route to the main web panel"""
return render_template('index.html', utils=HttpUtils)
@index.route('/login', methods=['GET'])
def login_route():
"""
Login GET route. It simply renders the index template, which will
redirect to the login page if the user is not authenticated.
"""
return render_template('index.html', utils=HttpUtils)
@index.route('/register', methods=['GET'])
def register_route():
"""
Register GET route. It simply renders the index template, which will
redirect to the registration page if no users are present.
"""
def index():
""" Route to the main web panel """
return render_template('index.html', utils=HttpUtils)

View file

@ -0,0 +1,55 @@
import datetime
import re
from flask import Blueprint, request, redirect, render_template, make_response
from platypush.backend.http.app import template_folder
from platypush.backend.http.utils import HttpUtils
from platypush.user import UserManager
login = Blueprint('login', __name__, template_folder=template_folder)
# Declare routes list
__routes__ = [
login,
]
@login.route('/login', methods=['GET', 'POST'])
def login():
""" Login page """
user_manager = UserManager()
session_token = request.cookies.get('session_token')
redirect_page = request.args.get('redirect')
if not redirect_page:
redirect_page = request.headers.get('Referer', '/')
if re.search('(^https?://[^/]+)?/login[^?#]?', redirect_page):
# Prevent redirect loop
redirect_page = '/'
if session_token:
user, session = user_manager.authenticate_user_session(session_token)
if user:
return redirect(redirect_page, 302) # lgtm [py/url-redirection]
if request.form:
username = request.form.get('username')
password = request.form.get('password')
remember = request.form.get('remember')
expires = datetime.datetime.utcnow() + datetime.timedelta(days=365) \
if remember else None
session = user_manager.create_user_session(username=username, password=password,
expires_at=expires)
if session:
redirect_target = redirect(redirect_page, 302) # lgtm [py/url-redirection]
response = make_response(redirect_target)
response.set_cookie('session_token', session.session_token, expires=expires)
return response
return render_template('index.html', utils=HttpUtils)
# vim:sw=4:ts=4:et:

View file

@ -12,7 +12,7 @@ __routes__ = [
@logout.route('/logout', methods=['GET', 'POST'])
def logout_route():
def logout():
"""Logout page"""
user_manager = UserManager()
redirect_page = request.args.get(
@ -23,7 +23,7 @@ def logout_route():
if not session_token:
abort(417, 'Not logged in')
user, _ = user_manager.authenticate_user_session(session_token)[:2]
user, _ = user_manager.authenticate_user_session(session_token)
if not user:
abort(403, 'Invalid session token')

View file

@ -1,220 +0,0 @@
from typing import List, Optional
from flask import Blueprint, jsonify, request
from platypush.backend.http.app import template_folder
from platypush.backend.http.app.utils import UserAuthStatus, authenticate
from platypush.backend.http.utils import HttpUtils
from platypush.exceptions.user import (
InvalidCredentialsException,
InvalidOtpCodeException,
UserException,
)
from platypush.config import Config
from platypush.context import get_plugin
from platypush.user import UserManager
otp = Blueprint('otp', __name__, template_folder=template_folder)
# Declare routes list
__routes__ = [
otp,
]
def _get_otp_and_qrcode():
otp = get_plugin('otp') # pylint: disable=redefined-outer-name
qrcode = get_plugin('qrcode')
assert (
otp and qrcode
), 'The otp and/or qrcode plugins are not available in your installation'
return otp, qrcode
def _get_username():
user = HttpUtils.current_user()
if not user:
raise InvalidCredentialsException('Invalid user session')
return str(user.username)
def _get_otp_uri_and_qrcode(username: str, otp_secret: Optional[str] = None):
if not otp_secret:
return None, None
otp, qrcode = _get_otp_and_qrcode() # pylint: disable=redefined-outer-name
otp_uri = (
otp.provision_time_otp(
name=username,
secret=otp_secret,
issuer=f'platypush@{Config.get_device_id()}',
).output
if otp_secret
else None
)
otp_qrcode = (
qrcode.generate(content=otp_uri, format='png').output.get('data')
if otp_uri
else None
)
return otp_uri, otp_qrcode
def _verify_code(code: str, otp_secret: str) -> bool:
otp, _ = _get_otp_and_qrcode() # pylint: disable=redefined-outer-name
return otp.verify_time_otp(otp=code, secret=otp_secret).output
def _dump_response(
username: str,
otp_secret: Optional[str] = None,
backup_codes: Optional[List[str]] = None,
):
otp_uri, otp_qrcode = _get_otp_uri_and_qrcode(username, otp_secret)
return jsonify(
{
'username': username,
'otp_secret': otp_secret,
'otp_uri': otp_uri,
'qrcode': otp_qrcode,
'backup_codes': backup_codes or [],
}
)
def _get_otp():
username = _get_username()
user_manager = UserManager()
otp_secret = user_manager.get_otp_secret(username)
return _dump_response(
username=username,
otp_secret=otp_secret,
)
def _authenticate_user(username: str, password: Optional[str]):
assert password, 'The password field is required when setting up OTP'
user, auth_status = UserManager().authenticate_user( # type: ignore
username, password, skip_2fa=True, with_status=True
)
if not user:
raise InvalidCredentialsException(auth_status.value[2])
def _post_otp():
body = request.json
assert body, 'Invalid request body'
username = _get_username()
dry_run = body.get('dry_run', False)
otp_secret = body.get('otp_secret')
if not dry_run:
_authenticate_user(username, body.get('password'))
if otp_secret:
code = body.get('code')
assert code, 'The code field is required when setting up OTP'
if not _verify_code(code, otp_secret):
raise InvalidOtpCodeException()
user_manager = UserManager()
user_otp, backup_codes = user_manager.enable_otp(
username=username,
otp_secret=otp_secret,
dry_run=dry_run,
)
return _dump_response(
username=username,
otp_secret=str(user_otp.otp_secret),
backup_codes=backup_codes,
)
def _delete_otp():
body = request.json
assert body, 'Invalid request body'
username = _get_username()
_authenticate_user(username, body.get('password'))
user_manager = UserManager()
user_manager.disable_otp(username)
return jsonify({'status': 'ok'})
@otp.route('/otp/config', methods=['GET', 'POST', 'DELETE'])
@authenticate()
def otp_route():
"""
:return: The user's current MFA/OTP configuration:
.. code-block:: json
{
"username": "testuser",
"otp_secret": "JBSA6ZUZ5DPEK7YV",
"otp_uri": "otpauth://totp/testuser?secret=JBSA6ZUZ5DPEK7YV&issuer=platypush@localhost",
"qrcode": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtWK6eAAABwklEQVR4nO3dMW7CQBAF0",
"backup_codes": [
"1A2B3C4D5E",
"6F7G8H9I0J",
"KLMNOPQRST",
"UVWXYZ1234",
"567890ABCD",
"EFGHIJKLMN",
"OPQRSTUVWX",
"YZ12345678",
"90ABCDEF12",
"34567890AB"
]
}
"""
try:
if request.method.lower() == 'get':
return _get_otp()
if request.method.lower() == 'post':
return _post_otp()
if request.method.lower() == 'delete':
return _delete_otp()
return jsonify({'error': 'Method not allowed'}), 405
except AssertionError as e:
return jsonify({'error': str(e)}), 400
except InvalidCredentialsException:
return UserAuthStatus.INVALID_CREDENTIALS.to_response()
except InvalidOtpCodeException:
return UserAuthStatus.INVALID_OTP_CODE.to_response()
except UserException as e:
return jsonify({'error': e.__class__.__name__, 'message': str(e)}), 401
except Exception as e:
HttpUtils.log.error(f'Error while processing OTP request: {e}', exc_info=True)
return jsonify({'error': str(e)}), 500
@otp.route('/otp/refresh-codes', methods=['POST'])
def refresh_codes():
"""
:return: A new set of backup codes for the user.
"""
username = _get_username()
user_manager = UserManager()
otp_secret = user_manager.get_otp_secret(username)
if not otp_secret:
return jsonify({'error': 'OTP not configured for the user'}), 400
backup_codes = user_manager.refresh_user_backup_codes(username)
return jsonify({'backup_codes': backup_codes})
# vim:sw=4:ts=4:et:

View file

@ -1,7 +1,4 @@
from typing import Optional
from urllib.parse import urlparse
from flask import Blueprint, jsonify, request, send_from_directory
from flask import Blueprint, jsonify, send_from_directory
from platypush.config import Config
from platypush.backend.http.app import template_folder
@ -14,37 +11,13 @@ __routes__ = [
]
def _get_plugin(url: Optional[str] = None) -> Optional[str]:
if not url:
return None
path = urlparse(url).path.lstrip('/').split('/')
if len(path) > 1 and path[0] == 'plugin':
return path[1]
return None
@pwa.route('/manifest.json', methods=['GET'])
def manifest_json():
"""Generated manifest file for the PWA"""
device_id = Config.get_device_id()
referer = request.headers.get('Referer')
plugin = _get_plugin(referer)
start_url = '/'
name = f'Platypush @ {device_id}'
short_name = device_id
if plugin:
start_url = f'/plugin/{plugin}'
name = f'{plugin} @ {device_id}'
short_name = plugin
return jsonify(
{
"name": name,
"short_name": short_name,
"name": f'Platypush @ {Config.get("device_id")}',
"short_name": Config.get('device_id'),
"icons": [
{
"src": "/img/icons/favicon-16x16.png",
@ -121,9 +94,9 @@ def manifest_json():
],
"gcm_sender_id": "",
"gcm_user_visible_only": True,
"start_url": start_url,
"start_url": "/",
"permissions": ["gcm"],
"orientation": "any",
"orientation": "portrait",
"display": "standalone",
"theme_color": "#000000",
"background_color": "#ffffff",

View file

@ -0,0 +1,62 @@
import datetime
import re
from flask import Blueprint, request, redirect, render_template, make_response, abort
from platypush.backend.http.app import template_folder
from platypush.backend.http.utils import HttpUtils
from platypush.user import UserManager
register = Blueprint('register', __name__, template_folder=template_folder)
# Declare routes list
__routes__ = [
register,
]
@register.route('/register', methods=['GET', 'POST'])
def register():
""" Registration page """
user_manager = UserManager()
redirect_page = request.args.get('redirect')
if not redirect_page:
redirect_page = request.headers.get('Referer', '/')
if re.search('(^https?://[^/]+)?/register[^?#]?', redirect_page):
# Prevent redirect loop
redirect_page = '/'
session_token = request.cookies.get('session_token')
if session_token:
user, session = user_manager.authenticate_user_session(session_token)
if user:
return redirect(redirect_page, 302) # lgtm [py/url-redirection]
if user_manager.get_user_count() > 0:
return redirect('/login?redirect=' + redirect_page, 302) # lgtm [py/url-redirection]
if request.form:
username = request.form.get('username')
password = request.form.get('password')
confirm_password = request.form.get('confirm_password')
remember = request.form.get('remember')
if password == confirm_password:
user_manager.create_user(username=username, password=password)
session = user_manager.create_user_session(username=username, password=password,
expires_at=datetime.datetime.utcnow() + datetime.timedelta(days=1)
if not remember else None)
if session:
redirect_target = redirect(redirect_page, 302) # lgtm [py/url-redirection]
response = make_response(redirect_target)
response.set_cookie('session_token', session.session_token)
return response
else:
abort(400, 'Password mismatch')
return render_template('index.html', utils=HttpUtils)
# vim:sw=4:ts=4:et:

View file

@ -7,7 +7,7 @@ from typing import Optional
from tornado.web import RequestHandler, stream_request_body
from platypush.backend.http.app.utils import logger
from platypush.backend.http.app.utils.auth import UserAuthStatus, get_auth_status
from platypush.backend.http.app.utils.auth import AuthStatus, get_auth_status
from ..mixins import PubSubMixin
@ -29,9 +29,8 @@ class StreamingRoute(RequestHandler, PubSubMixin, ABC):
"""
if self.auth_required:
auth_status = get_auth_status(self.request)
if auth_status != UserAuthStatus.OK:
if auth_status != AuthStatus.OK:
self.send_error(auth_status.value.code, error=auth_status.value.message)
self.finish()
return
self.logger.info(
@ -43,7 +42,6 @@ class StreamingRoute(RequestHandler, PubSubMixin, ABC):
Make sure that errors are always returned in JSON format.
"""
self.set_header("Content-Type", "application/json")
self.set_status(status_code)
self.finish(
json.dumps(
{"status": status_code, "error": error or responses.get(status_code)}

View file

@ -1,205 +0,0 @@
import os
import pathlib
from contextlib import contextmanager
from datetime import datetime as dt
from typing import IO, Optional, Tuple
from tornado.web import stream_request_body
from platypush.utils import get_mime_type
from .. import StreamingRoute
@stream_request_body
class FileRoute(StreamingRoute):
"""
Generic route to read the content of a file on the server.
"""
BUFSIZE = 1024
_bytes_written = 0
_out_f: Optional[IO[bytes]] = None
@classmethod
def path(cls) -> str:
"""
Route: GET /file?path=<path>[&download]
"""
return r"^/file$"
@property
def download(self) -> bool:
return 'download' in self.request.arguments
@property
def file_path(self) -> str:
return os.path.expanduser(
self.request.arguments.get('path', [b''])[0].decode('utf-8')
)
@property
def file_size(self) -> int:
return os.path.getsize(self.file_path)
@property
def _content_length(self) -> int:
return int(self.request.headers.get('Content-Length', 0))
@property
def range(self) -> Tuple[Optional[int], Optional[int]]:
range_hdr = self.request.headers.get('Range')
if not range_hdr:
return None, None
start, end = range_hdr.split('=')[-1].split('-')
start = int(start) if start else 0
end = int(end) if end else self.file_size - 1
return start, end
def set_headers(self):
self.set_header('Content-Length', str(os.path.getsize(self.file_path)))
self.set_header(
'Content-Type', get_mime_type(self.file_path) or 'application/octet-stream'
)
self.set_header('Accept-Ranges', 'bytes')
self.set_header(
'Last-Modified',
dt.fromtimestamp(os.path.getmtime(self.file_path)).strftime(
'%a, %d %b %Y %H:%M:%S GMT'
),
)
if self.download:
self.set_header(
'Content-Disposition',
f'attachment; filename="{os.path.basename(self.file_path)}"',
)
if self.range[0] is not None:
start, end = self.range
self.set_header(
'Content-Range',
f'bytes {start}-{end}/{self.file_size}',
)
self.set_status(206)
@contextmanager
def _serve(self):
path = self.file_path
if not path:
self.write_error(400, 'Missing path argument')
return
self.logger.debug('Received file read request for %r', path)
try:
with open(path, 'rb') as f:
self.set_headers()
yield f
except FileNotFoundError:
self.write_error(404, 'File not found')
yield
return
except PermissionError:
self.write_error(403, 'Permission denied')
yield
return
except Exception as e:
self.write_error(500, str(e))
yield
return
self.finish()
def on_finish(self) -> None:
if self._out_f:
try:
if not (self._out_f and self._out_f.closed):
self._out_f.close()
except Exception as e:
self.logger.warning('Error while closing the output file: %s', e)
self._out_f = None
return super().on_finish()
def _validate_upload(self, force: bool = False) -> bool:
if not self.file_path:
self.write_error(400, 'Missing path argument')
return False
if not self._out_f:
if not force and os.path.exists(self.file_path):
self.write_error(409, f'{self.file_path} already exists')
return False
self._bytes_written = 0
dir_path = os.path.dirname(self.file_path)
try:
pathlib.Path(dir_path).mkdir(parents=True, exist_ok=True)
self._out_f = open( # pylint: disable=consider-using-with
self.file_path, 'wb'
)
except PermissionError:
self.write_error(403, 'Permission denied')
return False
return True
def finish(self, *args, **kwargs): # type: ignore
try:
return super().finish(*args, **kwargs)
except Exception as e:
self.logger.warning('Error while finishing the request: %s', e)
def data_received(self, chunk: bytes):
# Ignore unless we're in POST/PUT mode
if self.request.method not in ('POST', 'PUT'):
return
force = self.request.method == 'PUT'
if not self._validate_upload(force=force):
self.finish()
return
if not chunk:
self.logger.debug('Received EOF from client')
self.finish()
return
assert self._out_f
self._out_f.write(chunk)
self._out_f.flush()
self._bytes_written += len(chunk)
self.logger.debug(
'Written chunk of size %d to %s, progress: %d/%d',
len(chunk),
self.file_path,
self._bytes_written,
self._content_length,
)
self.flush()
def get(self) -> None:
with self._serve() as f:
if f:
while True:
chunk = f.read(self.BUFSIZE)
if not chunk:
break
self.write(chunk)
self.flush()
def head(self) -> None:
with self._serve():
pass
def post(self) -> None:
self.logger.info('Receiving file POST upload request for %r', self.file_path)
def put(self) -> None:
self.logger.info('Receiving file PUT upload request for %r', self.file_path)

View file

@ -3,9 +3,7 @@ from typing import Optional
from platypush.backend.http.app.utils import logger, send_request
from platypush.backend.http.media.handlers import MediaHandler
from ._registry import clear_media_map, load_media_map, save_media_map
_init = False
from ._registry import load_media_map, save_media_map
def get_media_url(media_id: str) -> str:
@ -19,12 +17,6 @@ def register_media(source: str, subtitles: Optional[str] = None) -> MediaHandler
"""
Registers a media file and returns its associated media handler.
"""
global _init
if not _init:
clear_media_map()
_init = True
media_id = MediaHandler.get_media_id(source)
media_url = get_media_url(media_id)
media_map = load_media_map()

View file

@ -25,15 +25,10 @@ def load_media_map() -> MediaMap:
logger().warning('Could not load media map: %s', e)
return {}
parsed_map = {}
for media_id, media_info in media_map.items():
try:
parsed_map[media_id] = MediaHandler.build(**media_info)
except Exception as e:
logger().debug('Could not load media %s: %s', media_id, e)
continue
return parsed_map
return {
media_id: MediaHandler.build(**media_info)
for media_id, media_info in media_map.items()
}
def save_media_map(new_map: MediaMap):
@ -43,12 +38,3 @@ def save_media_map(new_map: MediaMap):
with media_map_lock:
redis = get_redis()
redis.mset({MEDIA_MAP_VAR: json.dumps(new_map, cls=Message.Encoder)})
def clear_media_map():
"""
Clears the media map from the server.
"""
with media_map_lock:
redis = get_redis()
redis.delete(MEDIA_MAP_VAR)

View file

@ -17,7 +17,7 @@ class MediaStreamRoute(StreamingRoute):
Route for media streams.
"""
SUPPORTED_METHODS = ['GET', 'HEAD', 'PUT', 'DELETE']
SUPPORTED_METHODS = ['GET', 'PUT', 'DELETE']
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
@ -50,23 +50,6 @@ class MediaStreamRoute(StreamingRoute):
except Exception as e:
self._on_error(e)
def head(self, media_id: Optional[str] = None):
"""
Streams a media resource by ID.
"""
if not media_id:
self.finish()
return
# Strip the extension
media_id = '.'.join(media_id.split('.')[:-1])
try:
self.stream_media(media_id, head=True)
except Exception as e:
self._on_error(e)
def put(self, *_, **__):
"""
The `PUT` route is used to prepare a new media resource for streaming.
@ -110,10 +93,10 @@ class MediaStreamRoute(StreamingRoute):
"""
Returns the list of registered media resources.
"""
self.set_header('Content-Type', 'application/json')
self.add_header('Content-Type', 'application/json')
self.finish(json.dumps([dict(media) for media in load_media_map().values()]))
def stream_media(self, media_id: str, head: bool = False):
def stream_media(self, media_id: str):
"""
Route to stream a media file given its ID.
"""
@ -124,11 +107,11 @@ class MediaStreamRoute(StreamingRoute):
range_hdr = self.request.headers.get('Range')
content_length = media_hndl.content_length
self.set_header('Accept-Ranges', 'bytes')
self.set_header('Content-Type', media_hndl.mime_type)
self.add_header('Accept-Ranges', 'bytes')
self.add_header('Content-Type', media_hndl.mime_type)
if 'download' in self.request.arguments:
self.set_header(
self.add_header(
'Content-Disposition',
'attachment'
+ ('; filename="{media_hndl.filename}"' if media_hndl.filename else ''),
@ -146,7 +129,7 @@ class MediaStreamRoute(StreamingRoute):
content_length = to_bytes - from_bytes
self.set_status(206)
self.set_header(
self.add_header(
'Content-Range',
f'bytes {from_bytes}-{to_bytes}/{media_hndl.content_length}',
)
@ -154,13 +137,7 @@ class MediaStreamRoute(StreamingRoute):
from_bytes = 0
to_bytes = STREAMING_BLOCK_SIZE
self.set_header('Content-Length', str(content_length))
if head:
self.flush()
self.finish()
return
self.add_header('Content-Length', str(content_length))
for chunk in media_hndl.get_data(
from_bytes=from_bytes,
to_bytes=to_bytes,

View file

@ -1,9 +1,7 @@
from .auth import (
UserAuthStatus,
authenticate,
authenticate_token,
authenticate_user_pass,
current_user,
get_auth_status,
)
from .bus import bus, send_message, send_request
@ -19,12 +17,10 @@ from .streaming import get_streaming_routes
from .ws import get_ws_routes
__all__ = [
'UserAuthStatus',
'authenticate',
'authenticate_token',
'authenticate_user_pass',
'bus',
'current_user',
'get_auth_status',
'get_http_port',
'get_ip_or_hostname',

View file

@ -1,15 +1,15 @@
import base64
from functools import wraps
from typing import Optional, Union
from typing import Optional
from flask import request, redirect
from flask import request, redirect, jsonify
from flask.wrappers import Response
from platypush.config import Config
from platypush.user import User, UserManager
from platypush.user import UserManager
from ..logger import logger
from .status import UserAuthStatus
from .status import AuthStatus
user_manager = UserManager()
@ -41,8 +41,8 @@ def get_cookie(req, name: str) -> Optional[str]:
return cookie.value
def authenticate_token(req) -> Optional[User]:
global_token = Config.get('user.global_token')
def authenticate_token(req):
token = Config.get('token')
user_token = None
if 'X-Token' in req.headers:
@ -55,27 +55,14 @@ def authenticate_token(req) -> Optional[User]:
user_token = get_arg(req, 'token')
if not user_token:
return None
return False
try:
# Stantard API token authentication
return user_manager.validate_api_token(user_token)
user_manager.validate_jwt_token(user_token)
return True
except Exception as e:
try:
# Legacy JWT token authentication
return user_manager.validate_jwt_token(user_token)
except Exception as ee:
logger().debug(
'Invalid token. API token error: %s, JWT token error: %s', e, ee
)
# Legacy global token authentication.
# The global token should be specified in the configuration file,
# as a root parameter named `token`.
if bool(global_token and user_token == global_token):
return User(username='__token__', user_id=1)
logger().info(e)
logger().debug(str(e))
return bool(token and user_token == token)
def authenticate_user_pass(req):
@ -104,7 +91,7 @@ def authenticate_user_pass(req):
return user_manager.authenticate_user(username, password)
def authenticate_session(req) -> Optional[User]:
def authenticate_session(req):
user = None
# Check the X-Session-Token header
@ -119,9 +106,9 @@ def authenticate_session(req) -> Optional[User]:
user_session_token = get_cookie(req, 'session_token')
if user_session_token:
user, _ = user_manager.authenticate_user_session(user_session_token)[:2]
user, _ = user_manager.authenticate_user_session(user_session_token)
return user
return user is not None
def authenticate(
@ -141,18 +128,18 @@ def authenticate(
skip_auth_methods=skip_auth_methods,
)
if auth_status == UserAuthStatus.OK:
if auth_status == AuthStatus.OK:
return f(*args, **kwargs)
if json:
return auth_status.to_response()
return jsonify(auth_status.to_dict()), auth_status.value.code
if auth_status == UserAuthStatus.REGISTRATION_REQUIRED:
if auth_status == AuthStatus.NO_USERS:
return redirect(
f'/register?redirect={redirect_page or request.url}', 307
)
if auth_status == UserAuthStatus.INVALID_CREDENTIALS:
if auth_status == AuthStatus.UNAUTHORIZED:
return redirect(f'/login?redirect={redirect_page or request.url}', 307)
return Response(
@ -167,67 +154,43 @@ def authenticate(
# pylint: disable=too-many-return-statements
def get_current_user_or_auth_status(
req, skip_auth_methods=None
) -> Union[User, UserAuthStatus]:
def get_auth_status(req, skip_auth_methods=None) -> AuthStatus:
"""
Returns the current user if authenticated, and the authentication status if
``with_status`` is True.
Check against the available authentication methods (except those listed in
``skip_auth_methods``) if the user is properly authenticated.
"""
n_users = user_manager.get_user_count()
skip_methods = skip_auth_methods or []
# User/pass HTTP authentication
http_auth_ok = True
if n_users > 0 and 'http' not in skip_methods:
response = authenticate_user_pass(req)
if response:
user = response[0] if isinstance(response, tuple) else response
if user:
return user
http_auth_ok = authenticate_user_pass(req)
if http_auth_ok:
return AuthStatus.OK
# Token-based authentication
token_auth_ok = True
if 'token' not in skip_methods:
user = authenticate_token(req)
if user:
return user
token_auth_ok = authenticate_token(req)
if token_auth_ok:
return AuthStatus.OK
# Session token based authentication
session_auth_ok = True
if n_users > 0 and 'session' not in skip_methods:
user = authenticate_session(req)
if user:
return user
return UserAuthStatus.INVALID_CREDENTIALS
return AuthStatus.OK if authenticate_session(req) else AuthStatus.UNAUTHORIZED
# At least a user should be created before accessing an authenticated resource
if n_users == 0 and 'session' not in skip_methods:
return UserAuthStatus.REGISTRATION_REQUIRED
return AuthStatus.NO_USERS
if ( # pylint: disable=too-many-boolean-expressions
('http' not in skip_methods and http_auth_ok)
or ('token' not in skip_methods and token_auth_ok)
or ('session' not in skip_methods and session_auth_ok)
):
return UserAuthStatus.OK
return AuthStatus.OK
return UserAuthStatus.INVALID_CREDENTIALS
def get_auth_status(req, skip_auth_methods=None) -> UserAuthStatus:
"""
Check against the available authentication methods (except those listed in
``skip_auth_methods``) if the user is properly authenticated.
"""
ret = get_current_user_or_auth_status(req, skip_auth_methods=skip_auth_methods)
return UserAuthStatus.OK if isinstance(ret, User) else ret
def current_user() -> Optional[User]:
"""
Returns the current user if authenticated.
"""
ret = get_current_user_or_auth_status(request)
return ret if isinstance(ret, User) else None
return AuthStatus.UNAUTHORIZED

View file

@ -1,76 +1,21 @@
from collections import namedtuple
from enum import Enum
from flask import jsonify
from platypush.user import AuthenticationStatus
StatusValue = namedtuple('StatusValue', ['code', 'error', 'message'])
StatusValue = namedtuple('StatusValue', ['code', 'message'])
class UserAuthStatus(Enum):
class AuthStatus(Enum):
"""
Models the status of the authentication.
"""
OK = StatusValue(200, AuthenticationStatus.OK, 'OK')
INVALID_AUTH_TYPE = StatusValue(
400, AuthenticationStatus.INVALID_AUTH_TYPE, 'Invalid authentication type'
)
INVALID_CREDENTIALS = StatusValue(
401, AuthenticationStatus.INVALID_CREDENTIALS, 'Invalid credentials'
)
INVALID_JWT_TOKEN = StatusValue(
401, AuthenticationStatus.INVALID_JWT_TOKEN, 'Invalid JWT token'
)
INVALID_OTP_CODE = StatusValue(
401, AuthenticationStatus.INVALID_OTP_CODE, 'Invalid OTP code'
)
INVALID_METHOD = StatusValue(
405, AuthenticationStatus.INVALID_METHOD, 'Invalid method'
)
MISSING_OTP_CODE = StatusValue(
401, AuthenticationStatus.MISSING_OTP_CODE, 'Missing OTP code'
)
MISSING_PASSWORD = StatusValue(
400, AuthenticationStatus.MISSING_PASSWORD, 'Missing password'
)
INVALID_SESSION = StatusValue(
401, AuthenticationStatus.INVALID_CREDENTIALS, 'Invalid session'
)
INVALID_TOKEN = StatusValue(
400, AuthenticationStatus.INVALID_JWT_TOKEN, 'Invalid token'
)
MISSING_USERNAME = StatusValue(
400, AuthenticationStatus.MISSING_USERNAME, 'Missing username'
)
PASSWORD_MISMATCH = StatusValue(
400, AuthenticationStatus.PASSWORD_MISMATCH, 'Password mismatch'
)
REGISTRATION_DISABLED = StatusValue(
401, AuthenticationStatus.REGISTRATION_DISABLED, 'Registrations are disabled'
)
REGISTRATION_REQUIRED = StatusValue(
412, AuthenticationStatus.REGISTRATION_REQUIRED, 'Please create a user first'
)
UNKNOWN_ERROR = StatusValue(
500, AuthenticationStatus.UNKNOWN_ERROR, 'Unknown error'
)
OK = StatusValue(200, 'OK')
UNAUTHORIZED = StatusValue(401, 'Unauthorized')
NO_USERS = StatusValue(412, 'Please create a user first')
def to_dict(self):
return {
'code': self.value[0],
'error': self.value[1].name,
'message': self.value[2],
'message': self.value[1],
}
def to_response(self):
return jsonify(self.to_dict()), self.value[0]
@staticmethod
def by_status(status: AuthenticationStatus):
for auth_status in UserAuthStatus:
if auth_status.value[1] == status:
return auth_status
return None

View file

@ -1,57 +1,24 @@
from multiprocessing import Lock
from platypush.bus.redis import RedisBus
from platypush.context import get_bus
from platypush.config import Config
from platypush.context import get_backend
from platypush.message import Message
from platypush.message.request import Request
from platypush.utils import get_message_response
from platypush.utils import get_redis_conf, get_message_response
from .logger import logger
class BusWrapper: # pylint: disable=too-few-public-methods
"""
Lazy singleton wrapper for the bus object.
"""
def __init__(self):
self._redis_queue = None
self._bus = None
self._bus_lock = Lock()
@property
def bus(self) -> RedisBus:
"""
Lazy getter/initializer for the bus object.
"""
with self._bus_lock:
if not self._bus:
self._bus = get_bus()
bus_: RedisBus = self._bus # type: ignore
return bus_
def post(self, msg):
"""
Send a message to the bus.
:param msg: The message to send.
"""
try:
self.bus.post(msg)
except Exception as e:
logger().exception(e)
_bus = BusWrapper()
_bus = None
def bus():
"""
Lazy getter/initializer for the bus object.
"""
return _bus.bus
global _bus # pylint: disable=global-statement
if _bus is None:
redis_queue = get_backend('http').bus.redis_queue # type: ignore
_bus = RedisBus(**get_redis_conf(), redis_queue=redis_queue)
return _bus
def send_message(msg, wait_for_response=True):

View file

@ -5,7 +5,7 @@ from threading import Thread
from tornado.ioloop import IOLoop
from tornado.websocket import WebSocketHandler
from platypush.backend.http.app.utils.auth import UserAuthStatus, get_auth_status
from platypush.backend.http.app.utils.auth import AuthStatus, get_auth_status
from ..mixins import MessageType, PubSubMixin
@ -25,9 +25,9 @@ class WSRoute(WebSocketHandler, Thread, PubSubMixin, ABC):
def open(self, *_, **__):
auth_status = get_auth_status(self.request)
if auth_status != UserAuthStatus.OK:
if auth_status != AuthStatus.OK:
self.close(code=1008, reason=auth_status.value.message) # Policy Violation
raise ValueError(f'Unauthorized connection: {auth_status.value.message}')
return
logger.info(
'Client %s connected to %s', self.request.remote_ip, self.request.path

View file

@ -1,10 +0,0 @@
{
"manifest": {
"events": {},
"install": {
"pip": []
},
"package": "platypush.backend.http",
"type": "backend"
}
}

View file

@ -0,0 +1,6 @@
manifest:
events: {}
install:
pip: []
package: platypush.backend.http
type: backend

View file

@ -1,6 +1,7 @@
from abc import ABC, abstractmethod
import hashlib
import logging
import os
from typing import Generator, Optional
from platypush.message import JSONAble
@ -56,6 +57,9 @@ class MediaHandler(JSONAble, ABC):
logging.exception(e)
errors[hndl_class.__name__] = str(e)
if os.path.exists(source):
source = f'file://{source}'
raise AttributeError(
f'The source {source} has no handlers associated. Errors: {errors}'
)

View file

@ -15,9 +15,6 @@ class FileHandler(MediaHandler):
prefix_handlers = ['file://']
def __init__(self, source, *args, **kwargs):
if isinstance(source, str) and os.path.exists(source):
source = f'file://{source}'
super().__init__(source, *args, **kwargs)
self.path = os.path.abspath(
@ -36,7 +33,7 @@ class FileHandler(MediaHandler):
), f'{source} is not a valid media file (detected format: {self.mime_type})'
self.extension = mimetypes.guess_extension(self.mime_type)
if self.url and self.extension and not self.url.endswith(self.extension):
if self.url and self.extension:
self.url += self.extension
self.content_length = os.path.getsize(self.path)

View file

@ -7,7 +7,6 @@ import re
from platypush.config import Config
from platypush.backend.http.app import template_folder
from platypush.backend.http.app.utils import current_user
class HttpUtils:
@ -131,9 +130,5 @@ class HttpUtils:
path = path[0] if len(path) == 1 else os.path.join(*path)
return os.path.isfile(path)
@staticmethod
def current_user():
return current_user()
# vim:sw=4:ts=4:et:

View file

@ -1 +1 @@
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><!--[if IE]><link rel="icon" href="/favicon.ico"><![endif]--><link rel="stylesheet" href="/fonts/poppins.css"><title>platypush</title><script defer="defer" src="/static/js/chunk-vendors.83e191d2.js"></script><script defer="defer" src="/static/js/app.53c00686.js"></script><link href="/static/css/chunk-vendors.d510eff2.css" rel="stylesheet"><link href="/static/css/app.f97a4bca.css" rel="stylesheet"><link rel="icon" type="image/svg+xml" href="/img/icons/favicon.svg"><link rel="icon" type="image/png" sizes="32x32" href="/img/icons/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/icons/favicon-16x16.png"><link rel="manifest" href="/manifest.json"><meta name="theme-color" content="#ffffff"><meta name="apple-mobile-web-app-capable" content="no"><meta name="apple-mobile-web-app-status-bar-style" content="default"><meta name="apple-mobile-web-app-title" content="Platypush"><link rel="apple-touch-icon" href="/img/icons/apple-touch-icon-152x152.png"><link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color="#ffffff"><meta name="msapplication-TileImage" content="/img/icons/msapplication-icon-144x144.png"><meta name="msapplication-TileColor" content="#000000"></head><body><noscript><strong>We're sorry but platypush doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>
<!doctype html><html lang="en"><head><meta charset="utf-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><!--[if IE]><link rel="icon" href="/favicon.ico"><![endif]--><link rel="stylesheet" href="/fonts/poppins.css"><title>platypush</title><script defer="defer" src="/static/js/chunk-vendors.aeea9c55.js"></script><script defer="defer" src="/static/js/app.e71ae2ab.js"></script><link href="/static/css/chunk-vendors.a2412607.css" rel="stylesheet"><link href="/static/css/app.5b1362a4.css" rel="stylesheet"><link rel="icon" type="image/svg+xml" href="/img/icons/favicon.svg"><link rel="icon" type="image/png" sizes="32x32" href="/img/icons/favicon-32x32.png"><link rel="icon" type="image/png" sizes="16x16" href="/img/icons/favicon-16x16.png"><link rel="manifest" href="/manifest.json"><meta name="theme-color" content="#ffffff"><meta name="apple-mobile-web-app-capable" content="no"><meta name="apple-mobile-web-app-status-bar-style" content="default"><meta name="apple-mobile-web-app-title" content="Platypush"><link rel="apple-touch-icon" href="/img/icons/apple-touch-icon-152x152.png"><link rel="mask-icon" href="/img/icons/safari-pinned-tab.svg" color="#ffffff"><meta name="msapplication-TileImage" content="/img/icons/msapplication-icon-144x144.png"><meta name="msapplication-TileColor" content="#000000"></head><body><noscript><strong>We're sorry but platypush doesn't work properly without JavaScript enabled. Please enable it to continue.</strong></noscript><div id="app"></div></body></html>

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

Some files were not shown because too many files have changed in this diff Show more