Skip to content

Commit

Permalink
Improve drawio binary finding functionality to use $PATH
Browse files Browse the repository at this point in the history
Fixes #23 and #24.

Prioritises config-option binary_path, then programs in $PATH,
then the hardcoded absolute paths. Also raises an error if
cannot find the executable as opposed to just attempting to run
draw.io as a Linux program anyway.
  • Loading branch information
modelmat committed May 10, 2022
1 parent df30fac commit f8e82e7
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
17 changes: 11 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,17 +42,18 @@ issue.
## Installation

1. `python3 -m pip install sphinxcontrib-drawio`
2. In your sphinx config:
2. In your sphinx config, add:

```python
extensions = [
"sphinxcontrib.drawio"
]
```

3. Add the binary to `$PATH`. For Windows add `C:\Program Files\draw.io` and on
Linux add `/opt/drawio/`.
4. (if running headless), `sudo apt install xvfb`
3. Add the draw.io binary to `$PATH`. See [Options: Binary Path](#binary-path)
for more details and alternative solutions.

4. If running headless, install Xvfb, e.g. via `$ sudo apt install xvfb`.

## Options
These values are placed in the `conf.py` of your sphinx project.
Expand All @@ -62,8 +63,12 @@ These values are placed in the `conf.py` of your sphinx project.
- *Default Value*: `None`

This allows for a specific override for the binary location. By default, this
gets chosen depending on the OS (Linux, Mac, or Windows) to the default
install path of the draw.io program.
chooses the `drawio` (or `draw.io.exe`) binary accessible in `$PATH`. However,
if this file does not exist, it picks the platform-appropriate path:

- Windows: `C:\Program Files\draw.io\draw.io.exe`
- Linux: `/opt/drawio/drawio` or `/opt/draw.io/drawio` (older versions)
- MacOS: `/Applications/draw.io.app/Contents/MacOS/draw.io`.

### Headless Mode
- *Formal Name*: `drawio_headless`
Expand Down
26 changes: 21 additions & 5 deletions sphinxcontrib/drawio/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import os
import os.path
import platform
import shutil
import subprocess
from hashlib import sha1
from pathlib import Path
Expand Down Expand Up @@ -205,14 +206,29 @@ def _drawio_export(self, input_abspath, options, out_filename):
):
return export_abspath

drawio_in_path = shutil.which("drawio")
draw_dot_io_in_path = shutil.which("draw.io")
WINDOWS_PATH = r"C:\Program Files\draw.io\draw.io.exe"
MACOS_PATH = "/Applications/draw.io.app/Contents/MacOS/draw.io"
LINUX_PATH = "/opt/drawio/drawio"
LINUX_OLD_PATH = "/opt/draw.io/drawio"

if builder.config.drawio_binary_path:
binary_path = builder.config.drawio_binary_path
elif platform.system() == "Windows":
binary_path = r"C:\Program Files\draw.io\draw.io.exe"
elif drawio_in_path:
binary_path = drawio_in_path
elif draw_dot_io_in_path:
binary_path = draw_dot_io_in_path
elif platform.system() == "Windows" and os.path.isfile(WINDOWS_PATH):
binary_path = WINDOWS_PATH
elif platform.system() == "Darwin" and os.path.isfile(MACOS_PATH):
binary_path = MACOS_PATH
elif platform.system() == "Linux" and os.path.isfile(LINUX_PATH):
binary_path = LINUX_PATH
elif platform.system() == "Linux" and os.path.isfile(LINUX_OLD_PATH):
binary_path = LINUX_OLD_PATH
else:
binary_path = "/opt/drawio/drawio"
if not os.path.isfile(binary_path):
binary_path = "/opt/draw.io/drawio"
raise DrawIOError("No drawio executable found")

scale_args = ["--scale", scale]
if output_format == "pdf" and float(scale) == 1.0:
Expand Down

0 comments on commit f8e82e7

Please sign in to comment.