Skip to content
This repository was archived by the owner on Nov 19, 2018. It is now read-only.

Commit 8f5d826

Browse files
committed
Support Graphviz installed from Conda
Graphviz installed using Conda is using .bat scripts to correctly setup running it on Windows.
1 parent 197ea7d commit 8f5d826

File tree

2 files changed

+42
-28
lines changed

2 files changed

+42
-28
lines changed

pydot_ng/__init__.py

+27-18
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,21 @@ def graph_from_incidence_matrix(matrix, node_prefix='', directed=False):
398398
return graph
399399

400400

401+
def is_qouted(path):
402+
return path.startswith('"') and path.endswith('"')
403+
404+
405+
is_windows = os.name == 'nt'
406+
is_anacoda = os.path.exists(os.path.join(sys.prefix, 'conda-meta'))
407+
408+
409+
def get_extension():
410+
if is_windows:
411+
return '.bat' if is_anacoda else '.exe'
412+
else:
413+
return ''
414+
415+
401416
def __find_executables(path):
402417
"""Used by find_graphviz
403418
@@ -409,7 +424,8 @@ def __find_executables(path):
409424
Otherwise returns None
410425
"""
411426

412-
success = False
427+
any_found = False
428+
413429
progs = {
414430
"dot": "",
415431
"twopi": "",
@@ -419,11 +435,12 @@ def __find_executables(path):
419435
"sfdp": "",
420436
}
421437

422-
was_quoted = False
423438
path = path.strip()
424-
if path.startswith('"') and path.endswith('"'):
439+
path_template = "{}"
440+
441+
if is_qouted(path):
425442
path = path[1:-1]
426-
was_quoted = True
443+
path_template = "\"{}\""
427444

428445
if not os.path.isdir(path):
429446
return None
@@ -432,22 +449,14 @@ def __find_executables(path):
432449
if progs[prg]:
433450
continue
434451

435-
prg_path = os.path.join(path, prg)
436-
prg_exe_path = prg_path + ".exe"
452+
ext = get_extension()
453+
prg_path = os.path.join(path, prg + ext)
437454

438455
if os.path.exists(prg_path):
439-
if was_quoted:
440-
prg_path = "\"{}\"".format(prg_path)
441-
progs[prg] = prg_path
442-
success = True
443-
444-
elif os.path.exists(prg_exe_path):
445-
if was_quoted:
446-
prg_exe_path = "\"{}\"".format(prg_exe_path)
447-
progs[prg] = prg_exe_path
448-
success = True
449-
450-
if success:
456+
any_found = True
457+
progs[prg] = path_template.format(prg_path)
458+
459+
if any_found:
451460
return progs
452461

453462
return None

test/test_pydot.py

+15-10
Original file line numberDiff line numberDiff line change
@@ -303,20 +303,25 @@ def test_find_executables_path_needs_strip(tmpdir):
303303
) == sorted(progs)
304304

305305

306-
def test_find_executables_unix_and_exe_exists(tmpdir):
306+
@pytest.mark.parametrize(
307+
"more_important,less_important",
308+
(("", ".exe"), (".bat", ".exe"), ("", ".bat")),
309+
ids=["unix-over-exe", "bar-over-exe", "unix-over-bat"],
310+
)
311+
def test_find_executables_priority(more_important, less_important, tmpdir):
307312
path = str(tmpdir)
308-
prog_unix_path = str(tmpdir.join("dot"))
309-
prog_exe_path = str(tmpdir.join("dot.exe"))
313+
prog_more_imp = str(tmpdir.join("dot" + more_important))
314+
prog_less_imp = str(tmpdir.join("dot" + less_important))
310315

311-
with open(prog_unix_path, "w"):
312-
with open(prog_exe_path, "w"):
316+
with open(prog_more_imp, "w"):
317+
with open(prog_less_imp, "w"):
313318
progs = pydot.__find_executables(path)
314-
assert progs["dot"] == prog_unix_path
315-
assert progs["dot"] != prog_exe_path
319+
assert progs["dot"] == prog_more_imp
320+
assert progs["dot"] != prog_less_imp
316321

317322

318323
@pytest.mark.parametrize("quoted", (True, False), ids=("quoted", "unqoted"))
319-
@pytest.mark.parametrize("extension", ("", ".exe"))
324+
@pytest.mark.parametrize("extension", ("", ".exe", ".bat"))
320325
@pytest.mark.parametrize(
321326
"program", ("dot", "twopi", "neato", "circo", "fdp", "sfdp")
322327
)
@@ -326,8 +331,8 @@ def test_find_executables(tmpdir, program, extension, quoted):
326331

327332
with open(prog_path, "w"):
328333
if quoted:
329-
path = "\"{}\"".format(path)
330-
prog_path = "\"{}\"".format(prog_path)
334+
path = '"{}"'.format(path)
335+
prog_path = '"{}"'.format(prog_path)
331336

332337
progs = pydot.__find_executables(str(path))
333338
assert progs[program] == prog_path

0 commit comments

Comments
 (0)