Beispiel #1
0
    def test_debug_pane_default_num_logs(self):
        """
        Test whether the API returns the last 100 logs when no max_lines parameter is not provided
        """
        test_core_log_message = "This is the gui test log message"
        expected_num_lines = 100

        # Log directory
        log_dir = SessionStartupConfig.load().get_log_dir()
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)
        gui_info_log_file_path = os.path.join(log_dir, 'tribler-gui-info.log')

        # write 200 (greater than expected_num_lines) test logs in file
        with open(gui_info_log_file_path, "w") as core_info_log_file:
            for log_index in xrange(200):  # write more logs
                core_info_log_file.write("%s %d\n" %
                                         (test_core_log_message, log_index))

        # Check number of logs returned is as expected
        def verify_max_logs_returned(response):
            json_response = json.loads(response)
            logs = json_response['content'].strip().split("\n")
            self.assertEqual(len(logs), expected_num_lines)

        self.should_check_equality = False
        return self.do_request('debug/log?process=gui&max_lines=', expected_code=200)\
            .addCallback(verify_max_logs_returned)
def setup_logging(gui=False):
    """
    Setup logging to write logs to files inside \
    .Tribler directory in each platforms
    """
    # First check if logger.conf is present or not
    base_path = getattr(sys, '_MEIPASS') if hasattr(sys, '_MEIPASS') else os.path.dirname(__file__)
    log_config = os.path.join(base_path, "logger.conf")

    if not os.path.exists(log_config):
        print "Log configuration file not found at location '%s'" % log_config
        return

    session_config = SessionStartupConfig.load()
    log_directory = session_config.get_log_dir()

    if not os.path.exists(log_directory):
        os.makedirs(log_directory)

    info_filename = 'tribler-gui-info.log' if gui else 'tribler-core-info.log'
    error_filename = 'tribler-gui-error.log' if gui else 'tribler-core-error.log'

    logging.info_log_file = os.path.join(log_directory, info_filename)
    logging.error_log_file = os.path.join(log_directory, error_filename)
    logging.config.fileConfig(log_config, disable_existing_loggers=False)
Beispiel #3
0
    def render_GET(self, request):
        """
        .. http:get:: /debug/log?process=<core|gui>&max_lines=<max_lines>

        A GET request to this endpoint returns a json with content of core or gui log file & max_lines requested

            **Example request**:

            .. sourcecode:: none

                curl -X GET http://localhost:8085/debug/log?process=core&max_lines=5

            **Example response**:

            A JSON with content of the log file & max_lines requested, for eg.
            {
                "max_lines" : 5,
                "content" :"INFO    1506675301.76   sqlitecachedb:181   Reading database version...
                            INFO    1506675301.76   sqlitecachedb:185   Current database version is 29
                            INFO    1506675301.76   sqlitecachedb:203   Beginning the first transaction...
                            INFO    1506675301.76         upgrade:93    tribler is in the latest version,...
                            INFO    1506675302.08  LaunchManyCore:254   lmc: Starting Dispersy..."
            }

        """

        # First, flush all the logs to make sure it is written to file
        for handler in logging.getLogger().handlers:
            handler.flush()

        # Get the location of log file
        param_process = request.args['process'][0] if request.args[
            'process'] else 'core'
        log_file_name = os.path.join(SessionStartupConfig.load().get_log_dir(),
                                     'tribler-%s-info.log' % param_process)

        # Default response
        response = {'content': '', 'max_lines': 0}

        # Check if log file exists and return last requested 'max_lines' of log
        if os.path.exists(log_file_name):
            try:
                max_lines = int(request.args['max_lines'][0])
                with open(log_file_name, 'r') as log_file:
                    response['content'] = self.tail(log_file, max_lines)
                response['max_lines'] = max_lines
            except ValueError:
                with open(log_file_name, 'r') as log_file:
                    response['content'] = self.tail(log_file,
                                                    100)  # default 100 lines
                response['max_lines'] = 0

        return json.dumps(response)
Beispiel #4
0
    def OnFinished(self,event=None):
        (name,icondata, iconmime) = self.page1.getNameIconData()

        # write changes to the pickled config file, because on shutdown, changes are not pickled!
        # this is done to spare the mypreferences-changes.

        state_dir = self.utility.session.get_state_dir()
        cfgfilename = self.utility.session.get_default_config_filename(state_dir)
        scfg = SessionStartupConfig.load(cfgfilename)
        
        for target in [scfg,self.utility.session]:
            try:
                target.set_nickname(name)
                target.set_mugshot(icondata, mime=iconmime)
            except:
                print_exc()

        scfg.save(cfgfilename)

        self.parent.WizardFinished(self, name, icondata, iconmime, scfg, cfgfilename)
Beispiel #5
0
    def OnFinished(self, event=None):
        (name, icondata, iconmime) = self.page1.getNameIconData()

        # write changes to the pickled config file, because on shutdown, changes are not pickled!
        # this is done to spare the mypreferences-changes.

        state_dir = self.utility.session.get_state_dir()
        cfgfilename = self.utility.session.get_default_config_filename(
            state_dir)
        scfg = SessionStartupConfig.load(cfgfilename)

        for target in [scfg, self.utility.session]:
            try:
                target.set_nickname(name)
                target.set_mugshot(icondata, mime=iconmime)
            except:
                print_exc()

        scfg.save(cfgfilename)

        self.parent.WizardFinished(self)
Beispiel #6
0
    def test_debug_pane_core_logs(self):
        """
        Test whether the API returns the logs
        """

        test_core_log_message = "This is the core test log message"
        max_lines = 100

        # Log directory
        log_dir = SessionStartupConfig.load().get_log_dir()
        if not os.path.exists(log_dir):
            os.makedirs(log_dir)

        # Fill logging files with statements
        core_info_log_file_path = os.path.join(log_dir,
                                               'tribler-core-info.log')

        # write 100 test lines which is used to test for its presence in the response
        with open(core_info_log_file_path, "w") as core_info_log_file:
            for log_index in xrange(max_lines):
                core_info_log_file.write("%s %d\n" %
                                         (test_core_log_message, log_index))

        def verify_log_exists(response):
            json_response = json.loads(response)
            logs = json_response['content'].strip().split("\n")

            # Check number of logs returned is correct
            self.assertEqual(len(logs), max_lines)

            # Check if test log message is present in the logs, at least once
            log_exists = any(
                (True for log in logs if test_core_log_message in log))
            self.assertTrue(log_exists,
                            "Test log not found in the debug log response")

        self.should_check_equality = False
        return self.do_request('debug/log?process=core&max_lines=%d' % max_lines, expected_code=200)\
            .addCallback(verify_log_exists)\
Beispiel #7
0
 def test_startup_session_load_no_filename(self):
     sci = SessionStartupConfig()
     sci.load()
     self.assertTrue(sci)
Beispiel #8
0
 def test_startup_session_load_corrupt(self):
     sci = SessionStartupConfig()
     sci.load(
         os.path.join(self.CONFIG_FILES_DIR, "corrupt_session_config.conf"))
     self.assertTrue(sci.sessconfig.has_section('general'))
Beispiel #9
0
    def test_startup_session_save_load(self):
        sci = SessionStartupConfig(CallbackConfigParser())
        file_path = os.path.join(self.session_base_dir, "startupconfig.conf")
        sci.save(file_path)

        sci.load(file_path)
Beispiel #10
0
    def start_session(self):
        """
        This function loads any previous configuration files from the TRIBLER_STATE_DIR environment variable and then
        starts a Tribler session.
        :return: Nothing.
        """
        if self._running:
            return False

        _logger.info("Set tribler_state_dir to %s" % os.environ['TRIBLER_STATE_DIR'])

        # Load configuration file (if exists)
        cfgfilename = Session.get_default_config_filename(os.environ['TRIBLER_STATE_DIR'])
        try:
            self._sconfig = SessionStartupConfig.load(cfgfilename)
            _logger.info("Loaded previous configuration file from %s" % cfgfilename)
        except:
            self._sconfig = SessionStartupConfig()
            self._sconfig.set_state_dir(os.environ['TRIBLER_STATE_DIR'])
            _logger.info("No previous configuration file found, creating one in %s" % os.environ['TRIBLER_STATE_DIR'])

        dlcfgfilename = get_default_dscfg_filename(self._sconfig.get_state_dir())
        _logger.debug("main: Download config %s", dlcfgfilename)
        try:
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        except:
            defaultDLConfig = DefaultDownloadStartupConfig.getInstance()

        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(os.environ['TRIBLER_DOWNLOAD_DIR'])
            self._sconfig.set_torrent_collecting_dir(os.path.join(os.environ['TRIBLER_DOWNLOAD_DIR'], ".%s" % STATEDIR_TORRENTCOLL_DIR))
            self._sconfig.set_swift_meta_dir(os.path.join(os.environ['TRIBLER_DOWNLOAD_DIR'], ".%s" % STATEDIR_SWIFTRESEED_DIR))

        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                _logger.info("Creating download directory: %s" % defaultDLConfig.get_dest_dir())
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                _logger.error("Couldn't create download directory! (%s)" % defaultDLConfig.get_dest_dir())

        # Disable unneeded dependencies
        self._sconfig.set_torrent_checking(False)
        self._sconfig.set_multicast_local_peer_discovery(False)
        #self._sconfig.set_megacache(False)
        #self._sconfig.set_swift_proc(False)
        self._sconfig.set_mainline_dht(False)
        #self._sconfig.set_torrent_collecting(False)
        #self._sconfig.set_libtorrent(False)
        self._sconfig.set_dht_torrent_collecting(False)
        #self._sconfig.set_videoplayer(False)

        self._sconfig.set_dispersy_tunnel_over_swift(False)
        self._sconfig.set_torrent_collecting_max_torrents(5000)

        # Start session
        _logger.info("Starting tribler session")
        self._session = Session(self._sconfig)
        self._session.start()

        self._swift = self._session.get_swift_proc() and self._session.get_swift_process()
        self._dispersy = self._session.get_dispersy_instance()

        _logger.info("libTribler session started!")
        self.define_communities()
Beispiel #11
0
    def start_session(self):
        """
        This function loads any previous configuration files from the TRIBLER_STATE_DIR environment variable and then
        starts a Tribler session.
        :return: Nothing.
        """
        if self._running:
            return False

        _logger.info("Set tribler_state_dir to %s" %
                     os.environ['TRIBLER_STATE_DIR'])

        # Load configuration file (if exists)
        cfgfilename = Session.get_default_config_filename(
            os.environ['TRIBLER_STATE_DIR'])
        try:
            self._sconfig = SessionStartupConfig.load(cfgfilename)
            _logger.info("Loaded previous configuration file from %s" %
                         cfgfilename)
        except:
            self._sconfig = SessionStartupConfig()
            self._sconfig.set_state_dir(os.environ['TRIBLER_STATE_DIR'])
            _logger.info(
                "No previous configuration file found, creating one in %s" %
                os.environ['TRIBLER_STATE_DIR'])

        dlcfgfilename = get_default_dscfg_filename(
            self._sconfig.get_state_dir())
        _logger.debug("main: Download config %s", dlcfgfilename)
        try:
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        except:
            defaultDLConfig = DefaultDownloadStartupConfig.getInstance()

        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(os.environ['TRIBLER_DOWNLOAD_DIR'])
            self._sconfig.set_torrent_collecting_dir(
                os.path.join(os.environ['TRIBLER_DOWNLOAD_DIR'],
                             ".%s" % STATEDIR_TORRENTCOLL_DIR))
            self._sconfig.set_swift_meta_dir(
                os.path.join(os.environ['TRIBLER_DOWNLOAD_DIR'],
                             ".%s" % STATEDIR_SWIFTRESEED_DIR))

        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                _logger.info("Creating download directory: %s" %
                             defaultDLConfig.get_dest_dir())
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                _logger.error("Couldn't create download directory! (%s)" %
                              defaultDLConfig.get_dest_dir())

        # Disable unneeded dependencies
        self._sconfig.set_torrent_checking(False)
        self._sconfig.set_multicast_local_peer_discovery(False)
        #self._sconfig.set_megacache(False)
        #self._sconfig.set_swift_proc(False)
        self._sconfig.set_mainline_dht(False)
        #self._sconfig.set_torrent_collecting(False)
        #self._sconfig.set_libtorrent(False)
        self._sconfig.set_dht_torrent_collecting(False)
        #self._sconfig.set_videoplayer(False)

        self._sconfig.set_dispersy_tunnel_over_swift(False)
        self._sconfig.set_torrent_collecting_max_torrents(5000)

        # Start session
        _logger.info("Starting tribler session")
        self._session = Session(self._sconfig)
        self._session.start()

        self._swift = self._session.get_swift_proc(
        ) and self._session.get_swift_process()
        self._dispersy = self._session.get_dispersy_instance()

        _logger.info("libTribler session started!")
        self.define_communities()
Beispiel #12
0
    def saveAll(self, event, skip_restart_dialog=False):
        if not self.Validate():
            return

        restart = False

        state_dir = self.utility.session.get_state_dir()
        cfgfilename = self.utility.session.get_default_config_filename(state_dir)
        scfg = SessionStartupConfig.load(cfgfilename)

        valdown = self._download_ctrl.GetValue()
        valup = self._upload_ctrl.GetValue()
        convert = lambda v: 0 if v == 'unlimited' else (-1 if v == '0' else int(v))
        for config_option, value in [('maxdownloadrate', convert(valdown)), ('maxuploadrate', convert(valup))]:
            if self.utility.read_config(config_option) != value:
                self.utility.write_config(config_option, value)
                if config_option == 'maxuploadrate':
                    self.guiUtility.utility.session.set_max_upload_speed(value)
                else:
                    self.guiUtility.utility.session.set_max_download_speed(value)

        valport = self._firewall_value.GetValue()
        if valport != str(self.utility.session.get_listen_port()):
            scfg.set_listen_port(int(valport))
            scfg.set_dispersy_port(int(valport) - 1)
            self.saveDefaultDownloadConfig(scfg)

            self.guiUtility.set_firewall_restart(True)
            restart = True

        showSave = int(self._disk_location_choice.IsChecked())
        if showSave != self.utility.read_config('showsaveas'):
            self.utility.write_config('showsaveas', showSave)
            self.saveDefaultDownloadConfig(scfg)

        valdir = self._disk_location_ctrl.GetValue()
        if valdir != self.utility.read_config(u'saveas'):
            self.utility.write_config(u'saveas', valdir)
            self.defaultDLConfig.set_dest_dir(valdir)

            self.saveDefaultDownloadConfig(scfg)
            restart = True

        default_number_hops = self._sliderhops.GetValue()
        if default_number_hops != self.utility.read_config('default_number_hops'):
            self.utility.write_config('default_number_hops', default_number_hops)
            self.saveDefaultDownloadConfig(scfg)

        default_anonymity_chkbox = self._default_anonymity_dialog.UseTunnels()
        if default_anonymity_chkbox != self.utility.read_config('default_anonymity_enabled'):
            self.utility.write_config('default_anonymity_enabled', default_anonymity_chkbox)
            self.saveDefaultDownloadConfig(scfg)

        default_safeseeding_chkbox = self._default_anonymity_dialog.UseSafeSeeding()
        if default_safeseeding_chkbox != self.utility.read_config('default_safeseeding_enabled'):
            self.utility.write_config('default_safeseeding_enabled', default_safeseeding_chkbox)
            self.saveDefaultDownloadConfig(scfg)

        becomeExitNode = self._become_exitnode.IsChecked()
        if becomeExitNode != scfg.get_tunnel_community_exitnode_enabled():
            scfg.set_tunnel_community_exitnode_enabled(becomeExitNode)
            restart = True

        useWebUI = self._use_webui.IsChecked()
        if useWebUI != self.utility.read_config('use_webui'):
            self.utility.write_config('use_webui', useWebUI)
            restart = True

        valwebuiport = self._webui_port.GetValue()
        if valwebuiport != str(self.utility.read_config('webui_port')):
            self.utility.write_config('webui_port', valwebuiport)
            restart = True

        useEMC = self._use_emc.IsChecked()
        if useEMC != self.utility.read_config('use_emc'):
            self.utility.write_config('use_emc', useEMC)

        valemcip = self._emc_ip.GetValue()
        if valemcip != str(self.utility.read_config('emc_ip')):
            self.utility.write_config('emc_ip', valemcip)

        valemcport = self._emc_port.GetValue()
        if valemcport != str(self.utility.read_config('emc_port')):
            self.utility.write_config('emc_port', valemcport)

        valemcusername = self._emc_username.GetValue()
        if valemcusername != str(self.utility.read_config('emc_username')):
            self.utility.write_config('emc_username', valemcusername)

        valemcpassword = self._emc_password.GetValue()
        if valemcpassword != str(self.utility.read_config('emc_password')):
            self.utility.write_config('emc_pasword', valemcpassword)

        curMintray = self.utility.read_config('mintray')
        if self._minimize_to_tray:
            minimizeToTray = 1 if self._minimize_to_tray.IsChecked() else 0
            if minimizeToTray != curMintray:
                self.utility.write_config('mintray', minimizeToTray)

        for target in [scfg, self.utility.session]:
            try:
                target.set_nickname(self._my_name_field.GetValue())
                if getattr(self, 'icondata', False):
                    target.set_mugshot(self.icondata, mime='image/jpeg')
            except:
                self._logger.exception("Could not set target")

        seeding_mode = self.utility.read_config('seeding_mode', 'downloadconfig')
        for i, mode in enumerate(('ratio', 'forever', 'time', 'never')):
            if getattr(self, '_seeding%d' % i).GetValue():
                self.utility.write_config('seeding_mode', mode, 'downloadconfig')

                if mode != seeding_mode:
                    restart = True

                break
        seeding_ratio = float(self._seeding0choice.GetStringSelection())
        self.utility.write_config("seeding_ratio", seeding_ratio, 'downloadconfig')

        hours_min = self._seeding2text.GetValue()
        hours_min = hours_min.split(':')
        if len(hours_min) > 0:
            if len(hours_min) > 1:
                seeding_time = (int(hours_min[0]) * 60 + int(hours_min[1]))
            else:
                seeding_time = int(hours_min[0])
        else:
            seeding_time = 0
        self.utility.write_config("seeding_time", seeding_time, 'downloadconfig')

        # Proxy settings
        old_ptype, old_server, old_auth = self.utility.session.get_libtorrent_proxy_settings()
        new_ptype = self._lt_proxytype.GetSelection()
        new_server = (self._lt_proxyserver.GetValue(), int(self._lt_proxyport.GetValue())
                      ) if self._lt_proxyserver.GetValue() and self._lt_proxyport.GetValue() else None
        new_auth = (self._lt_proxyusername.GetValue(), self._lt_proxypassword.GetValue()
                    ) if self._lt_proxyusername.GetValue() and self._lt_proxypassword.GetValue() else None
        if old_ptype != new_ptype or old_server != new_server or old_auth != new_auth:
            self.utility.session.set_libtorrent_proxy_settings(new_ptype, new_server, new_auth)
            scfg.set_libtorrent_proxy_settings(new_ptype, new_server, new_auth)

        enable_utp = self._enable_utp.GetValue()
        if enable_utp != self.utility.session.get_libtorrent_utp():
            self.utility.session.set_libtorrent_utp(enable_utp)
            scfg.set_libtorrent_utp(enable_utp)

        scfg.save(cfgfilename)

        self.utility.flush_config()

        if restart and not skip_restart_dialog:
            dlg = wx.MessageDialog(
                self, "A restart is required for these changes to take effect.\nDo you want to restart Tribler now?",
                                   "Restart required", wx.ICON_QUESTION | wx.YES_NO | wx.YES_DEFAULT)
            result = dlg.ShowModal()
            dlg.Destroy()
            if result == wx.ID_YES:
                self.guiUtility.frame.Restart()
        self.EndModal(1)
        event.Skip()
