Implement dry-run on Account() level

1) Set attribute self.dryrun depending on whether we are in dry-run mode.
2) Don't actually call hooks in --dry-run (just log what you would
   invoke
3) Don't write out the mbnames file in --dry-run mode.

Repository, and Folder levels still need to be protected in dry-run mode
as of now.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2011-09-15 13:56:04 +02:00
parent b7e0a51751
commit 33f55b5362
2 changed files with 12 additions and 3 deletions

View File

@ -65,9 +65,11 @@ class Account(CustomConfig.ConfigHelperMixin):
self.name = name self.name = name
self.metadatadir = config.getmetadatadir() self.metadatadir = config.getmetadatadir()
self.localeval = config.getlocaleval() self.localeval = config.getlocaleval()
#Contains the current :mod:`offlineimap.ui`, and can be used for logging etc. # current :mod:`offlineimap.ui`, can be used for logging:
self.ui = getglobalui() self.ui = getglobalui()
self.refreshperiod = self.getconffloat('autorefresh', 0.0) self.refreshperiod = self.getconffloat('autorefresh', 0.0)
# should we run in "dry-run" mode?
self.dryrun = self.config.getboolean('general', 'dry-run')
self.quicknum = 0 self.quicknum = 0
if self.refreshperiod == 0.0: if self.refreshperiod == 0.0:
self.refreshperiod = None self.refreshperiod = None
@ -312,6 +314,8 @@ class SyncableAccount(Account):
# wait for all threads to finish # wait for all threads to finish
for thr in folderthreads: for thr in folderthreads:
thr.join() thr.join()
# Write out mailbox names if required and not in dry-run mode
if not self.dryrun:
mbnames.write() mbnames.write()
localrepos.forgetfolders() localrepos.forgetfolders()
remoterepos.forgetfolders() remoterepos.forgetfolders()
@ -337,6 +341,8 @@ class SyncableAccount(Account):
return return
try: try:
self.ui.callhook("Calling hook: " + cmd) self.ui.callhook("Calling hook: " + cmd)
if self.dryrun: # don't if we are in dry-run mode
return
p = Popen(cmd, shell=True, p = Popen(cmd, shell=True,
stdin=PIPE, stdout=PIPE, stderr=PIPE, stdin=PIPE, stdout=PIPE, stderr=PIPE,
close_fds=True) close_fds=True)

View File

@ -461,6 +461,9 @@ class UIBase(object):
################################################## Hooks ################################################## Hooks
def callhook(self, msg): def callhook(self, msg):
if self.dryrun:
self.info("[DRYRUN] {}".format(msg))
else:
self.info(msg) self.info(msg)
################################################## Other ################################################## Other