def __enter__(self): """Enter method.""" try: if main_config.db_ssh_forward: forward = sshtunnel.SSHTunnelForwarder( credentials.machine_credentials()['ssh_address'], ssh_username=credentials.machine_credentials()['username'], ssh_password=credentials.machine_credentials()['password'], remote_bind_address=('127.0.0.1', 5432)) forward.start() self.forward = forward self.pgsql_port = forward.local_bind_port else: self.forward = None self.pgsql_port = '' pgsql_string = credentials.postgresql_connection( str(self.pgsql_port)) self.pgsql_string = pgsql_string self.conn = psycopg2.connect(**pgsql_string) self.conn.set_isolation_level( psycopg2.extensions.ISOLATION_LEVEL_AUTOCOMMIT) self.cur = self.conn.cursor( cursor_factory=psycopg2.extras.RealDictCursor) except Exception as e: self.close_db() if main_config.db_ssh_forward: self.forward.close() print(e) return self
from pip.req import parse_requirements install_reqs = parse_requirements('requirements.txt', session='hack') # reqs is a list of requirement # e.g. ['django==1.5.1', 'mezzanine==1.4.6'] reqs = [str(ir.req) for ir in install_reqs] except Exception: print('Failed to import parse_requirements.') # try: # setup( # name="latent_adv", # version="0.0.1", # packages=install_reqs, # ) # except Exception as e: # print( # 'Failed to install requirements and compile repo. ' # 'Try manually installing. %s' % e) config = Config() logger.info('Installed required packages and created paths.') params = credentials.postgresql_connection() sys_password = credentials.machine_credentials()['password'] os.popen('sudo -u postgres createuser -sdlP %s' % params['user'], 'w').write(sys_password) os.popen( 'sudo -u postgres createdb %s -O %s' % (params['database'], params['user']), 'w').write(sys_password) logger.info('Created DB.')