Beispiel #13
0
    def start_session(self):
        """
        This function loads any previous configuration files from the TRIBLER_STATE_DIR environment variable and then
        starts a Tribler session.
        :return: Nothing.
        """
        if self._running:
            return False

        _logger.info("Set tribler_state_dir to %s" %
                     os.environ['TRIBLER_STATE_DIR'])

        # Load configuration file (if exists)
        cfgfilename = Session.get_default_config_filename(
            os.environ['TRIBLER_STATE_DIR'])
        try:
            self._sconfig = SessionStartupConfig.load(cfgfilename)
            _logger.info("Loaded previous configuration file from %s" %
                         cfgfilename)
        except:
            self._sconfig = SessionStartupConfig()
            self._sconfig.set_state_dir(os.environ['TRIBLER_STATE_DIR'])
            _logger.info(
                "No previous configuration file found, creating one in %s" %
                os.environ['TRIBLER_STATE_DIR'])

        # Set torrent collecting directory:
        dlcfgfilename = get_default_dscfg_filename(
            self._sconfig.get_state_dir())
        _logger.debug("main: Download config %s", dlcfgfilename)
        try:
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        except:
            defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(os.environ['TRIBLER_DOWNLOAD_DIR'])
            self._sconfig.set_torrent_collecting_dir(
                os.path.join(os.environ['TRIBLER_DOWNLOAD_DIR']))

        # Create download directory:
        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                _logger.info("Creating download directory: %s" %
                             defaultDLConfig.get_dest_dir())
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                _logger.error("Couldn't create download directory! (%s)" %
                              defaultDLConfig.get_dest_dir())

        # TODO: This is temporary for testing:
        from jnius import autoclass
        python_activity = autoclass('org.renpy.android.PythonActivity')
        files_dir = python_activity.mActivity.getFilesDir().getAbsolutePath()
        install_dir = files_dir + u'/lib/python2.7/site-packages'
        _logger.info("Set tribler_install_dir to %s" % install_dir)
        self._sconfig.set_install_dir(install_dir)
        # TODO: ^End of temporary test.

        # Disable unwanted dependencies:
        self._sconfig.set_torrent_store(True)
        self._sconfig.set_torrent_checking(True)
        self._sconfig.set_multicast_local_peer_discovery(False)
        self._sconfig.set_mainline_dht(True)
        self._sconfig.set_dht_torrent_collecting(True)
        self._sconfig.set_torrent_collecting_max_torrents(5000)

        _logger.info("Starting Tribler session..")
        self._session = Session(self._sconfig)
        upgrader = self._session.prestart()
        while not upgrader.is_done:
            time.sleep(0.1)
        self._session.start()
        _logger.info("Tribler session started!")

        self._dispersy = self._session.get_dispersy_instance()
        self.define_communities()
Beispiel #14
0
 def test_startup_session_load_corrupt(self):
     sci = SessionStartupConfig()
     sci.load(
         os.path.join(self.CONFIG_FILES_DIR, "corrupt_session_config.conf"))
Beispiel #15
0
    def InitStage1(self, installdir, autoload_discovery=True,
                   use_torrent_search=True, use_channel_search=True):
        """ Stage 1 start: pre-start the session to handle upgrade.
        """

        # Make sure the installation dir is on the PATH
        os.environ['PATH'] += os.pathsep + os.path.abspath(installdir)

        self.gui_image_manager = GuiImageManager.getInstance(installdir)

        # Start Tribler Session
        defaultConfig = SessionStartupConfig()
        state_dir = defaultConfig.get_state_dir()

        # Switch to the state dir so relative paths can be used (IE, in LevelDB store paths)
        if not os.path.exists(state_dir):
            os.makedirs(state_dir)
        os.chdir(state_dir)

        cfgfilename = Session.get_default_config_filename(state_dir)

        self._logger.debug(u"Session config %s", cfgfilename)
        try:
            self.sconfig = SessionStartupConfig.load(cfgfilename)
        except:
            try:
                self.sconfig = convertSessionConfig(os.path.join(state_dir, 'sessconfig.pickle'), cfgfilename)
                convertMainConfig(state_dir, os.path.join(state_dir, 'abc.conf'),
                                  os.path.join(state_dir, STATEDIR_GUICONFIG))
            except:
                self.sconfig = SessionStartupConfig()
                self.sconfig.set_state_dir(state_dir)

        self.sconfig.set_install_dir(self.installdir)

        # TODO(emilon): Do we still want to force limit this? With the new
        # torrent store it should be pretty fast even with more that that.

        # Arno, 2010-03-31: Hard upgrade to 50000 torrents collected
        self.sconfig.set_torrent_collecting_max_torrents(50000)

        dlcfgfilename = get_default_dscfg_filename(self.sconfig.get_state_dir())
        self._logger.debug("main: Download config %s", dlcfgfilename)
        try:
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        except:
            try:
                defaultDLConfig = convertDefaultDownloadConfig(
                    os.path.join(state_dir, 'dlconfig.pickle'), dlcfgfilename)
            except:
                defaultDLConfig = DefaultDownloadStartupConfig.getInstance()

        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(get_default_dest_dir())
        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                # Could not create directory, ask user to select a different location
                dlg = wx.DirDialog(None,
                                   "Could not find download directory, please select a new location to store your downloads",
                                   style=wx.DEFAULT_DIALOG_STYLE)
                dlg.SetPath(get_default_dest_dir())
                if dlg.ShowModal() == wx.ID_OK:
                    new_dest_dir = dlg.GetPath()
                    defaultDLConfig.set_dest_dir(new_dest_dir)
                    defaultDLConfig.save(dlcfgfilename)
                    self.sconfig.save(cfgfilename)
                else:
                    # Quit
                    self.onError = lambda e: self._logger.error(
                        "tribler: quitting due to non-existing destination directory")
                    raise Exception()

        if not use_torrent_search:
            self.sconfig.set_enable_torrent_search(False)
        if not use_channel_search:
            self.sconfig.set_enable_channel_search(False)

        session = Session(self.sconfig, autoload_discovery=autoload_discovery)
        session.add_observer(self.show_upgrade_dialog, NTFY_UPGRADER, [NTFY_STARTED])
        self.upgrader = session.prestart()

        while not self.upgrader.is_done:
            wx.SafeYield()
            sleep(0.1)

        return session
Beispiel #16
0
    def __init__(self,
                 scfg=None,
                 ignore_singleton=False,
                 autoload_discovery=True):
        """
        A Session object is created which is configured following a copy of the
        SessionStartupConfig scfg. (copy constructor used internally)

        @param scfg SessionStartupConfig object or None, in which case we
        look for a saved session in the default location (state dir). If
        we can't find it, we create a new SessionStartupConfig() object to
        serve as startup config. Next, the config is saved in the directory
        indicated by its 'state_dir' attribute.

        In the current implementation only a single session instance can exist
        at a time in a process. The ignore_singleton flag is used for testing.
        """
        addObserver(self.unhandled_error_observer)

        patch_crypto_be_discovery()

        if not ignore_singleton:
            if Session.__single:
                raise RuntimeError("Session is singleton")
            Session.__single = self

        self._logger = logging.getLogger(self.__class__.__name__)

        self.ignore_singleton = ignore_singleton
        self.sesslock = NoDispersyRLock()

        # Determine startup config to use
        if scfg is None:  # If no override
            scfg = SessionStartupConfig.load()
        else:  # overrides any saved config
            # Work from copy
            scfg = SessionStartupConfig(copy.copy(scfg.sessconfig))

        def create_dir(fullpath):
            if not os.path.isdir(fullpath):
                os.makedirs(fullpath)

        def set_and_create_dir(dirname, setter, default_dir):
            if dirname is None:
                setter(default_dir)
            create_dir(dirname or default_dir)

        state_dir = scfg.get_state_dir()
        set_and_create_dir(state_dir, scfg.set_state_dir, state_dir)

        set_and_create_dir(
            scfg.get_torrent_store_dir(), scfg.set_torrent_store_dir,
            os.path.join(scfg.get_state_dir(), STATEDIR_TORRENT_STORE_DIR))

        # metadata store
        set_and_create_dir(
            scfg.get_metadata_store_dir(), scfg.set_metadata_store_dir,
            os.path.join(scfg.get_state_dir(), STATEDIR_METADATA_STORE_DIR))

        set_and_create_dir(
            scfg.get_peer_icon_path(), scfg.set_peer_icon_path,
            os.path.join(scfg.get_state_dir(), STATEDIR_PEERICON_DIR))

        create_dir(os.path.join(scfg.get_state_dir(), u"sqlite"))

        create_dir(os.path.join(scfg.get_state_dir(), STATEDIR_DLPSTATE_DIR))

        if GOTM2CRYPTO:
            permidmod.init()
            # Set params that depend on state_dir
            #
            # 1. keypair
            #
            pairfilename = scfg.get_permid_keypair_filename()

            if os.path.exists(pairfilename):
                self.keypair = permidmod.read_keypair(pairfilename)
            else:
                self.keypair = permidmod.generate_keypair()

                # Save keypair
                pubfilename = os.path.join(scfg.get_state_dir(), 'ecpub.pem')
                permidmod.save_keypair(self.keypair, pairfilename)
                permidmod.save_pub_key(self.keypair, pubfilename)

            multichain_pairfilename = scfg.get_multichain_permid_keypair_filename(
            )

            if os.path.exists(multichain_pairfilename):
                self.multichain_keypair = permidmod.read_keypair_multichain(
                    multichain_pairfilename)
            else:
                self.multichain_keypair = permidmod.generate_keypair_multichain(
                )

                # Save keypair
                multichain_pubfilename = os.path.join(scfg.get_state_dir(),
                                                      'ecpub_multichain.pem')
                permidmod.save_keypair_multichain(self.multichain_keypair,
                                                  multichain_pairfilename)
                permidmod.save_pub_key_multichain(self.multichain_keypair,
                                                  multichain_pubfilename)

        if not scfg.get_megacache():
            scfg.set_torrent_checking(0)

        self.sessconfig = scfg.sessconfig
        self.sessconfig.lock = self.sesslock

        self.selected_ports = scfg.selected_ports

        # Claim all random ports
        self.get_listen_port()
        self.get_dispersy_port()
        self.get_mainline_dht_listen_port()
        self.get_videoserver_port()

        self.get_anon_listen_port()
        self.get_tunnel_community_socks5_listen_ports()

        # Create handler for calling back the user via separate threads
        self.lm = TriblerLaunchMany()
        self.notifier = Notifier()

        # Checkpoint startup config
        self.save_session_config()

        self.sqlite_db = None
        self.upgrader_enabled = True
        self.dispersy_member = None
        self.readable_status = ''  # Human-readable string to indicate the status during startup/shutdown of Tribler

        self.autoload_discovery = autoload_discovery

        self.tribler_config = TriblerConfig(self)
        self.setup_tribler_gui_config()
