server/repository.py
changeset 9113 af6efc15fc90
parent 9105 55738c9dc26f
child 9127 aff75b69db92
equal deleted inserted replaced
9112:c859c7cef346 9113:af6efc15fc90
    34 from warnings import warn
    34 from warnings import warn
    35 from itertools import chain
    35 from itertools import chain
    36 from os.path import join
    36 from os.path import join
    37 from datetime import datetime
    37 from datetime import datetime
    38 from time import time, localtime, strftime
    38 from time import time, localtime, strftime
       
    39 from contextlib import contextmanager
    39 
    40 
    40 from logilab.common.decorators import cached, clear_cache
    41 from logilab.common.decorators import cached, clear_cache
    41 from logilab.common.deprecation import deprecated
    42 from logilab.common.deprecation import deprecated
    42 from logilab.common.compat import any
    43 from logilab.common.compat import any
    43 from logilab.common import flatten
    44 from logilab.common import flatten
   943             if session.timestamp < mintime:
   944             if session.timestamp < mintime:
   944                 self.close(session.id)
   945                 self.close(session.id)
   945                 nbclosed += 1
   946                 nbclosed += 1
   946         return nbclosed
   947         return nbclosed
   947 
   948 
       
   949     @deprecated("[4.0] use internal_cnx now\n"
       
   950                 "(Beware that integrity hook are now enabled by default)")
   948     def internal_session(self, cnxprops=None, safe=False):
   951     def internal_session(self, cnxprops=None, safe=False):
   949         """return a dbapi like connection/cursor using internal user which have
   952         """return a dbapi like connection/cursor using internal user which have
   950         every rights on the repository. The `safe` argument is a boolean flag
   953         every rights on the repository. The `safe` argument is a boolean flag
   951         telling if integrity hooks should be activated or not.
   954         telling if integrity hooks should be activated or not.
   952 
   955 
   955         time where no one is available, causing irremediable freeze...
   958         time where no one is available, causing irremediable freeze...
   956         """
   959         """
   957         session = InternalSession(self, cnxprops, safe)
   960         session = InternalSession(self, cnxprops, safe)
   958         session.set_cnxset()
   961         session.set_cnxset()
   959         return session
   962         return session
       
   963 
       
   964     @contextmanager
       
   965     def internal_cnx(self, safe=True):
       
   966         """return a Connection using internal user which have
       
   967         every rights on the repository. The `safe` argument is a boolean flag
       
   968         telling if integrity hooks should be activated or not.
       
   969 
       
   970         /!\ IN OPPOSITE OF THE OLDER INTERNAL_SESSION, INTERNAL CONNECTION ARE SAFE
       
   971         /!\ BY DEFAULT.
       
   972 
       
   973         This is to be used a context manager.
       
   974         """
       
   975         with InternalSession(self, safe) as session:
       
   976             with session.new_cnx() as cnx:
       
   977                 yield cnx
       
   978 
   960 
   979 
   961     def _get_session(self, sessionid, setcnxset=False, txid=None,
   980     def _get_session(self, sessionid, setcnxset=False, txid=None,
   962                      checkshuttingdown=True):
   981                      checkshuttingdown=True):
   963         """return the user associated to the given session identifier"""
   982         """return the user associated to the given session identifier"""
   964         if checkshuttingdown and self.shutting_down:
   983         if checkshuttingdown and self.shutting_down: