From da69fd81edac89a0eabf13ffda415dd8e22e4d4c Mon Sep 17 00:00:00 2001 From: Frank LENORMAND Date: Mon, 23 Mar 2020 09:02:30 +0300 Subject: [PATCH] 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 Signed-off-by: Nicolas Sebrecht --- offlineimap/accounts.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/offlineimap/accounts.py b/offlineimap/accounts.py index 9d49546..b9819ff 100644 --- a/offlineimap/accounts.py +++ b/offlineimap/accounts.py @@ -333,8 +333,11 @@ class SyncableAccount(Account): folderthreads = [] - hook = self.getconf('presynchook', '') - self.callhook(hook) + hook_env = { + 'OIMAP_ACCOUNT_NAME': self.getname(), + } + + self.callhook('presynchook', hook_env) if self.utf_8_support and self.remoterepos.getdecodefoldernames(): raise OfflineImapError("Configuration mismatch in account " + @@ -438,20 +441,23 @@ class SyncableAccount(Account): localrepos.holdordropconnections() remoterepos.holdordropconnections() - hook = self.getconf('postsynchook', '') - self.callhook(hook) + self.callhook('postsynchook', hook_env) - def callhook(self, cmd): + def callhook(self, name, env={}): # Check for CTRL-C or SIGTERM and run postsynchook. if Account.abort_NOW_signal.is_set(): return + cmd = self.getconf(name, '') if not cmd: return try: self.ui.callhook("Calling hook: " + cmd) if self.dryrun: 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, close_fds=True) r = p.communicate()