diff options
| author | Lex Neva <github.com@lexneva.name> | 2018-06-13 20:10:22 -0400 |
|---|---|---|
| committer | Lex Neva <github.com@lexneva.name> | 2018-06-15 21:44:52 -0400 |
| commit | 4c46c2eec1fb7cf9e85617030214bcb170b8b533 (patch) | |
| tree | e3f21dfc5627a9a80ea3deb5d3f8322e508661cc /lib | |
| parent | f9a5e4c03a073d403222f0d5b7810cdaab90145a (diff) | |
fix zip file corruption
Diffstat (limited to 'lib')
| -rw-r--r-- | lib/extensions/output.py | 4 | ||||
| -rw-r--r-- | lib/extensions/zip.py | 10 | ||||
| -rw-r--r-- | lib/utils/io.py | 21 |
3 files changed, 32 insertions, 3 deletions
diff --git a/lib/extensions/output.py b/lib/extensions/output.py index 72bbe37d..924c2d3a 100644 --- a/lib/extensions/output.py +++ b/lib/extensions/output.py @@ -35,12 +35,14 @@ class Output(InkstitchExtension): # in windows, failure to close here will keep the file locked temp_file.close() + # libembroidery likes to debug log things to stdout. No way to disable it. + save_stdout() write_embroidery_file(temp_file.name, stitch_plan, self.document.getroot()) # inkscape will read the file contents from stdout and copy # to the destination file that the user chose with open(temp_file.name) as output_file: - sys.stdout.write(output_file.read()) + sys.real_stdout.write(output_file.read()) # clean up the temp file os.remove(temp_file.name) diff --git a/lib/extensions/zip.py b/lib/extensions/zip.py index a7616536..ca12efdd 100644 --- a/lib/extensions/zip.py +++ b/lib/extensions/zip.py @@ -11,6 +11,7 @@ from ..i18n import _ from ..output import write_embroidery_file from ..stitch_plan import patches_to_stitch_plan from ..svg import render_stitch_plan, PIXELS_PER_MM +from ..utils.io import save_stdout class Zip(InkstitchExtension): @@ -48,12 +49,19 @@ class Zip(InkstitchExtension): files = [] + # libembroidery likes to debug log things to stdout. No way to disable it. + save_stdout() for format in self.formats: if getattr(self.options, format): output_file = os.path.join(path, "%s.%s" % (base_file_name, format)) write_embroidery_file(output_file, stitch_plan, self.document.getroot()) files.append(output_file) + # I'd love to do restore_stderr() here, but if I do, libembroidery's + # stuff still prints out and corrupts the zip! That's because it uses + # C's buffered stdout, so it hasn't actually written anything to the + # real standard output yet. + if not files: self.errormsg(_("No embroidery file formats selected.")) @@ -69,7 +77,7 @@ class Zip(InkstitchExtension): # inkscape will read the file contents from stdout and copy # to the destination file that the user chose with open(temp_file.name) as output_file: - sys.stdout.write(output_file.read()) + sys.real_stdout.write(output_file.read()) os.remove(temp_file.name) for file in files: diff --git a/lib/utils/io.py b/lib/utils/io.py index be1fdf24..44d48c2a 100644 --- a/lib/utils/io.py +++ b/lib/utils/io.py @@ -7,12 +7,31 @@ def save_stderr(): # GTK likes to spam stderr, which inkscape will show in a dialog. null = open(os.devnull, 'w') sys.stderr_dup = os.dup(sys.stderr.fileno()) + sys.real_stderr = os.fdopen(sys.stderr_dup, 'w') os.dup2(null.fileno(), 2) sys.stderr_backup = sys.stderr sys.stderr = StringIO() def restore_stderr(): + sys.real_stderr.close() os.dup2(sys.stderr_dup, 2) - sys.stderr_backup.write(sys.stderr.getvalue()) + sys.real_stderr.write(sys.stderr.getvalue()) sys.stderr = sys.stderr_backup + +# It's probably possible to generalize this code, but when I tried, +# the result was incredibly unreadable. +def save_stdout(): + null = open(os.devnull, 'w') + sys.stdout_dup = os.dup(sys.stdout.fileno()) + sys.real_stdout = os.fdopen(sys.stdout_dup, 'w') + os.dup2(null.fileno(), 1) + sys.stdout_backup = sys.stdout + sys.stdout = StringIO() + + +def restore_stdout(): + sys.real_stdout.close() + os.dup2(sys.stdout_dup, 1) + sys.real_stdout.write(sys.stdout.getvalue()) + sys.stdout = sys.stdout_backup |