Beispiel #17
0
    def saveAll(self, event, skip_restart_dialog=False):
        if not self.Validate():
            return

        restart = False

        state_dir = self.utility.session.get_state_dir()
        cfgfilename = self.utility.session.get_default_config_filename(
            state_dir)
        scfg = SessionStartupConfig.load(cfgfilename)

        valdown = self._download_ctrl.GetValue()
        valup = self._upload_ctrl.GetValue()
        convert = lambda v: 0 if v == 'unlimited' else (-1 if v == '0' else
                                                        int(v))
        for config_option, value in [('maxdownloadrate', convert(valdown)),
                                     ('maxuploadrate', convert(valup))]:
            if self.utility.read_config(config_option) != value:
                self.utility.write_config(config_option, value)
                if config_option == 'maxuploadrate':
                    self.guiUtility.utility.session.set_max_upload_speed(value)
                else:
                    self.guiUtility.utility.session.set_max_download_speed(
                        value)

        valport = self._firewall_value.GetValue()
        if valport != str(self.utility.session.get_listen_port()):
            scfg.set_listen_port(int(valport))
            scfg.set_dispersy_port(int(valport) - 1)
            self.saveDefaultDownloadConfig(scfg)

            self.guiUtility.set_firewall_restart(True)
            restart = True

        showSave = int(self._disk_location_choice.IsChecked())
        if showSave != self.utility.read_config('showsaveas'):
            self.utility.write_config('showsaveas', showSave)
            self.saveDefaultDownloadConfig(scfg)

        valdir = self._disk_location_ctrl.GetValue()
        if valdir != self.utility.read_config(u'saveas'):
            self.utility.write_config(u'saveas', valdir)
            self.defaultDLConfig.set_dest_dir(valdir)

            self.saveDefaultDownloadConfig(scfg)
            restart = True

        default_number_hops = self._sliderhops.GetValue()
        if default_number_hops != self.utility.read_config(
                'default_number_hops'):
            self.utility.write_config('default_number_hops',
                                      default_number_hops)
            self.saveDefaultDownloadConfig(scfg)

        default_anonymity_chkbox = self._default_anonymity_dialog.UseTunnels()
        if default_anonymity_chkbox != self.utility.read_config(
                'default_anonymity_enabled'):
            self.utility.write_config('default_anonymity_enabled',
                                      default_anonymity_chkbox)
            self.saveDefaultDownloadConfig(scfg)

        default_safeseeding_chkbox = self._default_anonymity_dialog.UseSafeSeeding(
        )
        if default_safeseeding_chkbox != self.utility.read_config(
                'default_safeseeding_enabled'):
            self.utility.write_config('default_safeseeding_enabled',
                                      default_safeseeding_chkbox)
            self.saveDefaultDownloadConfig(scfg)

        becomeExitNode = self._become_exitnode.IsChecked()
        if becomeExitNode != scfg.get_tunnel_community_exitnode_enabled():
            scfg.set_tunnel_community_exitnode_enabled(becomeExitNode)
            restart = True

        useWebUI = self._use_webui.IsChecked()
        if useWebUI != self.utility.read_config('use_webui'):
            self.utility.write_config('use_webui', useWebUI)
            restart = True

        valwebuiport = self._webui_port.GetValue()
        if valwebuiport != str(self.utility.read_config('webui_port')):
            self.utility.write_config('webui_port', valwebuiport)
            restart = True

        useEMC = self._use_emc.IsChecked()
        if useEMC != self.utility.read_config('use_emc'):
            self.utility.write_config('use_emc', useEMC)

        valemcip = self._emc_ip.GetValue()
        if valemcip != str(self.utility.read_config('emc_ip')):
            self.utility.write_config('emc_ip', valemcip)

        valemcport = self._emc_port.GetValue()
        if valemcport != str(self.utility.read_config('emc_port')):
            self.utility.write_config('emc_port', valemcport)

        valemcusername = self._emc_username.GetValue()
        if valemcusername != str(self.utility.read_config('emc_username')):
            self.utility.write_config('emc_username', valemcusername)

        valemcpassword = self._emc_password.GetValue()
        if valemcpassword != str(self.utility.read_config('emc_password')):
            self.utility.write_config('emc_pasword', valemcpassword)

        curMintray = self.utility.read_config('mintray')
        if self._minimize_to_tray:
            minimizeToTray = 1 if self._minimize_to_tray.IsChecked() else 0
            if minimizeToTray != curMintray:
                self.utility.write_config('mintray', minimizeToTray)

        for target in [scfg, self.utility.session]:
            try:
                target.set_nickname(self._my_name_field.GetValue())
                if getattr(self, 'icondata', False):
                    target.set_mugshot(self.icondata, mime='image/jpeg')
            except:
                self._logger.exception("Could not set target")

        seeding_mode = self.utility.read_config('seeding_mode',
                                                'downloadconfig')
        for i, mode in enumerate(('ratio', 'forever', 'time', 'never')):
            if getattr(self, '_seeding%d' % i).GetValue():
                self.utility.write_config('seeding_mode', mode,
                                          'downloadconfig')

                if mode != seeding_mode:
                    restart = True

                break
        seeding_ratio = float(self._seeding0choice.GetStringSelection())
        self.utility.write_config("seeding_ratio", seeding_ratio,
                                  'downloadconfig')

        hours_min = self._seeding2text.GetValue()
        hours_min = hours_min.split(':')
        if len(hours_min) > 0:
            if len(hours_min) > 1:
                seeding_time = (int(hours_min[0]) * 60 + int(hours_min[1]))
            else:
                seeding_time = int(hours_min[0])
        else:
            seeding_time = 0
        self.utility.write_config("seeding_time", seeding_time,
                                  'downloadconfig')

        # Proxy settings
        old_ptype, old_server, old_auth = self.utility.session.get_libtorrent_proxy_settings(
        )
        new_ptype = self._lt_proxytype.GetSelection()
        new_server = (self._lt_proxyserver.GetValue(),
                      int(self._lt_proxyport.GetValue())
                      ) if self._lt_proxyserver.GetValue(
                      ) and self._lt_proxyport.GetValue() else None
        new_auth = (self._lt_proxyusername.GetValue(),
                    self._lt_proxypassword.GetValue()
                    ) if self._lt_proxyusername.GetValue(
                    ) and self._lt_proxypassword.GetValue() else None
        if old_ptype != new_ptype or old_server != new_server or old_auth != new_auth:
            self.utility.session.set_libtorrent_proxy_settings(
                new_ptype, new_server, new_auth)
            scfg.set_libtorrent_proxy_settings(new_ptype, new_server, new_auth)

        enable_utp = self._enable_utp.GetValue()
        if enable_utp != self.utility.session.get_libtorrent_utp():
            self.utility.session.set_libtorrent_utp(enable_utp)
            scfg.set_libtorrent_utp(enable_utp)

        scfg.save(cfgfilename)

        self.utility.flush_config()

        if restart and not skip_restart_dialog:
            dlg = wx.MessageDialog(
                self,
                "A restart is required for these changes to take effect.\nDo you want to restart Tribler now?",
                "Restart required",
                wx.ICON_QUESTION | wx.YES_NO | wx.YES_DEFAULT)
            result = dlg.ShowModal()
            dlg.Destroy()
            if result == wx.ID_YES:
                self.guiUtility.frame.Restart()
        self.EndModal(1)
        event.Skip()
