[Bug 1202604] Review Request: python-sscg - Self-signed Certificate Generator

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



https://bugzilla.redhat.com/show_bug.cgi?id=1202604



--- Comment #6 from Zbigniew Jędrzejewski-Szmek <zbyszek@xxxxxxxxx> ---
It seems that tempfile.NamedTemporaryFile would do the right thing without
requiring all this manual cleanup:

--- src/sscg/__init__.py
+++ src/sscg/__init__.py
@@ -28,36 +28,24 @@ def write_certificate(options, cert, destination):
     # Create the temporary file in the same directory as the destination
     # This ensures that we can atomically move it to the final name.

+    f = tempfile.NamedTemporaryFile(dir=os.path.dirname(destination))
     try:
-        (fd, fpath) = tempfile.mkstemp(dir=os.path.dirname(destination))
-        if options.debug:
-            print(_("Creating temporary certificate file at
{}".format(fpath)))
-        f = os.fdopen(fd, "w")
-
-        f.write(crypto.dump_certificate(options.cert_format,
cert).decode("UTF-8"))
-        f.close()
-    except:
-        # Something went wrong. Remove the temporary file before failing.
-        print(_("Could not write to {0}. Error: {1}".format(
-              fpath, sys.exc_info()[1])),
-              file=sys.stderr)
-        os.unlink(fpath)
-        raise
+        f.write(crypto.dump_certificate(options.cert_format, cert))
+        f.flush()
+    except IOError as e:
+        raise Exception(_("Could not write to {0}. Error: {1}").format(f.name,
e))

     # Now atomically move the temporary file into place.
     # We use os.rename because this is guaranteed to be atomic if it succeeds
     # This operation can fail on some flavors of UNIX if the source and
     # destination are on different filesystems, but this should not be the
case.
+    if options.debug:
+        print(_("Renaming {} to {}").format(f.name, destination))
     try:
-        if options.debug:
-            print(_("Renaming {} to {}".format(fpath, destination)))
         os.rename(fpath, destination)
-    except:
-        # Something went wrong. Remove the temporary file before failing.

-- 
You are receiving this mail because:
You are on the CC list for the bug.
You are always notified about changes to this product and component
_______________________________________________
package-review mailing list
package-review@xxxxxxxxxxxxxxxxxxxxxxx
https://admin.fedoraproject.org/mailman/listinfo/package-review





[Index of Archives]     [Fedora Legacy]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [KDE Users]     [Fedora Tools]