From 46b2efb35920b04ee14ff003f51c5f67774f7d74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C5=82awomir=20Zborowski?= Date: Fri, 24 May 2024 00:05:48 +0200 Subject: [PATCH 1/3] Added information about extra data in the docs --- docs/src/apps.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/docs/src/apps.md b/docs/src/apps.md index b175761..922d520 100644 --- a/docs/src/apps.md +++ b/docs/src/apps.md @@ -182,6 +182,36 @@ example, `$APPDIR` points to the AppImage mount point at runtime. `{{ python-executable }} -I` starts a fully isolated Python instance. {% endraw %} +### Bundling data files + +`python-appimage` is also capable of bundling in auxilliary data files directly +into the resulting AppImage. `-x/--extra-data` switch exists for that task. +Consider following example. + +```bash +echo -n "foo" > foo +mkdir bar +echo -n "baz" > bar/baz +python-appimage [your regular parameters] -x foo bar/* +``` + +User data included in such a way becomes accessible to the Python code +contained within the AppImage in a form of regular files under the directory +pointed to by `APPDIR` environment variable. Example of Python 3 script +that reads these exemplary files is presented below. + +```python +import os, pathlib +for fileName in ("foo", "baz"): + print((pathlib.Path(os.getenv("APPDIR")) / fileName).read_text()) +``` + +Above code, when executed, would print following output. + +```bash +foo +baz +``` ## Advanced packaging From c8fde2906ac711a6089d2e6ca7cfcc4feb5ca3ae Mon Sep 17 00:00:00 2001 From: Maximilian Knespel Date: Thu, 10 Oct 2024 15:15:23 +0200 Subject: [PATCH 2/3] Replace distutils usage with os and shutil --- python_appimage/utils/fs.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/python_appimage/utils/fs.py b/python_appimage/utils/fs.py index a42e9b8..1f665dd 100644 --- a/python_appimage/utils/fs.py +++ b/python_appimage/utils/fs.py @@ -1,7 +1,6 @@ -from distutils.dir_util import mkpath as _mkpath, remove_tree as _remove_tree -from distutils.file_util import copy_file as _copy_file import errno import os +import shutil from .log import debug @@ -14,7 +13,7 @@ def make_tree(path): '''Create directories recursively if they don't exist ''' debug('MKDIR', path) - return _mkpath(path) + return os.makedirs(path, exist_ok=True) def copy_file(source, destination, update=False, verbose=True): @@ -23,7 +22,14 @@ def copy_file(source, destination, update=False, verbose=True): name = os.path.basename(source) if verbose: debug('COPY', '%s from %s', name, os.path.dirname(source)) - _copy_file(source, destination, update=update) + if os.path.exists(source) and ( + not update + or ( + not os.path.exists(destination) + or (os.path.getmtime(source) > os.path.getmtime(destination)) + ) + ): + shutil.copy(source, destination) def copy_tree(source, destination): @@ -38,7 +44,7 @@ def copy_tree(source, destination): for root, _, files in os.walk(source): relpath = os.path.relpath(root, source) dirname = os.path.join(destination, relpath) - _mkpath(dirname) + os.makedirs(dirname, exist_ok=True) for file_ in files: src = os.path.join(root, file_) dst = os.path.join(dirname, file_) @@ -70,6 +76,6 @@ def remove_tree(path): name = os.path.basename(path) debug('REMOVE', '%s from %s', name, os.path.dirname(path)) try: - _remove_tree(path) + shutil.rmtree(path) except OSError: pass From ea671fe7ed94c46d719049dda68401058a4972a8 Mon Sep 17 00:00:00 2001 From: Valentin Niess Date: Fri, 11 Oct 2024 11:03:22 +0200 Subject: [PATCH 3/3] Python 2 compat tweaks --- python_appimage/utils/fs.py | 40 +++++++++++++++++++++++++----------- python_appimage/utils/tmp.py | 1 + 2 files changed, 29 insertions(+), 12 deletions(-) diff --git a/python_appimage/utils/fs.py b/python_appimage/utils/fs.py index 1f665dd..5e6ec85 100644 --- a/python_appimage/utils/fs.py +++ b/python_appimage/utils/fs.py @@ -1,6 +1,29 @@ import errno import os -import shutil + +try: + from distutils.dir_util import mkpath as _mkpath + from distutils.dir_util import remove_tree as _remove_tree + from distutils.file_util import copy_file as _copy_file + +except ImportError: + import shutil + + def _mkpath(path): + os.makedirs(path, exist_ok=True) + + def _remove_tree(path): + shutil.rmtree(path) + + def _copy_file(source, destination, update=0): + if os.path.exists(source) and ( + not update + or ( + (not os.path.exists(destination)) + or (os.path.getmtime(source) > os.path.getmtime(destination)) + ) + ): + shutil.copyfile(source, destination) from .log import debug @@ -13,7 +36,7 @@ def make_tree(path): '''Create directories recursively if they don't exist ''' debug('MKDIR', path) - return os.makedirs(path, exist_ok=True) + return _mkpath(path) def copy_file(source, destination, update=False, verbose=True): @@ -22,14 +45,7 @@ def copy_file(source, destination, update=False, verbose=True): name = os.path.basename(source) if verbose: debug('COPY', '%s from %s', name, os.path.dirname(source)) - if os.path.exists(source) and ( - not update - or ( - not os.path.exists(destination) - or (os.path.getmtime(source) > os.path.getmtime(destination)) - ) - ): - shutil.copy(source, destination) + _copy_file(source, destination, update=update) def copy_tree(source, destination): @@ -44,7 +60,7 @@ def copy_tree(source, destination): for root, _, files in os.walk(source): relpath = os.path.relpath(root, source) dirname = os.path.join(destination, relpath) - os.makedirs(dirname, exist_ok=True) + _mkpath(dirname) for file_ in files: src = os.path.join(root, file_) dst = os.path.join(dirname, file_) @@ -76,6 +92,6 @@ def remove_tree(path): name = os.path.basename(path) debug('REMOVE', '%s from %s', name, os.path.dirname(path)) try: - shutil.rmtree(path) + _remove_tree(path) except OSError: pass diff --git a/python_appimage/utils/tmp.py b/python_appimage/utils/tmp.py index 842b3c8..3a65cc5 100644 --- a/python_appimage/utils/tmp.py +++ b/python_appimage/utils/tmp.py @@ -20,5 +20,6 @@ def TemporaryDirectory(): try: yield tmpdir finally: + debug('REMOVE', tmpdir) os.chdir(pwd) remove_tree(tmpdir)