Ejemplo n.º 1
0
class DBUpdater:
    def __init__(self, db_name, protocol="http", source=""):

        self.db_name= db_name
        fantom_tmp_path= os.path.join(fantom_tmp_dir, '%s.txt' % db_name)
        
        urls= all_db_urls[db_name]

        self.download_manager= DownloadManager(db_name, download_dir, protocol, urls)
        self.parser = Parser(db_name, fantom_tmp_path, self.download_manager, source)
        self.updater= BioDBUpdater(db_name, sql_db_path, fantom_tmp_path )
   

    def run(self):
        print "Downloading the database..."
        self.download_manager.download()
        print "Parsing files..."
        self.parser.parse()
        print "Updating sqlite database..."
        self.updater.store_database()
       

    def stats(self):
        self.n_ids=""
        self.n_levels=""
Ejemplo n.º 2
0
    def __init__(self, system_bus, mainloop):
        # Init dbus service.
        dbus.service.Object.__init__(self, system_bus, DSC_SERVICE_PATH)
        # Init.
        self.mainloop = mainloop
        self.pkg_cache = AptCache()
        self.exit_flag = False
        self.simulate = False
        self.all_upgrade_pkg_names = []
        self.download_dir = "/var/cache/apt/archives"

        self.is_upgrading = False
        self.in_update_list = False

        self.apt_action_pool = AptActionPool(self.pkg_cache)
        self.apt_action_pool.start()

        global_event.register_event('parse-download-error',
                                    self.send_parse_download_error)

        global_event.register_event("action-start", self.action_start)
        global_event.register_event("action-update", self.action_update)
        global_event.register_event("action-finish", self.action_finish)
        global_event.register_event("action-failed", self.action_failed)

        global_event.register_event("download-start", self.download_start)
        global_event.register_event("download-update", self.download_update)
        global_event.register_event("download-finish", self.download_finish)
        global_event.register_event("download-stop", self.download_stop)
        global_event.register_event("download-error", self.download_failed)

        global_event.register_event("update-list-start",
                                    self.update_list_start)
        global_event.register_event("update-list-finish",
                                    self.update_list_finish)
        global_event.register_event("update-list-failed",
                                    self.update_list_failed)
        global_event.register_event("update-list-update",
                                    self.update_list_update)

        self.packages_status = {}

        self.exit_manager = ExitManager(self.mainloop,
                                        self.is_update_list_running,
                                        self.is_download_action_running,
                                        self.is_apt_action_running,
                                        self.have_exit_request)
        self.exit_manager.start()

        self.set_download_dir('/var/cache/apt/archives')
        self.download_manager = DownloadManager(global_event, verbose=True)
Ejemplo n.º 3
0
    def _initialize_hosts(self):

        log.msg('Retry count: {}'.format(self.retry_count))
        max_retries = self.app.max_retries

        if self.retry_count == max_retries or not self.initialized:
            self.retry_count = 0

            hosts = self.app.pirate_bay_hosts

            host_config = hosts[self.host_index]
            web_host = "http://{}".format(host_config['host'])
            torrent_host = host_config['torrent_host']
            use_tor = host_config['use_tor_network']

            self.pirate_bay_client = ThePirateBay(web_host, use_tor)

            self.download_manager = DownloadManager()
            self.download_manager.initialize(torrent_host, use_tor, self.app)

            log.msg('Switching to different host')

            self.host_index += 1
            if self.host_index == len(hosts):
                self.host_index = 0
        else:
            self.retry_count += 1
Ejemplo n.º 4
0
    def __init__(self):
        """"""
        if not shared.configuration.configured:
            logger.warning("No configuration found!")

        #events system
        shared.events = Events()

        shared.max_downloads = shared.configuration.get_max_downloads()
        shared.max_download_speed = shared.configuration.get_max_download_speed(
        )

        ServiceManager.__init__(self)
        self.history = History()
        self.download_manager = DownloadManager(self.get_download_plugin,
                                                self.services)
Ejemplo n.º 5
0
    def __set_image_buttons(self):
        try:
            from .__init__ import __main__
        except ImportError:
            from __init__ import __main__
        buttons_frame = tkinter.Frame(master=self.f, height=50)
        buttons_frame.place(x=0,
                            rely=1 - self.BUTTONS_BUTTON_OFFSET / 100,
                            height=50,
                            relwidth=1,
                            anchor="sw")

        self.image_play = tkinter.PhotoImage(master=buttons_frame,
                                             file=__main__.i_f("play.png"))
        self.image_pause = tkinter.PhotoImage(master=buttons_frame,
                                              file=__main__.i_f("pause.png"))

        self.image_plus = tkinter.PhotoImage(master=buttons_frame,
                                             file=__main__.i_f("plus.png"))
        self.image_download = tkinter.PhotoImage(
            master=buttons_frame, file=__main__.i_f("download.png"))
        self.image_delete = ImageTk.PhotoImage(Image.open(
            __main__.i_f("plus.png")).rotate(45),
                                               master=self.f)
        image_next = tkinter.PhotoImage(master=buttons_frame,
                                        file=__main__.i_f("next.png"))
        image_back = tkinter.PhotoImage(master=buttons_frame,
                                        file=__main__.i_f("back.png"))

        self.action_button = ttk.Button(buttons_frame,
                                        image=self.image_plus,
                                        command=self.actionClick)
        self.action_button.place(width=50, height=50, relx=0.2, anchor="n")

        self.back_button = ttk.Button(buttons_frame,
                                      image=image_back,
                                      command=self.backButtonClick)
        self.back_button.image = image_back
        self.back_button.place(width=50, height=50, relx=0.35, anchor="n")

        self.play_button = ttk.Button(buttons_frame,
                                      image=self.image_pause,
                                      command=self.pauseButtonClick)
        self.play_button.image = self.image_pause
        self.play_button.place(width=50, height=50, relx=0.5, anchor="n")

        self.next_button = ttk.Button(buttons_frame,
                                      image=image_next,
                                      command=self.nextButtonClick)
        self.next_button.image = image_next
        self.next_button.place(width=50, height=50, relx=0.65, anchor="n")

        self.button_download = ttk.Button(
            buttons_frame,
            image=self.image_download,
            command=lambda: DownloadManager.get().download(self.item))
        self.button_download.place(width=50, height=50, relx=0.8, anchor="n")

        self.iconbitmap(__main__.icon())
Ejemplo n.º 6
0
    def __init__(self, config):
        self.config = config
        self.download_manager = DownloadManager()

        self.__get_widgets()
        self.__make_extensions_treeview()
        self.__connect_widgets()
        self.__add_config_notifications()

        # Set widget states from configuration
        self.status_icon_checkbutton.set_active(self.config.show_status_icon)
        self.main_window_checkbutton.set_active(self.config.show_main_window)
        self.notifications_checkbutton.set_active(self.config.show_notifications)
        self.quit_dialog_checkbutton.set_active(self.config.show_quit_dialog)
        self.autostart_checkbutton.set_active(self.config.autostart)

        self.main_window_checkbutton.set_sensitive(self.config.show_status_icon)

        if self.config.ask_for_location:
            self.ask_folder_radiobutton.set_active(True)
        else:
            self.specify_folder_radiobutton.set_active(True)
        self.default_folder_filechooserbutton.set_current_folder(self.config.default_folder)
        self.extensions_checkbutton.set_active(self.config.check_extensions)
        if not self.config.check_extensions:
            self.extensions_alignment.set_sensitive(False)

        for extension, folder in zip(self.config.extensions,
                self.config.extension_folders):
            self.extensions_model.append((extension, folder))

        self.manual_proxy_vbox.set_sensitive(False)
        if self.config.proxy_mode == "gnome":
            self.gnome_radiobutton.set_active(True)
        elif self.config.proxy_mode == "manual":
            self.manual_radiobutton.set_active(True)
        else:
            self.direct_radiobutton.set_active(True)

        self.proxy_entry.set_text(self.config.proxy_host)
        self.proxy_port_spinbutton.set_value(self.config.proxy_port)
        self.proxy_auth_checkbutton.set_active(self.config.proxy_auth)
        self.proxy_auth_hbox.set_sensitive(self.config.proxy_auth)
        self.proxy_user_entry.set_text(self.config.proxy_user)
        self.proxy_password_entry.set_text(self.config.proxy_password)
Ejemplo n.º 7
0
    def __init__(self, db_name, protocol="http", source=""):

        self.db_name= db_name
        fantom_tmp_path= os.path.join(fantom_tmp_dir, '%s.txt' % db_name)
        
        urls= all_db_urls[db_name]

        self.download_manager= DownloadManager(db_name, download_dir, protocol, urls)
        self.parser = Parser(db_name, fantom_tmp_path, self.download_manager, source)
        self.updater= BioDBUpdater(db_name, sql_db_path, fantom_tmp_path )
Ejemplo n.º 8
0
 def __init__(self, config_file_location):
     config = yaml.load(file(config_file_location, 'r'))
     synology_config = config['synology']
     self.downloadstation_credentials = DownloadStationCredentials(
         synology_config['url'], synology_config['username'],
         synology_config['password'])
     self.putio_credentials = PutioCredentials(config['putio']['token'])
     self.feed_details = FeedsDetails(config['monitor_minutes'],
                                      config['feeds'])
     download_manager = DownloadManager(self.downloadstation_credentials,
                                        self.putio_credentials,
                                        self.feed_details)
Ejemplo n.º 9
0
class Core(ServiceManager):
	""""""
	def __init__(self):
		""""""
		if not shared.configuration.configured:
			logger.warning("No configuration found!")

		#events system
		shared.events = Events()

		shared.max_downloads = shared.configuration.get_max_downloads()
		shared.max_download_speed = shared.configuration.get_max_download_speed()

		ServiceManager.__init__(self)
		self.history = History()
		self.download_manager = DownloadManager(self.get_download_plugin, self.services)
		
	def stop_all(self):
		""""""
		self.download_manager.quit()
		ServiceManager.stop_all(self)
Ejemplo n.º 10
0
    def __init__(self, system_bus, mainloop):
        # Init dbus service.
        dbus.service.Object.__init__(self, system_bus, DSC_SERVICE_PATH)
        # Init.
        self.mainloop = mainloop
        self.pkg_cache = AptCache()
        self.exit_flag = False
        self.simulate = False
        self.all_upgrade_pkg_names = []
        self.download_dir = "/var/cache/apt/archives"

        self.is_upgrading = False
        self.in_update_list = False

        self.apt_action_pool = AptActionPool(self.pkg_cache)
        self.apt_action_pool.start()

        global_event.register_event(
            'parse-download-error', self.send_parse_download_error)

        global_event.register_event("action-start", self.action_start)
        global_event.register_event("action-update", self.action_update)
        global_event.register_event("action-finish", self.action_finish)
        global_event.register_event("action-failed", self.action_failed)

        global_event.register_event("download-start", self.download_start)
        global_event.register_event("download-update", self.download_update)
        global_event.register_event("download-finish", self.download_finish)
        global_event.register_event("download-stop", self.download_stop)
        global_event.register_event("download-error", self.download_failed)

        global_event.register_event(
            "update-list-start", self.update_list_start)
        global_event.register_event(
            "update-list-finish", self.update_list_finish)
        global_event.register_event(
            "update-list-failed", self.update_list_failed)
        global_event.register_event(
            "update-list-update", self.update_list_update)

        self.packages_status = {}

        self.exit_manager = ExitManager(
            self.mainloop,
            self.is_update_list_running,
            self.is_download_action_running,
            self.is_apt_action_running,
            self.have_exit_request)
        self.exit_manager.start()

        self.set_download_dir('/var/cache/apt/archives')
        self.download_manager = DownloadManager(global_event, verbose=True)
Ejemplo n.º 11
0
class FileManagerTestCase(unittest.TestCase):
    def setUp(self):
        youtube = YoutubeDownloader(url=self.billboard2019_playlist())
        self.dm = DownloadManager(youtube)

    def billboard2019_playlist(self):
        '''Sample playlist'''
        return "https://www.youtube.com/playlist?list=" \
               "PLCzImqCYRfNfJOzblgIkAPkG0HuMBtT5j"

    """
    The open method is mocked without being used; this is to prevent the 
    positions file from being overwritten.
    """

    @mock.patch('download_manager.open')
    @mock.patch.object(youtube.YoutubeDownloader, 'download')
    def test_downloadPlaylist_downloadsToFolder(self, mock_download,
                                                mock_open):
        self.dm.download_all()

        mock_download.assert_called()

    @mock.patch('download_manager.open')
    @mock.patch.object(youtube.YoutubeDownloader, 'download')
    @mock.patch.object(download_manager.pickle, 'dump')
    def test_downloadAll_updatesLastFileDownloaded(self, mock_pickler,
                                                   mock_download, mock_open):
        self.dm.download_all()

        mock_pickler.assert_called()

    @mock.patch('download_manager.open')
    @mock.patch.object(youtube.YoutubeDownloader, 'download')
    def test_downloadAll_startsAtLastPositionDownloaded(
            self, mock_youtube_dl, mock_open):
        # Start at position 5
        self.dm.download_positions = {self.billboard2019_playlist(): 5}
        self.dm.download_all()
        mock_youtube_dl.assert_called_once_with(start_at_position=5)

    @mock.patch('download_manager.open')
    @mock.patch.object(download_manager.pickle, 'load')
    def test_getPositions_returnsPickleFilePositions(self, mock_pickler,
                                                     mock_open):
        expected_return_value = {self.billboard2019_playlist(): "1"}
        mock_pickler.return_value = expected_return_value

        with mock.patch('builtins.open'):
            positions = self.dm.load_positions()

        self.assertEqual(expected_return_value, positions)
Ejemplo n.º 12
0
    def run(self):
        self.__init_i18n()
        [args, headers] = self.__get_options()

        gnome.init(NAME, VERSION)
        gtk.gdk.threads_init()
        gtk.window_set_default_icon_list(*gui.get_icon_list([16, 22, 24, 32]))

        self.download_list = DownloadList()
        self.download_manager = DownloadManager()

        self.dbus_service = dbus_service.DBusService()

        # If the DBus service is already running, add downloads using it
        if not self.dbus_service.register():
            for uri in args:
                self.dbus_service.download_manager.AddDownload(
                    uri, os.getcwd(), headers)
            return 0

        self.dbus_service.register_object(dbus_service.DOWNLOAD_MGR_OBJ_PATH,
                                          self.download_list)

        self.main_window = MainWindow(self.config, self.download_list)
        self.dbus_service.register_object(dbus_service.MAIN_WINDOW_OBJ_PATH,
                                          self.main_window)
        if self.config.show_main_window:
            self.main_window.window.show()

        self.status_icon = TrayIcon(self.main_window)
        if not self.config.show_status_icon:
            self.status_icon.icon.hide_all()

        sys.excepthook = self.main_window.on_unhandled_exception

        self.download_list.load_from_xml()

        for uri in args:
            if self.config.ask_for_location:
                add = AddDownloadDialog(uri, headers)
                add.dialog.run()
            else:
                self.download_list.add_download(uri,
                                                self.config.default_folder,
                                                headers)

        gtk.main()
Ejemplo n.º 13
0
class PackageManager(dbus.service.Object):
    '''
    docs
    '''

    def invoked_log(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            info = "dbus method invoked: %s, arguments: %s" % (
                func.__name__, str(args[1:]))
            logger.debug(info)
            return func(*args, **kw)
        return wrapper

    def __init__(self, system_bus, mainloop):
        # Init dbus service.
        dbus.service.Object.__init__(self, system_bus, DSC_SERVICE_PATH)
        # Init.
        self.mainloop = mainloop
        self.pkg_cache = AptCache()
        self.exit_flag = False
        self.simulate = False
        self.all_upgrade_pkg_names = []
        self.download_dir = "/var/cache/apt/archives"

        self.is_upgrading = False
        self.in_update_list = False

        self.apt_action_pool = AptActionPool(self.pkg_cache)
        self.apt_action_pool.start()

        global_event.register_event(
            'parse-download-error', self.send_parse_download_error)

        global_event.register_event("action-start", self.action_start)
        global_event.register_event("action-update", self.action_update)
        global_event.register_event("action-finish", self.action_finish)
        global_event.register_event("action-failed", self.action_failed)

        global_event.register_event("download-start", self.download_start)
        global_event.register_event("download-update", self.download_update)
        global_event.register_event("download-finish", self.download_finish)
        global_event.register_event("download-stop", self.download_stop)
        global_event.register_event("download-error", self.download_failed)

        global_event.register_event(
            "update-list-start", self.update_list_start)
        global_event.register_event(
            "update-list-finish", self.update_list_finish)
        global_event.register_event(
            "update-list-failed", self.update_list_failed)
        global_event.register_event(
            "update-list-update", self.update_list_update)

        self.packages_status = {}

        self.exit_manager = ExitManager(
            self.mainloop,
            self.is_update_list_running,
            self.is_download_action_running,
            self.is_apt_action_running,
            self.have_exit_request)
        self.exit_manager.start()

        self.set_download_dir('/var/cache/apt/archives')
        self.download_manager = DownloadManager(global_event, verbose=True)

    def download_start(self, pkg_name, action_type):
        utils.set_running_lock(True)
        self.update_signal([("download-start", (pkg_name, action_type))])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_update(self, pkg_name, action_type, data):
        self.update_signal([("download-update", (
            pkg_name, action_type, data))])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_stop(self, pkg_name, action_type):
        self.update_signal([("download-stop", (pkg_name, action_type))])
        self.exit_manager.check()

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False

    def download_failed(self, pkg_name, action_type, e):
        logger.error("%s download failed with %s" % (pkg_name, e))
        utils.set_running_lock(False)
        self.update_signal([("download-failed", (pkg_name, action_type, e))])

        self.exit_manager.check()

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False

    def download_finish(self, action_id, action_type, all_pkg_names):
        utils.set_running_lock(False)
        self.update_signal([("download-finish", (action_id, action_type))])

        if action_type == ACTION_INSTALL:
            self.apt_action_pool.add_install_action(all_pkg_names)
        elif action_type == ACTION_UPGRADE:
            self.start_upgrade(all_pkg_names, action_id)
            self.is_upgrading = False

        self.exit_manager.check()

    def action_start(self, signal_content):
        utils.set_running_lock(True)
        self.update_signal([("action-start", signal_content)])
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True

    def action_update(self, signal_content):
        self.update_signal([("action-update", signal_content)])

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True

    def action_finish(self, signal_content):
        utils.set_running_lock(False)
        pkg_name, action_type, pkg_info_list = signal_content
        if action_type == ACTION_INSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_INSTALLED)
        elif action_type == ACTION_UPGRADE:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_UPGRADED)
        elif action_type == ACTION_UNINSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_UNINSTALLED)

        self.update_signal([("action-finish", signal_content)])
        self.exit_manager.check()

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def action_failed(self, signal_content):
        utils.set_running_lock(False)
        self.update_signal([("action-failed", signal_content)])

        self.exit_manager.check()
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def is_update_list_running(self):
        return self.in_update_list

    def is_download_action_running(self):
        return len(self.download_manager.download_task_info) > 0

    def is_apt_action_running(self):
        return len(self.apt_action_pool.active_mission_list) + len(
            self.apt_action_pool.wait_mission_list) > 0

    def have_exit_request(self):
        return self.exit_flag

    def update_list_start(self):
        self.in_update_list = True
        self.update_signal([("update-list-start", "")])

    def update_list_finish(self):
        self.update_signal([("update-list-merge", "")])
        self.pkg_cache.open(apb.OpProgress())
        db_build.BuildSoftwareDB(self.pkg_cache)

        self.update_signal([("update-list-finish", "")])
        self.in_update_list = False
        self.exit_manager.check()

    def update_list_failed(self):
        self.in_update_list = False
        self.update_signal([("update-list-failed", "")])

        self.exit_manager.check()

    def update_list_update(self, percent, status_message, speed_str):
        self.update_signal([(
            "update-list-update", (percent, status_message, speed_str))])

    def handle_dbus_reply(self, *reply):
        logger.info("%s (reply): %s" % (self.module_dbus_name, str(reply)))

    def handle_dbus_error(self, *error):
        logger.info("%s (error): %s" % (self.module_dbus_name, str(error)))

    def send_parse_download_error(self, pkg_name, action_type):
        self.update_signal([("parse-download-error", (pkg_name, action_type))])

    def get_unique_id(self):
        return str(uuid.uuid4())

    def get_real_pkg_name(self, pkg_name):
        if pkg_name in self.pkg_cache:
            return pkg_name
        elif (pkg_name + ":i386") in self.pkg_cache:
            return pkg_name + ":i386"
        else:
            return None

    def dist_upgrade(self):
        action_type = ACTION_UPGRADE
        action_id = '%s_%s' % (self.get_unique_id(), action_type)
        self.update_signal([(
            "ready-download-start", (action_id, action_type))])

        dist_upgrade_changes = self.pkg_cache.get_dist_upgrade_changes()
        # detect and prevent the system components from being uninstalled
        pkg_in_white_list = []
        download_pkgs = []
        self.all_upgrade_pkg_names = []
        for pkg in dist_upgrade_changes["delete"]:
            name = pkg.name
            if name in SYS_PKG_WHITE_LIST:
                pkg_in_white_list.append(name)
        if len(pkg_in_white_list) > 0:
            self.update_signal([(
                "remove-pkgs-in-white-list",
                (json.dumps(pkg_in_white_list), action_type))])
            logger.error("found remove system packages: %s" %
                json.dumps(pkg_in_white_list))
        else:
            for status in dist_upgrade_changes:
                for pkg in dist_upgrade_changes[status]:
                    self.all_upgrade_pkg_names.append(pkg.name)
                    if status == "delete" or status == "keep":
                        continue
                    if not parse_pkg.pkg_file_has_exist(pkg):
                        download_pkgs.append(pkg)

            self.update_signal([("ready-download-finish", (action_id, action_type))])
            if len(download_pkgs) == 0:
                global_event.emit("download-finish", action_id, action_type,
                        self.all_upgrade_pkg_names)
            else:
                names = []
                download_urls = []
                download_hash_infos = []
                pkg_sizes = []
                for pkg in download_pkgs:
                    names.append(pkg.name)
                    download_urls.append(pkg.candidate.uri)
                    download_hash_infos.append(pkg.candidate.md5)
                    pkg_sizes.append(pkg.candidate.size)
                self.download_manager.add_download(
                    action_id,
                    action_type,
                    download_urls,
                    download_sizes=pkg_sizes,
                    download_md5s=download_hash_infos,
                    all_task_names=names,
                    all_change_pkgs=self.all_upgrade_pkg_names,
                    file_save_dir=self.download_dir
                    )

    def add_download(self, pkg_name, action_type, simulate=False):
        installed = self.get_pkg_installed(pkg_name)
        if installed == 1:
            self.update_signal([("pkg-installed", (pkg_name, action_type))])
            return
        elif installed == -1:
            self.update_signal([("pkg-not-in-cache", (pkg_name, action_type))])
            return
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        status = pkg_infos["status"]
        self.update_signal([("ready-download-finish", (pkg_name, action_type))])
        if status == DOWNLOAD_STATUS_NOTNEED:
            self.download_finish(pkg_name, action_type, [pkg_name,])
        elif status == DOWNLOAD_STATUS_ERROR:
            self.update_signal([("parse-download-error", (pkg_name, action_type))])
        else:
            names = []
            download_urls = []
            download_hash_infos = []
            pkg_sizes = []
            for info in pkg_infos["info"]:
                names.append(info["name"])
                download_urls.append(info["url"])
                download_hash_infos.append(info["hash"])
                pkg_sizes.append(info["size"])

            self.download_manager.add_download(
                                        pkg_name,
                                        action_type,
                                        download_urls,
                                        download_sizes=pkg_sizes,
                                        download_md5s=download_hash_infos,
                                        all_task_names=[pkg_name],
                                        file_save_dir=self.download_dir
                                        )

    def start_upgrade(self, pkg_names, action_id):
        self.apt_action_pool.add_multi_upgrade_mission(pkg_names, action_id)

    def del_source_list_d(self):
        white_list_path = os.path.join(get_parent_dir(__file__), 'white_list.txt')
        if os.path.exists(white_list_path):
            with open(white_list_path) as fp:
                for line in fp:
                    line = line.strip()
                    if os.path.exists(line):
                        os.remove(line)

    def get_desktops(self, pkg_name):
        desktops = []
        try:
            pkg_obj = self.pkg_cache[pkg_name]
            for f in pkg_obj.installed_files:
                if f.endswith(".desktop"):
                    desktops.append(f)
        except:
            pass
        return desktops

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature='', out_signature='as')
    def read_no_notify_config(self, no_notify_config_path):
        if os.path.exists(no_notify_config_path):
            no_notify_config_str = read_file(no_notify_config_path)
            try:
                no_notify_config = eval(no_notify_config_str)

                if type(no_notify_config).__name__ != "list":
                    no_notify_config = []
            except Exception:
                no_notify_config = []

            return no_notify_config
        else:
            return []

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def add_no_notify_pkg(self, info):
        pkg_name, path = info
        no_notify_config = self.read_no_notify_config(path)

        if pkg_name not in no_notify_config:
            pkg_name = str(pkg_name)
            no_notify_config.append(pkg_name)
            write_file(path, str(no_notify_config))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def remove_no_notify_pkg(self, info):
        pkg_name, path = info
        pkg_name = str(pkg_name)
        path = str(path)
        no_notify_config = self.read_no_notify_config(path)

        if pkg_name in no_notify_config:
            write_file(path, str(filter(
                lambda config_pkg_name: config_pkg_name != pkg_name,
                no_notify_config)))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="i", out_signature="")
    def init_download_manager(self, number):
        pass

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="")
    def set_download_dir(self, local_dir):
        apt_pkg.config.set("Dir::Cache::Archives", local_dir)
        self.download_dir = local_dir

    @invoked_log
    @dbus.service.method(
        DSC_SERVICE_NAME, in_signature="s", out_signature="ai")
    def get_download_size(self, pkg_name):
        total_size = 0
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        status = pkg_infos["status"]
        if status == DOWNLOAD_STATUS_NOTNEED:
            total_size = get_pkg_own_size(self.pkg_cache, pkg_name)
            size_flag = PKG_SIZE_OWN
        elif status == DOWNLOAD_STATUS_ERROR:
            total_size = -1
            size_flag = PKG_SIZE_ERROR
        else:
            for info in pkg_infos["info"]:
                total_size += info["size"]
            size_flag = PKG_SIZE_DOWNLOAD
        return [size_flag, total_size]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="ai")
    def clean_download_cache(self):
        '''Clean download cache.'''
        # get action packages.
        remain_pkgs = []
        for pkg_name in self.download_manager.task_name_to_id.keys():
            remain_pkgs.append(pkg_name)

        for pkg_name in self.apt_action_pool.install_action_dict.keys():
            remain_pkgs.append(pkg_name)
        for pkg_name in self.apt_action_pool.upgrade_action_dict.keys():
            remain_pkgs.append(pkg_name)

        # Get depend packages.
        remain_pkgs_paths = []
        for pkg_name in remain_pkgs:
            result = get_pkg_dependence_file_path(self.pkg_cache, pkg_name)
            if not result:
                remain_pkgs_paths += result

        # Init clean size.
        packageNum = 0
        cleanSize = 0

        # Delete cache directory.
        cache_archive_dir = get_cache_archive_dir()
        if os.path.exists(cache_archive_dir):
            for root, folder, files in os.walk(cache_archive_dir):
                for file_name in files:
                    path = os.path.join(root, file_name)
                    if path.endswith(".deb") and (
                            path not in remain_pkgs_paths):
                        packageNum += 1
                        cleanSize += os.path.getsize(path)
                        remove_file(path)

        return [packageNum, cleanSize]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="b", out_signature="")
    def say_hello(self, simulate):
        # Set exit_flag with False to prevent backend exit,
        # this just useful that frontend start again before backend exit.

        self.exit_flag = False
        self.simulate = simulate

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def request_quit(self):
        # Set exit flag.
        self.exit_flag = True

        self.exit_manager.check()
        self.update_signal([("frontend-quit", "")])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def change_source_list(self, repo_urls):
        ubuntu_repo_url, deepin_repo_url = repo_urls
        new_source_list_content = get_source_list_contents(
            ubuntu_repo_url, deepin_repo_url)
        os.system('cp %s %s.save' % (SOURCE_LIST, SOURCE_LIST))
        with open(SOURCE_LIST, 'w') as fp:
            fp.write(new_source_list_content)

    @invoked_log
    @dbus.service.method(
        DSC_SERVICE_NAME, in_signature="", out_signature="(ss)")
    def RequestUpgradeStatus(self):
        if self.in_update_list:
            return ("cache-updating", json.dumps([]))
        elif self.is_upgrading:
            return ("upgrading", json.dumps([]))
        else:
            dist_upgrade_changes = self.pkg_cache.get_dist_upgrade_changes()
            return_infos = []
            for status in dist_upgrade_changes:
                for pkg in dist_upgrade_changes[status]:
                    return_infos.append(dict(
                        status=status,
                        name=pkg.name,
                        version=pkg.candidate.version,
                        size=pkg.candidate.size,
                        downloaded=parse_pkg.pkg_file_has_exist(pkg)
                        ))
            return ("normal", json.dumps(return_infos))

    @invoked_log
    @dbus.service.method(
        DSC_SERVICE_NAME, in_signature="a(si)", out_signature="")
    def RemoveWaitMissions(self, pkg_infos):
        self.apt_action_pool.remove_wait_missions(pkg_infos)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="s")
    def request_uninstall_pkgs(self):
        return json.dumps(self.pkg_cache.get_uninstall_pkgs())

    @invoked_log
    @dbus.service.method(
        DSC_SERVICE_NAME, in_signature="as", out_signature="as")
    def request_pkgs_install_version(self, pkg_names):
        pkg_versions = []
        for pkg_name in pkg_names:
            try:
                version = self.pkg_cache[pkg_name].versions[0].version
                pkg_versions.append(version)
            except:
                try:
                    version = self.pkg_cache[pkg_name+":i386"].versions[0].version
                    pkg_versions.append(version)
                except:
                    self.update_signal([("pkg-not-in-cache", pkg_name)])
        return pkg_versions

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="as")
    def is_pkg_in_cache(self, pkg_name):
        result = []
        try:
            self.pkg_cache[pkg_name]
            result.append(pkg_name)
        except:
            try:
                pkg_name += ":i386"
                self.pkg_cache[pkg_name]
                result.append(pkg_name)
            except:
                pass
        return result

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="as")
    def request_pkgs_uninstall_version(self, pkg_names):
        return self.pkg_cache.get_pkgs_uninstall_version(pkg_names)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def install_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                ThreadMethod(self.add_download, (real_pkg_name, ACTION_INSTALL, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg(self, pkg_name, purge):
        real_pkg_name = self.get_real_pkg_name(pkg_name)
        if real_pkg_name:
            self.apt_action_pool.add_uninstall_action(real_pkg_name, self.simulate, purge)
        else:
            self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg_from_desktop(self, desktop_path, purge):
        ThreadMethod(self.uninstall_pkg_from_desktop_thread, (desktop_path, purge)).start()

    def uninstall_pkg_from_desktop_thread(self, path, purge):
        pkg_name = self.get_pkg_name_from_path(path)
        if pkg_name:
            self.uninstall_pkg(pkg_name, purge)
        else:
            global_event.emit("action-failed", (path, ACTION_UNINSTALL, [], "no package found for path: %s" % path))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def get_pkg_name_from_path(self, path):
        pkg_name = utils.file_path_to_package_name(path)
        if pkg_name == "" and path.endswith(".desktop"):
            desktop_name = os.path.split(path)[1]
            path = utils.desktop_name_to_desktop_path(desktop_name)
            pkg_name = utils.file_path_to_package_name(path)
        return pkg_name

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def upgrade_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                self.update_signal([("ready-download-start", (real_pkg_name, ACTION_UPGRADE))])
                ThreadMethod(self.add_download, (real_pkg_name, ACTION_UPGRADE, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def DistUpgrade(self):
        ThreadMethod(self.dist_upgrade).start()

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def stop_download_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def cancel_upgrade_download(self):
        if getattr(self, 'upgrade_id'):
            self.download_manager.stop_wait_download(self.upgrade_id)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def remove_wait_downloads(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def request_pkg_short_desc(self, pkg_name):
        return self.pkg_cache.get_pkg_short_desc(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="as")
    def request_status(self):
        download_status = {
            ACTION_INSTALL : [],
            ACTION_UPGRADE : []}
        for info_dict in self.download_manager.download_task_info.values():
            pkg_name = info_dict["task_name"]
            if info_dict["action_type"] == ACTION_INSTALL:
                download_status[ACTION_INSTALL].append((pkg_name, info_dict["status"]))
            elif info_dict["action_type"] == ACTION_UPGRADE:
                download_status[ACTION_UPGRADE].append((pkg_name, info_dict["status"]))

        action_status = {
            ACTION_INSTALL : [],
            ACTION_UPGRADE : [],
            ACTION_UNINSTALL : []}
        for info_dict in self.apt_action_pool.install_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_INSTALL].append((pkg_name, info_dict["status"]))

        for info_dict in self.apt_action_pool.upgrade_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_UPGRADE].append((pkg_name, info_dict["status"]))

        for info_dict in self.apt_action_pool.uninstall_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_UNINSTALL].append((pkg_name, info_dict["status"]))

        return [str(download_status), str(action_status)]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def start_update_list(self):
        self.del_source_list_d()
        self.pkg_cache.open(apb.OpProgress())
        self.apt_action_pool.add_update_list_mission()

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="ab")
    def request_pkgs_install_status(self, pkg_names):
        _status = []
        for pkg in pkg_names:
            _status.append(self.pkg_cache.is_pkg_installed(pkg))
        return _status

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="(si)", out_signature="")
    def set_pkg_status(self, pkg_status):
        pkg_name, status = pkg_status
        self.pkg_cache.set_pkg_status(pkg_name, pkg_status)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="i")
    def get_pkg_installed(self, pkg_name):
        if self.is_pkg_in_cache(pkg_name):
            if self.pkg_cache.is_pkg_installed(pkg_name):
                return 1
            else:
                return 0
        else:
            return -1

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="ss", out_signature="s")
    def get_pkg_start_status(self, pkg_name, start_pkg_names):
        is_current_installed = self.get_pkg_installed(pkg_name)
        if is_current_installed == -1:
            return "unknown"
        elif is_current_installed == 0:
            return "uninstalled"
        else:
            desktops = self.get_desktops(pkg_name)
            names = start_pkg_names.split(",")
            for name in names:
                if self.get_pkg_installed(name) == 1:
                    desktops += self.get_desktops(name)
            return json.dumps(desktops)

    @dbus.service.signal(DSC_SERVICE_NAME, signature='a(sv)')
    # Use below command for test:
    # dbus-monitor --system "type='signal', interface='com.linuxdeepin.softwarecenter'"
    def update_signal(self, message):
        # The signal is emitted when this method exits
        # You can have code here if you wish
        info = str(message)
        logger.debug("dbus signal emit: %s" % info)
Ejemplo n.º 14
0
class PackageManager(dbus.service.Object):
    '''
    docs
    '''

    def __init__(self, system_bus, mainloop):
        log("init dbus")
        
        # Init dbus service.
        dbus.service.Object.__init__(self, system_bus, DSC_SERVICE_PATH)
        # Init.
        self.mainloop = mainloop
        self.pkg_cache = AptCache()
        self.exit_flag = False
        self.simulate = False
        self.all_upgrade_pkg_names = []

        self.is_upgrading = False
        self.in_update_list = False
        
        self.apt_action_pool = AptActionPool(self.pkg_cache)
        self.apt_action_pool.start()

        global_event.register_event('parse-download-error', self.send_parse_download_error)

        global_event.register_event("action-start", self.action_start)
        global_event.register_event("action-update", self.action_update)
        global_event.register_event("action-finish", self.action_finish)
        global_event.register_event("action-failed", self.action_failed)
        
        global_event.register_event("download-start", self.download_start)
        global_event.register_event("download-update", self.download_update)
        global_event.register_event("download-finish", self.download_finish)
        global_event.register_event("download-stop", self.download_stop)
        global_event.register_event("download-error", self.download_failed)
        
        global_event.register_event("update-list-start", self.update_list_start)
        global_event.register_event("update-list-finish", self.update_list_finish)
        global_event.register_event("update-list-failed", self.update_list_failed)
        global_event.register_event("update-list-update", self.update_list_update)

        self.packages_status = {}
        
        self.exit_manager = ExitManager(
            self.mainloop,
            self.is_update_list_running,
            self.is_download_action_running,
            self.is_apt_action_running,
            self.have_exit_request)
        self.exit_manager.start()
        
        log("init finish")
        self.set_download_dir('/var/cache/apt/archives')
        self.init_download_manager(5)

    def download_start(self, pkg_name, action_type):
        utils.set_running_lock(True)
        self.update_signal([("download-start", (pkg_name, action_type))])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_update(self, pkg_name, action_type, data):
        self.update_signal([("download-update", (pkg_name, action_type, data))])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_stop(self, pkg_name, action_type):
        self.update_signal([("download-stop", (pkg_name, action_type))])
        self.exit_manager.check()    

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False
        
    def download_failed(self, pkg_name, action_type, e):
        logger.error("%s download failed with %s" % (pkg_name, e))
        utils.set_running_lock(False)
        self.update_signal([("download-failed", (pkg_name, action_type, e))])
        
        self.exit_manager.check()    

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False
           
    def download_finish(self, action_id, action_type, all_pkg_names):
        utils.set_running_lock(False)
        self.update_signal([("download-finish", (action_id, action_type))])
        
        if action_type == ACTION_INSTALL:
            self.apt_action_pool.add_install_action(all_pkg_names)
        elif action_type == ACTION_UPGRADE:
            self.start_upgrade(all_pkg_names, action_id)
            self.is_upgrading = False
            
        self.exit_manager.check()

    def action_start(self, signal_content):
        utils.set_running_lock(True)
        self.update_signal([("action-start", signal_content)])
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True
        
    def action_update(self, signal_content):
        self.update_signal([("action-update", signal_content)])

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True
        
    def action_finish(self, signal_content):
        utils.set_running_lock(False)
        pkg_name, action_type, pkg_info_list = signal_content
        if action_type == ACTION_INSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(pkg_name, self.pkg_cache.PKG_STATUS_INSTALLED)
        elif action_type == ACTION_UPGRADE:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(pkg_name, self.pkg_cache.PKG_STATUS_UPGRADED)
        elif action_type == ACTION_UNINSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(pkg_name, self.pkg_cache.PKG_STATUS_UNINSTALLED)

        self.update_signal([("action-finish", signal_content)])
        self.exit_manager.check()

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def action_failed(self, signal_content):
        utils.set_running_lock(False)
        self.update_signal([("action-failed", signal_content)])
        
        self.exit_manager.check()
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def is_update_list_running(self):
        return self.in_update_list
    
    def is_download_action_running(self):
        return len(self.download_manager.fetch_files_dict) > 0
    
    def is_apt_action_running(self):
        return len(self.apt_action_pool.active_mission_list) + len(self.apt_action_pool.wait_mission_list) > 0
    
    def have_exit_request(self):
        return self.exit_flag
        
    def update_list_start(self):
        self.in_update_list = True
        self.update_signal([("update-list-start", "")])

    def update_list_finish(self):
        self.update_signal([("update-list-merge", "")])
        self.pkg_cache.open(apb.OpProgress())
        db_build.BuildSoftwareDB(self.pkg_cache)

        self.update_signal([("update-list-finish", "")])
        self.in_update_list = False
        self.exit_manager.check()

    def update_list_failed(self):
        self.in_update_list = False
        self.update_signal([("update-list-failed", "")])
        
        self.exit_manager.check()
        
    def update_list_update(self, percent, status_message, speed_str):
        self.update_signal([("update-list-update", (percent, status_message, speed_str))])

    def handle_dbus_reply(self, *reply):
        log("%s (reply): %s" % (self.module_dbus_name, str(reply)))        
        
    def handle_dbus_error(self, *error):
        log("%s (error): %s" % (self.module_dbus_name, str(error)))

    def send_parse_download_error(self, pkg_name, action_type):
        self.update_signal([("parse-download-error", (pkg_name, action_type))])

    def get_unique_id(self):
        return str(uuid.uuid4())

    def get_real_pkg_name(self, pkg_name):
        if pkg_name in self.pkg_cache:
            return pkg_name
        elif (pkg_name + ":i386") in self.pkg_cache:
            return pkg_name + ":i386"
        else:
            return None

    def add_upgrade_download_with_new_policy(self, pkg_names, action_type):
        action_id = '%s_%s' % (self.get_unique_id(), action_type)
        self.update_signal([("ready-download-start", (action_id, action_type))])

        real_pkg_dict, not_in_cache = parse_pkg.get_real_pkg_dict(self.pkg_cache, pkg_names)
        if not_in_cache:
            self.update_signal([("pkgs-not-in-cache", (json.dumps(not_in_cache), action_type))])
        else:
            (all_change_pkgs, mark_failed_pkg_dict, marked_delete_sys_pkgs
                    ) = parse_pkg.get_changes_pkgs(self.pkg_cache, real_pkg_dict)

            if mark_failed_pkg_dict:
                self.update_signal([("pkgs-mark-failed", (json.dumps(mark_failed_pkg_dict.keys()), action_type))])

            elif marked_delete_sys_pkgs:
                self.update_signal([("marked-delete-system-pkgs", (json.dumps(marked_delete_sys_pkgs), action_type))])
            else:
                self.update_signal([("ready-download-finish", (action_id, action_type))])
                download_pkg_infos = parse_pkg.check_pkg_download_info(all_change_pkgs)
                if download_pkg_infos[0] == DOWNLOAD_STATUS_ERROR:
                    self.update_signal([("pkgs-parse-download-error", (str(download_pkg_infos[1]), action_type))])
                elif download_pkg_infos[0] == DOWNLOAD_STATUS_NOTNEED:
                    self.start_upgrade(pkg_names, action_id)
                else:
                    (names, download_urls, download_hash_infos, pkg_sizes) = download_pkg_infos
                    self.all_upgrade_pkg_names = all_change_pkgs
                    self.download_manager.add_download(
                                            action_id,
                                            action_type,
                                            download_urls,
                                            all_pkg_names=pkg_names,
                                            all_change_pkgs=all_change_pkgs,
                                            file_save_dir=self.download_dir,
                                            )
        
    def add_download(self, pkg_name, action_type, simulate=False):
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        self.update_signal([("ready-download-finish", (pkg_name, action_type))])
        if pkg_infos[0] == DOWNLOAD_STATUS_NOTNEED:
            self.download_finish(pkg_name, action_type, [pkg_name,])
            print "Don't need download"
        elif pkg_infos[0] == DOWNLOAD_STATUS_ERROR:
            self.update_signal([("parse-download-error", (pkg_name, action_type))])
            print "Download error"
        else:
            (names, download_urls, download_hash_infos, pkg_sizes) = pkg_infos
            
            self.download_manager.add_download(
                                        pkg_name, 
                                        action_type, 
                                        download_urls, 
                                        all_pkg_names=[pkg_name,],
                                        file_save_dir=self.download_dir)

    def start_upgrade(self, pkg_names, action_id):
        self.apt_action_pool.add_multi_upgrade_mission(pkg_names, action_id)
        
    def del_source_list_d(self):
        white_list_path = os.path.join(get_parent_dir(__file__), 'white_list.txt')
        if os.path.exists(white_list_path):
            with open(white_list_path) as fp:
                for line in fp:
                    line = line.strip()
                    if os.path.exists(line):
                        os.remove(line)

    def get_desktops(self, pkg_name):
        desktops = []
        try:
            pkg_obj = self.pkg_cache[pkg_name]
            for f in pkg_obj.installed_files:
                if f.endswith(".desktop"):
                    desktops.append(f)
        except:
            pass
        return desktops

    @dbus.service.method(DSC_SERVICE_NAME, in_signature='', out_signature='as')
    def read_no_notify_config(self, no_notify_config_path):
        if os.path.exists(no_notify_config_path):
           no_notify_config_str = read_file(no_notify_config_path)
           try:
               no_notify_config = eval(no_notify_config_str)
               
               if type(no_notify_config).__name__ != "list":
                   no_notify_config = []
           except Exception:
               no_notify_config = []
               
           return no_notify_config
        else:
            return []

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def add_no_notify_pkg(self, info):
        pkg_name, path = info
        no_notify_config = self.read_no_notify_config(path)
        
        if pkg_name not in no_notify_config:
            pkg_name = str(pkg_name)
            no_notify_config.append(pkg_name)
            write_file(path, str(no_notify_config))

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def remove_no_notify_pkg(self, info):
        pkg_name, path = info
        pkg_name = str(pkg_name)
        path = str(path)
        no_notify_config = self.read_no_notify_config(path)
        
        if pkg_name in no_notify_config:
            write_file(path, str(filter(lambda config_pkg_name: config_pkg_name != pkg_name, no_notify_config)))

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="i", out_signature="")
    def init_download_manager(self, number):
        self.download_manager = DownloadManager(global_event, number, verbose=True)

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="")
    def set_download_dir(self, local_dir):
        apt_pkg.config.set("Dir::Cache::Archives", local_dir)
        self.download_dir = local_dir
    
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="ai")
    def get_download_size(self, pkg_name):
        total_size = 0
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        if pkg_infos[0] == DOWNLOAD_STATUS_NOTNEED:
            total_size = get_pkg_own_size(self.pkg_cache, pkg_name)
            size_flag = PKG_SIZE_OWN
        elif pkg_infos[0] == DOWNLOAD_STATUS_ERROR:
            total_size = -1
            size_flag = PKG_SIZE_ERROR
        else:
            (names, download_urls, download_hash_infos, pkg_sizes) = pkg_infos
            for size in pkg_sizes:
                total_size += size
            size_flag = PKG_SIZE_DOWNLOAD
        return [size_flag, total_size]

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="as")
    def get_upgrade_download_size(self, pkg_names):
        real_pkg_dict, not_in_cache = parse_pkg.get_real_pkg_dict(self.pkg_cache, pkg_names)
        if not_in_cache:
            return ["-1", json.dumps([])]
        else:
            (all_change_pkgs, mark_failed_pkg_dict, marked_delete_sys_pkgs
                    ) = parse_pkg.get_changes_pkgs(self.pkg_cache, real_pkg_dict)

            download_pkg_infos = parse_pkg.check_pkg_download_info(all_change_pkgs)
            change_pkg_names = [pkg.name for pkg in all_change_pkgs]
            if download_pkg_infos[0] == DOWNLOAD_STATUS_ERROR:
                return ["-1", json.dumps([])]
            elif download_pkg_infos[0] == DOWNLOAD_STATUS_NOTNEED:
                return ["0", json.dumps(change_pkg_names)]
            else:
                (names, download_urls, download_hash_infos, pkg_sizes) = download_pkg_infos
                total_size = 0
                for size in pkg_sizes:
                    total_size += size
                return [str(total_size), json.dumps(change_pkg_names)]

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="ai")
    def clean_download_cache(self):
        '''Clean download cache.'''
        # get action packages.
        remain_pkgs = []
        for (pkg_name, info_dict) in self.download_manager.fetch_files_dict.items():
            remain_pkgs.append(pkg_name)

        for (pkg_name, info_dict) in self.apt_action_pool.install_action_dict.items():
            remain_pkgs.append(pkg_name)
        for (pkg_name, info_dict) in self.apt_action_pool.upgrade_action_dict.items():
            remain_pkgs.append(pkg_name)
        
        # Get depend packages.
        remain_pkgs_paths = []
        for pkg_name in remain_pkgs:
            result = get_pkg_dependence_file_path(self.pkg_cache, pkg_name)
            if not result:
                remain_pkgs_paths += result

        # Init clean size.
        packageNum = 0
        cleanSize = 0
                
        # Delete cache directory.
        cache_archive_dir = get_cache_archive_dir()
        if os.path.exists(cache_archive_dir):
            for root, folder, files in os.walk(cache_archive_dir):
                for file_name in files:
                    path = os.path.join(root, file_name)
                    if path.endswith(".deb") and (path not in remain_pkgs_paths):
                        packageNum += 1
                        cleanSize += os.path.getsize(path)
                        remove_file(path)

        return [packageNum, cleanSize]
        
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="b", out_signature="")    
    def say_hello(self, simulate):
        # Set exit_flag with False to prevent backend exit, 
        # this just useful that frontend start again before backend exit.
        log("Say hello from frontend.")
        
        self.exit_flag = False
        self.simulate = simulate
        
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")    
    def request_quit(self):
        # Set exit flag.
        self.exit_flag = True
        
        self.exit_manager.check()
        self.update_signal([("frontend-quit", "")])

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def change_source_list(self, repo_urls):
        ubuntu_repo_url, deepin_repo_url = repo_urls
        new_source_list_content = get_source_list_contents(ubuntu_repo_url, deepin_repo_url)
        os.system('cp %s %s.save' % (SOURCE_LIST, SOURCE_LIST))
        with open(SOURCE_LIST, 'w') as fp:
            fp.write(new_source_list_content)
        
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="(sas)")
    # def request_upgrade_pkgs(self):  // old api name
    def RequestUpgradeStatus(self):
        """
        Get upgrade status
        
        @return: Return (status_code, [json.dumps(pkg_name, pkg_version)]), 
        status code is in below constants:
            - normal: no dpkg action, just return packages that need to upgrade;
            - upgrading: in upgrading packages;
            - cache-updating: in updating apt cache;
        """
        if self.in_update_list:
            return ("cache-updating", [])
        elif self.is_upgrading:
            return ("upgrading", [])
        else:
            cache_upgrade_pkgs = self.pkg_cache.get_upgrade_pkgs()
            return ("normal", cache_upgrade_pkgs)
    
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="s")
    def request_uninstall_pkgs(self):
        return json.dumps(self.pkg_cache.get_uninstall_pkgs())

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="as")    
    def request_pkgs_install_version(self, pkg_names):
        pkg_versions = []
        for pkg_name in pkg_names:
            try:
                version = self.pkg_cache[pkg_name].versions[0].version
                pkg_versions.append(version)
            except:
                try:
                    version = self.pkg_cache[pkg_name+":i386"].versions[0].version
                    pkg_versions.append(version)
                except:
                    self.update_signal([("pkg-not-in-cache", pkg_name)])
        return pkg_versions

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="as")    
    def is_pkg_in_cache(self, pkg_name):
        result = []
        try:
            self.pkg_cache[pkg_name]
            result.append(pkg_name)
        except:
            try:
                pkg_name += ":i386"
                self.pkg_cache[pkg_name]
                result.append(pkg_name)
            except:
                pass
        return result

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="as")    
    def request_pkgs_uninstall_version(self, pkg_names):
        return self.pkg_cache.get_pkgs_uninstall_version(pkg_names)

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def install_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                ThreadMethod(self.add_download, (real_pkg_name, ACTION_INSTALL, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])
    
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")    
    def uninstall_pkg(self, pkg_name, purge):
        real_pkg_name = self.get_real_pkg_name(pkg_name)
        if real_pkg_name:
            self.apt_action_pool.add_uninstall_action(real_pkg_name, self.simulate, purge)
        else:
            self.update_signal([("pkg-not-in-cache", pkg_name)])

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg_from_desktop(self, desktop_path, purge):
        ThreadMethod(self.uninstall_pkg_from_desktop_thread, (desktop_path, purge)).start()

    def uninstall_pkg_from_desktop_thread(self, path, purge):
        pkg_name = self.get_pkg_name_from_path(path)
        if pkg_name:
            self.uninstall_pkg(pkg_name, purge)
        else:
            global_event.emit("action-failed", (path, ACTION_UNINSTALL, [], "no package found for path: %s" % path))

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def get_pkg_name_from_path(self, path):
        result = subprocess.check_output(['dpkg', '-S', path]).strip().split(":")
        if len(result) == 2 and result[1].strip() == path.strip():
            return result[0].strip()
        else:
            return ""

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def upgrade_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                self.update_signal([("ready-download-start", (real_pkg_name, ACTION_UPGRADE))])
                ThreadMethod(self.add_download, (real_pkg_name, ACTION_UPGRADE, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def upgrade_pkgs_with_new_policy(self, pkg_names):
        ThreadMethod(self.add_upgrade_download_with_new_policy, (pkg_names, ACTION_UPGRADE)).start()

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def stop_download_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def cancel_upgrade_download(self):
        if getattr(self, 'upgrade_id'):
            self.download_manager.stop_wait_download(self.upgrade_id)
            
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def remove_wait_missions(self, pkg_infos):
        self.apt_action_pool.remove_wait_missions(pkg_infos)

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")    
    def remove_wait_downloads(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)
            
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def request_pkg_short_desc(self, pkg_name):
        return self.pkg_cache.get_pkg_short_desc(pkg_name)
    
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="as")
    def request_status(self):
        download_status = {
            ACTION_INSTALL : [],
            ACTION_UPGRADE : []}
        for (pkg_name, info_dict) in self.download_manager.fetch_files_dict.items():
            if info_dict["action_type"] == ACTION_INSTALL:
                download_status[ACTION_INSTALL].append((pkg_name, info_dict["status"]))
            elif info_dict["action_type"] == ACTION_UPGRADE:
                download_status[ACTION_UPGRADE].append((pkg_name, info_dict["status"]))
                
        action_status = {
            ACTION_INSTALL : [],
            ACTION_UPGRADE : [],
            ACTION_UNINSTALL : []}        
        for (pkg_name, info_dict) in self.apt_action_pool.install_action_dict.items():
            action_status[ACTION_INSTALL].append((pkg_name, info_dict["status"]))
        for (pkg_name, info_dict) in self.apt_action_pool.upgrade_action_dict.items():
            action_status[ACTION_UPGRADE].append((pkg_name, info_dict["status"]))
        for (pkg_name, info_dict) in self.apt_action_pool.uninstall_action_dict.items():
            action_status[ACTION_UNINSTALL].append((pkg_name, info_dict["status"]))
        
        return [str(download_status), str(action_status)]
    
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def start_update_list(self):
        log("start update list...")
        self.del_source_list_d()
        self.pkg_cache.open(apb.OpProgress())
        self.apt_action_pool.add_update_list_mission()
        log("start update list done")
        
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="ab")
    def request_pkgs_install_status(self, pkg_names):
        _status = []
        for pkg in pkg_names:
            _status.append(self.pkg_cache.is_pkg_installed(pkg))
        return _status
            
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="(si)", out_signature="")
    def set_pkg_status(self, pkg_status):
        pkg_name, status = pkg_status
        self.pkg_cache.set_pkg_status(pkg_name, pkg_status)

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="i")
    def get_pkg_installed(self, pkg_name):
        if self.is_pkg_in_cache(pkg_name):
            if self.pkg_cache.is_pkg_installed(pkg_name):
                return 1
            else:
                return 0
        else:
            return -1

    @dbus.service.method(DSC_SERVICE_NAME, in_signature="ss", out_signature="s")
    def get_pkg_start_status(self, pkg_name, start_pkg_names):
        is_current_installed = self.get_pkg_installed(pkg_name)
        if is_current_installed == -1:
            return "unknown"
        elif is_current_installed == 0:
            return "uninstalled"
        else:
            desktops = self.get_desktops(pkg_name)
            names = start_pkg_names.split(",")
            for name in names:
                if self.get_pkg_installed(name) == 1:
                    desktops += self.get_desktops(name)
            return json.dumps(desktops)

    @dbus.service.signal(DSC_SERVICE_NAME, signature='a(sv)')
    # Use below command for test:
    # dbus-monitor --system "type='signal', interface='com.linuxdeepin.softwarecenter'" 
    def update_signal(self, message):
        # The signal is emitted when this method exits
        # You can have code here if you wish
        pass
