Skip to content

Commit

Permalink
support appimages that cannot be partially extracted
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed May 26, 2023
1 parent fcca077 commit 84b766f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 23 deletions.
20 changes: 5 additions & 15 deletions src/lib/terminal.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import subprocess
import re
import asyncio
import threading
from typing import Callable, List, Union
from typing import Callable, List, Union, Optional
from .utils import log
import logging

_sanitizer = None
def sanitize(_input: str) -> str:
global _sanitizer

if not _sanitizer:
_sanitizer = re.compile(r'[^0-9a-zA-Z]+')

return re.sub(_sanitizer, " ", _input)

def sh(command: List[str], return_stderr=False) -> str:
def sh(command: List[str], return_stderr=False, **kwargs) -> str:
try:
log(f'Running {command}')

cmd = ['flatpak-spawn', '--host', *command]
output = subprocess.run(cmd, encoding='utf-8', shell=False, check=True, capture_output=True)
output = subprocess.run(cmd, encoding='utf-8', shell=False, check=True, capture_output=True, **kwargs)
output.check_returncode()
except subprocess.CalledProcessError as e:
print(e.stderr)
Expand All @@ -32,8 +22,8 @@ def sh(command: List[str], return_stderr=False) -> str:

return re.sub(r'\n$', '', output.stdout)

def threaded_sh(command: List[str], callback: Callable[[str], None]=None, return_stderr=False):
def run_command(command: List[str], callback: Callable[[str], None]=None):
def threaded_sh(command: List[str], callback: Optional[Callable[[str], None]]=None, return_stderr=False):
def run_command(command: List[str], callback: Optional[Callable[[str], None]]=None):
try:
output = sh(command, return_stderr)

Expand Down
29 changes: 21 additions & 8 deletions src/providers/AppImageProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,15 +250,15 @@ def install_file(self, el: AppImageListElement):

# replace executable path
desktop_file_content = re.sub(
r'Exec=.*$',
r'^Exec=.*$',
f"Exec={dest_appimage_file.get_path()}",
desktop_file_content,
flags=re.MULTILINE
)

# replace icon path
desktop_file_content = re.sub(
r'Icon=.*$',
r'^Icon=.*$',
f"Icon={dest_appimage_icon_file.get_path() if dest_appimage_icon_file else 'applications-other'}",
desktop_file_content,
flags=re.MULTILINE
Expand Down Expand Up @@ -359,12 +359,22 @@ def mount_appimage(self, file_path: str) -> str:

os.chmod(dest.get_path(), 0o755)

prefix = ['flatpak-spawn', '--host', dest.get_path(), '--appimage-extract']
for match in ['*.desktop', 'usr/share/icons/*', '*.svg', '*.png']:
run = [*prefix, match]
prefix = [dest.get_path(), '--appimage-extract']

logging.debug('Running ' + ' '.join(run))
subprocess.run(run, cwd=dest_path, stdout=subprocess.PIPE)
extraction_output = ''

# check if the appimage supports partial extraction
appimage_help = terminal.sh([dest.get_path(), '--appimage-help'], cwd=dest_path)
if '--appimage-extract [<pattern>]' in appimage_help:
for match in ['*.desktop', 'usr/share/icons/*', '*.svg', '*.png']:
run = [*prefix, match]

extraction_output = terminal.sh(run, cwd=dest_path)
else:
logging.debug('This AppImage does not support partial extraction, running ' + ' '.join(prefix))
extraction_output = terminal.sh([*prefix], cwd=dest_path)

logging.debug(f'Extracted appimage {file.get_path()} with log:\n\n======\n\n{extraction_output}\n\n======\n\n')

return f'{dest_path}/squashfs-root'

Expand Down Expand Up @@ -439,11 +449,14 @@ def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:

try_paths = [
extraction_folder.get_path() + f'/usr/share/icons/hicolor/scalable/apps/{desktop_entry_icon}.svg',
extraction_folder.get_path() + f'/usr/share/icons/hicolor/512x512/apps/{desktop_entry_icon}.png',
extraction_folder.get_path() + f'/usr/share/icons/hicolor/256x256/apps/{desktop_entry_icon}.png',
extraction_folder.get_path() + f'/usr/share/icons/hicolor/128x128/apps/{desktop_entry_icon}.png'
extraction_folder.get_path() + f'/usr/share/icons/hicolor/128x128/apps/{desktop_entry_icon}.png',
extraction_folder.get_path() + f'/usr/share/icons/hicolor/96x96apps/{desktop_entry_icon}.png'
]

for icon_xt in try_paths:
logging.debug('Looking for icon in: ' + icon_xt)
icon_xt_f = Gio.File.new_for_path(icon_xt)

if icon_xt_f.query_exists():
Expand Down

0 comments on commit 84b766f

Please sign in to comment.