Skip to content

Commit

Permalink
Fix export_plot on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
rihi committed Nov 29, 2023
1 parent 3392f02 commit dad2453
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 15 additions & 6 deletions decompiler/util/decoration.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
from __future__ import annotations

import os
import subprocess
import textwrap
from logging import warning
from re import compile
from subprocess import CompletedProcess, run
from subprocess import CompletedProcess, Popen, run
from sys import stdout
from tempfile import NamedTemporaryFile
from typing import Dict, Optional, TextIO
Expand Down Expand Up @@ -63,7 +64,7 @@ def _write_dot(self, handle: Optional[TextIO] = None):
"""Write the graph to the given handle or NamedTemporaryFile."""
if not handle:
handle = NamedTemporaryFile(mode="w+")
ToDotConverter.write(self._graph, handle)
handle.write(ToDotConverter.write(self._graph))
handle.flush()
handle.seek(0)
return handle
Expand All @@ -88,10 +89,18 @@ def export_plot(self, path: str, type="png"):
path -- Path to the plot to be created.
type -- a string describing the output type (commonly pdf, png)
"""
with self._write_dot() as handle:
result = run(["dot", f"-T{type}", f"-o{path}", f"{handle.name}"], capture_output=True)
if result.returncode:
raise ValueError(f"Could not plot graph! ({result.stderr.decode('utf-8')}")
with Popen(
["dot", f"-T{type}", f"-o{path}"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True
) as proc:
dot_source: str = ToDotConverter.write(self.graph)
stdout, stderr = proc.communicate(input=dot_source)

if proc.returncode:
raise ValueError(f"Could not plot graph! ({stderr})")


class DecoratedCFG(DecoratedGraph):
Expand Down
5 changes: 2 additions & 3 deletions decompiler/util/to_dot_converter.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
"""Module handling conversion to dot-format."""
from typing import TextIO

from networkx import DiGraph

Expand All @@ -16,10 +15,10 @@ def __init__(self, graph: DiGraph):
self._graph = graph

@classmethod
def write(cls, graph: DiGraph, handle: TextIO):
def write(cls, graph: DiGraph) -> str:
"""Write dot-format of given graph into handle."""
converter = cls(graph)
handle.write(converter._create_dot())
return converter._create_dot()

def _create_dot(self) -> str:
"""Create dot-file content."""
Expand Down

0 comments on commit dad2453

Please sign in to comment.