Ejemplo n.º 15
0
 def init_download_manager(self, number):
     self.download_manager = DownloadManager(global_event, number, verbose=True)
Ejemplo n.º 16
0
class TorrentMonitor(borg.Borg):

    host_index = 0
    retry_count = 0

    def __init__(self):
        super(TorrentMonitor, self).__init__()

        log.msg('Starting up monitor')

        if not hasattr(self, 'initialized'):
            self.initialized = False

        self.app = config.Application()

        self.initialize()

    def initialize(self):
        self._initialize_hosts()

        if not self.initialized:

            self.torrent_loop = task.LoopingCall(
                self.search_for_torrents
            )

            self.torrent_progress_loop = task.LoopingCall(
                self.download_manager.monitor_torrent_progress
            )

            self.retry_download_loop = task.LoopingCall(
                self.retry_download
            )

            deferreds = [
                self.torrent_loop.start(
                    self.app.monitor_torrent
                ),
                self.torrent_progress_loop.start(
                    self.app.torrent_progress
                ),
                self.retry_download_loop.start(
                    self.app.retry_download
                )
            ]

            for d in deferreds:
                d.addErrback(self._reconnect_stores)

            self.initialized = True

    def _initialize_hosts(self):

        log.msg('Retry count: {}'.format(self.retry_count))
        max_retries = self.app.max_retries

        if self.retry_count == max_retries or not self.initialized:
            self.retry_count = 0

            hosts = self.app.pirate_bay_hosts

            host_config = hosts[self.host_index]
            web_host = "http://{}".format(host_config['host'])
            torrent_host = host_config['torrent_host']
            use_tor = host_config['use_tor_network']

            self.pirate_bay_client = ThePirateBay(web_host, use_tor)

            self.download_manager = DownloadManager()
            self.download_manager.initialize(torrent_host, use_tor, self.app)

            log.msg('Switching to different host')

            self.host_index += 1
            if self.host_index == len(hosts):
                self.host_index = 0
        else:
            self.retry_count += 1

    def retry_download(self):
        log.msg('Going to reset the statuses of NOT_FOUND items in queue')

        TorrentQueue.update_status(
            new_status='PENDING', status='NOT_FOUND', async=False
        )

    def search_for_torrents(self):
        log.msg('Searching for my torrents')

        queue = TorrentQueue.load_pending_queue(async=False)

        for torrent in queue:
            req = self.pirate_bay_client.search(
                query=torrent.query,
                optimize_query=True,
                order=ORDERS.SEEDERS.DES,
                category=CATEGORIES.VIDEO.ALL
            )

            req.load_torrents(
                callback=self.on_torrents_found,
                errback=self.error_finding_torrents,
                db_id=torrent.id
            )

    def on_torrents_found(self, torrents, db_id):
        """ Callback for reponse from pirate bay sites
            :param: list of torrents
            :param: the id refrencing the queue database table
        """

        if len(torrents) > 0:
            log.msg('Torrent found for queue with id of {}'.format(db_id))

            self.download_manager.download(torrents[0], db_id)
        else:
            log.msg('No Torrents found for queue with id of {}'.format(db_id))

            TorrentQueue.update_status(
                new_status='NOT_FOUND', id=db_id, async=False
            )
            self.error_finding_torrents()

    def error_finding_torrents(self):
        log.msg('Going to try again')
        self._initialize_hosts()

    def _reconnect_stores(self, error):
        """Reconnect all the stores if there is some problem
        """

        if error is not None:
            log.err('Whooops, detected an error:')
            log.err(error)

        zstorm = getUtility(IZStorm)
        for name, store in zstorm.iterstores():
            log.msg('Reconnecting store {}...'.format(name))
            store.rollback()

        if not self.torrent_loop.running:
            time.sleep(0.5)
            d = self.torrent_loop.start(self.app.monitor_torrent)
            d.addErrback(self._reconnect_stores)

        if not self.torrent_progress_loop.running:
            time.sleep(0.5)
            d = self.torrent_progress_loop.start(self.app.torrent_progress)
            d.addErrback(self._reconnect_stores)

        if not self.retry_download_loop.running:
            time.sleep(0.5)
            d = self.retry_download_loop.start(self.app.retry_download)
            d.addErrback(self._reconnect_stores)

        self.error_finding_torrents()
Ejemplo n.º 17
0
 def setUp(self):
     youtube = YoutubeDownloader(url=self.billboard2019_playlist())
     self.dm = DownloadManager(youtube)
Ejemplo n.º 18
0
 def __init__(self):
     """"""
     DownloadManager.__init__(self)
     AddDownloadsManager.__init__(self)
     self.session_parser = SessionParser()
Ejemplo n.º 19
0
class PackageManager(dbus.service.Object):
    '''
    docs
    '''
    def invoked_log(func):
        @functools.wraps(func)
        def wrapper(*args, **kw):
            info = "dbus method invoked: %s, arguments: %s" % (func.__name__,
                                                               str(args[1:]))
            logger.debug(info)
            return func(*args, **kw)

        return wrapper

    def __init__(self, system_bus, mainloop):
        # Init dbus service.
        dbus.service.Object.__init__(self, system_bus, DSC_SERVICE_PATH)
        # Init.
        self.mainloop = mainloop
        self.pkg_cache = AptCache()
        self.exit_flag = False
        self.simulate = False
        self.all_upgrade_pkg_names = []
        self.download_dir = "/var/cache/apt/archives"

        self.is_upgrading = False
        self.in_update_list = False

        self.apt_action_pool = AptActionPool(self.pkg_cache)
        self.apt_action_pool.start()

        global_event.register_event('parse-download-error',
                                    self.send_parse_download_error)

        global_event.register_event("action-start", self.action_start)
        global_event.register_event("action-update", self.action_update)
        global_event.register_event("action-finish", self.action_finish)
        global_event.register_event("action-failed", self.action_failed)

        global_event.register_event("download-start", self.download_start)
        global_event.register_event("download-update", self.download_update)
        global_event.register_event("download-finish", self.download_finish)
        global_event.register_event("download-stop", self.download_stop)
        global_event.register_event("download-error", self.download_failed)

        global_event.register_event("update-list-start",
                                    self.update_list_start)
        global_event.register_event("update-list-finish",
                                    self.update_list_finish)
        global_event.register_event("update-list-failed",
                                    self.update_list_failed)
        global_event.register_event("update-list-update",
                                    self.update_list_update)

        self.packages_status = {}

        self.exit_manager = ExitManager(self.mainloop,
                                        self.is_update_list_running,
                                        self.is_download_action_running,
                                        self.is_apt_action_running,
                                        self.have_exit_request)
        self.exit_manager.start()

        self.set_download_dir('/var/cache/apt/archives')
        self.download_manager = DownloadManager(global_event, verbose=True)

    def download_start(self, pkg_name, action_type):
        utils.set_running_lock(True)
        self.update_signal([("download-start", (pkg_name, action_type))])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_update(self, pkg_name, action_type, data):
        self.update_signal([("download-update", (pkg_name, action_type, data))
                            ])
        if action_type == ACTION_UPGRADE:
            self.is_upgrading = True

    def download_stop(self, pkg_name, action_type):
        self.update_signal([("download-stop", (pkg_name, action_type))])
        self.exit_manager.check()

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False

    def download_failed(self, pkg_name, action_type, e):
        logger.error("%s download failed with %s" % (pkg_name, e))
        utils.set_running_lock(False)
        self.update_signal([("download-failed", (pkg_name, action_type, e))])

        self.exit_manager.check()

        if action_type == ACTION_UPGRADE:
            self.is_upgrading = False

    def download_finish(self, action_id, action_type, all_pkg_names):
        utils.set_running_lock(False)
        self.update_signal([("download-finish", (action_id, action_type))])

        if action_type == ACTION_INSTALL:
            self.apt_action_pool.add_install_action(all_pkg_names)
        elif action_type == ACTION_UPGRADE:
            self.start_upgrade(all_pkg_names, action_id)
            self.is_upgrading = False

        self.exit_manager.check()

    def action_start(self, signal_content):
        utils.set_running_lock(True)
        self.update_signal([("action-start", signal_content)])
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True

    def action_update(self, signal_content):
        self.update_signal([("action-update", signal_content)])

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = True

    def action_finish(self, signal_content):
        utils.set_running_lock(False)
        pkg_name, action_type, pkg_info_list = signal_content
        if action_type == ACTION_INSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_INSTALLED)
        elif action_type == ACTION_UPGRADE:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_UPGRADED)
        elif action_type == ACTION_UNINSTALL:
            for pkg_info in pkg_info_list:
                self.pkg_cache.set_pkg_status(
                    pkg_name, self.pkg_cache.PKG_STATUS_UNINSTALLED)

        self.update_signal([("action-finish", signal_content)])
        self.exit_manager.check()

        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def action_failed(self, signal_content):
        utils.set_running_lock(False)
        self.update_signal([("action-failed", signal_content)])

        self.exit_manager.check()
        if signal_content[1] == ACTION_UPGRADE:
            self.is_upgrading = False

    def is_update_list_running(self):
        return self.in_update_list

    def is_download_action_running(self):
        return len(self.download_manager.download_task_info) > 0

    def is_apt_action_running(self):
        return len(self.apt_action_pool.active_mission_list) + len(
            self.apt_action_pool.wait_mission_list) > 0

    def have_exit_request(self):
        return self.exit_flag

    def update_list_start(self):
        self.in_update_list = True
        self.update_signal([("update-list-start", "")])

    def update_list_finish(self):
        self.update_signal([("update-list-merge", "")])
        self.pkg_cache.open(apb.OpProgress())
        db_build.BuildSoftwareDB(self.pkg_cache)

        self.update_signal([("update-list-finish", "")])
        self.in_update_list = False
        self.exit_manager.check()

    def update_list_failed(self):
        self.in_update_list = False
        self.update_signal([("update-list-failed", "")])

        self.exit_manager.check()

    def update_list_update(self, percent, status_message, speed_str):
        self.update_signal([("update-list-update", (percent, status_message,
                                                    speed_str))])

    def handle_dbus_reply(self, *reply):
        logger.info("%s (reply): %s" % (self.module_dbus_name, str(reply)))

    def handle_dbus_error(self, *error):
        logger.info("%s (error): %s" % (self.module_dbus_name, str(error)))

    def send_parse_download_error(self, pkg_name, action_type):
        self.update_signal([("parse-download-error", (pkg_name, action_type))])

    def get_unique_id(self):
        return str(uuid.uuid4())

    def get_real_pkg_name(self, pkg_name):
        if pkg_name in self.pkg_cache:
            return pkg_name
        elif (pkg_name + ":i386") in self.pkg_cache:
            return pkg_name + ":i386"
        else:
            return None

    def dist_upgrade(self):
        action_type = ACTION_UPGRADE
        action_id = '%s_%s' % (self.get_unique_id(), action_type)
        self.update_signal([("ready-download-start", (action_id, action_type))
                            ])

        dist_upgrade_changes = self.pkg_cache.get_dist_upgrade_changes()
        # detect and prevent the system components from being uninstalled
        pkg_in_white_list = []
        download_pkgs = []
        self.all_upgrade_pkg_names = []
        for pkg in dist_upgrade_changes["delete"]:
            name = pkg.name
            if name in SYS_PKG_WHITE_LIST:
                pkg_in_white_list.append(name)
        if len(pkg_in_white_list) > 0:
            self.update_signal([("remove-pkgs-in-white-list",
                                 (json.dumps(pkg_in_white_list), action_type))
                                ])
            logger.error("found remove system packages: %s" %
                         json.dumps(pkg_in_white_list))
        else:
            for status in dist_upgrade_changes:
                for pkg in dist_upgrade_changes[status]:
                    self.all_upgrade_pkg_names.append(pkg.name)
                    if status == "delete" or status == "keep":
                        continue
                    if not parse_pkg.pkg_file_has_exist(pkg):
                        download_pkgs.append(pkg)

            self.update_signal([("ready-download-finish", (action_id,
                                                           action_type))])
            if len(download_pkgs) == 0:
                global_event.emit("download-finish", action_id, action_type,
                                  self.all_upgrade_pkg_names)
            else:
                names = []
                download_urls = []
                download_hash_infos = []
                pkg_sizes = []
                for pkg in download_pkgs:
                    names.append(pkg.name)
                    download_urls.append(pkg.candidate.uri)
                    download_hash_infos.append(pkg.candidate.md5)
                    pkg_sizes.append(pkg.candidate.size)
                self.download_manager.add_download(
                    action_id,
                    action_type,
                    download_urls,
                    download_sizes=pkg_sizes,
                    download_md5s=download_hash_infos,
                    all_task_names=names,
                    all_change_pkgs=self.all_upgrade_pkg_names,
                    file_save_dir=self.download_dir)

    def add_download(self, pkg_name, action_type, simulate=False):
        installed = self.get_pkg_installed(pkg_name)
        if installed == 1:
            self.update_signal([("pkg-installed", (pkg_name, action_type))])
            return
        elif installed == -1:
            self.update_signal([("pkg-not-in-cache", (pkg_name, action_type))])
            return
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        status = pkg_infos["status"]
        self.update_signal([("ready-download-finish", (pkg_name, action_type))
                            ])
        if status == DOWNLOAD_STATUS_NOTNEED:
            self.download_finish(pkg_name, action_type, [
                pkg_name,
            ])
        elif status == DOWNLOAD_STATUS_ERROR:
            self.update_signal([("parse-download-error", (pkg_name,
                                                          action_type))])
        else:
            names = []
            download_urls = []
            download_hash_infos = []
            pkg_sizes = []
            for info in pkg_infos["info"]:
                names.append(info["name"])
                download_urls.append(info["url"])
                download_hash_infos.append(info["hash"])
                pkg_sizes.append(info["size"])

            self.download_manager.add_download(
                pkg_name,
                action_type,
                download_urls,
                download_sizes=pkg_sizes,
                download_md5s=download_hash_infos,
                all_task_names=[pkg_name],
                file_save_dir=self.download_dir)

    def start_upgrade(self, pkg_names, action_id):
        self.apt_action_pool.add_multi_upgrade_mission(pkg_names, action_id)

    def del_source_list_d(self):
        white_list_path = os.path.join(get_parent_dir(__file__),
                                       'white_list.txt')
        if os.path.exists(white_list_path):
            with open(white_list_path) as fp:
                for line in fp:
                    line = line.strip()
                    if os.path.exists(line):
                        os.remove(line)

    def get_desktops(self, pkg_name):
        desktops = []
        try:
            pkg_obj = self.pkg_cache[pkg_name]
            for f in pkg_obj.installed_files:
                if f.endswith(".desktop"):
                    desktops.append(f)
        except:
            pass
        return desktops

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature='', out_signature='as')
    def read_no_notify_config(self, no_notify_config_path):
        if os.path.exists(no_notify_config_path):
            no_notify_config_str = read_file(no_notify_config_path)
            try:
                no_notify_config = eval(no_notify_config_str)

                if type(no_notify_config).__name__ != "list":
                    no_notify_config = []
            except Exception:
                no_notify_config = []

            return no_notify_config
        else:
            return []

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def add_no_notify_pkg(self, info):
        pkg_name, path = info
        no_notify_config = self.read_no_notify_config(path)

        if pkg_name not in no_notify_config:
            pkg_name = str(pkg_name)
            no_notify_config.append(pkg_name)
            write_file(path, str(no_notify_config))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def remove_no_notify_pkg(self, info):
        pkg_name, path = info
        pkg_name = str(pkg_name)
        path = str(path)
        no_notify_config = self.read_no_notify_config(path)

        if pkg_name in no_notify_config:
            write_file(
                path,
                str(
                    filter(lambda config_pkg_name: config_pkg_name != pkg_name,
                           no_notify_config)))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="i", out_signature="")
    def init_download_manager(self, number):
        pass

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="")
    def set_download_dir(self, local_dir):
        apt_pkg.config.set("Dir::Cache::Archives", local_dir)
        self.download_dir = local_dir

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="s",
                         out_signature="ai")
    def get_download_size(self, pkg_name):
        total_size = 0
        pkg_infos = get_pkg_download_info(self.pkg_cache, pkg_name)
        status = pkg_infos["status"]
        if status == DOWNLOAD_STATUS_NOTNEED:
            total_size = get_pkg_own_size(self.pkg_cache, pkg_name)
            size_flag = PKG_SIZE_OWN
        elif status == DOWNLOAD_STATUS_ERROR:
            total_size = -1
            size_flag = PKG_SIZE_ERROR
        else:
            for info in pkg_infos["info"]:
                total_size += info["size"]
            size_flag = PKG_SIZE_DOWNLOAD
        return [size_flag, total_size]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="ai")
    def clean_download_cache(self):
        '''Clean download cache.'''
        # get action packages.
        remain_pkgs = []
        for pkg_name in self.download_manager.task_name_to_id.keys():
            remain_pkgs.append(pkg_name)

        for pkg_name in self.apt_action_pool.install_action_dict.keys():
            remain_pkgs.append(pkg_name)
        for pkg_name in self.apt_action_pool.upgrade_action_dict.keys():
            remain_pkgs.append(pkg_name)

        # Get depend packages.
        remain_pkgs_paths = []
        for pkg_name in remain_pkgs:
            result = get_pkg_dependence_file_path(self.pkg_cache, pkg_name)
            if not result:
                remain_pkgs_paths += result

        # Init clean size.
        packageNum = 0
        cleanSize = 0

        # Delete cache directory.
        cache_archive_dir = get_cache_archive_dir()
        if os.path.exists(cache_archive_dir):
            for root, folder, files in os.walk(cache_archive_dir):
                for file_name in files:
                    path = os.path.join(root, file_name)
                    if path.endswith(".deb") and (path
                                                  not in remain_pkgs_paths):
                        packageNum += 1
                        cleanSize += os.path.getsize(path)
                        remove_file(path)

        return [packageNum, cleanSize]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="b", out_signature="")
    def say_hello(self, simulate):
        # Set exit_flag with False to prevent backend exit,
        # this just useful that frontend start again before backend exit.

        self.exit_flag = False
        self.simulate = simulate

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def request_quit(self):
        # Set exit flag.
        self.exit_flag = True

        self.exit_manager.check()
        self.update_signal([("frontend-quit", "")])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def change_source_list(self, repo_urls):
        ubuntu_repo_url, deepin_repo_url = repo_urls
        new_source_list_content = get_source_list_contents(
            ubuntu_repo_url, deepin_repo_url)
        os.system('cp %s %s.save' % (SOURCE_LIST, SOURCE_LIST))
        with open(SOURCE_LIST, 'w') as fp:
            fp.write(new_source_list_content)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="",
                         out_signature="(ss)")
    def RequestUpgradeStatus(self):
        if self.in_update_list:
            return ("cache-updating", json.dumps([]))
        elif self.is_upgrading:
            return ("upgrading", json.dumps([]))
        else:
            dist_upgrade_changes = self.pkg_cache.get_dist_upgrade_changes()
            return_infos = []
            for status in dist_upgrade_changes:
                for pkg in dist_upgrade_changes[status]:
                    return_infos.append(
                        dict(status=status,
                             name=pkg.name,
                             version=pkg.candidate.version,
                             size=pkg.candidate.size,
                             downloaded=parse_pkg.pkg_file_has_exist(pkg)))
            return ("normal", json.dumps(return_infos))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="a(si)",
                         out_signature="")
    def RemoveWaitMissions(self, pkg_infos):
        self.apt_action_pool.remove_wait_missions(pkg_infos)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="s")
    def request_uninstall_pkgs(self):
        return json.dumps(self.pkg_cache.get_uninstall_pkgs())

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="as",
                         out_signature="as")
    def request_pkgs_install_version(self, pkg_names):
        pkg_versions = []
        for pkg_name in pkg_names:
            try:
                version = self.pkg_cache[pkg_name].versions[0].version
                pkg_versions.append(version)
            except:
                try:
                    version = self.pkg_cache[pkg_name +
                                             ":i386"].versions[0].version
                    pkg_versions.append(version)
                except:
                    self.update_signal([("pkg-not-in-cache", pkg_name)])
        return pkg_versions

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="s",
                         out_signature="as")
    def is_pkg_in_cache(self, pkg_name):
        result = []
        try:
            self.pkg_cache[pkg_name]
            result.append(pkg_name)
        except:
            try:
                pkg_name += ":i386"
                self.pkg_cache[pkg_name]
                result.append(pkg_name)
            except:
                pass
        return result

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="as",
                         out_signature="as")
    def request_pkgs_uninstall_version(self, pkg_names):
        return self.pkg_cache.get_pkgs_uninstall_version(pkg_names)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def install_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                ThreadMethod(
                    self.add_download,
                    (real_pkg_name, ACTION_INSTALL, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg(self, pkg_name, purge):
        real_pkg_name = self.get_real_pkg_name(pkg_name)
        if real_pkg_name:
            self.apt_action_pool.add_uninstall_action(real_pkg_name,
                                                      self.simulate, purge)
        else:
            self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="sb", out_signature="")
    def uninstall_pkg_from_desktop(self, desktop_path, purge):
        ThreadMethod(self.uninstall_pkg_from_desktop_thread,
                     (desktop_path, purge)).start()

    def uninstall_pkg_from_desktop_thread(self, path, purge):
        pkg_name = self.get_pkg_name_from_path(path)
        if pkg_name:
            self.uninstall_pkg(pkg_name, purge)
        else:
            global_event.emit("action-failed",
                              (path, ACTION_UNINSTALL, [],
                               "no package found for path: %s" % path))

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def get_pkg_name_from_path(self, path):
        pkg_name = utils.file_path_to_package_name(path)
        if pkg_name == "" and path.endswith(".desktop"):
            desktop_name = os.path.split(path)[1]
            path = utils.desktop_name_to_desktop_path(desktop_name)
            pkg_name = utils.file_path_to_package_name(path)
        return pkg_name

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def upgrade_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            real_pkg_name = self.get_real_pkg_name(pkg_name)
            if real_pkg_name:
                self.update_signal([("ready-download-start",
                                     (real_pkg_name, ACTION_UPGRADE))])
                ThreadMethod(
                    self.add_download,
                    (real_pkg_name, ACTION_UPGRADE, self.simulate)).start()
            else:
                self.update_signal([("pkg-not-in-cache", pkg_name)])

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def DistUpgrade(self):
        ThreadMethod(self.dist_upgrade).start()

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def stop_download_pkg(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def cancel_upgrade_download(self):
        if getattr(self, 'upgrade_id'):
            self.download_manager.stop_wait_download(self.upgrade_id)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="as", out_signature="")
    def remove_wait_downloads(self, pkg_names):
        for pkg_name in pkg_names:
            self.download_manager.stop_wait_download(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="s")
    def request_pkg_short_desc(self, pkg_name):
        return self.pkg_cache.get_pkg_short_desc(pkg_name)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="as")
    def request_status(self):
        download_status = {ACTION_INSTALL: [], ACTION_UPGRADE: []}
        for info_dict in self.download_manager.download_task_info.values():
            pkg_name = info_dict["task_name"]
            if info_dict["action_type"] == ACTION_INSTALL:
                download_status[ACTION_INSTALL].append(
                    (pkg_name, info_dict["status"]))
            elif info_dict["action_type"] == ACTION_UPGRADE:
                download_status[ACTION_UPGRADE].append(
                    (pkg_name, info_dict["status"]))

        action_status = {
            ACTION_INSTALL: [],
            ACTION_UPGRADE: [],
            ACTION_UNINSTALL: []
        }
        for info_dict in self.apt_action_pool.install_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_INSTALL].append(
                (pkg_name, info_dict["status"]))

        for info_dict in self.apt_action_pool.upgrade_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_UPGRADE].append(
                (pkg_name, info_dict["status"]))

        for info_dict in self.apt_action_pool.uninstall_action_dict.values():
            pkg_name = info_dict["task_name"]
            action_status[ACTION_UNINSTALL].append(
                (pkg_name, info_dict["status"]))

        return [str(download_status), str(action_status)]

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="", out_signature="")
    def start_update_list(self):
        self.del_source_list_d()
        self.pkg_cache.open(apb.OpProgress())
        self.apt_action_pool.add_update_list_mission()

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="as",
                         out_signature="ab")
    def request_pkgs_install_status(self, pkg_names):
        _status = []
        for pkg in pkg_names:
            _status.append(self.pkg_cache.is_pkg_installed(pkg))
        return _status

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="(si)",
                         out_signature="")
    def set_pkg_status(self, pkg_status):
        pkg_name, status = pkg_status
        self.pkg_cache.set_pkg_status(pkg_name, pkg_status)

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME, in_signature="s", out_signature="i")
    def get_pkg_installed(self, pkg_name):
        if self.is_pkg_in_cache(pkg_name):
            if self.pkg_cache.is_pkg_installed(pkg_name):
                return 1
            else:
                return 0
        else:
            return -1

    @invoked_log
    @dbus.service.method(DSC_SERVICE_NAME,
                         in_signature="ss",
                         out_signature="s")
    def get_pkg_start_status(self, pkg_name, start_pkg_names):
        is_current_installed = self.get_pkg_installed(pkg_name)
        if is_current_installed == -1:
            return "unknown"
        elif is_current_installed == 0:
            return "uninstalled"
        else:
            desktops = self.get_desktops(pkg_name)
            names = start_pkg_names.split(",")
            for name in names:
                if self.get_pkg_installed(name) == 1:
                    desktops += self.get_desktops(name)
            return json.dumps(desktops)

    @dbus.service.signal(DSC_SERVICE_NAME, signature='a(sv)')
    # Use below command for test:
    # dbus-monitor --system "type='signal', interface='com.linuxdeepin.softwarecenter'"
    def update_signal(self, message):
        # The signal is emitted when this method exits
        # You can have code here if you wish
        info = str(message)
        logger.debug("dbus signal emit: %s" % info)
Ejemplo n.º 20
0
class PreferencesDialog:
    def __init__(self, config):
        self.config = config
        self.download_manager = DownloadManager()

        self.__get_widgets()
        self.__make_extensions_treeview()
        self.__connect_widgets()
        self.__add_config_notifications()

        # Set widget states from configuration
        self.status_icon_checkbutton.set_active(self.config.show_status_icon)
        self.main_window_checkbutton.set_active(self.config.show_main_window)
        self.notifications_checkbutton.set_active(self.config.show_notifications)
        self.quit_dialog_checkbutton.set_active(self.config.show_quit_dialog)
        self.autostart_checkbutton.set_active(self.config.autostart)

        self.main_window_checkbutton.set_sensitive(self.config.show_status_icon)

        if self.config.ask_for_location:
            self.ask_folder_radiobutton.set_active(True)
        else:
            self.specify_folder_radiobutton.set_active(True)
        self.default_folder_filechooserbutton.set_current_folder(self.config.default_folder)
        self.extensions_checkbutton.set_active(self.config.check_extensions)
        if not self.config.check_extensions:
            self.extensions_alignment.set_sensitive(False)

        for extension, folder in zip(self.config.extensions,
                self.config.extension_folders):
            self.extensions_model.append((extension, folder))

        self.manual_proxy_vbox.set_sensitive(False)
        if self.config.proxy_mode == "gnome":
            self.gnome_radiobutton.set_active(True)
        elif self.config.proxy_mode == "manual":
            self.manual_radiobutton.set_active(True)
        else:
            self.direct_radiobutton.set_active(True)

        self.proxy_entry.set_text(self.config.proxy_host)
        self.proxy_port_spinbutton.set_value(self.config.proxy_port)
        self.proxy_auth_checkbutton.set_active(self.config.proxy_auth)
        self.proxy_auth_hbox.set_sensitive(self.config.proxy_auth)
        self.proxy_user_entry.set_text(self.config.proxy_user)
        self.proxy_password_entry.set_text(self.config.proxy_password)

    def __get_widgets(self):
        """Get widgets from the glade file."""
        xml = gtk.glade.XML(gui.glade_file, domain=NAME.lower())

        self.dialog = xml.get_widget("preferences_dialog")

        # General tab
        self.status_icon_checkbutton = xml.get_widget("status_icon_checkbutton")
        self.main_window_checkbutton = xml.get_widget("main_window_checkbutton")
        self.notifications_checkbutton = xml.get_widget("notifications_checkbutton")
        self.quit_dialog_checkbutton = xml.get_widget("quit_dialog_checkbutton")

        self.autostart_checkbutton = xml.get_widget("autostart_checkbutton")

        # Folders tab
        self.ask_folder_radiobutton = xml.get_widget("ask_folder_radiobutton")
        self.specify_folder_radiobutton = xml.get_widget("specify_folder_radiobutton")
        self.default_folder_filechooserbutton = xml.get_widget("default_folder_filechooserbutton")
        self.extensions_checkbutton = xml.get_widget("extensions_checkbutton")
        self.extensions_alignment = xml.get_widget("extensions_alignment")
        self.extensions_treeview = xml.get_widget("extensions_treeview")
        self.add_extension_button = xml.get_widget("add_extension_button")
        self.remove_extension_button = xml.get_widget("remove_extension_button")

        # Network tab
        self.direct_radiobutton = xml.get_widget("direct_radiobutton")
        self.gnome_radiobutton = xml.get_widget("gnome_radiobutton")
        self.manual_radiobutton = xml.get_widget("manual_radiobutton")
        self.manual_proxy_vbox = xml.get_widget("manual_proxy_vbox")
        self.proxy_entry = xml.get_widget("proxy_entry")
        self.proxy_port_spinbutton = xml.get_widget("proxy_port_spinbutton")
        self.proxy_auth_checkbutton = xml.get_widget("proxy_auth_checkbutton")
        self.proxy_auth_hbox = xml.get_widget("proxy_auth_hbox")
        self.proxy_user_entry = xml.get_widget("proxy_user_entry")
        self.proxy_password_entry = xml.get_widget("proxy_password_entry")

        # Buttons
        self.close_button = xml.get_widget("close_button")

    def __make_extensions_treeview(self):
        self.extensions_model = gtk.ListStore(str, str)
        self.extensions_treeview.set_model(self.extensions_model)

        # Extension
        self.extension_cell_renderer = gtk.CellRendererText()
        self.extension_cell_renderer.props.xpad = 3
        self.extension_cell_renderer.props.ypad = 3
        self.extension_cell_renderer.props.editable = True
        self.extension_cell_renderer.connect("edited",
                self.__extension_cell_renderer_edited)
        self.extension_treeview_column = gtk.TreeViewColumn(_("Extension"),
                self.extension_cell_renderer)
        self.extension_treeview_column.set_cell_data_func(
                self.extension_cell_renderer, self.__extension_cell_data_func)
        self.extensions_treeview.append_column(self.extension_treeview_column)

        # Folder
        folder_cell_renderer = gtk.CellRendererText()
        folder_cell_renderer.props.xpad = 3
        folder_cell_renderer.props.ypad = 3
        folder_cell_renderer.props.editable = True
        folder_cell_renderer.connect("edited",
                self.__folder_cell_renderer_edited)
        folder_treeview_column = gtk.TreeViewColumn(_("Folder"),
                folder_cell_renderer)
        folder_treeview_column.set_cell_data_func(folder_cell_renderer,
                self.__folder_cell_data_func)
        self.extensions_treeview.append_column(folder_treeview_column)

    def __extension_cell_data_func(self, column, cell, model, iter):
        cell.props.text = model.get_value(iter, 0)

    def __folder_cell_data_func(self, column, cell, model, iter):
        cell.props.text = model.get_value(iter, 1)

    def __extension_cell_renderer_edited(self, cell, path, new_text):
        extensions = self.config.extensions
        if self.extensions_model[path][0] in extensions:
            extensions[extensions.index(self.extensions_model[path][0])] = new_text
            self.config.extensions = extensions
        self.extensions_model[path][0] = new_text

    def __folder_cell_renderer_edited(self, cell, path, new_text):
        extension_folders = self.config.extension_folders
        if self.extensions_model[path][1] in extension_folders:
            extension_folders[extension_folders.index(self.extensions_model[path][1])] = new_text
            self.config.extension_folders = extension_folders
        self.extensions_model[path][1] = new_text

    def __connect_widgets(self):
        """Connect to the widget signals we are interested in."""
        # General tab
        self.status_icon_checkbutton.connect("toggled",
                self.__status_icon_checkbutton_toggled)
        self.main_window_checkbutton.connect("toggled",
                self.__main_window_checkbutton_toggled)
        self.notifications_checkbutton.connect("toggled",
                self.__notifications_checkbutton_toggled)
        self.quit_dialog_checkbutton.connect("toggled",
                self.__quit_dialog_checkbutton_toggled)

        self.autostart_checkbutton.connect("toggled",
                self.__autostart_checkbutton_toggled)

        # Folders tab
        self.ask_folder_radiobutton.connect("toggled",
                self.__ask_folder_radiobutton_toggled)
        self.default_folder_filechooserbutton.connect("current-folder-changed",
                self.__default_folder_filechooserbutton_current_folder_changed)
        self.extensions_checkbutton.connect("toggled",
                self.__extensions_checkbutton_toggled)
        self.add_extension_button.connect("clicked",
                self.__add_extension_button_clicked)
        self.remove_extension_button.connect("clicked",
                self.__remove_extension_button_clicked)

        selection = self.extensions_treeview.get_selection()
        selection.connect("changed",
                self.__extensions_treeview_selection_changed)

        # Network tab
        self.direct_radiobutton.connect("toggled",
                self.__direct_radiobutton_toggled)
        self.gnome_radiobutton.connect("toggled",
                self.__gnome_radiobutton_toggled)
        self.manual_radiobutton.connect("toggled",
                self.__manual_radiobutton_toggled)
        self.proxy_entry.connect("changed",
                self.__proxy_entry_changed)
        self.proxy_port_spinbutton.connect("value-changed",
                self.__proxy_port_spinbutton_value_changed)
        self.proxy_auth_checkbutton.connect("toggled",
                self.__proxy_auth_checkbutton_toggled)
        self.proxy_user_entry.connect("changed",
                self.__proxy_user_entry_changed)
        self.proxy_password_entry.connect("changed",
                self.__proxy_password_entry_changed)

        # Buttons
        self.close_button.connect("clicked", self.__close_button_clicked)

    def __add_config_notifications(self):
        """Adds callbacks which gets called when configuration keys changes
        in gconf."""
        self.config.add_notify(config.KEY_SHOW_STATUS_ICON,
                self.__show_status_icon_key_changed)
        self.config.add_notify(config.KEY_SHOW_MAIN_WINDOW,
                self.__show_main_window_key_changed)
        self.config.add_notify(config.KEY_SHOW_NOTIFICATIONS,
                self.__show_notifications_key_changed)
        self.config.add_notify(config.KEY_SHOW_QUIT_DIALOG,
                self.__show_quit_dialog_key_changed)
        self.config.add_notify(config.KEY_AUTOSTART,
                self.__autostart_key_changed)
        self.config.add_notify(config.KEY_ASK_FOR_LOCATION,
                self.__ask_for_location_key_changed)
        self.config.add_notify(config.KEY_DEFAULT_FOLDER,
                self.__default_folder_key_changed)
        self.config.add_notify(config.KEY_CHECK_EXTENSIONS,
                self.__check_extensions_key_changed)
        self.config.add_notify(config.KEY_PROXY_MODE,
                self.__proxy_mode_key_changed)
        self.config.add_notify(config.KEY_PROXY_HOST,
                self.__proxy_host_key_changed)
        self.config.add_notify(config.KEY_PROXY_PORT,
                self.__proxy_port_key_changed)
        self.config.add_notify(config.KEY_PROXY_AUTH,
                self.__proxy_auth_key_changed)
        self.config.add_notify(config.KEY_PROXY_USER,
                self.__proxy_user_key_changed)
        self.config.add_notify(config.KEY_PROXY_PASSWORD,
                self.__proxy_password_key_changed)

    def __show_status_icon_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.status_icon_checkbutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            value = entry.value.get_bool()
            status_icon = TrayIcon()
            # status_icon.icon.set_visible(value)
            if value:
                status_icon.icon.show_all()
            else:
                status_icon.icon.hide_all()

            self.status_icon_checkbutton.set_active(value)
            # Main window must be showed if no status icon is showed
            if not value:
                self.main_window_checkbutton.set_sensitive(False)
                self.config.show_main_window = True
            else:
                if not self.main_window_checkbutton.get_property("sensitive"):
                    self.main_window_checkbutton.set_sensitive(True)
        else:
            self.status_icon_checkbutton.set_active(True)

    def __show_main_window_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.main_window_checkbutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            self.main_window_checkbutton.set_active(entry.value.get_bool())
        else:
            self.main_window_checkbutton.set_active(True)

    def __show_notifications_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.notifications_checkbutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            self.notifications_checkbutton.set_active(entry.value.get_bool())
        else:
            self.notifications_checkbutton.set_active(True)

    def __show_quit_dialog_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.quit_dialog_checkbutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            self.quit_dialog_checkbutton.set_active(entry.value.get_bool())
        else:
            self.quit_dialog_checkbutton.set_active(True)

    def __autostart_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.autostart_checkbutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            value = entry.value.get_bool()
            self.autostart_checkbutton.set_active(value)

            autostart_dir = os.path.expanduser(AUTOSTART_DIR)
            if value:
                try:
                    item = gnomedesktop.item_new_from_basename(DESKTOP_FILE,
                            gnomedesktop.LOAD_ONLY_IF_EXISTS)
                except gobject.GError:
                    raise ValueError("File path not a .desktop file")

                if not item:
                    raise ValueError("URI not found")

                # Looks like gnomedesktop.DesktopItem.save(str, bool) isn't
                # implemented so copying manually for now.
                if not os.path.exists(autostart_dir):
                    os.makedirs(autostart_dir)

                shutil.copy2(item.get_location().replace("file://", ""),
                        autostart_dir)
            else:
                autostart_file = os.path.join(autostart_dir, DESKTOP_FILE)
                if os.path.exists(autostart_file):
                    os.remove(autostart_file)
        else:
            self.autostart_checkbutton.set_active(True)

    def __ask_for_location_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.ask_folder_radiobutton.set_active(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            value = entry.value.get_bool()
            self.ask_folder_radiobutton.set_active(value)
            if not value:
                self.specify_folder_radiobutton.set_active(True)
        else:
            self.ask_folder_radiobutton.set_active(True)

    def __default_folder_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_STRING:
            folder = entry.value.to_string()
            self.default_folder_filechooserbutton.set_current_folder(folder)

    def __check_extensions_key_changed(self, client, cnxn_id, entry, data):
        if not entry.value:
            self.extensions_checkbutton.set_active(True)
            self.extensions_alignment.set_sensitive(True)
        elif entry.value.type == gconf.VALUE_BOOL:
            value = entry.value.get_bool()
            self.extensions_checkbutton.set_active(value)
            self.extensions_alignment.set_sensitive(value)
        else:
            self.extensions_checkbutton.set_active(True)
            self.extensions_alignment.set_sensitive(True)

    def __proxy_mode_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_STRING:
            mode = entry.value.to_string()
            if mode == "direct":
                self.direct_radiobutton.set_active(True)
                self.manual_proxy_vbox.set_sensitive(False)
            elif mode == "gnome":
                self.gnome_radiobutton.set_active(True)
                self.manual_proxy_vbox.set_sensitive(False)
                if self.config.use_http_proxy:
                    if self.config.use_http_proxy_auth:
                        self.download_manager.set_proxy("http", "http://%s:%s@%s:%s" % (self.config.http_proxy_user, self.config.http_proxy_pass, self.config.http_proxy_host, self.config.http_proxy_port))
                        if self.config.use_same_proxy:
                            self.download_manager.set_proxy("https", "https://%s:%s@%s:%s" % (self.config.http_proxy_user, self.config.http_proxy_pass, self.config.proxy_https_host, self.config.proxy_https_port))
                            self.download_manager.set_proxy("ftp", "ftp://%s:%s@%s:%s" % (self.config.http_proxy_user, self.config.http_proxy_pass, self.config.proxy_ftp_host, self.config.proxy_ftp_port))
                    else:
                        self.download_manager.set_proxy("http", "http://%s:%s" % (self.config.http_proxy_host, self.config.http_proxy_port))
                        self.download_manager.set_proxy("https", "https://%s:%s" % (self.config.proxy_https_host, self.config.proxy_https_port))
                        self.download_manager.set_proxy("ftp", "ftp://%s:%s" % (self.config.proxy_ftp_host, self.config.proxy_ftp_port))
            elif mode == "manual":
                self.manual_radiobutton.set_active(True)
                self.manual_proxy_vbox.set_sensitive(True)
                if self.config.proxy_auth:
                    self.download_manager.set_proxy("http", "http://%s:%s@%s:%s" % (self.config.proxy_user, self.config.proxy_password, self.config.proxy_host, self.config.proxy_port))
                else:
                    self.download_manager.set_proxy("http", "http://%s:%s" % (self.config.proxy_host, self.config.proxy_port))

    def __proxy_host_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_STRING:
            self.proxy_entry.set_text(entry.value.to_string())

    def __proxy_port_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_INT:
            self.proxy_port_spinbutton.set_value(entry.value.get_int())

    def __proxy_auth_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_BOOL:
            auth = entry.value.get_bool()
            self.proxy_auth_checkbutton.set_active(auth)
            self.proxy_auth_hbox.set_sensitive(auth)

    def __proxy_user_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_STRING:
            self.proxy_user_entry.set_text(entry.value.to_string())

    def __proxy_password_key_changed(self, client, cnxn_id, entry, data):
        if entry.value.type == gconf.VALUE_STRING:
            self.proxy_password_entry.set_text(entry.value.to_string())

    def __status_icon_checkbutton_toggled(self, checkbutton):
        self.config.show_status_icon = checkbutton.get_active()

    def __main_window_checkbutton_toggled(self, checkbutton):
        self.config.show_main_window = checkbutton.get_active()

    def __notifications_checkbutton_toggled(self, checkbutton):
        self.config.show_notifications = checkbutton.get_active()

    def __quit_dialog_checkbutton_toggled(self, checkbutton):
        self.config.show_quit_dialog = checkbutton.get_active()

    def __autostart_checkbutton_toggled(self, checkbutton):
        self.config.autostart = checkbutton.get_active()

    def __ask_folder_radiobutton_toggled(self, radiobutton):
        active = radiobutton.get_active()
        self.config.ask_for_location = active
        self.default_folder_filechooserbutton.set_sensitive(not active)

    def __default_folder_filechooserbutton_current_folder_changed(self,
            filechooserbutton):
        folder = filechooserbutton.get_current_folder()
        # Prevent infinite loop
        if self.config.default_folder != folder:
            self.config.default_folder = folder

    def __extensions_checkbutton_toggled(self, checkbutton):
        self.config.check_extensions = checkbutton.get_active()

    def __extensions_treeview_selection_changed(self, selection):
        """When selection changes set sensitivity appropriately."""
        (extensions_model, extensions_iter) = selection.get_selected()
        if extensions_iter:
            self.remove_extension_button.set_sensitive(True)
        else:
            self.remove_extension_button.set_sensitive(False)

    def __add_extension_button_clicked(self, button):
        extensions = self.config.extensions
        extensions.append("*.*")
        self.config.extensions = extensions

        extension_folders = self.config.extension_folders
        extension_folders.append(self.config.default_folder)
        self.config.extension_folders = extension_folders

        iter = self.extensions_model.append(("*.*", self.config.default_folder))
        path = self.extensions_model.get_path(iter)
        self.extensions_treeview.set_cursor_on_cell(path,
                self.extension_treeview_column, self.extension_cell_renderer,
                True)

    def __remove_extension_button_clicked(self, button):
        selection = self.extensions_treeview.get_selection()
        (extensions_model, extensions_iter) = selection.get_selected()
        if extensions_iter:
            extension = extensions_model.get_value(extensions_iter, 0)
            extension_folder = extensions_model.get_value(extensions_iter, 1)
            extensions_model.remove(extensions_iter)
            extensions = self.config.extensions
            if extension in extensions:
                extensions.remove(extension)
                self.config.extensions = extensions
            extension_folders = self.config.extension_folders
            if extension_folder in extension_folders:
                extension_folders.remove(extension_folder)
                self.config.extension_folders = extension_folders

    def __direct_radiobutton_toggled(self, radiobutton):
        if radiobutton.get_active():
            self.config.proxy_mode = "direct"

    def __gnome_radiobutton_toggled(self, radiobutton):
        if radiobutton.get_active():
            self.config.proxy_mode = "gnome"

    def __manual_radiobutton_toggled(self, radiobutton):
        if radiobutton.get_active():
            self.config.proxy_mode = "manual"

    def __proxy_entry_changed(self, entry):
        self.config.proxy_host = entry.get_text()

    def __proxy_port_spinbutton_value_changed(self, spinbutton):
        self.config.proxy_port = spinbutton.get_value_as_int()

    def __proxy_auth_checkbutton_toggled(self, checkbutton):
        self.config.proxy_auth = checkbutton.get_active()

    def __proxy_user_entry_changed(self, entry):
        self.config.proxy_user = entry.get_text()

    def __proxy_password_entry_changed(self, entry):
        self.config.proxy_password = entry.get_text()

    def __close_button_clicked(self, button):
        self.dialog.hide()
Ejemplo n.º 21
0
 def __init__(self):
     """"""
     DownloadManager.__init__(self)
     AddDownloadsManager.__init__(self)
     self.session_parser = SessionParser()
Ejemplo n.º 22
0
 def download(self,item=None):
     item = self.audios[self.count] if type(item)!=vk_audio.AudioObj else item;
     DownloadManager.get().download(item);