From 72361c99e18d6edeb3f4318fea58446abe084dd5 Mon Sep 17 00:00:00 2001 From: ed Date: Thu, 1 Aug 2024 18:29:25 +0000 Subject: [PATCH] add import chickenbits --- README.md | 36 ++++++++++++++++++++++++++++++++++++ copyparty/__main__.py | 3 +++ copyparty/cert.py | 2 +- copyparty/httpcli.py | 3 +++ copyparty/httpconn.py | 3 +++ copyparty/mtag.py | 7 +++++-- copyparty/th_srv.py | 18 ++++++++++++++++++ copyparty/util.py | 12 ++++++++++++ 8 files changed, 81 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 51b6287c..2d6ba3a3 100644 --- a/README.md +++ b/README.md @@ -111,6 +111,7 @@ turn almost any device into a file server with resumable uploads/downloads using * [HTTP API](#HTTP-API) - see [devnotes](./docs/devnotes.md#http-api) * [dependencies](#dependencies) - mandatory deps * [optional dependencies](#optional-dependencies) - install these to enable bonus features + * [dependency chickenbits](#dependency-chickenbits) - prevent loading an optional dependency * [optional gpl stuff](#optional-gpl-stuff) * [sfx](#sfx) - the self-contained "binary" (recommended!) * [copyparty.exe](#copypartyexe) - download [copyparty.exe](https://github.com/9001/copyparty/releases/latest/download/copyparty.exe) (win8+) or [copyparty32.exe](https://github.com/9001/copyparty/releases/latest/download/copyparty32.exe) (win7+) @@ -2044,6 +2045,41 @@ enable [smb](#smb-server) support (**not** recommended): `pyvips` gives higher quality thumbnails than `Pillow` and is 320% faster, using 270% more ram: `sudo apt install libvips42 && python3 -m pip install --user -U pyvips` +### dependency chickenbits + +prevent loading an optional dependency , for example if: + +* you have an incompatible version installed and it causes problems +* you just don't want copyparty to use it, maybe to save ram + +set any of the following environment variables to disable its associated optional feature, + +| env-var | what it does | +| -------------------- | ------------ | +| `PRTY_NO_CFSSL` | never attempt to generate self-signed certificates using [cfssl](https://github.com/cloudflare/cfssl) | +| `PRTY_NO_FFMPEG` | **audio transcoding** goes byebye, **thumbnailing** must be handled by Pillow/libvips | +| `PRTY_NO_FFPROBE` | **audio transcoding** goes byebye, **thumbnailing** must be handled by Pillow/libvips, **metadata-scanning** must be handled by mutagen | +| `PRTY_NO_IPV6` | disable some ipv6 support (should not be necessary since windows 2000) | +| `PRTY_NO_LZMA` | disable streaming xz compression of incoming uploads | +| `PRTY_NO_MP` | disable all use of the python `multiprocessing` module (actual multithreading, cpu-count for parsers/thumbnailers) | +| `PRTY_NO_MUTAGEN` | do not use [mutagen](https://pypi.org/project/mutagen/) for reading metadata from media files; will fallback to ffprobe | +| `PRTY_NO_PIL` | disable all [Pillow](https://pypi.org/project/pillow/)-based thumbnail support; will fallback to libvips or ffmpeg | +| `PRTY_NO_PILF` | disable Pillow `ImageFont` text rendering, used for folder thumbnails | +| `PRTY_NO_PIL_AVIF` | disable 3rd-party Pillow plugin for [AVIF support](https://pypi.org/project/pillow-avif-plugin/) | +| `PRTY_NO_PIL_HEIF` | disable 3rd-party Pillow plugin for [HEIF support](https://pypi.org/project/pyheif-pillow-opener/) | +| `PRTY_NO_PIL_WEBP` | disable use of native webp support in Pillow | +| `PRTY_NO_PSUTIL` | do not use [psutil](https://pypi.org/project/psutil/) for reaping stuck hooks and plugins on Windows | +| `PRTY_NO_SQLITE` | disable all database-related functionality (file indexing, metadata indexing, most file deduplication logic) | +| `PRTY_NO_TLS` | disable native HTTPS support; if you still want to accept HTTPS connections then TLS must now be terminated by a reverse-proxy | +| `PRTY_NO_VIPS` | disable all [libvips](https://pypi.org/project/pyvips/)-based thumbnail support; will fallback to Pillow or ffmpeg | + +example: `PRTY_NO_PIL=1 python3 copyparty-sfx.py` + +* `PRTY_NO_PIL` saves ram +* `PRTY_NO_VIPS` saves ram and startup time +* python2.7 on windows: `PRTY_NO_FFMPEG` + `PRTY_NO_FFPROBE` saves startup time + + ## optional gpl stuff some bundled tools have copyleft dependencies, see [./bin/#mtag](bin/#mtag) diff --git a/copyparty/__main__.py b/copyparty/__main__.py index fff4ccdc..9392713d 100644 --- a/copyparty/__main__.py +++ b/copyparty/__main__.py @@ -68,6 +68,9 @@ if True: # pylint: disable=using-constant-test from typing import Any, Optional try: + if os.environ.get("PRTY_NO_TLS"): + raise Exception() + HAVE_SSL = True import ssl except: diff --git a/copyparty/cert.py b/copyparty/cert.py index 1923edfc..7618c876 100644 --- a/copyparty/cert.py +++ b/copyparty/cert.py @@ -9,7 +9,7 @@ import time from .__init__ import ANYWIN from .util import Netdev, runcmd, wrename, wunlink -HAVE_CFSSL = True +HAVE_CFSSL = not os.environ.get("PRTY_NO_CFSSL") if True: # pylint: disable=using-constant-test from .util import NamedLogger, RootLogger diff --git a/copyparty/httpcli.py b/copyparty/httpcli.py index 3689872f..ba739146 100644 --- a/copyparty/httpcli.py +++ b/copyparty/httpcli.py @@ -25,6 +25,9 @@ from operator import itemgetter import jinja2 # typechk try: + if os.environ.get("PRTY_NO_LZMA"): + raise Exception() + import lzma except: pass diff --git a/copyparty/httpconn.py b/copyparty/httpconn.py index e6c3a9da..e5ba148a 100644 --- a/copyparty/httpconn.py +++ b/copyparty/httpconn.py @@ -9,6 +9,9 @@ import threading # typechk import time try: + if os.environ.get("PRTY_NO_TLS"): + raise Exception() + HAVE_SSL = True import ssl except: diff --git a/copyparty/mtag.py b/copyparty/mtag.py index 77421bf2..c1a1a4f4 100644 --- a/copyparty/mtag.py +++ b/copyparty/mtag.py @@ -48,8 +48,8 @@ def have_ff(scmd: str) -> bool: return bool(shutil.which(scmd)) -HAVE_FFMPEG = have_ff("ffmpeg") -HAVE_FFPROBE = have_ff("ffprobe") +HAVE_FFMPEG = not os.environ.get("PRTY_NO_FFMPEG") and have_ff("ffmpeg") +HAVE_FFPROBE = not os.environ.get("PRTY_NO_FFPROBE") and have_ff("ffprobe") class MParser(object): @@ -337,6 +337,9 @@ class MTag(object): if self.backend == "mutagen": self._get = self.get_mutagen try: + if os.environ.get("PRTY_NO_MUTAGEN"): + raise Exception() + from mutagen import version # noqa: F401 except: self.log("could not load Mutagen, trying FFprobe instead", c=3) diff --git a/copyparty/th_srv.py b/copyparty/th_srv.py index 2bb12239..aab47e4d 100644 --- a/copyparty/th_srv.py +++ b/copyparty/th_srv.py @@ -45,22 +45,34 @@ HAVE_AVIF = False HAVE_WEBP = False try: + if os.environ.get("PRTY_NO_PIL"): + raise Exception() + from PIL import ExifTags, Image, ImageFont, ImageOps HAVE_PIL = True try: + if os.environ.get("PRTY_NO_PILF"): + raise Exception() + ImageFont.load_default(size=16) HAVE_PILF = True except: pass try: + if os.environ.get("PRTY_NO_PIL_WEBP"): + raise Exception() + Image.new("RGB", (2, 2)).save(BytesIO(), format="webp") HAVE_WEBP = True except: pass try: + if os.environ.get("PRTY_NO_PIL_HEIF"): + raise Exception() + from pyheif_pillow_opener import register_heif_opener register_heif_opener() @@ -69,6 +81,9 @@ try: pass try: + if os.environ.get("PRTY_NO_PIL_AVIF"): + raise Exception() + import pillow_avif # noqa: F401 # pylint: disable=unused-import HAVE_AVIF = True @@ -80,6 +95,9 @@ except: pass try: + if os.environ.get("PRTY_NO_VIPS"): + raise Exception() + HAVE_VIPS = True import pyvips diff --git a/copyparty/util.py b/copyparty/util.py index 2b69b8e5..a0589760 100644 --- a/copyparty/util.py +++ b/copyparty/util.py @@ -98,6 +98,9 @@ except: pass try: + if os.environ.get("PRTY_NO_SQLITE"): + raise Exception() + HAVE_SQLITE3 = True import sqlite3 @@ -106,6 +109,9 @@ except: HAVE_SQLITE3 = False try: + if os.environ.get("PRTY_NO_PSUTIL"): + raise Exception() + HAVE_PSUTIL = True import psutil except: @@ -140,6 +146,9 @@ if TYPE_CHECKING: FAKE_MP = False try: + if os.environ.get("PRTY_NO_MP"): + raise ImportError() + import multiprocessing as mp # import multiprocessing.dummy as mp @@ -158,6 +167,9 @@ else: try: + if os.environ.get("PRTY_NO_IPV6"): + raise Exception() + socket.inet_pton(socket.AF_INET6, "::1") HAVE_IPV6 = True except: