]>
git.ipfire.org Git - thirdparty/kernel/stable-queue.git/blob - scripts/splitmbox.py
1 #! /usr/bin/env python3
2 """ Split a Unix-style mailbox into individual files
4 Written by Aquarius <aquarius@kryogenix.org>
5 Usage: splitmbox.py <mailbox-file> <directory>
7 This will create files numbered 01,02,03... in <directory>. The
8 number of prefixed zeroes will make all filenames in the
9 directory the same length. """
11 import mailbox
,sys
,getopt
,string
,os
,subprocess
14 USAGE
= """Usage: splitmbox.py [ OPTION ] <mailbox-file> <directory>
17 Show this help text and exit
19 Show file version and exit
21 This will create files numbered 01,02,03... in <directory>. The
22 number of prefixed zeroes will make all filenames in the
23 directory the same length. """
27 optlist
, args
= getopt
.getopt(sys
.argv
[1:],'hv',['help','version'])
28 except getopt
.error
, error_text
:
34 if tuple[0] == '-h' or tuple[0] == '--help':
37 if tuple[0] == '-v' or tuple[0] == '--version':
44 mbox_fname
, output_dir
= args
45 if output_dir
[-1] != '/':
46 output_dir
= output_dir
+ '/'
48 # Make the output directory, if required
51 except os
.error
,ertxt
:
52 if string
.find(str(ertxt
),'File exists') == -1:
53 print 'Failed to create or use directory',output_dir
,'[',ertxt
,']'
57 mbox_file
= open(mbox_fname
)
59 print "Failed to open file",mbox_fname
62 mbox
= mailbox
.UnixMailbox(mbox_file
)
64 # Find out how many messages in the mailbox
72 # Now do it again, outputting files
75 mbox_file
= open(mbox_fname
)
76 mbox
= mailbox
.UnixMailbox(mbox_file
)
78 digits
= len(str(count
))
85 fname
= output_dir
+'msg.'+('0'*digits
+str(count
))[-digits
:]
86 print 'Writing ', fname
87 outfile
= open(fname
,'w')
88 outfile
.write('From foo@baz ');
89 outfile
.write(subprocess
.check_output('date'));
93 for s
in msg
.fp
.readlines():