Skip to content

Instantly share code, notes, and snippets.

@alteist
Created September 6, 2013 10:09
Show Gist options
  • Save alteist/6461959 to your computer and use it in GitHub Desktop.
Save alteist/6461959 to your computer and use it in GitHub Desktop.
This dirty script converts "The Bat!" mail databases to Unix mbox ones. It scans directory for .TBB files, reconstructs dir structure and converts databases using another program by Денис Трачук through wine.
#!/usr/bin/python
# -*- coding: utf-8 -*-
"""
thebat2mbox.py v0.1
Copyleft 2013 by alteist@gmail.com
This dirty script for *nix converts "The Bat!" mail databases to Unix mbox ones. It scans directory for .TBB files,
reconstructs dir structure and converts databases using another program by Денис Трачук through wine.
Usage:
1. install wine
2. get the tbb2mbx12.zip (http://plugring.farmanager.com/plugin.php?pid=379&l=ru) and extract tbb2mbx.exe to the dir where this script lies
3. python thebat2mbox.py the_bat_dir out_dir
"""
import os
import sys
import subprocess
pwd = os.getcwd()
# Check deps
if subprocess.Popen("which wine", stdout=subprocess.PIPE, shell=True).communicate()[0] == '':
exit("Check if wine is installed and in $PATH")
if not os.path.isfile("tbb2mbx.exe"):
exit("Please download tbb2mbx12.zip from http://plugring.farmanager.com/plugin.php?pid=379&l=ru and extract tbb2mbx.exe here in %s" % pwd)
tbb2mbx = "wine tbb2mbx.exe"
mbx_extension = ".mbx"
errors = 0
cmds = []
# Read arguments
if len(sys.argv) > 1:
the_bat_dir = sys.argv[1]
else:
the_bat_dir = pwd
if len(sys.argv) > 2:
out_dir = sys.argv[2]
else:
out_dir = "%s/mboxes_from_thebat" % pwd
# Find The Bat base files
print ("Searching *.TBB in %s" % the_bat_dir)
p = subprocess.Popen("find %s -name *.TBB" % the_bat_dir, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
lines = output.decode(sys.getfilesystemencoding()).splitlines()
print ("Found %s TBB's" % len(lines))
# Parse paths and construct convert commands to execute them later
for line in lines:
try:
values = line.replace(the_bat_dir, ".").split("/")
mbx_path = u"/".join(values[1:-2])
mbx_filename = values[-2]
except ValueError:
errors += 1
print ("ValueError exception on %s" % values)
else:
cmds.append("mkdir -p %s/%s" % (out_dir, mbx_path))
try:
cmd_convert = u'%s "z:%s" "z:%s/%s/%s%s"' % (tbb2mbx, line, out_dir, mbx_path, mbx_filename, mbx_extension)
cmds.append(cmd_convert)
except UnicodeDecodeError as e:
print ("UnicodeDecodeError exception %s on %s" % (e, line))
errors += 1
else:
pass
print ("Parsed %s TBB's with %s errors" % (len(lines) - errors, errors))
print ("- - - - - - - - - - - - - - - - - - -")
#print (cmds)
# Execute commands
if raw_input("Convert %s TBB's from %s to %s? (y/n)" % (len(lines), the_bat_dir, out_dir)) == "y":
errors = 0
for cmd in cmds:
print ("Executing %s" % cmd)
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
if err:
errors += 1
print ("Error: %s" % err)
print ("Processed %s TBB's with %s errors" % (len(lines) - errors, errors))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment