export env. variables when running account hooks
This commit allows account hooks (pre/post sync) to access contextual information: * `OIMAP_ACCOUNT_NAME`: name of the account being synchronized * `OIMAP_HOOK_NAME`: name of the hook being run (one of `postsynchook`, `presynchook`) These variables allow using the same hook program (e.g. a script) for all account synchronisation operations, and running different commands depending on the stage of the synchronisation or the target account. Signed-off-by: Frank LENORMAND <lenormf@gmail.com> Signed-off-by: Nicolas Sebrecht <nicolas.s-dev@laposte.net>
This commit is contained in:
parent
4562b1c5d1
commit
da69fd81ed
|
@ -333,8 +333,11 @@ class SyncableAccount(Account):
|
||||||
|
|
||||||
folderthreads = []
|
folderthreads = []
|
||||||
|
|
||||||
hook = self.getconf('presynchook', '')
|
hook_env = {
|
||||||
self.callhook(hook)
|
'OIMAP_ACCOUNT_NAME': self.getname(),
|
||||||
|
}
|
||||||
|
|
||||||
|
self.callhook('presynchook', hook_env)
|
||||||
|
|
||||||
if self.utf_8_support and self.remoterepos.getdecodefoldernames():
|
if self.utf_8_support and self.remoterepos.getdecodefoldernames():
|
||||||
raise OfflineImapError("Configuration mismatch in account " +
|
raise OfflineImapError("Configuration mismatch in account " +
|
||||||
|
@ -438,20 +441,23 @@ class SyncableAccount(Account):
|
||||||
localrepos.holdordropconnections()
|
localrepos.holdordropconnections()
|
||||||
remoterepos.holdordropconnections()
|
remoterepos.holdordropconnections()
|
||||||
|
|
||||||
hook = self.getconf('postsynchook', '')
|
self.callhook('postsynchook', hook_env)
|
||||||
self.callhook(hook)
|
|
||||||
|
|
||||||
def callhook(self, cmd):
|
def callhook(self, name, env={}):
|
||||||
# Check for CTRL-C or SIGTERM and run postsynchook.
|
# Check for CTRL-C or SIGTERM and run postsynchook.
|
||||||
if Account.abort_NOW_signal.is_set():
|
if Account.abort_NOW_signal.is_set():
|
||||||
return
|
return
|
||||||
|
cmd = self.getconf(name, '')
|
||||||
if not cmd:
|
if not cmd:
|
||||||
return
|
return
|
||||||
try:
|
try:
|
||||||
self.ui.callhook("Calling hook: " + cmd)
|
self.ui.callhook("Calling hook: " + cmd)
|
||||||
if self.dryrun:
|
if self.dryrun:
|
||||||
return
|
return
|
||||||
p = Popen(cmd, shell=True,
|
env = env.copy()
|
||||||
|
env.update(os.environ)
|
||||||
|
env['OIMAP_HOOK_NAME'] = name
|
||||||
|
p = Popen(cmd, shell=True, env=env,
|
||||||
stdin=PIPE, stdout=PIPE, stderr=PIPE,
|
stdin=PIPE, stdout=PIPE, stderr=PIPE,
|
||||||
close_fds=True)
|
close_fds=True)
|
||||||
r = p.communicate()
|
r = p.communicate()
|
||||||
|
|
Loading…
Reference in New Issue