Skip to content

BUG: Path errors when using python-appimage inside a python.AppImage #99

Open

Description

@K9Kraken

Issue

When using python-appimage from within a appimage: ./python.AppImage -m python_appimage
python-appimage failed to package builds because it passed a relative AppDir path and searched for the output AppImage in the wrong directory.

Fix
Convert AppDir to an absolute path and search for the generated AppImage in the original working directory.

Files modified (from squashfs-root/)
1. opt/python3.15/lib/python3.15/site-packages/python_appimage/appimage/build.py
   Added: appdir = os.path.abspath(appdir)

2. opt/python3.15/lib/python3.15/site-packages/python_appimage/commands/build/local.py
   Changed: appimage = glob.glob(pattern)[0]
   To:     appimage = glob.glob(os.path.join(pwd, pattern))[0]

Why

An AppImage runs in a different environment,
which can affect how relative paths are resolved.

The build tool used relative paths, so it couldn't
reliably find the AppDir or the finished AppImage.

Using absolute paths fixes both problems.

local.py

import glob
import os
import shutil

from ...appimage import build_appimage, relocate_python
from ...utils.tmp import TemporaryDirectory


__all__ = ['execute']


def _unpack_args(args):
    '''Unpack command line arguments
    '''
    return args.python, args.destination


def execute(python=None, destination=None):
    '''Build a Python AppImage using a local installation
    '''
    pwd = os.getcwd()
    with TemporaryDirectory() as tmpdir:
        relocate_python(python)

        dirname, pattern = None, None
        if destination is not None:
            dirname, pattern = os.path.split(destination)
        if not pattern:
            pattern = 'python*.AppImage'

        build_appimage()

        appimage = glob.glob(os.path.join(pwd, pattern))[0]
        if dirname:
            dirname = os.path.abspath(os.path.join(pwd, dirname))
        else:
            dirname = pwd

        shutil.move(appimage, os.path.join(dirname, os.path.basename(appimage)))

build.py

import os
import platform
import re
import subprocess
import sys

from ..utils.compat import decode
from ..utils.deps import ensure_appimagetool
from ..utils.log import debug, log


__all__ = ['build_appimage']


def build_appimage(appdir=None, *, arch=None, destination=None):
    '''Build an AppImage from an AppDir
    '''
    if appdir is None:
        appdir = 'AppDir'
    appdir = os.path.abspath(appdir)

    log('BUILD', os.path.basename(appdir))
    appimagetool = ensure_appimagetool()

    if arch is None:
        arch = platform.machine()
    cmd = ['ARCH=' + arch, appimagetool, '--no-appstream', appdir]
    if destination is not None:
        cmd.append(destination)
    cmd = ' '.join(cmd)

    debug('SYSTEM', cmd)
    p = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
                                          stderr=subprocess.STDOUT)

    appimage_pattern = re.compile('should be packaged as ([^ ]+[.]AppImage)')

    stdout = []
    while True:
        out = decode(p.stdout.readline())
        stdout.append(out)
        if out == '' and p.poll() is not None:
            break
        elif out:
            out = out.replace('%', '%%')[:-1]
            for line in out.split(os.linesep):
                if line.startswith('WARNING') and \
                    not line[9:].startswith('zsyncmake command is missing'):
                    log('WARNING', line[9:])
                elif line.startswith('Error'):
                    raise RuntimeError(line)
                else:
                    if destination is None:
                        match = appimage_pattern.search(line)
                        if match is not None:
                            destination = match.group(1)
                    debug('APPIMAGE', line)

    rc = p.poll()
    if rc != 0 and not os.path.exists(destination):
        print(''.join(stdout))
        sys.stdout.flush()
        raise RuntimeError('Could not build AppImage')

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions