Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve drawio binary finding functionality to use $PATH #71

Merged
merged 1 commit into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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