]> git.phdru.name Git - mimedecode.git/commitdiff
Create the directory for the output files
authorOleg Broytman <phd@phdru.name>
Thu, 13 Mar 2014 21:29:06 +0000 (01:29 +0400)
committerOleg Broytman <phd@phdru.name>
Thu, 13 Mar 2014 21:29:06 +0000 (01:29 +0400)
Create the directory for the output files (-O) if it doesn't exits.

ANNOUNCE
mimedecode.docbook
mimedecode.py

index 201eccd67b0d0b5e5bcb15b9b2a96f0e567c340f..5bd1a55f072cf5b607c74a21f7a8fca5b53d329f 100644 (file)
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -38,6 +38,8 @@ headers/bodies/messages to files.
 
    Add option -O to set the destination directory for output files.
 
+   Create the directory for the output files (-O) if it doesn't exits.
+
    Fix a minor bug: if a multipart message (or a subpart) lacks any textual
 content - avoid putting an excessive newline.
 
index 9acc3209b228b2c4c25898205fc9f46251bb0231..58ca08d5b2cabbf342ffc8dde8ffca9d975c50dd 100644 (file)
       <term>-O dest_dir</term>
       <listitem>
          <para>
-           Set destination directory for the output files; the directory must
-           exist. Default is current directory.
+           Set destination directory for the output files; if the directory
+           doesn't exist it will be created. Default is current directory.
           </para>
       </listitem>
    </varlistentry>
index 392e70774e5f1cb2537c5b94fa87906a71470f22..c1731d6336c5ab8eff9e5928a61a9cce1181ddc2 100755 (executable)
@@ -396,7 +396,16 @@ def decode_message(msg):
 
 
 def open_output_file(filename):
-    return open(os.path.join(g.destination_dir, filename), 'w')
+    fullpath = os.path.abspath(os.path.join(g.destination_dir, filename))
+    full_dir = os.path.dirname(fullpath)
+    create = not os.path.isdir(full_dir)
+    if create:
+        os.makedirs(full_dir)
+    try:
+        return open(fullpath, 'w')
+    except:
+        if create:
+            os.removedirs(full_dir)
 
 
 class GlobalOptions: