From bfff00e395f74d2aae05cf94624cc6287349f11b Mon Sep 17 00:00:00 2001 From: Nikolaus Schulz Date: Mon, 7 Apr 2008 20:16:29 +0000 Subject: Code structuring: made a separate group of IMAP functions. No code changes, just moved functions around. --- archivemail.py | 123 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 65 insertions(+), 58 deletions(-) (limited to 'archivemail.py') diff --git a/archivemail.py b/archivemail.py index 55c3934..45a0cdc 100755 --- a/archivemail.py +++ b/archivemail.py @@ -1482,67 +1482,10 @@ def _archive_imap(mailbox_name, final_archive_name): if not options.quiet: stats.display() -############### misc functions ############### - - -def set_signal_handlers(): - """set signal handlers to clean up temporary files on unexpected exit""" - # Make sure we clean up nicely - we don't want to leave stale procmail - # lockfiles about if something bad happens to us. This is quite - # important, even though procmail will delete stale files after a while. - signal.signal(signal.SIGHUP, clean_up_signal) # signal 1 - # SIGINT (signal 2) is handled as a python exception - signal.signal(signal.SIGQUIT, clean_up_signal) # signal 3 - signal.signal(signal.SIGTERM, clean_up_signal) # signal 15 - -def clean_up(): - """Delete stale files""" - vprint("cleaning up ...") - _stale.clean() - - -def clean_up_signal(signal_number, stack_frame): - """Delete stale files -- to be registered as a signal handler. - - Arguments: - signal_number -- signal number of the terminating signal - stack_frame -- the current stack frame - - """ - # this will run the above clean_up(), since unexpected_error() - # will abort with sys.exit() and clean_up will be registered - # at this stage - unexpected_error("received signal %s" % signal_number) +############### IMAP functions ############### -def check_sane_destdir(dir): - """Do a very primitive check if the given directory looks like a reasonable - destination directory and bail out if it doesn't.""" - assert(dir) - if not os.path.isdir(dir): - user_error("output directory does not exist: '%s'" % dir) - if not os.access(dir, os.W_OK): - user_error("no write permission on output directory: '%s'" % dir) - if is_world_writable(dir): - unexpected_error(("output directory is world-writable: '%s' -- " - "I feel nervous!") % dir) - - -def is_world_writable(path): - """Return true if the path is world-writable, false otherwise""" - assert(path) - return (os.stat(path)[stat.ST_MODE] & stat.S_IWOTH) - - -def nice_size_str(size): - """Return given size in bytes as '12kB', '1.2MB'""" - kb = size / 1024.0 - mb = kb / 1024.0 - if mb >= 1.0: return str(round(mb, 1)) + 'MB' - if kb >= 1.0: return str(round(kb)) + 'kB' - return str(size) + 'B' - def parse_imap_url(url): """Parse IMAP URL and return username, password (if appliciable), servername and foldername.""" @@ -1577,6 +1520,7 @@ def parse_imap_url(url): unexpected_error("Invalid IMAP connection string") return username, password, server, folder + def imap_getdelim(imap_server): """Return the IMAP server's hierarchy delimiter. Assumes there is only one.""" # This function will break if the LIST reply doesn't meet our expectations. @@ -1604,6 +1548,69 @@ def imap_getdelim(imap_server): return None return delim + +############### misc functions ############### + + +def set_signal_handlers(): + """set signal handlers to clean up temporary files on unexpected exit""" + # Make sure we clean up nicely - we don't want to leave stale procmail + # lockfiles about if something bad happens to us. This is quite + # important, even though procmail will delete stale files after a while. + signal.signal(signal.SIGHUP, clean_up_signal) # signal 1 + # SIGINT (signal 2) is handled as a python exception + signal.signal(signal.SIGQUIT, clean_up_signal) # signal 3 + signal.signal(signal.SIGTERM, clean_up_signal) # signal 15 + + +def clean_up(): + """Delete stale files""" + vprint("cleaning up ...") + _stale.clean() + + +def clean_up_signal(signal_number, stack_frame): + """Delete stale files -- to be registered as a signal handler. + + Arguments: + signal_number -- signal number of the terminating signal + stack_frame -- the current stack frame + + """ + # this will run the above clean_up(), since unexpected_error() + # will abort with sys.exit() and clean_up will be registered + # at this stage + unexpected_error("received signal %s" % signal_number) + + +def check_sane_destdir(dir): + """Do a very primitive check if the given directory looks like a reasonable + destination directory and bail out if it doesn't.""" + assert(dir) + if not os.path.isdir(dir): + user_error("output directory does not exist: '%s'" % dir) + if not os.access(dir, os.W_OK): + user_error("no write permission on output directory: '%s'" % dir) + if is_world_writable(dir): + unexpected_error(("output directory is world-writable: '%s' -- " + "I feel nervous!") % dir) + + +def is_world_writable(path): + """Return true if the path is world-writable, false otherwise""" + assert(path) + return (os.stat(path)[stat.ST_MODE] & stat.S_IWOTH) + + +def nice_size_str(size): + """Return given size in bytes as '12kB', '1.2MB'""" + kb = size / 1024.0 + mb = kb / 1024.0 + if mb >= 1.0: return str(round(mb, 1)) + 'MB' + if kb >= 1.0: return str(round(kb)) + 'kB' + return str(size) + 'B' + + def get_filename(msg): """If the given rfc822.Message can be identified with a file (no mbox), return the filename, otherwise raise AttributeError.""" -- cgit v1.2.3