If it won't be simple, it simply won't be. [Hire me, source code] by Miki Tebeka, CEO, 353Solutions

Wednesday, January 28, 2009

Find Module Version

Quick script to check which version of Python module is installed.

#!/usr/bin/env python
'''Find python module version'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

def valueof(v):
if callable(v):
try:
return v()
except Exception:
return None
return v

def load_module(module_name):
module = __import__(module_name)

# __import__("a.b") will give us a
if ("." in module_name):
names = module_name.split(".")[1:]
while names:
name = names.pop(0)
module = getattr(module, name)

return module

def find_module_version(module_name):
module = load_module(module_name)
attrs = set(dir(module))

for known in ("__version__", "version", "version_string"):
if known in attrs:
v = valueof(getattr(module, known))
if v:
return v

for attr in attrs:
if "version" in attr.lower():
v = getattr(module, attr)
if not v:
continue
v = valueof(v)
if v:
return v

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser

parser = OptionParser("usage: %prog MODULE_NAME")

opts, args = parser.parse_args(argv[1:])
if len(args) != 1:
parser.error("wrong number of arguments") # Will exit

module_name = args[0]

try:
version = find_module_version(module_name)
except ImportError, e:
raise SystemExit("error: can't import %s (%s)" % (module_name, e))

if version:
print version
else:
raise SystemExit("error: can't find version for %s" % module_name)

if __name__ == "__main__":
main()

Friday, January 16, 2009

Which build utility to run?

I have the following as a script called "mk" and set in my .vimrc
set makeprg=mk


#!/bin/bash
# Guess which make utility to use

# Miki Tebeka <miki.tebeka@gmail.com>

makecmd=""
if [ -f SConstruct ]; then
makecmd="scons -Q -D"
elif [ -f build.xml ]; then
makecmd="ant"
elif [ -f Makefile ]; then
makecmd="make"
elif [ -f makefile ]; then
makecmd="make"
elif [ "$OSTYPE" == "WINNT" ]; then
proj=`ls *.dsp 2>/dev/null`
if [ -f $proj ]; then
makecmd="msdev $proj /MAKE"
fi
fi

if [ -z "$makecmd" ]; then
echo "can't find project file"
exit 1
fi

$makecmd $@

Wednesday, January 07, 2009

Moved to bitbucket

All(?) the code from this blog is now at BitBucket

imapclean

#!/usr/bin/env python
'''Clean your IMAP inbox

Copy all message to "Archive" and delete them from Inbox
'''

__author__ = "Miki Tebeka <miki.tebeka@gmail.com>"

from imaplib import IMAP4
import logging as log

def move_messages(server, user, passwd, purge=0):
imap = IMAP4(server)
imap.login(user, passwd)
imap.select()
messages = imap.search(None, "ALL")[1][0].split()
for msg_id in messages:
flags = imap.fetch(msg_id, "(FLAGS)")[1][0]
if not flags.startswith(msg_id):
log.error("bad reply for %s flags - %s" % (msg_id, flags))
continue
if "Deleted" in flags:
log.info("skipping %s - already deleted" % msg_id)
continue
log.info("deleting %s" % msg_id)
imap.copy(msg_id, "Archive")
imap.store(msg_id, "+FLAGS", "\\Deleted")

if purge:
log.info("purging deleted messages")
imap.expunge()
log.info("logging out")
imap.close()
imap.logout()

def main(argv=None):
if argv is None:
import sys
argv = sys.argv

from optparse import OptionParser
from getpass import getpass, getuser

parser = OptionParser("usage: %prog")
parser.add_option("-p", "--purge", help="purge deleted messages",
action="store_true", default=0, dest="purge")
parser.add_option("-v", "--verbose", help="be verbose",
dest="verbose", action="store_true", default=0)
parser.add_option("--server", help="imap server",
default="localhost", dest="server")
parser.add_option("--user", help="IMAP user name",
dest="user", default="")
parser.add_option("--passwd", help="IMAP password",
dest="passwd", default="")

opts, args = parser.parse_args(argv[1:])
if args:
parser.error("wrong number of arguments") # Will exit

level = log.DEBUG if opts.verbose else log.ERROR
log.basicConfig(format="%(levelname)s: %(message)s",
level=level)

opts.user = opts.user or getuser()
opts.passwd = opts.passwd or getpass()

try:
move_messages(opts.server, opts.user, opts.passwd, opts.purge)
except Exception, e:
if opts.verbose:
log.exception(e)
else:
log.error(e)
raise SystemExit(1)

if __name__ == "__main__":
main()


Yeah, the pygments experiment worked.

Blog Archive