devtools/__init__.py
changeset 10439 45e18b4a7466
parent 10438 f27a489994e8
child 10440 eecb7bbb6795
equal deleted inserted replaced
10438:f27a489994e8 10439:45e18b4a7466
   525         return getattr(self.base_handler, attrname)
   525         return getattr(self.base_handler, attrname)
   526 
   526 
   527 
   527 
   528 ### postgres test database handling ############################################
   528 ### postgres test database handling ############################################
   529 
   529 
   530 def _startpgcluster(datadir):
   530 def startpgcluster(pyfile):
   531     """Start a postgresql cluster using datadir and a random port number"""
   531     """Start a postgresql cluster next to pyfile and using a random port number"""
       
   532     datadir = join(os.path.dirname(pyfile), 'data',
       
   533                    'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0])
   532     if not exists(datadir):
   534     if not exists(datadir):
   533         try:
   535         try:
   534             subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C'])
   536             subprocess.check_call(['initdb', '-D', datadir, '-E', 'utf-8', '--locale=C'])
   535 
   537 
   536         except OSError, err:
   538         except OSError, err:
   557                           'You should add the postgresql bin folder to your PATH '
   559                           'You should add the postgresql bin folder to your PATH '
   558                           '(/usr/lib/postgresql/9.1/bin for example).')
   560                           '(/usr/lib/postgresql/9.1/bin for example).')
   559         raise
   561         raise
   560 
   562 
   561 
   563 
   562 def _stoppgcluster(datadir):
   564 def stoppgcluster(pyfile):
   563     """Kill the postgresql cluster running in datadir"""
   565     """Kill the postgresql cluster running next to pyfile"""
       
   566     datadir = join(os.path.dirname(pyfile), 'data',
       
   567                    'pgdb-%s' % os.path.splitext(os.path.basename(pyfile))[0])
   564     subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast'])
   568     subprocess.call(['pg_ctl', 'stop', '-D', datadir, '-m', 'fast'])
   565 
   569 
   566 
   570 
   567 class PostgresTestDataBaseHandler(TestDataBaseHandler):
   571 class PostgresTestDataBaseHandler(TestDataBaseHandler):
   568     DRIVER = 'postgres'
   572     DRIVER = 'postgres'
   571     db_cache = {}
   575     db_cache = {}
   572     explored_glob = set()
   576     explored_glob = set()
   573 
   577 
   574     __CTL = set()
   578     __CTL = set()
   575 
   579 
   576     @classmethod
       
   577     def killall(cls):
       
   578         for datadir in cls.__CTL:
       
   579             _stoppgcluster(datadir)
       
   580 
       
   581     def __init__(self, *args, **kwargs):
   580     def __init__(self, *args, **kwargs):
   582         super(PostgresTestDataBaseHandler, self).__init__(*args, **kwargs)
   581         super(PostgresTestDataBaseHandler, self).__init__(*args, **kwargs)
   583         datadir = realpath(join(self.config.apphome, 'pgdb'))
       
   584         if datadir in self.__CTL:
       
   585             return
       
   586         _startpgcluster(datadir)
       
   587         self.__CTL.add(datadir)
       
   588         if 'global-db-name' not in self.system_source:
   582         if 'global-db-name' not in self.system_source:
   589             self.system_source['global-db-name'] = self.system_source['db-name']
   583             self.system_source['global-db-name'] = self.system_source['db-name']
   590             self.system_source['db-name'] = self.system_source['db-name'] + str(os.getpid())
   584             self.system_source['db-name'] = self.system_source['db-name'] + str(os.getpid())
   591 
   585 
   592     @property
   586     @property
   808         init_repository(self.config, interactive=False,
   802         init_repository(self.config, interactive=False,
   809                         init_config=self.init_config)
   803                         init_config=self.init_config)
   810 
   804 
   811 import atexit
   805 import atexit
   812 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
   806 atexit.register(SQLiteTestDataBaseHandler._cleanup_all_tmpdb)
   813 atexit.register(PostgresTestDataBaseHandler.killall)
       
   814 
   807 
   815 
   808 
   816 def install_sqlite_patch(querier):
   809 def install_sqlite_patch(querier):
   817     """This patch hotfixes the following sqlite bug :
   810     """This patch hotfixes the following sqlite bug :
   818        - http://www.sqlite.org/cvstrac/tktview?tn=1327,33
   811        - http://www.sqlite.org/cvstrac/tktview?tn=1327,33