forked from manubot/manubot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.py
58 lines (52 loc) · 1.61 KB
/
util.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import logging
import shutil
import subprocess
import functools
@functools.lru_cache()
def get_pandoc_info():
"""
Return path and version information for the system's pandoc and
pandoc-citeproc commands. When Pandoc is installed,
the output will look like:
```python
{
'pandoc': True,
'pandoc path': '/PATH_TO_EXECUTABLES/pandoc',
'pandoc version': (2, 5),
'pandoc-citeproc': True,
'pandoc-citeproc path': '/PATH_TO_EXECUTABLES/pandoc-citeproc',
'pandoc-citeproc version': (0, 15),
}
```
If the executables are missing, the output will be like:
```python
{
'pandoc': False,
'pandoc-citeproc': False,
}
```
"""
stats = dict()
for command in "pandoc", "pandoc-citeproc":
path = shutil.which(command)
stats[command] = bool(path)
if not path:
continue
version = subprocess.check_output(
args=[command, "--version"], universal_newlines=True
)
logging.debug(version)
version, *discard = version.splitlines()
discard, version = version.strip().split()
from packaging.version import parse as parse_version
version = parse_version(version).release
stats[f"{command} version"] = version
stats[f"{command} path"] = path
logging.info("\n".join(f"{k}: {v}" for k, v in stats.items()))
return stats
def get_pandoc_version() -> (int, int, int):
"""
Return pandoc version as tuple of major and minor
version numbers, for example: (2, 7, 2)
"""
return get_pandoc_info()["pandoc version"]