def siteDownload(self, address): from Site.Site import Site from Site import SiteManager SiteManager.site_manager.load() logging.info("Opening a simple connection server") global file_server from File import FileServer file_server = FileServer("127.0.0.1", 1234) file_server_thread = gevent.spawn(file_server.start, check_sites=False) site = Site(address) on_completed = gevent.event.AsyncResult() def onComplete(evt): evt.set(True) site.onComplete.once(lambda: onComplete(on_completed)) print("Announcing...") site.announce() s = time.time() print("Downloading...") site.downloadContent("content.json", check_modifications=True) print("Downloaded in %.3fs" % (time.time()-s))
def site(request): threads_before = [obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet)] # Reset ratelimit RateLimit.queue_db = {} RateLimit.called_db = {} site = Site("1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT") # Always use original data assert "1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT" in site.storage.getPath("") # Make sure we dont delete everything shutil.rmtree(site.storage.getPath(""), True) shutil.copytree(site.storage.getPath("") + "-original", site.storage.getPath("")) # Add to site manager SiteManager.site_manager.get("1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT") site.announce = mock.MagicMock(return_value=True) # Don't try to find peers from the net def cleanup(): site.delete() site.content_manager.contents.db.close("Test cleanup") site.content_manager.contents.db.timer_check_optional.kill() SiteManager.site_manager.sites.clear() db_path = "%s/content.db" % config.data_dir os.unlink(db_path) del ContentDb.content_dbs[db_path] gevent.killall([obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet) and obj not in threads_before]) request.addfinalizer(cleanup) site.greenlet_manager.stopGreenlets() site = Site("1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT") # Create new Site object to load content.json files if not SiteManager.site_manager.sites: SiteManager.site_manager.sites = {} SiteManager.site_manager.sites["1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT"] = site site.settings["serving"] = True return site
def site_temp(request): threads_before = [ obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet) ] with mock.patch("Config.config.data_dir", config.data_dir + "-temp"): site_temp = Site("1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT") site_temp.settings["serving"] = True site_temp.announce = mock.MagicMock( return_value=True) # Don't try to find peers from the net def cleanup(): site_temp.delete() site_temp.content_manager.contents.db.close("Test cleanup") site_temp.content_manager.contents.db.timer_check_optional.kill() db_path = "%s-temp/content.db" % config.data_dir os.unlink(db_path) del ContentDb.content_dbs[db_path] gevent.killall([ obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet) and obj not in threads_before ]) request.addfinalizer(cleanup) site_temp.log = logging.getLogger("Temp:%s" % site_temp.address_short) return site_temp
def siteAnnounce(self, address): from Site.Site import Site from Site import SiteManager SiteManager.site_manager.load() logging.info("Opening a simple connection server") global file_server from File import FileServer file_server = FileServer("127.0.0.1", 1234) file_server.start() logging.info("Announcing site %s to tracker..." % address) site = Site(address) s = time.time() site.announce() print("Response time: %.3fs" % (time.time() - s)) print(site.peers)
def siteNeedFile(self, address, inner_path): from Site.Site import Site from Site import SiteManager SiteManager.site_manager.load() def checker(): while 1: s = time.time() time.sleep(1) print("Switch time:", time.time() - s) gevent.spawn(checker) logging.info("Opening a simple connection server") global file_server from File import FileServer file_server = FileServer("127.0.0.1", 1234) file_server_thread = gevent.spawn(file_server.start, check_sites=False) site = Site(address) site.announce() print(site.needFile(inner_path, update=True))
def site_temp(request): threads_before = [ obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet) ] with mock.patch("Config.config.data_dir", config.data_dir + "-temp"): site_temp = Site("1TeSTvb4w2PWE81S2rEELgmX2GCCExQGT") site_temp.announce = mock.MagicMock( return_value=True) # Don't try to find peers from the net def cleanup(): site_temp.storage.deleteFiles() site_temp.content_manager.contents.db.deleteSite(site_temp) site_temp.content_manager.contents.db.close() time.sleep(0.01) # Wait for db close db_path = "%s-temp/content.db" % config.data_dir os.unlink(db_path) del ContentDb.content_dbs[db_path] gevent.killall([ obj for obj in gc.get_objects() if isinstance(obj, gevent.Greenlet) and obj not in threads_before ]) request.addfinalizer(cleanup) return site_temp