From fc902af10c9c1d1908c39f652386e1aec27c1651 Mon Sep 17 00:00:00 2001 From: Calum Lind Date: Thu, 1 Dec 2016 19:04:18 +0000 Subject: [PATCH] [Core] Remove all lt 1.1 deprecated code references --- deluge/core/alertmanager.py | 5 +- deluge/core/core.py | 6 +- deluge/core/preferencesmanager.py | 174 +++++++++--------------------- deluge/core/torrent.py | 26 +---- deluge/core/torrentmanager.py | 27 ++--- deluge/tests/test_error.py | 2 + deluge/tests/test_torrent.py | 4 +- deluge/tests/test_web_api.py | 2 +- 8 files changed, 69 insertions(+), 177 deletions(-) diff --git a/deluge/core/alertmanager.py b/deluge/core/alertmanager.py index e13893565..5bc03e7a9 100644 --- a/deluge/core/alertmanager.py +++ b/deluge/core/alertmanager.py @@ -46,10 +46,7 @@ class AlertManager(component.Component): lt.alert.category_t.ip_block_notification | lt.alert.category_t.performance_warning) - try: - self.session.apply_settings({'alert_mask': alert_mask}) - except AttributeError: - self.session.set_alert_mask(alert_mask) + self.session.apply_settings({'alert_mask': alert_mask}) # handlers is a dictionary of lists {"alert_type": [handler1,h2,..]} self.handlers = {} diff --git a/deluge/core/core.py b/deluge/core/core.py index b526a5130..2000b0fdd 100644 --- a/deluge/core/core.py +++ b/deluge/core/core.py @@ -149,11 +149,7 @@ class Core(component.Component): settings (dict): A dict of lt session settings to apply. """ - - try: - self.session.apply_settings(settings) - except AttributeError: - self.session.set_settings(settings) + self.session.apply_settings(settings) def __save_session_state(self): """Saves the libtorrent session state""" diff --git a/deluge/core/preferencesmanager.py b/deluge/core/preferencesmanager.py index 5e974d8f0..77b02e7ec 100644 --- a/deluge/core/preferencesmanager.py +++ b/deluge/core/preferencesmanager.py @@ -159,23 +159,6 @@ class PreferencesManager(component.Component): log.debug('Config key: %s set to %s..', key, value) on_set_func(key, value) - def session_set_setting(self, key, value): - try: - self.session.apply_settings({key: value}) - except AttributeError: - # Deprecated in libtorrent 1.1 - if key in ('enable_lsd', 'enable_upnp', 'enable_natpmp', 'enable_dht'): - start_stop = key.replace('enable', 'start') if value else key.replace('enable', 'stop') - getattr(self.session, start_stop)() - elif key == 'dht_bootstrap_nodes': - self.session.add_dht_router('router.bittorrent.com', 6881) - self.session.add_dht_router('router.utorrent.com', 6881) - self.session.add_dht_router('router.bitcomet.com', 6881) - elif key == 'peer_tos': - self.session.set_settings({key: chr(value)}) - else: - self.session.set_settings({key: value}) - def _on_config_value_change(self, key, value): if self.get_state() == 'Started': self.do_config_set_func(key, value) @@ -213,23 +196,9 @@ class PreferencesManager(component.Component): log.debug('Listen Interface: %s, Ports: %s with use_sys_port: %s', interface, listen_ports, self.config['listen_use_sys_port']) interfaces = ['%s:%s' % (interface, port) for port in range(listen_ports[0], listen_ports[1]+1)] - try: - self.session.apply_settings({'listen_system_port_fallback': self.config['listen_use_sys_port'], - 'listen_interfaces': ''.join(interfaces)}) - except AttributeError: - # Deprecated in libtorrent 1.1 - # If a single port range then always enable re-use port flag. - reuse_port = True if listen_ports[0] == listen_ports[1] else self.config['listen_reuse_port'] - flags = ((lt.listen_on_flags_t.listen_no_system_port - if not self.config['listen_use_sys_port'] else 0) | - (lt.listen_on_flags_t.listen_reuse_address - if reuse_port else 0)) - try: - self.session.listen_on(listen_ports[0], listen_ports[1], interface, flags) - except RuntimeError as ex: - if ex.message == 'Invalid Argument': - log.error('Error setting listen interface (must be IP Address): %s %s-%s', - interface, listen_ports[0], listen_ports[1]) + self.core.apply_session_settings( + {'listen_system_port_fallback': self.config['listen_use_sys_port'], + 'listen_interfaces': ''.join(interfaces)}) def _on_set_outgoing_ports(self, key, value): self.__set_outgoing_ports() @@ -245,33 +214,26 @@ class PreferencesManager(component.Component): else: num_ports = 0 log.debug('Outgoing port set to %s with range: %s', port, num_ports) - try: - self.session.apply_settings({'outgoing_port': port, 'num_outgoing_ports': num_ports}) - except AttributeError: - # Deprecated in libtorrent 1.1 - ports = [0, 0] if self.config['random_outgoing_ports'] else self.config['outgoing_ports'] - log.debug('Outgoing port set to %s', ports) - self.session_set_setting('outgoing_ports', (ports[0], ports[1])) + self.core.apply_session_settings({'outgoing_port': port, 'num_outgoing_ports': num_ports}) def _on_set_peer_tos(self, key, value): try: - self.session_set_setting('peer_tos', int(value, 16)) + self.core.apply_session_setting('peer_tos', int(value, 16)) except ValueError as ex: log.error('Invalid tos byte: %s', ex) def _on_set_dht(self, key, value): dht_bootstraps = 'router.bittorrent.com:6881,router.utorrent.com:6881,router.bitcomet.com:6881' - self.session_set_setting('dht_bootstrap_nodes', dht_bootstraps) - self.session_set_setting('enable_dht', value) + self.core.apply_session_settings({'dht_bootstrap_nodes': dht_bootstraps, 'enable_dht': value}) def _on_set_upnp(self, key, value): - self.session_set_setting('enable_upnp', value) + self.core.apply_session_setting('enable_upnp', value) def _on_set_natpmp(self, key, value): - self.session_set_setting('enable_natpmp', value) + self.core.apply_session_setting('enable_natpmp', value) def _on_set_lsd(self, key, value): - self.session_set_setting('enable_lsd', value) + self.core.apply_session_setting('enable_lsd', value) def _on_set_utpex(self, key, value): if value: @@ -289,73 +251,60 @@ class PreferencesManager(component.Component): def _on_set_encryption(self, key, value): # Convert Deluge enc_level values to libtorrent enc_level values. pe_enc_level = {0: lt.enc_level.plaintext, 1: lt.enc_level.rc4, 2: lt.enc_level.both} - try: - self.session.apply_settings({'out_enc_policy': lt.enc_policy(self.config['enc_out_policy']), - 'in_enc_policy': lt.enc_policy(self.config['enc_in_policy']), - 'allowed_enc_level': lt.enc_level(pe_enc_level[self.config['enc_level']]), - 'prefer_rc4': True}) - except AttributeError: - # Deprecated in libtorrent 1.1 - pe_settings = lt.pe_settings() - pe_settings.out_enc_policy = lt.enc_policy(self.config['enc_out_policy']) - pe_settings.in_enc_policy = lt.enc_policy(self.config['enc_in_policy']) - pe_settings.allowed_enc_level = lt.enc_level(pe_enc_level[self.config['enc_level']]) - pe_settings.prefer_rc4 = True - self.session.set_pe_settings(pe_settings) - pe_sess_settings = self.session.get_pe_settings() - log.debug('encryption settings:\n\t\t\tout_policy: %s\n\t\t\ - in_policy: %s\n\t\t\tlevel: %s\n\t\t\tprefer_rc4: %s', - pe_sess_settings.out_enc_policy, - pe_sess_settings.in_enc_policy, - pe_sess_settings.allowed_enc_level, - pe_sess_settings.prefer_rc4) + self.core.apply_session_settings( + {'out_enc_policy': lt.enc_policy(self.config['enc_out_policy']), + 'in_enc_policy': lt.enc_policy(self.config['enc_in_policy']), + 'allowed_enc_level': lt.enc_level(pe_enc_level[self.config['enc_level']]), + 'prefer_rc4': True}) def _on_set_max_connections_global(self, key, value): - self.session_set_setting('connections_limit', value) + self.core.apply_session_setting('connections_limit', value) def _on_set_max_upload_speed(self, key, value): # We need to convert Kb/s to B/s value = -1 if value < 0 else int(value * 1024) - self.session_set_setting('upload_rate_limit', value) + self.core.apply_session_setting('upload_rate_limit', value) def _on_set_max_download_speed(self, key, value): # We need to convert Kb/s to B/s value = -1 if value < 0 else int(value * 1024) - self.session_set_setting('download_rate_limit', value) + self.core.apply_session_setting('download_rate_limit', value) def _on_set_max_upload_slots_global(self, key, value): - self.session_set_setting('unchoke_slots_limit', value) + self.core.apply_session_setting('unchoke_slots_limit', value) def _on_set_max_half_open_connections(self, key, value): - self.session_set_setting('half_open_limit', value) + self.core.apply_session_setting('half_open_limit', value) def _on_set_max_connections_per_second(self, key, value): - self.session_set_setting('connection_speed', value) + self.core.apply_session_setting('connection_speed', value) def _on_set_ignore_limits_on_local_network(self, key, value): - self.session_set_setting('ignore_limits_on_local_network', value) + self.core.apply_session_setting('ignore_limits_on_local_network', value) def _on_set_share_ratio_limit(self, key, value): - self.session_set_setting('share_ratio_limit', value) + # This value is a float percentage in deluge, but libtorrent needs int percentage. + self.core.apply_session_setting('share_ratio_limit', int(value * 100)) def _on_set_seed_time_ratio_limit(self, key, value): - self.session_set_setting('seed_time_ratio_limit', value) + # This value is a float percentage in deluge, but libtorrent needs int percentage. + self.core.apply_session_setting('seed_time_ratio_limit', int(value * 100)) def _on_set_seed_time_limit(self, key, value): # This value is stored in minutes in deluge, but libtorrent wants seconds - self.session_set_setting('seed_time_limit', int(value * 60)) + self.core.apply_session_setting('seed_time_limit', int(value * 60)) def _on_set_max_active_downloading(self, key, value): - self.session_set_setting('active_downloads', value) + self.core.apply_session_setting('active_downloads', value) def _on_set_max_active_seeding(self, key, value): - self.session_set_setting('active_seeds', value) + self.core.apply_session_setting('active_seeds', value) def _on_set_max_active_limit(self, key, value): - self.session_set_setting('active_limit', value) + self.core.apply_session_setting('active_limit', value) def _on_set_dont_count_slow_torrents(self, key, value): - self.session_set_setting('dont_count_slow_torrents', value) + self.core.apply_session_setting('dont_count_slow_torrents', value) def _on_set_send_info(self, key, value): """sends anonymous stats home""" @@ -403,51 +352,32 @@ class PreferencesManager(component.Component): self.new_release_timer.stop() def _on_set_proxy(self, key, value): - try: - if key == 'i2p_proxy' or value['type'] == 6: - proxy_settings = { - 'proxy_type': lt.proxy_type.i2p_proxy, - 'i2p_hostname': value['hostname'], - 'i2p_port': value['port'] - } - else: - proxy_settings = { - 'proxy_type': value['type'], - 'proxy_hostname': value['hostname'], - 'proxy_port': value['port'], - 'proxy_username': value['username'], - 'proxy_password': value['password'], - 'proxy_hostnames': value['proxy_hostnames'], - 'proxy_peer_connections': value['proxy_peer_connections'], - } - self.session.apply_settings(proxy_settings) - except AttributeError: - proxy_settings = lt.proxy_settings() - proxy_settings.hostname = value['hostname'] - proxy_settings.port = value['port'] - if key == 'i2p_proxy': - try: - self.session.set_i2p_proxy(proxy_settings) - except RuntimeError as ex: - log.error('Unable to set I2P Proxy: %s', ex) - else: - proxy_settings.type = lt.proxy_type(value['type']) - proxy_settings.username = value['username'] - proxy_settings.password = value['password'] - proxy_settings.hostname = value['hostname'] - proxy_settings.port = value['port'] - proxy_settings.proxy_hostnames = value['proxy_hostnames'] - proxy_settings.proxy_peer_connections = value['proxy_peer_connections'] - self.session.set_proxy(proxy_settings) + if key == 'i2p_proxy' or value['type'] == 6: + proxy_settings = { + 'proxy_type': lt.proxy_type.i2p_proxy, + 'i2p_hostname': value['hostname'], + 'i2p_port': value['port'] + } + else: + proxy_settings = { + 'proxy_type': value['type'], + 'proxy_hostname': value['hostname'], + 'proxy_port': value['port'], + 'proxy_username': value['username'], + 'proxy_password': value['password'], + 'proxy_hostnames': value['proxy_hostnames'], + 'proxy_peer_connections': value['proxy_peer_connections'], + } + self.core.apply_session_settings(proxy_settings) def _on_set_i2p_proxy(self, key, value): self._on_set_proxy(key, value) def _on_set_rate_limit_ip_overhead(self, key, value): - self.session_set_setting('rate_limit_ip_overhead', value) + self.core.apply_session_setting('rate_limit_ip_overhead', value) def _on_set_anonymous_mode(self, key, value): - self.session_set_setting('anonymous_mode', value) + self.core.apply_session_setting('anonymous_mode', value) def _on_set_geoip_db_location(self, key, geoipdb_path): # Load the GeoIP DB for country look-ups if available @@ -460,10 +390,10 @@ class PreferencesManager(component.Component): log.warning('Unable to find GeoIP database file: %s', geoipdb_path) def _on_set_cache_size(self, key, value): - self.session_set_setting('cache_size', value) + self.core.apply_session_setting('cache_size', value) def _on_set_cache_expiry(self, key, value): - self.session_set_setting('cache_expiry', value) + self.core.apply_session_setting('cache_expiry', value) def _on_auto_manage_prefer_seeds(self, key, value): - self.session_set_setting('auto_manage_prefer_seeds', value) + self.core.apply_session_setting('auto_manage_prefer_seeds', value) diff --git a/deluge/core/torrent.py b/deluge/core/torrent.py index c7550b83b..1b286e7f7 100644 --- a/deluge/core/torrent.py +++ b/deluge/core/torrent.py @@ -229,12 +229,7 @@ class Torrent(object): self.magnet = magnet self.status = self.handle.status() - try: - self.torrent_info = self.handle.get_torrent_info() - except RuntimeError: - # Deprecated in libtorrent 1.1 (i.e. exception check is not needed) - self.torrent_info = None - + self.torrent_info = self.handle.get_torrent_info() self.has_metadata = self.status.has_metadata self.options = TorrentOptions() @@ -391,12 +386,7 @@ class Torrent(object): return self.torrent_info.map_file(idx, byte_offset, 0).piece for idx in range(self.torrent_info.num_files()): - try: - file_size = self.torrent_info.files().file_size(idx) - except AttributeError: - # Deprecated in libtorrent 1.1 - file_size = self.torrent_info.file_at(idx).size - + file_size = self.torrent_info.files().file_size(idx) two_percent_bytes = int(0.02 * file_size) # Get the pieces for the byte offsets first_start = get_file_piece(idx, 0) @@ -495,11 +485,7 @@ class Torrent(object): return if len(file_priorities) != self.torrent_info.num_files(): log.debug('file_priorities len != num_files') - torrent_fprios = self.handle.file_priorities() - # Workaround for libtorrent 1.1 changing default priorities from 1 to 4. - if 4 in torrent_fprios: - torrent_fprios = [1 if x == 4 else x for x in torrent_fprios] - self.options['file_priorities'] = torrent_fprios + self.options['file_priorities'] = self.handle.file_priorities() return if log.isEnabledFor(logging.DEBUG): @@ -638,11 +624,7 @@ class Torrent(object): session_paused = component.get('Core').session.is_paused() old_state = self.state self.set_status_message() - try: - status_error = status.errc.message() if status.errc.value() else '' - except AttributeError: - # Deprecated in libtorrent 1.1 - status_error = status.error + status_error = status.errc.message() if status.errc.value() else '' if self.forced_error: self.state = 'Error' diff --git a/deluge/core/torrentmanager.py b/deluge/core/torrentmanager.py index 73bec4734..ac24d863a 100644 --- a/deluge/core/torrentmanager.py +++ b/deluge/core/torrentmanager.py @@ -1050,15 +1050,7 @@ class TorrentManager(component.Component): except (RuntimeError, KeyError): return - try: - error_message = decode_string(alert.error_message) - except AttributeError: - # Deprecated in libtorrent 1.1 - error_message = decode_string(alert.msg) - # If alert.msg is empty then it's a '-1' code so fallback to a.e.message. Note that alert.msg - # cannot be replaced by a.e.message because the code is included in the string (for non-'-1'). - if not error_message: - error_message = decode_string(alert.error.message()) + error_message = decode_string(alert.error_message()) log.debug('Tracker Error Alert: %s [%s]', decode_string(alert.message()), error_message) torrent.set_tracker_status('Error: ' + error_message) @@ -1069,12 +1061,8 @@ class TorrentManager(component.Component): torrent = self.torrents[torrent_id] except (RuntimeError, KeyError): return - try: - storage_path = os.path.normpath(alert.storage_path) - except AttributeError: - # Deprecated in libtorrent 1.1 - storage_path = os.path.normpath(alert.handle.save_path()) - torrent.set_download_location(storage_path) + + torrent.set_download_location(os.path.normpath(alert.storage_path)) torrent.set_move_completed(False) torrent.moving_storage = False torrent.update_state() @@ -1086,12 +1074,13 @@ class TorrentManager(component.Component): def on_alert_storage_moved_failed(self, alert): """Alert handler for libtorrent storage_moved_failed_alert""" - log.warning('on_alert_storage_moved_failed: %s', decode_string(alert.message())) try: torrent_id = str(alert.handle.info_hash()) torrent = self.torrents[torrent_id] except (RuntimeError, KeyError): return + + log.warning('on_alert_storage_moved_failed: %s', decode_string(alert.message())) # Set an Error message and pause the torrent alert_msg = decode_string(alert.message()).split(':', 1)[1].strip() torrent.moving_storage = False @@ -1186,11 +1175,7 @@ class TorrentManager(component.Component): except (RuntimeError, KeyError): return - try: - new_name = decode_string(alert.new_name) - except AttributeError: - # Deprecated in libtorrent 1.1 - new_name = decode_string(alert.name) + new_name = decode_string(alert.new_name) log.debug('index: %s name: %s', alert.index, new_name) # We need to see if this file index is in a waiting_on_folder dict diff --git a/deluge/tests/test_error.py b/deluge/tests/test_error.py index 6917101ae..3708061d0 100644 --- a/deluge/tests/test_error.py +++ b/deluge/tests/test_error.py @@ -14,6 +14,8 @@ class ErrorTestCase(unittest.TestCase): msg = 'Some message' e = deluge.error.DelugeError(msg) self.assertEquals(str(e), msg) + from twisted.internet.defer import DebugInfo + del DebugInfo.__del__ # Hides all errors self.assertEquals(e._args, (msg,)) self.assertEquals(e._kwargs, {}) diff --git a/deluge/tests/test_torrent.py b/deluge/tests/test_torrent.py index 839aff34c..7e8433d81 100644 --- a/deluge/tests/test_torrent.py +++ b/deluge/tests/test_torrent.py @@ -85,7 +85,7 @@ class TorrentTestCase(BaseTestCase): if idx in prioritized_piece_indexes: self.assertEquals(priorities[idx], 7) else: - self.assertEquals(priorities[idx], 1) + self.assertEquals(priorities[idx], 4) # self.print_priority_list(priorities) @@ -101,7 +101,7 @@ class TorrentTestCase(BaseTestCase): # Test the priority of the prioritized pieces for i in priorities: - self.assertEquals(priorities[i], 1) + self.assertEquals(priorities[i], 4) # self.print_priority_list(priorities) diff --git a/deluge/tests/test_web_api.py b/deluge/tests/test_web_api.py index 147e27f10..600064a8d 100644 --- a/deluge/tests/test_web_api.py +++ b/deluge/tests/test_web_api.py @@ -142,7 +142,7 @@ class WebAPITestCase(WebServerTestBase): ret = yield self.deluge_web.web_api.get_torrent_files('ab570cdd5a17ea1b61e970bb72047de141bce173') self.assertEquals(ret['type'], 'dir') self.assertEquals(ret['contents'], {u'azcvsupdater_2.6.2.jar': - {'priority': 1, u'index': 0, u'offset': 0, 'progress': 0.0, u'path': + {'priority': 4, u'index': 0, u'offset': 0, 'progress': 0.0, u'path': u'azcvsupdater_2.6.2.jar', 'type': 'file', u'size': 307949}}) @defer.inlineCallbacks