def main(): JoomlaSessionMaker = connect.connect('joomla', 'joomla', 'mysql.teknolog.fi', 'joomla', 'mysql+oursql', create_metadata=False) joomlasession = JoomlaSessionMaker() RegistrySessionMaker = connect.connect('members', 'members', 'postgre.teknolog.fi', 'members') registrysession = RegistrySessionMaker() sync_databases(joomlasession, registrysession) registrysession.commit() joomlasession.commit() return 0
def main(): JoomlaSessionMaker = connect.connect('joomla', 'joomla', 'mimer.teknolog.fi', 'joomla', 'mysql+oursql', create_metadata=False) joomlasession = JoomlaSessionMaker() RegistrySessionMaker = connect.connect('members', 'members', 'mimer.teknolog.fi', 'members') registrysession = RegistrySessionMaker() sync_databases(joomlasession, registrysession) registrysession.commit() joomlasession.commit() return 0
def main(): SessionMaker = connect.connect('members', 'members', 'localhost', 'members') session = SessionMaker() alreadyimported = [] # uid from email with cc.hut.fi # fetch users without username and cc.hut.fi email no_username = session.query(Member).filter(Member.username_fld == None) from_email = no_username.join(ContactInformation).filter( ContactInformation.email_fld.like('%cc.hut.fi')).all() for member in from_email: username = member.contactinfo.email_fld.split('@')[0] alreadyimported.append(username) set_username(member, username) # uid from ldapsearch query stored in csv_file. print("Already imported: alreadyimported") not_found = [] conflictingset = set() if len(sys.argv) > 1: reader = csv.reader(open(sys.argv[1], 'r')) for cn, uid in reader: if uid not in alreadyimported: firstname, surname = split_ldap_common_name(cn) try: member = get_member_with_real_name(firstname, surname, session) set_username(member, uid) except PersonNotFoundException: not_found.append((cn, uid)) except DuplicateNamesException as e: conflictingset.add(tuple(e.query.all())) print("NOT FOUND" + '#' * 70) writer = csv.writer(open('not_found.csv', 'w')) for cn, uid in not_found: writer.writerow((cn, uid)) print(cn, uid) print("CONFLICTS" + '#' * 70) for conflicts in conflictingset: for conflictingmember in conflicts: print(conflictingmember.getWholeName()) print("-" * 80) if input("commit? [y/N]").lower() in ('y', 'yes'): print("Committing...") session.commit() return 0
def main2(): filename = "test.csv" if len(sys.argv) > 1: filename = sys.argv[1] SessionMaker = connect.connect("members", "members", "localhost", "members") session = SessionMaker() # add_spexposts(session) add_posts(filename, session)
def main(): SessionMaker = connect.connect('members', 'members', 'localhost', 'members') session= SessionMaker() phuxes = common.get_members_with_current_membership_id(session, "Phux") departmentdict = {} for phux in phuxes: try: departmentdict.setdefault(phux.department[0].department.name_fld, []).append(phux.contactinfo.email_fld) except IndexError: # No phuxes continue for department, emails in departmentdict.items(): print(department) for email in emails: print(email) print()
def main(): SessionMaker = connect.connect('members', 'members', 'localhost', 'members') session = SessionMaker() memberdictlist = csvimporter.parse_csv(sys.argv[1], ',', '"') for memberdict in memberdictlist: new_member = create_phux(session) csvimporter.update_contact_info(new_member, memberdict, dry_run = False) birthdate = memberdict["birthDate_fld_format"] if birthdate: datelist = [int(s) for s in birthdate.split('.')] datelist.reverse() new_member.birthDate_fld = datetime.datetime(*datelist) if memberdict["subscribedtomodulen_fld_format"]: new_member.subscribedtomodulen_fld = 0; departmentstr = memberdict["department_format"] if departmentstr: department = get_department(session, DEPARTMENTS[int(departmentstr) - 1]) new_departmentmship = create_row(DepartmentMembership, session) new_departmentmship.member = new_member new_departmentmship.department = department new_departmentmship.startTime_fld = datetime.datetime(day=29, month=8, year=int(memberdict["registeredYear"])) session.add(new_departmentmship) genderstr = memberdict["gender_fld_format"] if genderstr: new_member.gender_fld = int(genderstr) print("Dirty: ", session.dirty) print("New: ", session.new) session.commit()
def main(): SessionMaker = connect.connect('members', 'members', 'localhost', 'members') session = SessionMaker() memberdictlist = csvimporter.parse_csv(sys.argv[1], ',', '"') for memberdict in memberdictlist: new_member = create_phux(session) csvimporter.update_contact_info(new_member, memberdict, dry_run=False) birthdate = memberdict["birthDate_fld_format"] if birthdate: datelist = [int(s) for s in birthdate.split('.')] datelist.reverse() new_member.birthDate_fld = datetime.datetime(*datelist) if memberdict["subscribedtomodulen_fld_format"]: new_member.subscribedtomodulen_fld = 0 departmentstr = memberdict["department_format"] if departmentstr: department = get_department(session, DEPARTMENTS[int(departmentstr) - 1]) new_departmentmship = create_row(DepartmentMembership, session) new_departmentmship.member = new_member new_departmentmship.department = department new_departmentmship.startTime_fld = datetime.datetime( day=29, month=8, year=int(memberdict["registeredYear"])) session.add(new_departmentmship) genderstr = memberdict["gender_fld_format"] if genderstr: new_member.gender_fld = int(genderstr) print("Dirty: ", session.dirty) print("New: ", session.new) session.commit()
def main(): filename = "test.csv" if len(sys.argv) > 1: filename = sys.argv[1] SessionMaker = connect.connect("members", "members", "localhost", "members") session = SessionMaker() member_dict_pairs, notfoundlist, conflictingset = import_csv(filename, session) toolongvalues = [] try: [update_contact_info(member, memberdict, dry_run=False) for member, memberdict in member_dict_pairs] except TooLongValueException as e: toolongvalues.append(str(e)) print_notfound_and_conflicts(notfoundlist, conflictingset) print("Too long values:" + "#" * 60) print(toolongvalues) for toolong in toolongvalues: print(toolong) # session.commit() return 0
def main(): SessionMaker = connect.connect('members', 'members', 'localhost', 'members') session = SessionMaker() memberdictlist = csvimporter.parse_csv(sys.argv[1], ',', None) for memberdict in memberdictlist: memberquery = session.query(Member).filter(Member.surName_fld == memberdict["Efternamn"]) birthdatestr = "" try: birthdatestr = memberdict["födelsedatum"] except KeyError: pass if birthdatestr: datelist = [int(s) for s in birthdatestr.split('.')] datelist.reverse() birthdate = datetime.datetime(*datelist) memberquery = memberquery.filter(Member.birthDate_fld == birthdate) else: memberquery = memberquery.join( ContactInformation).filter( ContactInformation.phone_fld == memberdict["Telefon"]) try: member = memberquery.one() member.contactinfo.email_fld = memberdict["e-post"] except sqlalchemy.orm.exc.NoResultFound: print("Not found: ", memberquery) session.commit()
def connect_with_config(self, configsection): usernameparameter = "dbusername" host = self.configparser.get(configsection, "host") port = int(self.configparser.get(configsection, "port")) database = self.configparser.get(configsection, "database") dbtype = self.configparser.get(configsection, "dbtype") dbusername = self.configparser.get(configsection, usernameparameter) dbpassword = self.get_password( configsection, usernameparameter, dbusername) or "" # Can be None which doesn't store. create_metadata = bool( self.configparser.get(configsection, "create_metadata")) sshconnecting = False threadfinishedmutex = thread.allocate_lock() while True: try: SessionMaker = connect.connect(dbusername, dbpassword, host + ":" + str(port), database, dbtype, create_metadata) self.store_credentials(self.configfile, configsection, usernameparameter, dbusername, dbpassword) return SessionMaker except sqlalchemy.exc.OperationalError as e: print(repr(e)) if re.match( "(.*role .* does not exist.*)|" + "(.*password authentication failed for user.*)|" + "(.*no password supplied.*)", repr(e)): dbusername, dbpassword = self.askcredentialsfunc( "Fel inloggningsinformation för databasen!") elif re.match( "(.*no pg_hba.conf entry for host.*)|" + "(.*pg_hba.conf rejects connection for host.*)", repr(e)): # Test if port is open s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("localhost", int(port))) s.shutdown(2) print("Nätverksporten är redan öppen. Testar om" + " portforwarding är redan igång...") except: # Port not open. Commence SSH port forwarding. print("Du måste logga in på servern.") authfunc = lambda user, passwd: sshwrapper.connect_ssh( host, user, passwd) sshport = int( self.configparser.get(configsection, "sshport")) if (sshport): print("Using ssh port:", sshport) authfunc = lambda user, passwd: ( sshwrapper.connect_ssh(host, user, passwd, sshport)) client = self.askcredentials(authfunc, configsection, "sshusername")[0] thread.start_new_thread( lambda: sshwrapper.portforward( client, threadfinishedmutex, 'localhost', port, port), ()) sshconnecting = True host = "localhost" elif re.match( ".*could not connect to server: Connection refused.*", repr(e)) and sshconnecting: time.sleep(1) if threadfinishedmutex.locked(): # SSH-thread exited threadfinishedmutex.release() raise UnsuccesfulPortforward() else: raise (e)
def connect_with_config(self, configsection): usernameparameter = "dbusername" host = self.configparser.get(configsection, "host") port = int(self.configparser.get(configsection, "port")) database = self.configparser.get(configsection, "database") dbtype = self.configparser.get(configsection, "dbtype") dbusername = self.configparser.get(configsection, usernameparameter) dbpassword = self.get_password(configsection, usernameparameter, dbusername) or "" # Can be None which doesn't store. create_metadata = bool(self.configparser.get(configsection, "create_metadata")) sshconnecting = False threadfinishedmutex = thread.allocate_lock() while True: try: SessionMaker = connect.connect(dbusername, dbpassword, host+":"+str(port), database, dbtype, create_metadata) self.store_credentials(self.configfile, configsection, usernameparameter, dbusername, dbpassword) return SessionMaker except sqlalchemy.exc.OperationalError as e: print(repr(e)) if re.match( "(.*role .* does not exist.*)|" + "(.*password authentication failed for user.*)|" + "(.*no password supplied.*)", repr(e)): dbusername, dbpassword = self.askcredentialsfunc( "Fel inloggningsinformation för databasen!") elif re.match("(.*no pg_hba.conf entry for host.*)|" + "(.*pg_hba.conf rejects connection for host.*)", repr(e)): # Test if port is open s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) try: s.connect(("localhost", int(port))) s.shutdown(2) print("Nätverksporten är redan öppen. Testar om" + " portforwarding är redan igång...") except: # Port not open. Commence SSH port forwarding. print("Du måste logga in på servern.") authfunc = lambda user, passwd: sshwrapper.connect_ssh( host, user, passwd) sshport = int(self.configparser.get( configsection, "sshport")) if (sshport): print("Using ssh port:", sshport) authfunc = lambda user, passwd: ( sshwrapper.connect_ssh( host, user, passwd, sshport)) client = self.askcredentials( authfunc, configsection, "sshusername")[0] thread.start_new_thread( lambda: sshwrapper.portforward( client, threadfinishedmutex, 'localhost', port, port), ()) sshconnecting = True host = "localhost" elif re.match(".*could not connect to server: Connection refused.*", repr(e)) and sshconnecting: time.sleep(1) if threadfinishedmutex.locked(): # SSH-thread exited threadfinishedmutex.release() raise UnsuccesfulPortforward() else: raise(e)