def __init__(self, name, modid, version, db_connect, streamsize=1 << 16): self.name = name self.db = psycopg.connect(db_connect) self.streamsize = streamsize self.pos = 0 self.statement = "FROM modules NATURAL JOIN module_files NATURAL JOIN files WHERE moduleid = '%s' AND version = '%s' AND filename = '%s'" % ( modid, version, name)
def db_command(config, command, database='postgres'): '''Perform some sort of database-level command. Retry 10 times if we fail by conflicting with another user. Since PostgreSQL version 8.1 there is a database "postgres", before "template1" seems to habe been used, so we fall back to it. Compare to issue2550543. ''' template1 = connection_dict(config) template1['database'] = database try: conn = psycopg.connect(**template1) except psycopg.OperationalError as message: if str(message).find('database "postgres" does not exist') >= 0: return db_command(config, command, database='template1') raise hyperdb.DatabaseError(message) conn.set_isolation_level(0) cursor = conn.cursor() try: for n in range(10): if pg_command(cursor, command): return finally: conn.close() raise RuntimeError('10 attempts to create database failed')
def sql_open_connection(self): db = connection_dict(self.config, 'database') logging.getLogger('roundup.hyperdb').info( 'open database %r'%db['database']) try: conn = psycopg.connect(**db) except psycopg.OperationalError, message: raise hyperdb.DatabaseError(message)
def user_exists(args): try: con = psycopg.connect(_dsn(args, dbname='template1')) except psycopg.OperationalError, e: if 'does not exist' in str(e): return False else: raise
def db_exists(config): """Check if database already exists""" db = connection_dict(config, 'database') try: conn = psycopg.connect(**db) conn.close() return 1 except: return 0
def database_exists(args): try: con = psycopg.connect(_dsn(args)) except psycopg.OperationalError, e: # Database doesn't exist if str(e) == 'FATAL: database "%s" does not exist\n' % args['dbname']: return False else: raise
def db_exists(config): """Check if database already exists""" db = connection_dict(config, 'database') try: conn = psycopg.connect(**db) conn.close() return 1 except: return 0
def main(): """Parse url, and put the ships into our database.""" config = configparser.ConfigParser() if not config.read('muninrc'): print("Error: Found no configuration file in ./muninrc.", file=sys.stderr) return 1 DSN = "dbname=%s user=%s" % (config.get('Database', 'dbname'), config.get('Database', 'user')) if config.has_option('Database', 'password'): DSN += ' password=%s' % config.get('Database', 'password') if config.has_option('Database', 'host'): DSN += ' host=%s' % config.get('Database', 'host') connection = psycopg.connect(DSN) cursor = connection.cursor() parser = argparse.ArgumentParser(description='Planetarion ship stats importer for Munin.') default_round = config.getint('Planetarion', 'current_round') default_stats = 'https://game.planetarion.com/manual.pl?page=stats' parser.add_argument('-r', '--round', type=int, default=default_round, help="Default: the value of 'Planetarion/current_round' in muninrc (%s)" % (default_round)) parser.add_argument('-u', '--url', default=default_stats, help="Default: %s" % (default_stats)) args = parser.parse_args() useragent = "Munin (Python-urllib/%s); BotNick/%s; Admin/%s" % (urllib.request.__version__, config.get("Connection", "nick"), config.get("Auth", "owner_nick")) cursor.execute("DELETE FROM ship WHERE round=%s;", (args.round,)) req = urllib.request.Request(args.url) req.add_header('User-Agent', useragent) stats = urllib.request.urlopen(req).read().decode() for line in sre.findall(stats): line = list(line) ship = {} 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 ('-', '',): ship[key] = line[index] ship['total_cost'] = ship['metal'] + ship['crystal'] + ship['eonium'] if ship['type'] == 'EMP': ship['type'] = 'Emp' fields = ['round'] params = [args.round] for key in ship: fields.append(key) params.append(ship[key]) query = QUERY % (', '.join(fields), ', '.join(len(params) * ['%s'])) cursor.execute(query, tuple(params)) connection.commit()
def create_conn(self,config): dsn = 'user=%s dbname=%s' % (config.get("Database", "user"), config.get("Database", "dbname")) if config.has_option("Database", "password"): dsn += ' password=%s' % config.get("Database", "password") if config.has_option("Database", "host"): dsn += ' host=%s' % config.get("Database", "host") conn=psycopg.connect(dsn) conn.autocommit(1) return conn
def _dbadminexec(d, execstring, template=False): """Execute a command as the DB admin using arguments from 'd' """ try: if template: dsn = _dsn(d, dbname='template1', user=d['admin'], userpass=d['adminpass']) else: dsn = _dsn(d, user=d['admin'], userpass=d['adminpass']) con = psycopg.connect(dsn) except psycopg.OperationalError, e: # Badadmin Username: Fail back to user raise ValueError, "Unable to connect as supplied DBA admin - %s " %e
def sql_open_connection(self): db = connection_dict(self.config, 'database') logging.getLogger('roundup.hyperdb').info( 'open database %r'%db['database']) try: conn = psycopg.connect(**db) except psycopg.OperationalError as message: raise hyperdb.DatabaseError(message) cursor = conn.cursor() if ISOLATION_LEVEL_REPEATABLE_READ is not None: lvl = isolation_levels [self.config.RDBMS_ISOLATION_LEVEL] conn.set_isolation_level(lvl) return (conn, cursor)
def __init__(self, config, client, munin): # Private variables self.notprefix = r"~|-|\." self.pubprefix = r"!" self.privprefix = "@" self.client = client self.munin = munin self.ctrl_list = {} self.config = config # database variables (also private) self.mod_dir = "mod" self.user = config.get("Database", "user") self.dbname = config.get("Database", "dbname") self.dsn = "user=%s dbname=%s" % (self.user, self.dbname) if config.has_option("Database", "password"): self.dsn += " password=%s" % config.get("Database", "password") if config.has_option("Database", "host"): self.dsn += " host=%s" % config.get("Database", "host") # database connection and cursor self.conn = psycopg.connect(self.dsn) self.conn.set_isolation_level(0) self.cursor = self.conn.cursor() self.galstatus = galstatus.galstatus(self.client, self.conn, self.cursor, config) # Necessary regexps (still private) self.welcomre = re.compile(r"\S+\s+001.*", re.I) # obey P self.pinvitere = re.compile(r"^:[email protected]\s+INVITE\s+\S+\s+:#(\S+)", re.I) # privmsg command regexes self.privmsgre = re.compile(r"^:(\S+)!(\S+)@(\S+)\s+PRIVMSG\s+(\S+)\s+:(.*)") self.ischannelre = re.compile(r"(#\S+)") self.pnickre = re.compile(r"(\S{2,15})\.users\.netgamers\.org") self.reg_controllers() self.commandre = re.compile(r"^(%s|%s|%s)(.*)\s*$" % (self.notprefix, self.pubprefix, self.privprefix)) self.loadmodre = re.compile(r"^loadmod\s+(\S+)") self.helpre = re.compile(r"^help(\s+(\S+))?") self.scanre = re.compile("http://[^/]+/showscan.pl\?scan_id=([0-9a-zA-Z]+)") self.scangrpre = re.compile("http://[^/]+/showscan.pl\?scan_grp=([0-9a-zA-Z]+)")
def main(url="http://game.planetarion.com/manual.pl?page=stats"): """Parse url, and put the ships into our database.""" config = ConfigParser.ConfigParser() if not config.read("muninrc"): print >>sys.stderr, "Error: Found no configuration file in ./muninrc." return 1 DSN = "dbname=%s user=%s" % (config.get("Database", "dbname"), config.get("Database", "user")) if config.has_option("Database", "password"): DSN += " password=%s" % config.get("Database", "password") if config.has_option("Database", "host"): DSN += " host=%s" % config.get("Database", "host") connection = psycopg.connect(DSN) cursor = connection.cursor() useragent = "Munin (Python-urllib/%s); BotNick/%s; Admin/%s" % ( urllib2.__version__, config.get("Connection", "nick"), config.get("Auth", "owner_nick"), ) cursor.execute("DELETE FROM ship;") cursor.execute("SELECT setval('ship_id_seq', 1, false);") req = urllib2.Request(url) req.add_header("User-Agent", useragent) stats = urllib2.urlopen(req).read() for line in sre.findall(stats): line = list(line) ship = {} 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 ("-", ""): ship[key] = line[index] ship["total_cost"] = ship["metal"] + ship["crystal"] + ship["eonium"] fields = [] params = [] for key in ship: fields.append(key) params.append(ship[key]) query = QUERY % (", ".join(fields), ", ".join(len(params) * ["%s"])) cursor.execute(query, tuple(params)) connection.commit()
def db_command(config, command, database='postgres'): '''Perform some sort of database-level command. Retry 10 times if we fail by conflicting with another user. Since PostgreSQL version 8.1 there is a database "postgres", before "template1" seems to habe been used, so we fall back to it. Compare to issue2550543. ''' template1 = connection_dict(config) template1['database'] = database try: conn = psycopg.connect(**template1) except psycopg.OperationalError, message: if str(message).find('database "postgres" does not exist') >= 0: return db_command(config, command, database='template1') raise hyperdb.DatabaseError(message)
def main(url="http://game.planetarion.com/manual.pl?page=stats"): """Parse url, and put the ships into our database.""" config = ConfigParser.ConfigParser() if not config.read('muninrc'): print >> sys.stderr, "Error: Found no configuration file in ./muninrc." return 1 DSN = "dbname=%s user=%s" % (config.get('Database', 'dbname'), config.get('Database', 'user')) if config.has_option('Database', 'password'): DSN += ' password=%s' % config.get('Database', 'password') if config.has_option('Database', 'host'): DSN += ' host=%s' % config.get('Database', 'host') connection = psycopg.connect(DSN) cursor = connection.cursor() cursor.execute("DELETE FROM ship;") cursor.execute("SELECT setval('ship_id_seq', 1, false);") stats = urllib2.urlopen(url).read() for line in sre.findall(stats): line = list(line) ship = {} 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 ('-', '',): ship[key] = line[index] ship['total_cost'] = ship['metal'] + ship['crystal'] + ship['eonium'] fields = [] params = [] for key in ship: fields.append(key) params.append(ship[key]) query = QUERY % (', '.join(fields), ', '.join(len(params) * ['%s'])) cursor.execute(query, tuple(params)) connection.commit()
except: print "Probable duplicate error" con.commit() os.chdir(cwd) print wd.release() def main(): #mods = [m for m in os.listdir(os.getcwd()) if m.startswith('m')] cur=con.cursor() cur.execute("select distinct moduleid from modules lm where not exists (select 1 from module_files where module_ident = lm.module_ident and filename='index.cnxml') order by moduleid") res=cur.fetchall() mods=[r[0] for r in res] print "%s modules to store" % len(mods) for mod in mods: print '%s%% Storing %s' % ((mods.index(mod)*100)/len(mods),mod) saveModule(mod) if __name__ == '__main__': try: instance = '-%s' % sys.argv[1] except IndexError: instance = '' output = sp.Popen(["pg_lsclusters", "-h"], stdout=sp.PIPE).communicate()[0] port = [line.split()[2] for line in output.split('\n') if line.startswith('8.2 main')][0] con = psycopg.connect('dbname=repository%s user=rhaptos port=%s' % (instance,port)) main()
def open_db(self): self.conn = psycopg.connect("host=%s dbname=%s user=%s password=%s" % (self.host, self.db, self.user, self.pwd))
query+=" WHERE prop_id=%s" self.cursor.execute(query,(prop_id,)) voters={} voters['yes']=[] voters['no']=[] voters['abstain']=[] yes=0;no=0 for r in self.cursor.dictfetchall(): if r['vote'] == 'yes': yes+=r['carebears'] voters['yes'].append(r) elif r['vote'] == 'no': no+=r['carebears'] voters['no'].append(r) elif r['vote'] == 'abstain': voters['abstain'].append(r) return (voters, yes, no) user="******" db="patools30" conn=psycopg.connect("user=%s dbname=%s" %(user,db)) conn.serialize() conn.autocommit() curs=conn.cursor() m=migrator(curs) m.add_padding()
import os from psycopg2 import psycopg1 as psycopg import traceback sys.path.insert(0, "custom") import scan t_start=time.time() t1=t_start ofile=file("pid.sleipnir", "w") ofile.write("%s" % (os.getpid(),)) ofile.close() conn=psycopg.connect("dbname=patools27 user=munin") conn.serialize() conn.autocommit() cursor=conn.cursor() query="SELECT rand_id FROM scanparser_queue" cursor.execute(query) result=cursor.dictfetchall() for r in result: rid=str(r['rand_id']) query="SELECT rand_id FROM scan WHERE"
from univ_settings import DATABASE_CONFIG as config from collections import defaultdict import csv from cStringIO import StringIO from ersatzpg.utffile import utffile connstr = [] if config.has_key('host'): connstr.append("host=%s" % config['host']) if config.has_key('port'): connstr.append("port=%s" % config['port']) if config.has_key('sslmode'): connstr.append("sslmode=%s" % config['sslmode']) connstr.append("dbname=%s user=%s password=%s" % (config['db'], config['user'], config['pw'])) connection = psycopg.connect(' '.join(connstr)) cursor = connection.cursor() results = defaultdict(lambda: []) special_terms = [] with utffile('searchterms.csv') as f: for s in f: print s if s.startswith('<'): special_terms.append(s) elif ' ' in s: #cursor.execute("select identifier, sitetext ilike '%{s}%' from sites_ajax_joined;".format(s=s)) cursor.execute( "select uid, class, website, sitevec @@ plainto_tsquery('{s}') from pages_for_class_train_vec;" .format(s=s)) for k, c, w, v in cursor.fetchall(): results[(k, c, w)].append(int(v))
def dbi(): dbi = psycopg.connect("dbname=persdb host=localhost\ user=postgres password=postgres") return dbi
def __connect_to_database(self): self.__conn = psycopg1.connect("host=" + self.__ServerIp + " port=" + self.__ServerPort + " dbname=postgres user=postgres") self.__cur = self.__conn.cursor()
def __init__(self, name, modid, version, db_connect, streamsize=1<<16): self.name = name self.db = psycopg.connect(db_connect) self.streamsize = streamsize self.pos = 0 self.statement = "FROM modules NATURAL JOIN module_files NATURAL JOIN files WHERE moduleid = '%s' AND version = '%s' AND filename = '%s'" % (modid,version,name)
def connect(self, host, database, user, password): return driver.connect("dbname=%s user=%s password=%s" % \ (database, user, password))
def __init__(self,options): self.options=options self.l_schemas=[] self.dbname = options.dbname maketree( self.dbname ) self.dbconn = psycopg.connect('host=%s port=%s dbname=%s user=%s' % \ (self.options.dbhost, self.options.dbport, self.options.dbname, self.options.dbuser)) self.pg_opts= '-h %s -p %s -U %s %s' % (self.options.dbhost, self.options.dbport, self.options.dbuser, self.options.dbname) self.getVersion() self.list_nsp() if self.version == '8.3': self.cr83 = self.getsql("function.body.83") else: self.cr83 = "" if options.tables or options.views or options.all: # prepare the work print "Dump schema and extract pg_restore listing :", self.pg_dump_schema() self.get_restore_list() print "OK" if options.tables or options.all: print "Extract Tables :", for schema in self.l_schemas: self.extract_tables(schema) print "OK" if options.sequences or options.all: print "Extract Sequences :", for schema in self.l_schemas: self.extract_seq(schema) print "OK" if options.views or options.all: print "Extract Views :", for schema in self.l_schemas: self.extract_views(schema) print "OK" if options.functions or options.all: print "Extract Functions :", for schema in self.l_schemas: self.extract_functions(schema) print "OK" if options.triggers or options.all: print "Extract Triggers :", for schema in self.l_schemas: self.extract_triggers(schema) print "OK"
def __init__(self, options, schemas): self.options = options self.l_schemas = schemas self.dbname = options.dbname maketree(self.dbname) self.dbconn = psycopg.connect('host=%s port=%s dbname=%s user=%s' % \ (self.options.dbhost, self.options.dbport, self.options.dbname, self.options.dbuser)) self.pg_opts = '-h %s -p %s -U %s %s' % ( self.options.dbhost, self.options.dbport, self.options.dbuser, self.options.dbname) self.getVersion() self.list_nsp() if self.version == '8.3': self.cr83 = self.getsql("function.body.83") else: self.cr83 = "" if options.tables or options.views or options.all: # prepare the work print "Dump schema and extract pg_restore listing :", self.pg_dump_schema() self.get_restore_list() print "OK" if options.tables or options.all: print "Extract Tables :", for schema in self.l_schemas: print schema, self.extract_tables(schema) print "OK" if options.sequences or options.all: print "Extract Sequences :", for schema in self.l_schemas: print schema, self.extract_seq(schema) print "OK" if options.views or options.all: print "Extract Views :", for schema in self.l_schemas: print schema, self.extract_views(schema) print "OK" if options.functions or options.all: print "Extract Functions :", for schema in self.l_schemas: print schema, self.extract_functions(schema) print "OK" if options.triggers or options.all: print "Extract Triggers :", for schema in self.l_schemas: print schema, self.extract_triggers(schema) print "OK"
import psycopg2.psycopg1 as psycopg from univ_settings import DATABASE_CONFIG as config from collections import defaultdict import csv from cStringIO import StringIO from ersatzpg.utffile import utffile connstr = [] if config.has_key('host'): connstr.append("host=%s" % config['host']) if config.has_key('port'): connstr.append("port=%s" % config['port']) if config.has_key('sslmode'): connstr.append("sslmode=%s" % config['sslmode']) connstr.append("dbname=%s user=%s password=%s" % (config['db'], config['user'], config['pw'])) connection = psycopg.connect(' '.join(connstr)) cursor = connection.cursor() results = defaultdict(lambda:[]) special_terms = [] with utffile('searchterms.csv') as f: for s in f: print s if s.startswith('<'): special_terms.append(s) elif ' ' in s: #cursor.execute("select identifier, sitetext ilike '%{s}%' from sites_ajax_joined;".format(s=s)) cursor.execute("select uid, class, website, sitevec @@ plainto_tsquery('{s}') from pages_for_class_train_vec;".format(s=s)) for k,c,w,v in cursor.fetchall(): results[(k,c,w)].append(int(v)) else: cursor.execute("select uid, class, website, sitevec @@ to_tsquery('{s}') from pages_for_class_train_vec;".format(s=s))
import sys from psycopg2 import psycopg1 as psycopg user = "******" try: old_db = sys.argv[1] new_db = sys.argv[2] except BaseException: print("Usage: %s <old_db> <new_db>" % (sys.argv[0])) sys.exit(0) old_conn = psycopg.connect("user=%s dbname=%s" % (user, old_db)) new_conn = psycopg.connect("user=%s dbname=%s" % (user, new_db)) old_curs = old_conn.cursor() new_curs = new_conn.cursor() old_curs.execute( "SELECT id, pnick,userlevel,alias_nick,sponsor, phone, pubphone, passwd, salt, carebears, available_cookies, last_cookie_date FROM user_list") for u in old_curs.dictfetchall(): new_curs.execute("INSERT INTO user_list (id,pnick,userlevel,alias_nick,sponsor,phone,pubphone,passwd,salt,carebears,available_cookies,last_cookie_date) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)", ( u['id'], u['pnick'], u['userlevel'], u['alias_nick'], u['sponsor'], u['phone'], [False, True][int(u['pubphone'])], u['passwd'], u['salt'], u['carebears'], u['available_cookies'], u['last_cookie_date'])) old_curs.execute("SELECT user_id, friend_id FROM phone")
def connect(self, host, database, user, password): return driver.connect("dbname=%s user=%s password=%s" % \ (database, user, password))
fd.write(f['file']) fd.close() if newfile and not newmod: wd.add(f['filename']) os.chdir(wd.path) if newmod: wd.add(target) wd.commit(message, version) def main(mods_vers): print "%s modules to store" % len(mods_vers) for mod in mods_vers: print '%s%% Storing %s' % ( (mods_vers.index(mod) * 100) / len(mods_vers), mod) saveModule(mod) if __name__ == '__main__': output = sp.Popen(["pg_lsclusters", "-h"], stdout=sp.PIPE).communicate()[0] port = [ line.split()[2] for line in output.split('\n') if line.startswith('8.2 main') ][0] con = psycopg.connect('dbname=repository user=rhaptos port=%s' % (port)) mod_vers = [x.split("/") for x in sys.argv[1:]] main(mod_vers)
def setupDBConnection(self, portal): """Set up the database""" out = StringIO() # Create the Database Connection if not hasattr(portal.aq_base,'_dbopts'): if 'DB_OPTS_TEMP' in portal.aq_parent.objectIds(): portal._dbopts = getattr(portal.aq_parent['DB_OPTS_TEMP'],'_dbopts',DEFAULT_DB_OPTS).copy() portal.aq_parent.manage_delObjects(['DB_OPTS_TEMP']) else: # This an old ZMI which makes upgrading difficult. Attempt to # extract authentication credentials from Z Psycopg connection. if 'devrep' not in portal.aq_parent.objectIds(): raise "No Z Psycoppg Database Connection found" db = portal.aq_parent.devrep # Attempt to parse the connection string. Abort on any error # by letting the exception propagate. items = db.connection_string.split(' ') settings = {} for item in items: k, v = item.split('=') settings[k] = v portal._dbopts = {} portal._dbopts['admin'] = settings.get('user', '') portal._dbopts['adminpass'] = settings.get('password', '') portal._dbopts['user'] = settings.get('user', '') portal._dbopts['userpass'] = settings.get('password', '') portal._dbopts['dbname'] = settings.get('dbname', '') portal._dbopts['server'] = settings.get('server', '') portal._dbopts['port'] = settings.get('port', '') d = portal._dbopts if not user_exists(d): create_user(d) out.write('Created user %(user)s\n' %d) # We now have a regular database username provided if not database_exists(d): create_database(d) out.write('Created database %(dbname)s\n' %d) # We now have an existing database con = psycopg.connect(_dsn(d)) c = con.cursor() c.execute("SELECT 1 FROM pg_class WHERE relname='modules'") if c.rowcount: raise ValueError, "Database populated, not using to avoid dataloss" c.execute("SELECT 1 FROM pg_language WHERE lanname='plpgsql'") if not c.rowcount: install_plpgsql(d) out.write('Install plpgsql in database %(dbname)s\n' %d) #Newest versions have the type, but need the compatability code c.execute("SELECT 1 FROM pg_proc WHERE proname='rank_cd'") if not c.rowcount: ts_location = install_tsearch(d) out.write('Install tsearch in database %s from %s\n' %(d['dbname'], ts_location)) # Finally ready to create the Database Adapter portal.manage_addProduct['ZPsycopgDA'].manage_addZPsycopgConnection(id=d['dbname']+'DA',title='Rhaptos Repository DA',encoding='utf-8',connection_string=_dsn(d), zdatetime=False) out.write('Install Database Adapter in %s for database %s\n' %(portal.Title(), d['dbname']))
config.get("Database", "user")) if config.has_option('Database', 'password'): DSN += ' password=%s' % config.get('Database', 'password') if config.has_option('Database', 'host'): DSN += ' host=%s' % config.get('Database', 'host') t_start=time.time() t1=t_start ofile=file("pid.hugin", "w") ofile.write("%s" % (os.getpid(),)) ofile.close() while True: try: conn=psycopg.connect(DSN) cursor=conn.cursor() cursor.execute("SELECT max_tick()") last_tick=int(cursor.fetchone()[0]) if not last_tick: last_tick = -1 from_web = False if len(sys.argv) == 5: try: planets = open(sys.argv[1], 'r') except Exception, e: print "Failed to open planet listing." print e.__str__()
modfiles=cur.dictfetchall() for f in modfiles: fpath=os.path.join(target,f['filename']) newfile = not(os.path.isfile(fpath)) fd=open(fpath,'w') fd.write(f['file']) fd.close() if newfile and not newmod: wd.add(f['filename']) os.chdir(wd.path) if newmod: wd.add(target) wd.commit(message,version) def main(mods_vers): print "%s modules to store" % len(mods_vers) for mod in mods_vers: print '%s%% Storing %s' % ((mods_vers.index(mod)*100)/len(mods_vers),mod) saveModule(mod) if __name__ == '__main__': output = sp.Popen(["pg_lsclusters", "-h"], stdout=sp.PIPE).communicate()[0] port = [line.split()[2] for line in output.split('\n') if line.startswith('8.2 main')][0] con = psycopg.connect('dbname=repository user=rhaptos port=%s' % (port)) mod_vers=[x.split("/") for x in sys.argv[1:]] main(mod_vers)