Skip to content

Commit

Permalink
call commenceShutdown(None, None) and some formatting cosmetics
Browse files Browse the repository at this point in the history
  • Loading branch information
stroeder committed Jul 17, 2016
1 parent 6ef1fb2 commit 02d3119
Showing 1 changed file with 27 additions and 15 deletions.
42 changes: 27 additions & 15 deletions Demo/pyasn1/syncrepl.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,27 +15,28 @@
python-ldap 2.4.10+
"""

# Import modules from Python standard lib
import shelve,signal,time,sys,logging

# Import the python-ldap modules
import ldap,ldapurl
import ldap
import ldapurl
# Import specific classes from python-ldap
from ldap.ldapobject import ReconnectLDAPObject
from ldap.syncrepl import SyncreplConsumer

# Import modules from Python standard lib
import shelve,signal,time,sys,logging


# Global state
watcher_running = True
ldap_connection = False


class SyncReplConsumer(ReconnectLDAPObject,SyncreplConsumer):
class SyncReplConsumer(ReconnectLDAPObject, SyncreplConsumer):
"""
Syncrepl Consumer interface
"""

def __init__(self,db_path,*args,**kwargs):
def __init__(self, db_path, *args, **kwargs):
# Initialise the LDAP Connection first
ldap.ldapobject.ReconnectLDAPObject.__init__(self, *args, **kwargs)
# Now prepare the data store
Expand All @@ -55,19 +56,22 @@ def syncrepl_set_cookie(self,cookie):
self.__data['cookie'] = cookie

def syncrepl_entry(self,dn,attributes,uuid):
# First we determine the type of change we have here (and store away the previous data for later if needed)
# First we determine the type of change we have here
# (and store away the previous data for later if needed)
previous_attributes = dict()
if uuid in self.__data:
change_type = 'modify'
previous_attributes = self.__data[uuid]
else:
change_type = 'add'
# Now we store our knowledge of the existence of this entry (including the DN as an attribute for convenience)
# Now we store our knowledge of the existence of this entry
# (including the DN as an attribute for convenience)
attributes['dn'] = dn
self.__data[uuid] = attributes
# Debugging
print 'Detected', change_type, 'of entry:', dn
# If we have a cookie then this is not our first time being run, so it must be a change
# If we have a cookie then this is not our first time being run,
# so it must be a change
if 'ldap_cookie' in self.__data:
self.perform_application_sync(dn, attributes, previous_attributes)

Expand All @@ -80,11 +84,18 @@ def syncrepl_delete(self,uuids):
del self.__data[uuid]

def syncrepl_present(self,uuids,refreshDeletes=False):
# If we have not been given any UUID values, then we have recieved all the present controls...
# If we have not been given any UUID values,
# then we have recieved all the present controls...
if uuids is None:
# We only do things if refreshDeletes is false as the syncrepl extension will call syncrepl_delete instead when it detects a delete notice
# We only do things if refreshDeletes is false as the syncrepl
# extension will call syncrepl_delete instead when it detects a
# delete notice
if refreshDeletes is False:
deletedEntries = [uuid for uuid in self.__data.keys() if uuid not in self.__presentUUIDs and uuid != 'ldap_cookie']
deletedEntries = [
uuid
for uuid in self.__data.keys()
if uuid not in self.__presentUUIDs and uuid != 'ldap_cookie'
]
self.syncrepl_delete( deletedEntries )
# Phase is now completed, reset the list
self.__presentUUIDs = {}
Expand Down Expand Up @@ -113,6 +124,7 @@ def commenceShutdown(signum, stack):
# Tear down the server connection
if( ldap_connection ):
ldap_connection.close_db()
ldap_connection.unbind_s()
del ldap_connection

# Shutdown
Expand Down Expand Up @@ -144,11 +156,11 @@ def commenceShutdown(signum, stack):
while watcher_running:
print 'Connecting to LDAP server now...'
# Prepare the LDAP server connection (triggers the connection as well)
ldap_connection = SyncReplConsumer(database_path,ldap_url.initializeUrl())
ldap_connection = SyncReplConsumer(database_path, ldap_url.initializeUrl())

# Now we login to the LDAP server
try:
ldap_connection.simple_bind_s(ldap_url.who,ldap_url.cred)
ldap_connection.simple_bind_s(ldap_url.who, ldap_url.cred)
except ldap.INVALID_CREDENTIALS, e:
print 'Login to LDAP server failed: ', str(e)
sys.exit(1)
Expand All @@ -172,7 +184,7 @@ def commenceShutdown(signum, stack):
pass
except KeyboardInterrupt:
# User asked to exit
commenceShutdown()
commenceShutdown(None, None)
pass
except Exception, e:
# Handle any exception
Expand Down

0 comments on commit 02d3119

Please sign in to comment.