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 diff --git a/python_appimage/utils/fs.py b/python_appimage/utils/fs.py index a42e9b8..5e6ec85 100644 --- a/python_appimage/utils/fs.py +++ b/python_appimage/utils/fs.py @@ -1,8 +1,30 @@ -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 +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 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)