Skip to content

Commit

Permalink
improved extraction logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed May 26, 2023
1 parent 275ba28 commit fcca077
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 25 deletions.
8 changes: 7 additions & 1 deletion src/GearleverWindow.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import logging
from .InstalledAppsList import InstalledAppsList
from .AppDetails import AppDetails
from .providers.providers_list import appimage_provider
from .models.AppListElement import AppListElement
from .State import state
from .lib import utils
Expand Down Expand Up @@ -90,6 +91,8 @@ def __init__(self, from_file=False, **kwargs):
self.container_stack.add_controller(self.drop_target_controller)
self.container_stack.add_child(self.drag_drop_ui)

self.connect('close-request', self.on_close_request)

self.set_child(self.container_stack)

# Show app details
Expand Down Expand Up @@ -171,4 +174,7 @@ def on_uninstalled_app(self, widget, data):
if self.from_file:
return self.close()

self.on_show_installed_list(widget, data)
self.on_show_installed_list(widget, data)

def on_close_request(self, widget):
appimage_provider.extraction_folder_cleanup()
51 changes: 27 additions & 24 deletions src/providers/AppImageProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import filecmp
from xdg import DesktopEntry
import subprocess
import random
import signal

from ..lib import terminal
Expand Down Expand Up @@ -304,7 +305,7 @@ def install_file(self, el: AppImageListElement):
logging.error('Appimage installation error: ' + str(e))

try:
self.post_file_extraction_cleanup(extraction=extracted_appimage)
self.extraction_folder_cleanup()
except Exception as g:
logging.error('Appimage cleanup error: ' + str(g))

Expand Down Expand Up @@ -339,34 +340,36 @@ def create_list_element_from_file(self, file: Gio.File) -> AppImageListElement:

return el

def post_file_extraction_cleanup(self, extraction: ExtractedAppImage):
def extraction_folder_cleanup(self):
logging.debug(f'Clearing {self.extraction_folder}')
if os.path.exists(self.extraction_folder):
logging.debug(f'Clearing {self.extraction_folder}')
shutil.rmtree(self.extraction_folder)

def mount_appimage(self, file_path: str) -> str:
# Start the process and redirect its standard output to a pipe
logging.debug('Running ' + ' '.join(['flatpak-spawn', '--host', file_path, '--appimage-mount']))
self.process = subprocess.Popen(['flatpak-spawn', '--host', file_path, '--appimage-mount'], stdout=subprocess.PIPE)

if self.process.stdout:
# Read the output of the process line by line
while True:
output = self.process.stdout.readline()
if output == '' and self.process.poll() is not None:
break
if output:
out = output.decode('utf-8').strip()
logging.debug(f'Appimage, mounted {out}')

return out

raise InternalError('Failed to mount appimage')

random_str = ''.join((random.choice('abcdxyzpqr123456789') for i in range(10)))
dest_path = f'{self.extraction_folder}/gearlever_{random_str}'

file = Gio.File.new_for_path(file_path)
dest = Gio.File.new_for_path(f'{dest_path}/tmp.appimage')

if not os.path.exists(f'{dest_path}'):
os.mkdir(f'{dest_path}')

gio_copy(file, dest)

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]

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

return f'{dest_path}/squashfs-root'

def unmount_appimage(self):
if self.process:
logging.debug(f'Appimage, unmounted')
self.process.send_signal(signal.SIGTERM)
return

def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:
if not el.trusted:
Expand Down

0 comments on commit fcca077

Please sign in to comment.