def __init__(self, config=None): """ Create a new TriblerConfig instance. :param config: a ConfigObj instance :raises an InvalidConfigException if ConfigObj is invalid """ self._logger = logging.getLogger(self.__class__.__name__) if config is None: file_name = os.path.join(self.get_default_state_dir(), FILENAME) if os.path.exists(file_name): config = ConfigObj(file_name, configspec=CONFIG_SPEC_PATH, encoding='latin_1') else: config = ConfigObj(configspec=CONFIG_SPEC_PATH, encoding='latin_1') self.config = config self.validate() # set defaults downloads path if not self.config['download_defaults']['saveas']: self.config['download_defaults']['saveas'] = get_default_dest_dir() self.selected_ports = {} self._set_video_analyser_path()
def network_create_engine_wrapper(self,lm_network_engine_wrapper_created_callback,pstate,lm_network_vod_event_callback,initialdlstatus=None): """ Called by any thread, assume dllock already acquired """ if DEBUG: print >>sys.stderr,"SwiftDownloadImpl: create_engine_wrapper()" if self.get_mode() == DLMODE_VOD: self.lm_network_vod_event_callback = lm_network_vod_event_callback if not self.dlconfig.has_key('swiftmetadir') and not os.path.isdir(self.get_dest_dir()): # We must be dealing with a checkpoint from a previous release (<6.1.0). Move the swift metadata to the right directory. metadir = os.path.join(get_default_dest_dir(), STATEDIR_SWIFTRESEED_DIR) self.set_swift_meta_dir(metadir) if not os.path.exists(metadir): os.makedirs(metadir) is_multifile = self.get_dest_dir().endswith("." + self.get_def().get_roothash_as_hex()) path_old = self.get_dest_dir() path_new = os.path.join(metadir, self.get_def().get_roothash_as_hex() if is_multifile else os.path.split(self.get_dest_dir())[1]) try: if is_multifile: shutil.move(path_old, path_new + '.mfspec') self.dlconfig['saveas'] = os.path.split(self.get_dest_dir())[0] shutil.move(path_old + '.mhash', path_new + '.mhash') shutil.move(path_old + '.mbinmap', path_new + '.mbinmap') except: print_exc() # Synchronous: starts process if needed self.sp = self.session.lm.spm.get_or_create_sp(self.session.get_swift_working_dir(),self.session.get_torrent_collecting_dir(),self.get_swift_listen_port(), self.get_swift_httpgw_listen_port(), self.get_swift_cmdgw_listen_port() ) self.sp.start_download(self) self.session.lm.rawserver.add_task(self.network_check_swift_alive,SWIFT_ALIVE_CHECK_INTERVAL)
def setup_tribler_gui_config(self): """ Initialize the TriblerGUI configuration file and make sure that we have all required values. """ configfilepath = os.path.join(self.get_state_dir(), STATEDIR_GUICONFIG) gui_config = CallbackConfigParser() DefaultDownloadStartupConfig.getInstance().set_dest_dir( get_default_dest_dir()) # Load the config file. if os.path.exists(configfilepath): gui_config.read_file(configfilepath, 'utf-8-sig') if not gui_config.has_section('Tribler'): gui_config.add_section('Tribler') for k, v in tribler_defaults['Tribler'].iteritems(): if not gui_config.has_option(k, v): gui_config.set('Tribler', k, v) if not gui_config.has_section('downloadconfig'): gui_config.add_section('downloadconfig') for k, v in DefaultDownloadStartupConfig.getInstance( ).dlconfig._sections['downloadconfig'].iteritems(): if not gui_config.has_option(k, v): gui_config.set('downloadconfig', k, v) # Make sure we use the same ConfigParser instance for both Utility and DefaultDownloadStartupConfig. DefaultDownloadStartupConfig.getInstance().dlconfig = gui_config gui_config.write_file(configfilepath)
def network_create_engine_wrapper( self, lm_network_engine_wrapper_created_callback, pstate, lm_network_vod_event_callback, initialdlstatus=None): """ Called by any thread, assume dllock already acquired """ if DEBUG: print >> sys.stderr, "SwiftDownloadImpl: create_engine_wrapper()" if self.get_mode() == DLMODE_VOD: self.lm_network_vod_event_callback = lm_network_vod_event_callback if not self.dlconfig.has_key('swiftmetadir') and not os.path.isdir( self.get_dest_dir()): # We must be dealing with a checkpoint from a previous release (<6.1.0). Move the swift metadata to the right directory. metadir = os.path.join(get_default_dest_dir(), STATEDIR_SWIFTRESEED_DIR) self.set_swift_meta_dir(metadir) if not os.path.exists(metadir): os.makedirs(metadir) is_multifile = self.get_dest_dir().endswith( "." + self.get_def().get_roothash_as_hex()) path_old = self.get_dest_dir() path_new = os.path.join( metadir, self.get_def().get_roothash_as_hex() if is_multifile else os.path.split(self.get_dest_dir())[1]) try: if is_multifile: shutil.move(path_old, path_new + '.mfspec') self.dlconfig['saveas'] = os.path.split( self.get_dest_dir())[0] shutil.move(path_old + '.mhash', path_new + '.mhash') shutil.move(path_old + '.mbinmap', path_new + '.mbinmap') except: print_exc() # Synchronous: starts process if needed self.sp = self.session.lm.spm.get_or_create_sp( self.session.get_swift_working_dir(), self.session.get_torrent_collecting_dir(), self.get_swift_listen_port(), self.get_swift_httpgw_listen_port(), self.get_swift_cmdgw_listen_port()) self.sp.start_download(self) self.session.lm.rawserver.add_task(self.network_check_swift_alive, SWIFT_ALIVE_CHECK_INTERVAL)
def __init__(self, config=None): """ Create a new TriblerConfig instance. :param config: a ConfigObj instance :raises an InvalidConfigException if ConfigObj is invalid """ self._logger = logging.getLogger(self.__class__.__name__) if config is None: file_name = os.path.join(self.get_default_state_dir(), FILENAME) if os.path.exists(file_name): config = ConfigObj(file_name, configspec=CONFIG_SPEC_PATH, default_encoding='utf-8') else: config = ConfigObj(configspec=CONFIG_SPEC_PATH, default_encoding='utf-8') self.config = config self.validate() # set defaults downloads path if not self.config['download_defaults']['saveas']: self.config['download_defaults']['saveas'] = get_default_dest_dir() self.selected_ports = {}
def __init__(self, scfg=None, ignore_singleton=False): """ 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. """ # ProxyService 90s Test_ # self.start_time = time.time() # _ProxyService 90s Test if not ignore_singleton: if Session.__single: raise RuntimeError, "Session is singleton" Session.__single = self self.sesslock = RLock() # Determine startup config to use if scfg is None: # If no override try: # Then try to read from default location state_dir = Session.get_default_state_dir() cfgfilename = Session.get_default_config_filename(state_dir) scfg = SessionStartupConfig.load(cfgfilename) except: # If that fails, create a fresh config with factory defaults print_exc() scfg = SessionStartupConfig() self.sessconfig = scfg.sessconfig else: # overrides any saved config # Work from copy self.sessconfig = copy.copy(scfg.sessconfig) # Create dir for session state, if not exist state_dir = self.sessconfig["state_dir"] if state_dir is None: state_dir = Session.get_default_state_dir() self.sessconfig["state_dir"] = state_dir if not os.path.isdir(state_dir): os.makedirs(state_dir) collected_torrent_dir = self.sessconfig["torrent_collecting_dir"] if not collected_torrent_dir: collected_torrent_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_TORRENTCOLL_DIR) self.sessconfig["torrent_collecting_dir"] = collected_torrent_dir collected_subtitles_dir = self.sessconfig.get("subtitles_collecting_dir", None) if not collected_subtitles_dir: collected_subtitles_dir = os.path.join(self.sessconfig["state_dir"], STATEDIR_SUBSCOLL_DIR) self.sessconfig["subtitles_collecting_dir"] = collected_subtitles_dir if not os.path.exists(collected_torrent_dir): os.makedirs(collected_torrent_dir) if not self.sessconfig["peer_icon_path"]: self.sessconfig["peer_icon_path"] = os.path.join(self.sessconfig["state_dir"], STATEDIR_PEERICON_DIR) # PERHAPS: load default TorrentDef and DownloadStartupConfig from state dir # Let user handle that, he's got default_state_dir, etc. # Core init # print >>sys.stderr,'Session: __init__ config is', self.sessconfig if GOTM2CRYPTO: permidmod.init() # # Set params that depend on state_dir # # 1. keypair # pairfilename = os.path.join(self.sessconfig["state_dir"], "ec.pem") if self.sessconfig["eckeypairfilename"] is None: self.sessconfig["eckeypairfilename"] = pairfilename if os.access(self.sessconfig["eckeypairfilename"], os.F_OK): # May throw exceptions self.keypair = permidmod.read_keypair(self.sessconfig["eckeypairfilename"]) else: self.keypair = permidmod.generate_keypair() # Save keypair pubfilename = os.path.join(self.sessconfig["state_dir"], "ecpub.pem") permidmod.save_keypair(self.keypair, pairfilename) permidmod.save_pub_key(self.keypair, pubfilename) # 2. Downloads persistent state dir dlpstatedir = os.path.join(self.sessconfig["state_dir"], STATEDIR_DLPSTATE_DIR) if not os.path.isdir(dlpstatedir): os.mkdir(dlpstatedir) # 3. tracker trackerdir = self.get_internal_tracker_dir() if not os.path.isdir(trackerdir): os.mkdir(trackerdir) if self.sessconfig["tracker_dfile"] is None: self.sessconfig["tracker_dfile"] = os.path.join(trackerdir, "tracker.db") if self.sessconfig["tracker_allowed_dir"] is None: self.sessconfig["tracker_allowed_dir"] = trackerdir if self.sessconfig["tracker_logfile"] is None: if sys.platform == "win32": # Not "Nul:" but "nul" is /dev/null on Win32 sink = "nul" else: sink = "/dev/null" self.sessconfig["tracker_logfile"] = sink # 4. superpeer.txt and crawler.txt if self.sessconfig["superpeer_file"] is None: self.sessconfig["superpeer_file"] = os.path.join( self.sessconfig["install_dir"], LIBRARYNAME, "Core", "superpeer.txt" ) if "crawler_file" not in self.sessconfig or self.sessconfig["crawler_file"] is None: self.sessconfig["crawler_file"] = os.path.join( self.sessconfig["install_dir"], LIBRARYNAME, "Core", "Statistics", "crawler.txt" ) # 5. peer_icon_path if self.sessconfig["peer_icon_path"] is None: self.sessconfig["peer_icon_path"] = os.path.join(self.sessconfig["state_dir"], STATEDIR_PEERICON_DIR) if not os.path.isdir(self.sessconfig["peer_icon_path"]): os.mkdir(self.sessconfig["peer_icon_path"]) # 6. Poor man's versioning of SessionConfig, add missing # default values. Really should use PERSISTENTSTATE_CURRENTVERSION # and do conversions. for key, defvalue in sessdefaults.iteritems(): if key not in self.sessconfig: self.sessconfig[key] = defvalue # 7. proxyservice_dir if self.sessconfig[ "overlay" ]: # NIELS: proxyservice_on/off is set at runtime, always make sure proxyservice_ and self.sessconfig['proxyservice_status'] == PROXYSERVICE_ON: if self.sessconfig["proxyservice_dir"] is None: self.sessconfig["proxyservice_dir"] = os.path.join(get_default_dest_dir(), PROXYSERVICE_DESTDIR) # Jelle: under linux, default_dest_dir can be /tmp. Then proxyservice_dir can be deleted in between # sessions. if not os.path.isdir(self.sessconfig["proxyservice_dir"]): os.makedirs(self.sessconfig["proxyservice_dir"]) if not "live_aux_seeders" in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig["live_aux_seeders"] = sessdefaults["live_aux_seeders"] if not "nat_detect" in self.sessconfig: self.sessconfig["nat_detect"] = sessdefaults["nat_detect"] if not "puncturing_internal_port" in self.sessconfig: self.sessconfig["puncturing_internal_port"] = sessdefaults["puncturing_internal_port"] if not "stun_servers" in self.sessconfig: self.sessconfig["stun_servers"] = sessdefaults["stun_servers"] if not "pingback_servers" in self.sessconfig: self.sessconfig["pingback_servers"] = sessdefaults["pingback_servers"] if not "mainline_dht" in self.sessconfig: self.sessconfig["mainline_dht"] = sessdefaults["mainline_dht"] # SWIFTPROC if self.sessconfig["swiftpath"] is None: if sys.platform == "win32": self.sessconfig["swiftpath"] = os.path.join(self.sessconfig["install_dir"], "swift.exe") else: self.sessconfig["swiftpath"] = os.path.join(self.sessconfig["install_dir"], "swift") # Checkpoint startup config self.save_pstate_sessconfig() # Create handler for calling back the user via separate threads self.uch = UserCallbackHandler(self) # Create engine with network thread self.lm = TriblerLaunchMany() self.lm.register(self, self.sesslock) self.lm.start()
def test_get_default_dest_dir(self): self.assertIsInstance(get_default_dest_dir(), unicode)
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
def test_get_default_dest_dir(self): self.assertIsInstance(get_default_dest_dir(), six.text_type)
def vod_event_callback(d, event, params): if event == VODEVENT_START: stream = params["stream"] length = params["length"] mimetype = params["mimetype"] global FIRST_ITERATION, QUIT_NOW, SESSION epoch_server = None epoch_local = time.time() bitrate = None estduration = None currentSize = 0 partialSize = length start = int(config['start']) end = int(config['end']) if FIRST_ITERATION: if config['debug']: print >> sys.stderr, "main: Seeking to second: ", config[ 'start'], "estimated duration: ", estduration file = None blocksize = d.get_def().get_piece_length() if d.get_def().is_multifile_torrent(): file = d.get_selected_files()[0] bitrate = d.get_def().get_bitrate(file) if bitrate is not None: estduration = float(length) / float(bitrate) if config['debug']: print >> sys.stderr, "main: Seeking: bitrate: ", bitrate, "duration: ", estduration if start < int(estduration): seekbyte = float(bitrate * start) # Works only with TS container if mimetype == 'video/mp2t': # Ric if it is a ts stream we can round the start # byte to the beginning of a ts packet (ts pkt = 188 bytes) seekbyte = seekbyte - seekbyte % 188 stream.seek(int(seekbyte)) if config['debug']: print >> sys.stderr, "main: Seeking: seekbyte: ", seekbyte, "start time: ", config[ 'start'] FIRST_ITERATION = False else: print >> sys.stderr, "main: Starting time exceeds video duration!!" if end != '': # Determine the final size of the stream depending on the end Time endbyte = float(bitrate * int(config['end'])) partialSize = endbyte - seekbyte else: print >> sys.stderr, "Seeking to the the beginning" stream.seek(0) basename = config['videoOut'] + '.mpeg' if config['destdir'] == 'default download dir': config['destdir'] = get_default_dest_dir() filename = os.path.join(config['destdir'], basename) if config['debug']: print >> sys.stderr, "main: Saving the file in the following location: ", filename f = open(filename, "wb") prev_data = None while not FIRST_ITERATION and (currentSize < partialSize): data = stream.read() if config['debug']: print >> sys.stderr, "main: VOD ready callback: reading", type( data) print >> sys.stderr, "main: VOD ready callback: reading", len( data) if len(data) == 0 or data == prev_data: if config['debug']: print >> sys.stderr, "main: Same data replicated: we reached the end of the stream" break f.write(data) currentSize += len(data) prev_data = data # Stop the download if not config['continueDownload']: #SESSION.remove_ d.stop() #seek(0) if config['quitAfter']: QUIT_NOW = True f.close() stream.close() print >> sys.stderr, "main: Seeking: END!!" if config['createNewTorr']: createTorr(filename)
def test_get_default_dest_dir(self): self.assertIsInstance(get_default_dest_dir(), unicode) self.assertIsInstance(get_default_dscfg_filename(""), str)
def vod_event_callback(d,event,params): if event == VODEVENT_START: stream = params["stream"] length = params["length"] mimetype = params["mimetype"] global FIRST_ITERATION, QUIT_NOW, SESSION epoch_server = None epoch_local = time.time() bitrate = None estduration = None currentSize = 0 partialSize = length start = int(config['start']) end = int(config['end']) if FIRST_ITERATION: if config['debug']: print >>sys.stderr, "main: Seeking to second: ", config['start'], "estimated duration: ", estduration file = None blocksize = d.get_def().get_piece_length() if d.get_def().is_multifile_torrent(): file = d.get_selected_files()[0] bitrate = d.get_def().get_bitrate(file) if bitrate is not None: estduration = float(length) / float(bitrate) if config['debug']: print >> sys.stderr, "main: Seeking: bitrate: ", bitrate, "duration: ", estduration if start < int(estduration): seekbyte = float(bitrate * start) # Works only with TS container if mimetype == 'video/mp2t': # Ric if it is a ts stream we can round the start # byte to the beginning of a ts packet (ts pkt = 188 bytes) seekbyte = seekbyte - seekbyte%188 stream.seek(int(seekbyte)) if config['debug']: print >>sys.stderr, "main: Seeking: seekbyte: ", seekbyte, "start time: ", config['start'] FIRST_ITERATION = False else: print >>sys.stderr, "main: Starting time exceeds video duration!!" if end != '': # Determine the final size of the stream depending on the end Time endbyte = float( bitrate * int(config['end']) ) partialSize = endbyte - seekbyte else: print >>sys.stderr, "Seeking to the the beginning" stream.seek(0) basename = config['videoOut'] + '.mpeg' if config['destdir'] == 'default download dir': config['destdir'] = get_default_dest_dir() filename = os.path.join(config['destdir'], basename) if config['debug']: print >>sys.stderr, "main: Saving the file in the following location: ", filename f = open(filename,"wb") prev_data = None while not FIRST_ITERATION and (currentSize < partialSize): data = stream.read() if config['debug']: print >>sys.stderr,"main: VOD ready callback: reading",type(data) print >>sys.stderr,"main: VOD ready callback: reading",len(data) if len(data) == 0 or data == prev_data: if config['debug']: print >>sys.stderr, "main: Same data replicated: we reached the end of the stream" break f.write(data) currentSize += len(data) prev_data = data # Stop the download if not config['continueDownload']: #SESSION.remove_ d.stop() #seek(0) if config['quitAfter']: QUIT_NOW = True f.close() stream.close() print >> sys.stderr, "main: Seeking: END!!" if config['createNewTorr']: createTorr(filename)
def network_create_engine_wrapper(self, pstate, checkpoint_disabled=False, share_mode=False): with self.dllock: self._logger.debug("LibtorrentDownloadImpl: network_create_engine_wrapper()") atp = {} atp["save_path"] = os.path.normpath(os.path.join(get_default_dest_dir(), self.get_dest_dir())) atp["storage_mode"] = lt.storage_mode_t.storage_mode_sparse atp["paused"] = True atp["auto_managed"] = False atp["duplicate_is_error"] = False atp["hops"] = self.get_hops() if share_mode: atp["flags"] = lt.add_torrent_params_flags_t.flag_share_mode self.set_checkpoint_disabled(checkpoint_disabled) resume_data = pstate.get('state', 'engineresumedata') if pstate else None if not isinstance(self.tdef, TorrentDefNoMetainfo): metainfo = self.tdef.get_metainfo() torrentinfo = lt.torrent_info(metainfo) self.orig_files = [file_entry.path.decode('utf-8') for file_entry in torrentinfo.files()] is_multifile = len(self.orig_files) > 1 commonprefix = os.path.commonprefix(self.orig_files) if is_multifile else '' swarmname = commonprefix.partition(os.path.sep)[0] if is_multifile and swarmname != self.correctedinfoname: for i, filename_old in enumerate(self.orig_files): filename_new = os.path.join(self.correctedinfoname, filename_old[len(swarmname) + 1:]) # Path should be unicode if Libtorrent is using std::wstring (on Windows), # else we use str (on Linux). try: torrentinfo.rename_file(i, filename_new) except TypeError: torrentinfo.rename_file(i, filename_new.encode("utf-8")) self.orig_files[i] = filename_new atp["ti"] = torrentinfo has_resume_data = resume_data and isinstance(resume_data, dict) if has_resume_data: atp["resume_data"] = lt.bencode(resume_data) else: atp["url"] = self.tdef.get_url() or "magnet:?xt=urn:btih:" + hexlify(self.tdef.get_infohash()) atp["name"] = self.tdef.get_name_as_unicode() def on_torrent_added(handle): self.handle = handle if self.handle.is_valid(): self.set_selected_files() user_stopped = pstate.get('download_defaults', 'user_stopped') if pstate else False # If we lost resume_data always resume download in order to force checking if not user_stopped or not resume_data: self.handle.resume() # If we only needed to perform checking, pause download after it is complete self.pause_after_next_hashcheck = user_stopped self.set_vod_mode(self.get_mode() == DLMODE_VOD) # Limit the amount of connections if we have specified that self.handle.set_max_connections(self.session.config.get_libtorrent_max_conn_download()) return self def on_torrent_failed(failure): self._logger.error("Could not add torrent to LibtorrentManager %s", self.tdef.get_name_as_unicode()) self.cew_scheduled = False return Failure((self, pstate)) return self.ltmgr.add_torrent(self, atp).addCallbacks(on_torrent_added, on_torrent_failed)
def __init__(self, scfg=None, ignore_singleton=False): """ 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.sesslock = RLock() # Determine startup config to use if scfg is None: # If no override try: # Then try to read from default location state_dir = Session.get_default_state_dir() cfgfilename = Session.get_default_config_filename(state_dir) scfg = SessionStartupConfig.load(cfgfilename) except: # If that fails, create a fresh config with factory defaults print_exc() scfg = SessionStartupConfig() self.sessconfig = scfg.sessconfig else: # overrides any saved config # Work from copy self.sessconfig = copy.copy(scfg.sessconfig) # Create dir for session state, if not exist state_dir = self.sessconfig['state_dir'] if state_dir is None: state_dir = Session.get_default_state_dir() self.sessconfig['state_dir'] = state_dir if not os.path.isdir(state_dir): os.makedirs(state_dir) collected_torrent_dir = self.sessconfig['torrent_collecting_dir'] if not collected_torrent_dir: collected_torrent_dir = os.path.join(self.sessconfig['state_dir'], STATEDIR_TORRENTCOLL_DIR) self.sessconfig['torrent_collecting_dir'] = collected_torrent_dir if not os.path.exists(collected_torrent_dir): os.makedirs(collected_torrent_dir) if not self.sessconfig['peer_icon_path']: self.sessconfig['peer_icon_path'] = os.path.join( self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR) # PERHAPS: load default TorrentDef and DownloadStartupConfig from state dir # Let user handle that, he's got default_state_dir, etc. # Core init Tribler.Core.Overlay.permid.init() #print 'Session: __init__ config is', self.sessconfig # # Set params that depend on state_dir # # 1. keypair # pairfilename = os.path.join(self.sessconfig['state_dir'], 'ec.pem') if self.sessconfig['eckeypairfilename'] is None: self.sessconfig['eckeypairfilename'] = pairfilename if os.access(self.sessconfig['eckeypairfilename'], os.F_OK): # May throw exceptions self.keypair = Tribler.Core.Overlay.permid.read_keypair( self.sessconfig['eckeypairfilename']) else: self.keypair = Tribler.Core.Overlay.permid.generate_keypair() # Save keypair pubfilename = os.path.join(self.sessconfig['state_dir'], 'ecpub.pem') Tribler.Core.Overlay.permid.save_keypair(self.keypair, pairfilename) Tribler.Core.Overlay.permid.save_pub_key(self.keypair, pubfilename) # 2. Downloads persistent state dir dlpstatedir = os.path.join(self.sessconfig['state_dir'], STATEDIR_DLPSTATE_DIR) if not os.path.isdir(dlpstatedir): os.mkdir(dlpstatedir) # 3. tracker trackerdir = self.get_internal_tracker_dir() if not os.path.isdir(trackerdir): os.mkdir(trackerdir) if self.sessconfig['tracker_dfile'] is None: self.sessconfig['tracker_dfile'] = os.path.join( trackerdir, 'tracker.db') if self.sessconfig['tracker_allowed_dir'] is None: self.sessconfig['tracker_allowed_dir'] = trackerdir if self.sessconfig['tracker_logfile'] is None: if sys.platform == "win32": # Not "Nul:" but "nul" is /dev/null on Win32 sink = 'nul' else: sink = '/dev/null' self.sessconfig['tracker_logfile'] = sink # 4. superpeer.txt if self.sessconfig['superpeer_file'] is None: self.sessconfig['superpeer_file'] = os.path.join( self.sessconfig['install_dir'], 'Tribler', 'Core', 'superpeer.txt') # 5. download_help_dir if self.sessconfig['download_help_dir'] is None: self.sessconfig['download_help_dir'] = os.path.join( get_default_dest_dir(), DESTDIR_COOPDOWNLOAD) # Jelle: under linux, default_dest_dir can be /tmp. Then download_help_dir can be deleted inbetween # sessions. if not os.path.isdir(self.sessconfig['download_help_dir']): os.makedirs(self.sessconfig['download_help_dir']) # 6. peer_icon_path if self.sessconfig['peer_icon_path'] is None: self.sessconfig['peer_icon_path'] = os.path.join( self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR) if not os.path.isdir(self.sessconfig['peer_icon_path']): os.mkdir(self.sessconfig['peer_icon_path']) # 7. NAT type detection if not 'nat_detect' in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig['nat_detect'] = sessdefaults['nat_detect'] self.sessconfig['puncturing_private_port'] = sessdefaults[ 'puncturing_private_port'] self.sessconfig['stun_servers'] = sessdefaults['stun_servers'] self.sessconfig['puncturing_coordinators'] = sessdefaults[ 'puncturing_coordinators'] if not 'live_aux_seeders' in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig['live_aux_seeders'] = sessdefaults[ 'live_aux_seeders'] # Checkpoint startup config self.save_pstate_sessconfig() # Create handler for calling back the user via separate threads self.uch = UserCallbackHandler(self) # Create engine with network thread self.lm = TriblerLaunchMany(self, self.sesslock) self.lm.start()
def __init__(self,scfg=None,ignore_singleton=False): """ 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.sesslock = RLock() # Determine startup config to use if scfg is None: # If no override try: # Then try to read from default location state_dir = Session.get_default_state_dir() cfgfilename = Session.get_default_config_filename(state_dir) scfg = SessionStartupConfig.load(cfgfilename) except: # If that fails, create a fresh config with factory defaults print_exc() scfg = SessionStartupConfig() self.sessconfig = scfg.sessconfig else: # overrides any saved config # Work from copy self.sessconfig = copy.copy(scfg.sessconfig) # Create dir for session state, if not exist state_dir = self.sessconfig['state_dir'] if state_dir is None: state_dir = Session.get_default_state_dir() self.sessconfig['state_dir'] = state_dir if not os.path.isdir(state_dir): os.makedirs(state_dir) collected_torrent_dir = self.sessconfig['torrent_collecting_dir'] if not collected_torrent_dir: collected_torrent_dir = os.path.join(self.sessconfig['state_dir'], STATEDIR_TORRENTCOLL_DIR) self.sessconfig['torrent_collecting_dir'] = collected_torrent_dir if not os.path.exists(collected_torrent_dir): os.makedirs(collected_torrent_dir) if not self.sessconfig['peer_icon_path']: self.sessconfig['peer_icon_path'] = os.path.join(self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR) # PERHAPS: load default TorrentDef and DownloadStartupConfig from state dir # Let user handle that, he's got default_state_dir, etc. # Core init Tribler.Core.Overlay.permid.init() #print 'Session: __init__ config is', self.sessconfig # # Set params that depend on state_dir # # 1. keypair # pairfilename = os.path.join(self.sessconfig['state_dir'],'ec.pem') if self.sessconfig['eckeypairfilename'] is None: self.sessconfig['eckeypairfilename'] = pairfilename if os.access(self.sessconfig['eckeypairfilename'],os.F_OK): # May throw exceptions self.keypair = Tribler.Core.Overlay.permid.read_keypair(self.sessconfig['eckeypairfilename']) else: self.keypair = Tribler.Core.Overlay.permid.generate_keypair() # Save keypair pubfilename = os.path.join(self.sessconfig['state_dir'],'ecpub.pem') Tribler.Core.Overlay.permid.save_keypair(self.keypair,pairfilename) Tribler.Core.Overlay.permid.save_pub_key(self.keypair,pubfilename) # 2. Downloads persistent state dir dlpstatedir = os.path.join(self.sessconfig['state_dir'],STATEDIR_DLPSTATE_DIR) if not os.path.isdir(dlpstatedir): os.mkdir(dlpstatedir) # 3. tracker trackerdir = self.get_internal_tracker_dir() if not os.path.isdir(trackerdir): os.mkdir(trackerdir) if self.sessconfig['tracker_dfile'] is None: self.sessconfig['tracker_dfile'] = os.path.join(trackerdir,'tracker.db') if self.sessconfig['tracker_allowed_dir'] is None: self.sessconfig['tracker_allowed_dir'] = trackerdir if self.sessconfig['tracker_logfile'] is None: if sys.platform == "win32": # Not "Nul:" but "nul" is /dev/null on Win32 sink = 'nul' else: sink = '/dev/null' self.sessconfig['tracker_logfile'] = sink # 4. superpeer.txt if self.sessconfig['superpeer_file'] is None: self.sessconfig['superpeer_file'] = os.path.join(self.sessconfig['install_dir'],'Tribler','Core','superpeer.txt') # 5. download_help_dir if self.sessconfig['download_help_dir'] is None: self.sessconfig['download_help_dir'] = os.path.join(get_default_dest_dir(),DESTDIR_COOPDOWNLOAD) # Jelle: under linux, default_dest_dir can be /tmp. Then download_help_dir can be deleted inbetween # sessions. if not os.path.isdir(self.sessconfig['download_help_dir']): os.makedirs(self.sessconfig['download_help_dir']) # 6. peer_icon_path if self.sessconfig['peer_icon_path'] is None: self.sessconfig['peer_icon_path'] = os.path.join(self.sessconfig['state_dir'],STATEDIR_PEERICON_DIR) if not os.path.isdir(self.sessconfig['peer_icon_path']): os.mkdir(self.sessconfig['peer_icon_path']) # 7. NAT type detection if not 'nat_detect' in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig['nat_detect'] = sessdefaults['nat_detect'] self.sessconfig['puncturing_private_port'] = sessdefaults['puncturing_private_port'] self.sessconfig['stun_servers'] = sessdefaults['stun_servers'] self.sessconfig['puncturing_coordinators'] = sessdefaults['puncturing_coordinators'] if not 'live_aux_seeders' in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig['live_aux_seeders'] = sessdefaults['live_aux_seeders'] # Checkpoint startup config self.save_pstate_sessconfig() # Create handler for calling back the user via separate threads self.uch = UserCallbackHandler(self) # Create engine with network thread self.lm = TriblerLaunchMany(self,self.sesslock) self.lm.start()
def __init__(self,scfg=None,ignore_singleton=False): """ 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. """ # ProxyService 90s Test_ # self.start_time = time.time() # _ProxyService 90s Test if not ignore_singleton: if Session.__single: raise RuntimeError, "Session is singleton" Session.__single = self self.sesslock = NoDispersyRLock() # Determine startup config to use if scfg is None: # If no override try: # Then try to read from default location state_dir = Session.get_default_state_dir() cfgfilename = Session.get_default_config_filename(state_dir) scfg = SessionStartupConfig.load(cfgfilename) except: # If that fails, create a fresh config with factory defaults print_exc() scfg = SessionStartupConfig() self.sessconfig = scfg.sessconfig else: # overrides any saved config # Work from copy self.sessconfig = copy.copy(scfg.sessconfig) #Niels: 11/05/2012, turning off overlay self.sessconfig['overlay'] = 0 self.sessconfig['crawler'] = 0 # Create dir for session state, if not exist state_dir = self.sessconfig['state_dir'] if state_dir is None: state_dir = Session.get_default_state_dir() self.sessconfig['state_dir'] = state_dir if not os.path.isdir(state_dir): os.makedirs(state_dir) collected_torrent_dir = self.sessconfig['torrent_collecting_dir'] if not collected_torrent_dir: collected_torrent_dir = os.path.join(self.sessconfig['state_dir'], STATEDIR_TORRENTCOLL_DIR) self.sessconfig['torrent_collecting_dir'] = collected_torrent_dir collected_subtitles_dir = self.sessconfig.get('subtitles_collecting_dir',None) if not collected_subtitles_dir: collected_subtitles_dir = os.path.join(self.sessconfig['state_dir'], STATEDIR_SUBSCOLL_DIR) self.sessconfig['subtitles_collecting_dir'] = collected_subtitles_dir if not os.path.exists(collected_torrent_dir): os.makedirs(collected_torrent_dir) if not self.sessconfig['peer_icon_path']: self.sessconfig['peer_icon_path'] = os.path.join(self.sessconfig['state_dir'], STATEDIR_PEERICON_DIR) # PERHAPS: load default TorrentDef and DownloadStartupConfig from state dir # Let user handle that, he's got default_state_dir, etc. # Core init #print >>sys.stderr,'Session: __init__ config is', self.sessconfig if GOTM2CRYPTO: permidmod.init() # # Set params that depend on state_dir # # 1. keypair # pairfilename = os.path.join(self.sessconfig['state_dir'],'ec.pem') if self.sessconfig['eckeypairfilename'] is None: self.sessconfig['eckeypairfilename'] = pairfilename if os.access(self.sessconfig['eckeypairfilename'],os.F_OK): # May throw exceptions self.keypair = permidmod.read_keypair(self.sessconfig['eckeypairfilename']) else: self.keypair = permidmod.generate_keypair() # Save keypair pubfilename = os.path.join(self.sessconfig['state_dir'],'ecpub.pem') permidmod.save_keypair(self.keypair,pairfilename) permidmod.save_pub_key(self.keypair,pubfilename) # 2. Downloads persistent state dir dlpstatedir = os.path.join(self.sessconfig['state_dir'],STATEDIR_DLPSTATE_DIR) if not os.path.isdir(dlpstatedir): os.mkdir(dlpstatedir) # 3. tracker trackerdir = self.get_internal_tracker_dir() if not os.path.exists(trackerdir): os.mkdir(trackerdir) if self.sessconfig['tracker_dfile'] is None: self.sessconfig['tracker_dfile'] = os.path.join(trackerdir,'tracker.db') if self.sessconfig['tracker_allowed_dir'] is None: self.sessconfig['tracker_allowed_dir'] = trackerdir if self.sessconfig['tracker_logfile'] is None: if sys.platform == "win32": # Not "Nul:" but "nul" is /dev/null on Win32 sink = 'nul' else: sink = '/dev/null' self.sessconfig['tracker_logfile'] = sink # 4. superpeer.txt and crawler.txt if self.sessconfig['superpeer_file'] is None: self.sessconfig['superpeer_file'] = os.path.join(self.sessconfig['install_dir'],LIBRARYNAME,'Core','superpeer.txt') if 'crawler_file' not in self.sessconfig or self.sessconfig['crawler_file'] is None: self.sessconfig['crawler_file'] = os.path.join(self.sessconfig['install_dir'], LIBRARYNAME,'Core','Statistics','crawler.txt') # 5. peer_icon_path if self.sessconfig['peer_icon_path'] is None: self.sessconfig['peer_icon_path'] = os.path.join(self.sessconfig['state_dir'],STATEDIR_PEERICON_DIR) if not os.path.isdir(self.sessconfig['peer_icon_path']): os.mkdir(self.sessconfig['peer_icon_path']) # 6. Poor man's versioning of SessionConfig, add missing # default values. Really should use PERSISTENTSTATE_CURRENTVERSION # and do conversions. for key,defvalue in sessdefaults.iteritems(): if key not in self.sessconfig: self.sessconfig[key] = defvalue # 7. proxyservice_dir if self.sessconfig['overlay']: #NIELS: proxyservice_on/off is set at runtime, always make sure proxyservice_ and self.sessconfig['proxyservice_status'] == PROXYSERVICE_ON: if self.sessconfig['proxyservice_dir'] is None: self.sessconfig['proxyservice_dir'] = os.path.join(get_default_dest_dir(), PROXYSERVICE_DESTDIR) # Jelle: under linux, default_dest_dir can be /tmp. Then proxyservice_dir can be deleted in between # sessions. if not os.path.isdir(self.sessconfig['proxyservice_dir']): os.makedirs(self.sessconfig['proxyservice_dir']) if not 'live_aux_seeders' in self.sessconfig: # Poor man's versioning, really should update PERSISTENTSTATE_CURRENTVERSION self.sessconfig['live_aux_seeders'] = sessdefaults['live_aux_seeders'] if not 'nat_detect' in self.sessconfig: self.sessconfig['nat_detect'] = sessdefaults['nat_detect'] if not 'puncturing_internal_port' in self.sessconfig: self.sessconfig['puncturing_internal_port'] = sessdefaults['puncturing_internal_port'] if not 'stun_servers' in self.sessconfig: self.sessconfig['stun_servers'] = sessdefaults['stun_servers'] if not 'pingback_servers' in self.sessconfig: self.sessconfig['pingback_servers'] = sessdefaults['pingback_servers'] if not 'mainline_dht' in self.sessconfig: self.sessconfig['mainline_dht'] = sessdefaults['mainline_dht'] # SWIFTPROC if self.sessconfig['swiftpath'] is None: if sys.platform == "win32": self.sessconfig['swiftpath'] = os.path.join(self.sessconfig['install_dir'],"swift.exe") else: self.sessconfig['swiftpath'] = os.path.join(self.sessconfig['install_dir'],"swift") # Checkpoint startup config self.save_pstate_sessconfig() # Create handler for calling back the user via separate threads self.uch = UserCallbackHandler(self) # Create engine with network thread self.lm = TriblerLaunchMany() self.lm.register(self,self.sesslock) self.lm.start()
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