hooks/syncsources.py
branchstable
changeset 7735 71adfd6bab38
parent 7650 278fe9c1f3ad
child 7879 9aae456abab5
equal deleted inserted replaced
7734:59fea81647e5 7735:71adfd6bab38
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    17 # with CubicWeb.  If not, see <http://www.gnu.org/licenses/>.
    18 """hooks for repository sources synchronization"""
    18 """hooks for repository sources synchronization"""
    19 
    19 
    20 from socket import gethostname
    20 from socket import gethostname
    21 
    21 
       
    22 from logilab.common.decorators import clear_cache
    22 from yams.schema import role_name
    23 from yams.schema import role_name
    23 
    24 
    24 from cubicweb import ValidationError
    25 from cubicweb import ValidationError
    25 from cubicweb.selectors import is_instance
    26 from cubicweb.selectors import is_instance
    26 from cubicweb.server import SOURCE_TYPES, hook
    27 from cubicweb.server import SOURCE_TYPES, hook
    64         if self.entity.name == 'system':
    65         if self.entity.name == 'system':
    65             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    66             raise ValidationError(self.entity.eid, {None: 'cant remove system source'})
    66         SourceRemovedOp(self._cw, uri=self.entity.name)
    67         SourceRemovedOp(self._cw, uri=self.entity.name)
    67 
    68 
    68 
    69 
    69 class SourceUpdatedOp(hook.DataOperationMixIn, hook.Operation):
    70 class SourceConfigUpdatedOp(hook.DataOperationMixIn, hook.Operation):
    70 
    71 
    71     def precommit_event(self):
    72     def precommit_event(self):
    72         self.__processed = []
    73         self.__processed = []
    73         for source in self.get_data():
    74         for source in self.get_data():
    74             if not self.session.deleted_in_transaction(source.eid):
    75             if not self.session.deleted_in_transaction(source.eid):
    77 
    78 
    78     def postcommit_event(self):
    79     def postcommit_event(self):
    79         for source, conf in self.__processed:
    80         for source, conf in self.__processed:
    80             source.repo_source.update_config(source, conf)
    81             source.repo_source.update_config(source, conf)
    81 
    82 
       
    83 
       
    84 class SourceRenamedOp(hook.LateOperation):
       
    85 
       
    86     def precommit_event(self):
       
    87         source = self.session.repo.sources_by_uri[self.oldname]
       
    88         if source.copy_based_source:
       
    89             sql = 'UPDATE entities SET asource=%(newname)s WHERE asource=%(oldname)s'
       
    90         else:
       
    91             sql = 'UPDATE entities SET source=%(newname)s, asource=%(newname)s WHERE source=%(oldname)s'
       
    92         self.session.system_sql(sql, {'oldname': self.oldname,
       
    93                                       'newname': self.newname})
       
    94 
       
    95     def postcommit_event(self):
       
    96         repo = self.session.repo
       
    97         # XXX race condition
       
    98         source = repo.sources_by_uri.pop(self.oldname)
       
    99         source.uri = self.newname
       
   100         source.public_config['uri'] = self.newname
       
   101         repo.sources_by_uri[self.newname] = source
       
   102         repo._type_source_cache.clear()
       
   103         clear_cache(repo, 'source_defs')
       
   104         if not source.copy_based_source:
       
   105             repo._extid_cache.clear()
       
   106             repo._clear_planning_caches()
       
   107             for cnxset in repo.cnxsets:
       
   108                 cnxset.source_cnxs[self.oldname] = cnxset.source_cnxs.pop(self.oldname)
       
   109 
       
   110 
    82 class SourceUpdatedHook(SourceHook):
   111 class SourceUpdatedHook(SourceHook):
    83     __regid__ = 'cw.sources.configupdate'
   112     __regid__ = 'cw.sources.configupdate'
    84     __select__ = SourceHook.__select__ & is_instance('CWSource')
   113     __select__ = SourceHook.__select__ & is_instance('CWSource')
    85     events = ('after_update_entity',)
   114     events = ('before_update_entity',)
    86     def __call__(self):
   115     def __call__(self):
    87         if 'config' in self.entity.cw_edited:
   116         if 'config' in self.entity.cw_edited:
    88             SourceUpdatedOp.get_instance(self._cw).add_data(self.entity)
   117             SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity)
       
   118         if 'name' in self.entity.cw_edited:
       
   119             oldname, newname = self.entity.cw_edited.oldnewvalue('name')
       
   120             SourceRenamedOp(self._cw, oldname=oldname, newname=newname)
       
   121 
    89 
   122 
    90 class SourceHostConfigUpdatedHook(SourceHook):
   123 class SourceHostConfigUpdatedHook(SourceHook):
    91     __regid__ = 'cw.sources.hostconfigupdate'
   124     __regid__ = 'cw.sources.hostconfigupdate'
    92     __select__ = SourceHook.__select__ & is_instance('CWSourceHostConfig')
   125     __select__ = SourceHook.__select__ & is_instance('CWSourceHostConfig')
    93     events = ('after_add_entity', 'after_update_entity', 'before_delete_entity',)
   126     events = ('after_add_entity', 'after_update_entity', 'before_delete_entity',)
    95         if self.entity.match(gethostname()):
   128         if self.entity.match(gethostname()):
    96             if self.event == 'after_update_entity' and \
   129             if self.event == 'after_update_entity' and \
    97                    not 'config' in self.entity.cw_edited:
   130                    not 'config' in self.entity.cw_edited:
    98                 return
   131                 return
    99             try:
   132             try:
   100                 SourceUpdatedOp.get_instance(self._cw).add_data(self.entity.cwsource)
   133                 SourceConfigUpdatedOp.get_instance(self._cw).add_data(self.entity.cwsource)
   101             except IndexError:
   134             except IndexError:
   102                 # XXX no source linked to the host config yet
   135                 # XXX no source linked to the host config yet
   103                 pass
   136                 pass
   104 
   137 
   105 
   138