Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 524f146

Browse files
committed
Added mboxconvert.py
1 parent 52ca98a commit 524f146

2 files changed

Lines changed: 114 additions & 0 deletions

File tree

Demo/scripts/README

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ linktree.py Make a copy of a tree with links to original files
2929
lll.py Find and list symbolic links in current directory
3030
lpwatch.py Watch BSD line printer queues
3131
markov.py Markov chain simulation of words or characters
32+
mboxconvvert.py Convert MH or MMDF mailboxes to unix mailbox format
3233
methfix.py Fix old method syntax def f(self, (a1, ..., aN)):
3334
mkreal.py Turn a symbolic link into a real file or directory
3435
mpzpi.py test mpz -- print digits of pi (compare pi.py)

Demo/scripts/mboxconvert.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
#! /usr/local/bin/python
2+
3+
# Convert MH directories (1 message per file) or MMDF mailboxes (4x^A
4+
# delimited) to unix mailbox (From ... delimited) on stdout.
5+
# If -f is given, files contain one message per file (e.g. MH messages)
6+
7+
import rfc822
8+
import sys
9+
import time
10+
import os
11+
import stat
12+
import getopt
13+
import regex
14+
15+
def main():
16+
dofile = mmdf
17+
try:
18+
opts, args = getopt.getopt(sys.argv[1:], 'f')
19+
except getopt.error, msg:
20+
sys.stderr.write('%s\n' % msg)
21+
sys.exit(2)
22+
for o, a in opts:
23+
if o == '-f':
24+
dofile = message
25+
if not args:
26+
args = ['-']
27+
sts = 0
28+
for arg in args:
29+
if arg == '-' or arg == '':
30+
sts = dofile(sys.stdin) or sts
31+
elif os.path.isdir(arg):
32+
sts = mh(arg) or sts
33+
elif os.path.isfile(arg):
34+
try:
35+
f = open(arg)
36+
except IOError, msg:
37+
sys.stderr.write('%s: %s\n' % (arg, msg))
38+
sts = 1
39+
continue
40+
sts = dofile(f) or sts
41+
f.close()
42+
else:
43+
sys.stderr('%s: not found\n' % arg)
44+
sts = 1
45+
if sts:
46+
sys.exit(sts)
47+
48+
numeric = regex.compile('[1-9][0-9]*')
49+
50+
def mh(dir):
51+
sts = 0
52+
msgs = os.listdir(dir)
53+
for msg in msgs:
54+
if numeric.match(msg) != len(msg):
55+
continue
56+
fn = os.path.join(dir, msg)
57+
try:
58+
f = open(fn)
59+
except IOError, msg:
60+
sys.stderr.write('%s: %s\n' % (fn, msg))
61+
sts = 1
62+
continue
63+
sts = message(f) or sts
64+
return sts
65+
66+
def mmdf(f):
67+
sts = 0
68+
while 1:
69+
line = f.readline()
70+
if not line:
71+
break
72+
if line == '\1\1\1\1\n':
73+
sts = message(f, line) or sts
74+
else:
75+
sys.stderr.write(
76+
'Bad line in MMFD mailbox: %s\n' % `line`)
77+
return sts
78+
79+
def message(f, delimiter = ''):
80+
sts = 0
81+
# Parse RFC822 header
82+
m = rfc822.Message(f)
83+
# Write unix header line
84+
fullname, email = m.getaddr('From')
85+
tt = m.getdate('Date')
86+
if tt:
87+
t = time.mktime(tt)
88+
else:
89+
sys.stderr.write(
90+
'Unparseable date: %s\n' % `m.getheader('Date')`)
91+
t = os.fstat(f.fileno())[stat.ST_MTIME]
92+
print 'From', email, time.ctime(t)
93+
# Copy RFC822 header
94+
for line in m.headers:
95+
print line,
96+
print
97+
# Copy body
98+
while 1:
99+
line = f.readline()
100+
if line == delimiter:
101+
break
102+
if not line:
103+
sys.stderr.write('Unexpected EOF in message\n')
104+
sts = 1
105+
break
106+
if line[:5] == 'From ':
107+
line = '>' + line
108+
print line,
109+
# Print trailing newline
110+
print
111+
return sts
112+
113+
main()

0 commit comments

Comments
 (0)