Skip to content

Commit

Permalink
Mime types (#7)
Browse files Browse the repository at this point in the history
* re rename to installer

* add to path

* fix name -> uid ; code cleanness
  • Loading branch information
cartoush authored and mouuff committed Jan 17, 2019
1 parent c7ac23c commit c2c94d3
Show file tree
Hide file tree
Showing 9 changed files with 67 additions and 16 deletions.
6 changes: 6 additions & 0 deletions iquail/controller/controller_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,9 @@ def start_run_or_update(self):
""" Start update"""
pass

@abstractmethod
def is_graphical(self):
"""Tells if controller is graphical or terminal
:return: Boolean
"""
pass
4 changes: 4 additions & 0 deletions iquail/controller/controller_console.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def callback_solution_unreachable_error(self, exception_info):
input("Press enter to continue...")

def callback_solution_not_removable_error(self, exception_info):
pass
print("[*] Impossible to remove / update application, please close application first!")
input("Press enter to exit...")

Expand Down Expand Up @@ -67,3 +68,6 @@ def start_uninstall(self):

def press_to_exit(self):
input("Press enter to exit ...")

def is_graphical(self):
return False
3 changes: 3 additions & 0 deletions iquail/controller/controller_tkinter/controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,3 +195,6 @@ def start_run_or_update(self):
start_frame = FrameUpdating
self._start_tk(start_frame,
"%s update" % self.manager.get_name())

def is_graphical(self):
return True
1 change: 0 additions & 1 deletion iquail/helper/configuration.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ def __init__(self, filename):
self._config = {}

def read(self):
Path(self._filename).touch()
self._parser.read(self._filename)
if self._default_scope not in self._parser:
self._parser[self._default_scope] = {}
Expand Down
13 changes: 9 additions & 4 deletions iquail/helper/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,16 @@ def self_remove_directory(directory):
_delete_atexit(directory)


def rerun_as_admin():
def rerun_as_admin(graphical):
if OS_LINUX:
# os.system('pkexec %s %s' % (get_script_path(),
# ' '.join(sys.argv[1:])))
raise NotImplementedError
cmd = None
if graphical is False:
cmd = ['sudo', '-A']
elif shutil.which('gksudo'):
cmd = ['gksudo', '--']
elif shutil.which('kdesudo'):
cmd = ['kdesudo']
sys.exit(os.execvp(cmd[0], cmd + sys.argv))
elif OS_WINDOWS:
if not ctypes.windll.shell32.IsUserAnAdmin():
ctypes.windll.shell32.ShellExecuteW(None,
Expand Down
15 changes: 10 additions & 5 deletions iquail/installer/installer_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,25 +20,31 @@ def __init__(self,
name,
icon,
publisher,
install_systemwide=False,
console=False,
binary_options='',
install_path='default',
install_path=None,
launch_with_quail=True):
self._install_systemwide = install_systemwide
self._launch_with_quail = launch_with_quail
self._binary_name = binary
self._binary_options = binary_options
self._name = name
self._icon = icon
self._publisher = publisher
self._console = console
self._install_path = self.build_install_path() if install_path is 'default' else install_path
self._install_path = self.build_install_path() if install_path is None else install_path
self._solution_path = os.path.join(self._install_path, 'solution')


def get_solution_icon(self):
"""Get solution's icon"""
return self.get_solution_path(self._icon)

@property
def install_systemwide(self):
return self._install_systemwide

@property
def binary_options(self):
"""Options for the binary"""
Expand Down Expand Up @@ -99,9 +105,8 @@ def build_install_path(self):
"""Build install path
This function can be overridden to install files to somewhere else
"""
return os.path.join(str(pathlib.Path.home()),
Constants.IQUAIL_ROOT_NAME,
self.uid)
base = '/opt/' if self.install_systemwide else pathlib.Path.home()
return os.path.join(base, Constants.IQUAIL_ROOT_NAME, self.uid)

def get_solution_path(self, *args):
"""Get solution path"""
Expand Down
23 changes: 20 additions & 3 deletions iquail/installer/installer_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def __init__(self, linux_desktop_conf={}, linux_exec_flags='', *args, **kwargs):
self._uninstall_shortcut = self._desktop("%s_uninstall" % self.uid)

def _desktop(self, name):
return os.path.join(str(pathlib.Path.home()),
".local", "share", "applications",
"%s.desktop" % name)
return os.path.join(os.path.join(str(pathlib.Path.root), "/usr") if self._install_systemwide
else os.path.join(str(pathlib.Path.home()), ".local"),
"share", "applications", "%s.desktop" % name)

def _write_desktop(self, filename, app_config):
"""Write desktop entry"""
Expand All @@ -49,6 +49,10 @@ def is_shortcut(self, dest):
# TODO: abs shortcut path & add desktop var
return os.path.isfile(dest)

def build_install_path(self):
return '/opt/' + os.path.join(Constants.IQUAIL_ROOT_NAME, self.name) if self._install_systemwide else \
os.path.join(str(pathlib.Path.home()), Constants.IQUAIL_ROOT_NAME, self.name)

def _register(self):
self.add_shortcut(dest=self._launch_shortcut,
**self._desktop_conf)
Expand All @@ -58,14 +62,27 @@ def _register(self):
Exec=self.iquail_binary + " " + Constants.ARGUMENT_UNINSTALL,
Icon=self.get_solution_icon(),
Terminal='true' if self.console else 'false')
self.add_to_path(self.binary, self._binary_name)

def _unregister(self):
self.delete_shortcut(self._launch_shortcut)
self.delete_shortcut(self._uninstall_shortcut)
self.remove_from_path(self.binary)

def _registered(self):
if not self.is_shortcut(self._launch_shortcut):
return False
if not self.is_shortcut(self._uninstall_shortcut):
return False
return True

def add_to_path(self, binary, name):
os.symlink(binary, self.build_symlink_path(name))

def remove_from_path(self, name):
os.remove(self.build_symlink_path(name))

def build_symlink_path(self, name):
return os.path.join("/usr/bin" if self._install_systemwide
else os.path.join(str(pathlib.Path.home()), '.local/bin'), name)

16 changes: 14 additions & 2 deletions iquail/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@
import stat
import sys
import typing
from .helper import misc

from . import helper
from .constants import Constants
from .solution.solutioner import Solutioner


class Manager:
def __init__(self, installer, solution, builder):
def __init__(self, installer, solution, builder, graphical):
self._graphical = graphical
self._installer = installer
self._solution = solution
self._builder = builder
Expand Down Expand Up @@ -89,6 +91,8 @@ def is_new_version_available(self):
def install_part_solution(self):
"""part 1 of the installation will install the solution
"""
#permission checked here because tkinter calls this method directly instead of install()
self.check_permissions()
self.apply_conf() # because conf have been just selected
self._solutioner.install()
self._set_solution_installed_version()
Expand All @@ -113,14 +117,16 @@ def install(self):
def update(self):
"""Update process"""
# TODO: kill solution here
self.check_permissions()
self._solutioner.update()
self._set_solution_installed_version()

def uninstall(self):
""" Uninstall process
"""
self._solutioner.uninstall()
self.check_permissions()
self._installer.unregister()
self._solutioner.uninstall()

def is_installed(self):
"""Check if solution is installed"""
Expand All @@ -134,3 +140,9 @@ def run(self):
args = list(filter(lambda x: "--iquail" not in x, sys.argv[1:]))
binary_args = [os.path.basename(binary)] + args
os.execl(binary, *binary_args)

def check_permissions(self):
if self._installer.install_systemwide and os.geteuid() != 0:
print('Root access is required for further action, relaunching as root')
misc.rerun_as_admin(self._graphical)

2 changes: 1 addition & 1 deletion iquail/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def run(solution, installer, builder=None, controller=None):
builder = Builder()
if not controller:
controller = ControllerConsole()
manager = Manager(installer, solution, builder)
manager = Manager(installer, solution, builder, controller.is_graphical())
controller.setup(manager)
if args.iquail_rm:
shutil.rmtree(args.iquail_rm)
Expand Down

0 comments on commit c2c94d3

Please sign in to comment.