Beispiel #18
0
    def __init__(self, scfg=None, ignore_singleton=False, autoload_discovery=True):
        """
        A Session object is created which is configured following a copy of the
        SessionStartupConfig scfg. (copy constructor used internally)

        @param scfg SessionStartupConfig object or None, in which case we
        look for a saved session in the default location (state dir). If
        we can't find it, we create a new SessionStartupConfig() object to
        serve as startup config. Next, the config is saved in the directory
        indicated by its 'state_dir' attribute.

        In the current implementation only a single session instance can exist
        at a time in a process. The ignore_singleton flag is used for testing.
        """
        if not ignore_singleton:
            if Session.__single:
                raise RuntimeError("Session is singleton")
            Session.__single = self

        self._logger = logging.getLogger(self.__class__.__name__)

        self.ignore_singleton = ignore_singleton
        self.sesslock = NoDispersyRLock()

        # Determine startup config to use
        if scfg is None:  # If no override
            scfg = SessionStartupConfig.load()
        else:  # overrides any saved config
            # Work from copy
            scfg = SessionStartupConfig(copy.copy(scfg.sessconfig))

        def create_dir(fullpath):
            if not os.path.isdir(fullpath):
                os.makedirs(fullpath)

        def set_and_create_dir(dirname, setter, default_dir):
            if dirname is None:
                setter(default_dir)
            create_dir(dirname or default_dir)

        state_dir = scfg.get_state_dir()
        set_and_create_dir(state_dir, scfg.set_state_dir, state_dir)

        set_and_create_dir(scfg.get_torrent_store_dir(),
                           scfg.set_torrent_store_dir,
                           os.path.join(scfg.get_state_dir(), STATEDIR_TORRENT_STORE_DIR))

        # metadata store
        set_and_create_dir(scfg.get_metadata_store_dir(),
                           scfg.set_metadata_store_dir,
                           os.path.join(scfg.get_state_dir(), STATEDIR_METADATA_STORE_DIR))

        set_and_create_dir(scfg.get_peer_icon_path(), scfg.set_peer_icon_path,
                           os.path.join(scfg.get_state_dir(), STATEDIR_PEERICON_DIR))

        create_dir(os.path.join(scfg.get_state_dir(), u"sqlite"))

        create_dir(os.path.join(scfg.get_state_dir(), STATEDIR_DLPSTATE_DIR))

        # Reset the nickname to something not related to the host name, it was
        # really silly to have this default on the first place.
        # TODO: Maybe move this to the upgrader?
        if socket.gethostname() in scfg.get_nickname():
            scfg.set_nickname("Tribler user")

        if GOTM2CRYPTO:
            permidmod.init()
            # Set params that depend on state_dir
            #
            # 1. keypair
            #
            pairfilename = scfg.get_permid_keypair_filename()

            if os.access(pairfilename, os.F_OK):
                # May throw exceptions
                self.keypair = permidmod.read_keypair(pairfilename)
            else:
                self.keypair = permidmod.generate_keypair()

                # Save keypair
                pubfilename = os.path.join(scfg.get_state_dir(), 'ecpub.pem')
                permidmod.save_keypair(self.keypair, pairfilename)
                permidmod.save_pub_key(self.keypair, pubfilename)

        if not scfg.get_megacache():
            scfg.set_torrent_checking(0)

        self.sessconfig = scfg.sessconfig
        self.sessconfig.lock = self.sesslock

        self.selected_ports = scfg.selected_ports

        # Claim all random ports
        self.get_listen_port()
        self.get_dispersy_port()
        self.get_mainline_dht_listen_port()
        self.get_videoplayer_port()

        self.get_anon_listen_port()
        self.get_tunnel_community_socks5_listen_ports()

        # Create handler for calling back the user via separate threads
        self.lm = TriblerLaunchMany()
        self.notifier = Notifier(use_pool=True)

        # Checkpoint startup config
        self.save_pstate_sessconfig()

        self.sqlite_db = None

        self.autoload_discovery = autoload_discovery
