def run_for(self, args):
     """Running commands from args namespace"""
     # try to call default framework if any
     if not args.framework:
         if not self.default_framework:
             message = "A default framework for category {} was requested where there is none".format(self.name)
             logger.error(message)
             UI.return_main_screen(status_code=1)
         self.default_framework.run_for(args)
         return
     self.frameworks[args.framework].run_for(args)
 def run_for(self, args):
     """Running commands from args namespace"""
     logger.debug("Call run_for on {}".format(self.name))
     if args.remove:
         if args.destdir:
             message = "You can't specify a destination dir while removing a framework"
             logger.error(message)
             UI.return_main_screen()
         self.remove()
     else:
         install_path = None
         if args.destdir:
             install_path = os.path.abspath(os.path.expanduser(args.destdir))
         self.setup(install_path)
    def setup(self):
        """Method call to setup the Framework"""
        if not self.is_installable:
            logger.error("You can't install that framework on that machine")
            UI.return_main_screen()
            return

        if self.need_root_access and os.geteuid() != 0:
            logger.debug("Requesting root access")
            cmd = ["sudo", "-E", "env", "PATH={}".format(os.getenv("PATH"))]
            cmd.extend(sys.argv)
            MainLoop().quit(subprocess.call(cmd))

        # be a normal, kind user as we don't want normal files to be written as root
        switch_to_current_user()
        def done(download_result):
            res = download_result[md5_url]

            if res.error:
                logger.error(res.error)
                UI.return_main_screen()
                return

            # Should be ASCII anyway.
            md5 = res.buffer.getvalue().decode('utf-8').split()[0]
            logger.debug("Downloaded MD5 is {}".format(md5))

            logger.debug("Preparing to download the main archive.")

            download_url = self.DOWNLOAD_URL_PAT.format(release=RELEASE, suf='')

            self.download_requests.append((download_url, md5))
            self.start_download_and_install()
    def decompress_and_install_done(self, result):
        self._install_done = True
        error_detected = False
        for fd in result:
            if result[fd].error:
                logger.error(result[fd].error)
                error_detected = True
            fd.close()
        if error_detected:
            UI.return_main_screen()
            return

        # install desktop file
        if self.desktop_file_name:
            self.create_launcher()

        UI.delayed_display(DisplayMessage("Installation done"))
        UI.return_main_screen()
    def remove(self):
        """Remove current framework if installed

        Not that we only remove desktop file, launcher icon and dir content, we do not remove
        packages as they might be in used for other framework"""
        # check if it's installed and so on.
        super().remove()

        UI.display(DisplayMessage("Removing {}".format(self.name)))
        if self.desktop_filename:
            with suppress(FileNotFoundError):
                os.remove(get_launcher_path(self.desktop_filename))
        if self.icon_filename:
            with suppress(FileNotFoundError):
                os.remove(get_icon_path(self.icon_filename))
        with suppress(FileNotFoundError):
            shutil.rmtree(self.install_path)
        self.remove_from_config()

        UI.delayed_display(DisplayMessage("Suppression done"))
        UI.return_main_screen()
    def download_and_requirements_done(self):
        # wait for both side to be done
        if self._download_done_callback_called or (not self.result_download or not self.result_requirement):
            return
        self._download_done_callback_called = True

        self.pbar.finish()
        # display eventual errors
        error_detected = False
        fd = None
        if self.result_requirement.error:
            logger.error(self.result_requirement.error)
            error_detected = True
        for url in self.result_download:
            if self.result_download[url].error:
                logger.error(self.result_download[url].error)
                error_detected = True
            fd = self.result_download[url].fd
        if error_detected:
            UI.return_main_screen()
            return
        self.decompress_and_install(fd)
    def get_metadata_and_check_license(self, result):
        """Download files to download + license and check it"""
        logger.debug("Parse download metadata")

        error_msg = result[self.download_page].error
        if error_msg:
            logger.error("An error occurred while downloading {}: {}".format(self.download_page, error_msg))
            UI.return_main_screen()

        url, md5sum = (None, None)
        with StringIO() as license_txt:
            in_license = False
            in_download = False
            for line in result[self.download_page].buffer:
                line_content = line.decode()

                if self.expect_license:
                    in_license = self.parse_license(line_content, license_txt, in_license)

                (download, in_download) = self.parse_download_link(line_content, in_download)
                if download is not None:
                    (newurl, newmd5sum) = download
                    url = newurl if newurl is not None else url
                    md5sum = newmd5sum if newmd5sum is not None else md5sum
                    logger.debug("Found download link for {}, md5sum: {}".format(url, md5sum))

            if url is None or (self.require_md5 and md5sum is None):
                logger.error("Download page changed its syntax or is not parsable")
                UI.return_main_screen()
            self.download_requests.append((url, md5sum))

            if license_txt.getvalue() != "":
                logger.debug("Check license agreement.")
                UI.display(LicenseAgreement(strip_tags(license_txt.getvalue()).strip(),
                                            self.start_download_and_install,
                                            UI.return_main_screen))
            elif self.expect_license:
                logger.error("We were expecting to find a license on the download page, we didn't.")
                UI.return_main_screen()
            else:
                self.start_download_and_install()
        return
 def test_return_to_mainscreen(self):
     """We call the return to main screen on the UIPlug"""
     UI.return_main_screen()
     self.assertTrue(self.mockUIPlug._return_main_screen.called)
 def remove(self):
     """Method call to remove the current framework"""
     if not self.is_installed:
         logger.error(_("You can't remove {} as it isn't installed".format(self.name)))
         UI.return_main_screen(status_code=1)
         return