Skip to content

Commit

Permalink
do not chmod original file if is not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
mijorus committed May 30, 2023
1 parent 4977311 commit 87dcc1c
Showing 1 changed file with 25 additions and 28 deletions.
53 changes: 25 additions & 28 deletions src/providers/AppImageProvider.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
from ..models.AppListElement import AppListElement, InstalledStatus
from ..lib.async_utils import _async
from ..lib.utils import log, cleanhtml, get_giofile_content_type, get_gsettings, gio_copy, get_file_hash
from ..components.CustomComponents import LabelStart
from ..models.Models import FlatpakHistoryElement, AppUpdateElement, InternalError
from typing import List, Callable, Union, Dict, Optional, List, TypedDict
from gi.repository import GLib, Gtk, Gdk, GdkPixbuf, Gio, GObject, Pango, Adw
Expand Down Expand Up @@ -66,10 +65,9 @@ def __init__(self):
self.update_messages = []

self.extraction_folder = GLib.get_tmp_dir() + '/it.mijorus.gearlever/appimages'
self.mount_appimage_process: Optional[subprocess.Popen] = None

def list_installed(self) -> List[AppImageListElement]:
default_folder_path = self.get_appimages_default_destination_path()
default_folder_path = self._get_appimages_default_destination_path()
desktop_files_dir = f'{GLib.get_user_data_dir()}/applications/'
output = []

Expand Down Expand Up @@ -104,9 +102,9 @@ def list_installed(self) -> List[AppImageListElement]:
return output

def is_installed(self, el: AppImageListElement) -> bool:
if el.file_path and os.path.exists(self.get_appimages_default_destination_path()):
for file_name in os.listdir(self.get_appimages_default_destination_path()):
installed_gfile = Gio.File.new_for_path(self.get_appimages_default_destination_path() + '/' + file_name)
if el.file_path and os.path.exists(self._get_appimages_default_destination_path()):
for file_name in os.listdir(self._get_appimages_default_destination_path()):
installed_gfile = Gio.File.new_for_path(self._get_appimages_default_destination_path() + '/' + file_name)
loaded_gfile = Gio.File.new_for_path(el.file_path)

if get_giofile_content_type(installed_gfile) in self.supported_mimes:
Expand All @@ -131,7 +129,7 @@ def get_icon(self, el: AppImageListElement) -> Gtk.Image:
return Gtk.Image.new_from_icon_name(el.desktop_entry.getIcon())

if el.trusted:
extracted = self.extract_appimage(el)
extracted = self._load_appimage_metadata(el)

if extracted.icon_file and os.path.exists(extracted.icon_file.get_path()):
return Gtk.Image.new_from_file(extracted.icon_file.get_path())
Expand All @@ -149,7 +147,7 @@ def refresh_title(self, el: AppImageListElement) -> str:
el.name = el.desktop_entry.getName()

if el.trusted:
extracted = self.extract_appimage(el)
extracted = self._load_appimage_metadata(el)
if extracted.desktop_entry:
el.name = extracted.desktop_entry.getName()

Expand Down Expand Up @@ -188,7 +186,7 @@ def updates_need_refresh(self) -> bool:

def run(self, el: AppImageListElement):
if el.trusted:
os.chmod(el.file_path, 0o755)
self._make_file_executable(el, el.file_path)
terminal.threaded_sh([f'{el.file_path}'], return_stderr=True)

def can_install_file(self, file: Gio.File) -> bool:
Expand All @@ -207,11 +205,11 @@ def install_file(self, el: AppImageListElement):
extracted_appimage: Optional[ExtractedAppImage] = None

try:
extracted_appimage = self.extract_appimage(el)
extracted_appimage = self._load_appimage_metadata(el)
dest_file_info = extracted_appimage.appimage_file.query_info('*', Gio.FileQueryInfoFlags.NOFOLLOW_SYMLINKS)

# Move .appimage to its default location
appimages_destination_path = self.get_appimages_default_destination_path()
appimages_destination_path = self._get_appimages_default_destination_path()

if not os.path.exists(f'{appimages_destination_path}'):
os.mkdir(f'{appimages_destination_path}')
Expand All @@ -229,7 +227,7 @@ def install_file(self, el: AppImageListElement):

log(f'file copied to {appimages_destination_path}')

os.chmod(dest_appimage_file.get_path(), 0o755)
self._make_file_executable(el, dest_appimage_file.get_path())
el.file_path = dest_appimage_file.get_path()

# copy the icon file
Expand Down Expand Up @@ -356,11 +354,12 @@ def extraction_folder_cleanup(self):
if os.path.exists(self.extraction_folder):
shutil.rmtree(self.extraction_folder)

def mount_appimage(self, file_path: str) -> str:

def _extract_appimage(self, el: AppImageListElement) -> str:
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)
file = Gio.File.new_for_path(el.file_path)
dest = Gio.File.new_for_path(f'{dest_path}/tmp.appimage')

if not os.path.exists(f'{dest_path}'):
Expand All @@ -370,8 +369,7 @@ def mount_appimage(self, file_path: str) -> str:

gio_copy(file, dest)

logging.debug(f'Chmod file {dest.get_path()}')
os.chmod(dest.get_path(), 0o755)
self._make_file_executable(el, dest.get_path())

appimage_extract_support = False

Expand Down Expand Up @@ -412,10 +410,7 @@ def mount_appimage(self, file_path: str) -> str:

return f'{dest_path}/squashfs-root'

def unmount_appimage(self):
return

def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:
def _load_appimage_metadata(self, el: AppImageListElement) -> ExtractedAppImage:
if not el.trusted:
raise InternalError(message=_('Cannot load an untrusted AppImage'))

Expand All @@ -440,9 +435,7 @@ def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:
shutil.rmtree(tmp_folder.get_path())

if tmp_folder.make_directory_with_parents(None):
os.chmod(file.get_path(), 0o755)

mounted_appimage_path = self.mount_appimage(file.get_path())
mounted_appimage_path = self._extract_appimage(el)
extraction_folder = Gio.File.new_for_path(mounted_appimage_path)

try:
Expand Down Expand Up @@ -504,9 +497,6 @@ def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:
except Exception as e:
logging.error(str(e))

finally:
self.unmount_appimage()

result = ExtractedAppImage()
result.desktop_entry = desktop_entry
result.extraction_folder = tmp_folder.get_path()
Expand All @@ -521,5 +511,12 @@ def extract_appimage(self, el: AppImageListElement) -> ExtractedAppImage:

return result

def get_appimages_default_destination_path(self) -> str:
return get_gsettings().get_string('appimages-default-folder').replace('~', GLib.get_home_dir())
def _get_appimages_default_destination_path(self) -> str:
return get_gsettings().get_string('appimages-default-folder').replace('~', GLib.get_home_dir())

def _make_file_executable(self, el: AppImageListElement, file_path: str):
if el.trusted:
logging.debug('Chmod file ' + file_path)
os.chmod(el.file_path, 0o755)
else:
raise InternalError(message=_('Cannot load an untrusted AppImage'))

0 comments on commit 87dcc1c

Please sign in to comment.