def main(url=Config.get("URL", "ships"), debug=False): stats = urllib2.urlopen(url).read() session.execute(Ship.__table__.delete()) session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ( '-', '', ): setattr(ship, key, line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % ( ship.name, ship.class_, ship.race, ship.type, ) session.add(ship) session.commit() session.close()
def flux_passwd(self, user): if not Config.getboolean("FluxBB", "enabled"): return -1 if session.execute("SELECT username FROM %susers WHERE LOWER(username) LIKE '%s';" % (Config.get("FluxBB", "prefix"), user.name.lower())).rowcount > 0: return session.execute("UPDATE %susers SET password='******' WHERE LOWER(username) LIKE '%s';" % (Config.get("FluxBB", "prefix"), user.passwd, user.name.lower())).rowcount else: group = Config.get("FluxBB", "memgroup") if user.is_member() else Config.get("FluxBB", "galgroup") if group == 0: return -1 return session.execute("INSERT INTO %susers (group_id, username, password, email, title) VALUES ('%s', '%s', '%s', '%s', '%s');" % ( Config.get("FluxBB", "prefix"), group, user.name, user.passwd, user.email, user.level)).rowcount
def loadfromapi(url,rclass, gen_mods=False): # Delete old data session.execute(rclass.__table__.delete()) # Fetch and parse new data req = urllib2.Request(url) req.add_header('User-Agent', useragent) if url[-8:] == "settings": json.load(urllib2.urlopen(req), object_hook=add_setting) add_setting({"timestamp": str(int(time.time()))}) else: json.load(urllib2.urlopen(req), object_hook=hook_factory(rclass, gen_mods)) session.commit()
def main(url=Config.get("URL", "ships")): # Remove old stats session.execute(Ship.__table__.delete()) session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) # Fetch and parse new stats req = urllib2.Request(url) req.add_header('User-Agent', useragent) json.load(urllib2.urlopen(req), object_hook=add_ship) session.commit() session.close()
def main(url = Config.get("URL", "ships"), debug=False): req = urllib2.Request(url) req.add_header('User-Agent', useragent) stats = urllib2.urlopen(req).read() session.execute(Ship.__table__.delete()) if Config.get("DB", "dbms") == "mysql": session.execute(text("ALTER TABLE ships AUTO_INCREMENT=1;", bindparams=[false])) else: session.execute(text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ('-', '',): setattr(ship,key,line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % (ship.name, ship.class_, ship.race, ship.type,) session.add(ship) session.commit() session.close()
def main(url = Config.get("URL", "ships"), debug=False): stats = urllib2.urlopen(url).read() session.execute(Ship.__table__.delete()) session.execute(text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ('-', '',): setattr(ship,key,line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % (ship.name, ship.class_, ship.race, ship.type,) session.add(ship) session.commit() session.close()
def load_planet_id_search(): # If we have any ids in the planet_new_id_search table, # match them up with planet_temp using x,y,z session.execute(text("UPDATE planet_temp SET id = (SELECT id FROM planet_new_id_search WHERE planet_temp.x = planet_new_id_search.x AND planet_temp.y = planet_new_id_search.y AND planet_temp.z = planet_new_id_search.z) WHERE id IS NULL;")) # Empty out the two search tables session.execute(planet_new_id_search.delete()) session.execute(planet_old_id_search.delete()) # Insert from the new tick any planets without id if session.execute(text("INSERT INTO planet_new_id_search (id, x, y, z, race, size, score, value, xp) SELECT id, x, y, z, race, size, score, value, xp FROM planet_temp WHERE planet_temp.id IS NULL;")).rowcount < 1: return None # Insert from the previous tick any planets without # an equivalent planet from the new tick if session.execute(text("INSERT INTO planet_old_id_search (id, x, y, z, race, size, score, value, xp, vdiff) SELECT id, x, y, z, race, size, score, value, xp, vdiff FROM planet WHERE planet.id NOT IN (SELECT id FROM planet_temp WHERE id IS NOT NULL) AND planet.active = :true;", bindparams=[true])).rowcount < 1: return None # If either of the two search tables do not have any planets # to match moved in (.rowcount() < 1) then return None, else: return 1
def main(url=Config.get("URL", "ships"), debug=False): req = urllib2.Request(url) req.add_header('User-Agent', useragent) stats = urllib2.urlopen(req).read() session.execute(Ship.__table__.delete()) if Config.get("DB", "dbms") == "mysql": session.execute( text("ALTER TABLE ships AUTO_INCREMENT=1;", bindparams=[false])) else: session.execute( text("SELECT setval('ships_id_seq', 1, :false);", bindparams=[false])) for line in sre.findall(stats): ship = Ship() line = list(line) for index, key in enumerate(keys): if line[index] in mapping: line[index] = mapping[line[index]] elif line[index].isdigit(): line[index] = int(line[index]) if line[index] not in ( '-', '', ): setattr(ship, key, line[index]) ship.total_cost = ship.metal + ship.crystal + ship.eonium if debug: print "%12s%12s%12s%12s" % ( ship.name, ship.class_, ship.race, ship.type, ) session.add(ship) session.commit() session.close()
if len(sys.argv) > 2 and sys.argv[1] == "--migrate": round = sys.argv[2] if round.isdigit(): round = "r"+round elif len(sys.argv) > 1 and sys.argv[1] == "--new": round = None else: print "To setup a database for a new Merlin install: createdb.py --new" print "To migrate without saving previoud round data: createdb.py --migrate temp" print "To migrate from an old round use: createdb.py --migrate <previous_round>" sys.exit() if round: print "Moving tables to '%s' schema"%(round,) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round,))) except ProgrammingError: print "Oops! Either you don't have permission to modify schemas or you already have a backup called '%s'" % (round,) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel print "Creating schema and tables" try: session.execute(text("CREATE SCHEMA public;"))
if round.isdigit(): round = "r"+round noschema= (len(sys.argv) > 3 and sys.argv[3] == "--noschema") elif len(sys.argv) > 1 and sys.argv[1] == "--new": round = None else: print "To setup a database for a new Merlin install: createdb.py --new" print "To migrate without saving previoud round data: createdb.py --migrate temp" print "To migrate from an old round use: createdb.py --migrate <previous_round>" print "For multiple bots sharing a DB, after the first migration use: createdb.py --migrate <previous_round> --noschema" sys.exit() if round and not mysql and not noschema: print "Moving tables to '%s' schema"%(round,) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round,))) except ProgrammingError: print "Oops! Either you don't have permission to modify schemas or you already have a backup called '%s'" % (round,) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel if not mysql: print "Creating schema and tables" try: session.execute(text("CREATE SCHEMA public;"))
if round.isdigit(): round = "r" + round noschema = (len(sys.argv) > 3 and sys.argv[3] == "--noschema") elif len(sys.argv) > 1 and sys.argv[1] == "--new": round = None else: print "To setup a database for a new Merlin install: createdb.py --new" print "To migrate without saving previoud round data: createdb.py --migrate temp" print "To migrate from an old round use: createdb.py --migrate <previous_round>" print "For multiple bots sharing a DB, after the first migration use: createdb.py --migrate <previous_round> --noschema" sys.exit() if round and not mysql and not noschema: print "Moving tables to '%s' schema" % (round, ) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round, ))) except ProgrammingError: print "Oops! Either you don't have permission to modify schemas or you already have a backup called '%s'" % ( round, ) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel if not mysql: print "Creating schema and tables" try:
def rollback(message): """Rollback to a given tick. Tick must be repeated for confirmation.""" msg = message.get_msg().split()[1:] if len(msg) != 2: message.reply("rollback <tick> <tick>") return if msg[0] != msg[1]: message.reply("Ticks must match!") return if not msg[0].isdigit(): message.reply("Ticks should be numeric.") return if int(msg[0]) > Updates.current_tick(): message.reply( "Timetravel module not installed. Cannot rollback to a future tick." ) return session.execute(text("DELETE FROM updates WHERE id > %s;" % (msg[0]))) session.execute( text("DELETE FROM planet_exiles WHERE tick > %s;" % (msg[0]))) session.execute( text("DELETE FROM planet_idles WHERE tick > %s;" % (msg[0]))) session.execute( text("DELETE FROM planet_value_drops WHERE tick > %s;" % (msg[0]))) session.execute( text("DELETE FROM planet_landings WHERE tick > %s;" % (msg[0]))) session.execute( text("DELETE FROM planet_landed_on WHERE tick > %s;" % (msg[0]))) session.execute( text("DELETE from galaxy_history where tick > %s;" % (msg[0]))) session.execute( text("DELETE from planet_history where tick > %s;" % (msg[0]))) session.execute( text("DELETE from alliance_history where tick > %s;" % (msg[0]))) session.execute( text("DELETE from cluster_history where tick > %s;" % (msg[0]))) session.commit() message.reply("Rollback complete. Bot will update at next tick.")
if len(sys.argv) > 2 and sys.argv[1] == "--migrate": round = sys.argv[2] if round.isdigit(): round = "r" + round elif len(sys.argv) > 1 and sys.argv[1] == "--new": round = None else: print "To setup a database for a new Merlin install: createdb.py --new" print "To migrate without saving previoud round data: createdb.py --migrate temp" print "To migrate from an old round use: createdb.py --migrate <previous_round>" sys.exit() if round: print "Moving tables to '%s' schema" % (round, ) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round, ))) except ProgrammingError: print "Oops! Either you don't have permission to modify schemas or you already have a backup called '%s'" % ( round, ) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel print "Creating schema and tables" try:
def rollback(message): """Rollback to a given tick. Tick must be repeated for confirmation.""" msg = message.get_msg().split()[1:] if len(msg) != 2: message.reply("rollback <tick> <tick>") return if msg[0] != msg[1]: message.reply("Ticks must match!") return if not msg[0].isdigit(): message.reply("Ticks should be numeric.") return if int(msg[0]) > Updates.current_tick(): message.reply("Timetravel module not installed. Cannot rollback to a future tick.") return session.execute(text("DELETE FROM updates WHERE id > %s;" % (msg[0]))) session.execute(text("DELETE FROM planet_exiles WHERE tick > %s;" % (msg[0]))) session.execute(text("DELETE FROM planet_idles WHERE tick > %s;" % (msg[0]))) session.execute(text("DELETE FROM planet_value_drops WHERE tick > %s;" % (msg[0]))) session.execute(text("DELETE FROM planet_landings WHERE tick > %s;" % (msg[0]))) session.execute(text("DELETE FROM planet_landed_on WHERE tick > %s;" % (msg[0]))) session.execute(text("DELETE from galaxy_history where tick > %s;" % (msg[0]))) session.execute(text("DELETE from planet_history where tick > %s;" % (msg[0]))) session.execute(text("DELETE from alliance_history where tick > %s;" % (msg[0]))) session.execute(text("DELETE from cluster_history where tick > %s;" % (msg[0]))) session.commit() message.reply("Rollback complete. Bot will update at next tick.")
if not (planet_tick == galaxy_tick == alliance_tick): print "Varying ticks found, sleeping" print "Planet: %s, Galaxy: %s, Alliance: %s" % (planet_tick, galaxy_tick, alliance_tick) time.sleep(30) continue if not planet_tick > last_tick: print "Stale ticks found, sleeping" time.sleep(60) continue t2 = time.time() - t1 print "Loaded dumps from webserver in %.3f seconds" % (t2,) t1 = time.time() # Empty out the temp tables session.execute(galaxy_temp.delete()) session.execute(planet_temp.delete()) session.execute(alliance_temp.delete()) # Insert the data to the temporary tables, some DBMS do not support # multiple row insert in the same statement so we have to do it one at # a time which is a bit slow unfortunatly # Previously got around this with: # INSERT INTO .. row UNION row UNION row... # Some DBMS complained the resultant query was too long for the planet # dumps, so back at one row per statement planet_insert = "INSERT INTO planet_temp (x, y, z, planetname, rulername, race, size, score, value, xp) " planet_insert += "VALUES (%s, %s, %s, '%s', '%s', '%s', %s, %s, %s, %s);" for line in planets: p = line.strip().split("\t") session.execute(
from Core.config import Config from Core.db import Base, session import shipstats if len(sys.argv) > 2 and sys.argv[1] == "--migrate": round = sys.argv[2] if round.isdigit(): round = "r" + round else: round = None print "To migrate from an old round use: createdb.py --migrate <previous_round>" if round: print "Moving tables to '%s' schema" % (round,) try: session.execute(text("ALTER SCHEMA public RENAME TO %s;" % (round,))) except ProgrammingError: print "Oops! It looks like you already have a backup called '%s'" % (round,) session.rollback() sys.exit() else: session.commit() finally: session.close() print "Importing database models" from Core.maps import Channel print "Creating schema and tables" try: session.execute(text("CREATE SCHEMA public;"))