Beispiel #19
0
    def InitStage1(self,
                   installdir,
                   autoload_discovery=True,
                   use_torrent_search=True,
                   use_channel_search=True):
        """ Stage 1 start: pre-start the session to handle upgrade.
        """

        self.gui_image_manager = GuiImageManager.getInstance(installdir)

        # Start Tribler Session
        defaultConfig = SessionStartupConfig()
        state_dir = defaultConfig.get_state_dir()

        # Switch to the state dir so relative paths can be used (IE, in LevelDB store paths)
        if not os.path.exists(state_dir):
            os.makedirs(state_dir)
        os.chdir(state_dir)

        cfgfilename = Session.get_default_config_filename(state_dir)

        self._logger.debug(u"Session config %s", cfgfilename)

        self.sconfig = SessionStartupConfig.load(cfgfilename)
        self.sconfig.set_install_dir(self.installdir)

        if not self.sconfig.get_watch_folder_path():
            default_watch_folder_dir = os.path.join(get_home_dir(),
                                                    u'Downloads',
                                                    u'TriblerWatchFolder')
            self.sconfig.set_watch_folder_path(default_watch_folder_dir)
            if not os.path.exists(default_watch_folder_dir):
                os.makedirs(default_watch_folder_dir)

        # TODO(emilon): Do we still want to force limit this? With the new
        # torrent store it should be pretty fast even with more that that.

        # Arno, 2010-03-31: Hard upgrade to 50000 torrents collected
        self.sconfig.set_torrent_collecting_max_torrents(50000)

        dlcfgfilename = get_default_dscfg_filename(
            self.sconfig.get_state_dir())
        self._logger.debug("main: Download config %s", dlcfgfilename)

        if os.path.exists(dlcfgfilename):
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        else:
            defaultDLConfig = DefaultDownloadStartupConfig.getInstance()

        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(get_default_dest_dir())
        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                # Could not create directory, ask user to select a different location
                dlg = wx.DirDialog(
                    None,
                    "Could not find download directory, please select a new location to store your downloads",
                    style=wx.DEFAULT_DIALOG_STYLE)
                dlg.SetPath(get_default_dest_dir())
                if dlg.ShowModal() == wx.ID_OK:
                    new_dest_dir = dlg.GetPath()
                    defaultDLConfig.set_dest_dir(new_dest_dir)
                    defaultDLConfig.save(dlcfgfilename)
                    self.sconfig.save(cfgfilename)
                else:
                    # Quit
                    self.onError = lambda e: self._logger.error(
                        "tribler: quitting due to non-existing destination directory"
                    )
                    raise Exception()

        if not use_torrent_search:
            self.sconfig.set_enable_torrent_search(False)
        if not use_channel_search:
            self.sconfig.set_enable_channel_search(False)

        session = Session(self.sconfig, autoload_discovery=autoload_discovery)
        session.add_observer(self.show_upgrade_dialog, NTFY_UPGRADER,
                             [NTFY_STARTED])

        while not session.upgrader.is_done:
            wx.SafeYield()
            sleep(0.1)

        return session
Beispiel #20
0
    def start_session(self):
        """
        This function loads any previous configuration files from the TRIBLER_STATE_DIR environment variable and then
        starts a Tribler session.
        :return: Nothing.
        """
        if self._running:
            return False

        _logger.info("Set tribler_state_dir to %s" % os.environ["TRIBLER_STATE_DIR"])

        # Load configuration file (if exists)
        cfgfilename = Session.get_default_config_filename(os.environ["TRIBLER_STATE_DIR"])
        try:
            self._sconfig = SessionStartupConfig.load(cfgfilename)
            _logger.info("Loaded previous configuration file from %s" % cfgfilename)
        except:
            self._sconfig = SessionStartupConfig()
            self._sconfig.set_state_dir(os.environ["TRIBLER_STATE_DIR"])
            _logger.info("No previous configuration file found, creating one in %s" % os.environ["TRIBLER_STATE_DIR"])

        # Set torrent collecting directory:
        dlcfgfilename = get_default_dscfg_filename(self._sconfig.get_state_dir())
        _logger.debug("main: Download config %s", dlcfgfilename)
        try:
            defaultDLConfig = DefaultDownloadStartupConfig.load(dlcfgfilename)
        except:
            defaultDLConfig = DefaultDownloadStartupConfig.getInstance()
        if not defaultDLConfig.get_dest_dir():
            defaultDLConfig.set_dest_dir(os.environ["TRIBLER_DOWNLOAD_DIR"])
            self._sconfig.set_torrent_collecting_dir(os.path.join(os.environ["TRIBLER_DOWNLOAD_DIR"]))

        # Create download directory:
        if not os.path.isdir(defaultDLConfig.get_dest_dir()):
            try:
                _logger.info("Creating download directory: %s" % defaultDLConfig.get_dest_dir())
                os.makedirs(defaultDLConfig.get_dest_dir())
            except:
                _logger.error("Couldn't create download directory! (%s)" % defaultDLConfig.get_dest_dir())

        # TODO: This is temporary for testing:
        from jnius import autoclass

        python_activity = autoclass("org.renpy.android.PythonActivity")
        files_dir = python_activity.mActivity.getFilesDir().getAbsolutePath()
        install_dir = files_dir + u"/lib/python2.7/site-packages"
        _logger.info("Set tribler_install_dir to %s" % install_dir)
        self._sconfig.set_install_dir(install_dir)
        # TODO: ^End of temporary test.

        # Disable unwanted dependencies:
        self._sconfig.set_torrent_store(True)
        self._sconfig.set_torrent_checking(True)
        self._sconfig.set_multicast_local_peer_discovery(False)
        self._sconfig.set_mainline_dht(True)
        self._sconfig.set_dht_torrent_collecting(True)
        self._sconfig.set_torrent_collecting_max_torrents(5000)

        _logger.info("Starting Tribler session..")
        self._session = Session(self._sconfig)
        upgrader = self._session.prestart()
        while not upgrader.is_done:
            time.sleep(0.1)
        self._session.start()
        _logger.info("Tribler session started!")

        self._dispersy = self._session.get_dispersy_instance()
        self.define_communities()