1# This file is part of Scapy
2# See http://www.secdev.org/projects/scapy for more information
3# Copyright (C) Philippe Biondi <phil@secdev.org>
4# This program is published under a GPLv2 license
5
6"""
7External link to programs
8"""
9
10import os
11import subprocess
12from scapy.error import log_loading
13
14# Notice: this file must not be called before main.py, if started
15# in interactive mode, because it needs to be called after the
16# logger has been setup, to be able to print the warning messages
17
18# MATPLOTLIB
19
20try:
21    from matplotlib import get_backend as matplotlib_get_backend
22    from matplotlib import pyplot as plt
23    from matplotlib.lines import Line2D
24    MATPLOTLIB = 1
25    if "inline" in matplotlib_get_backend():
26        MATPLOTLIB_INLINED = 1
27    else:
28        MATPLOTLIB_INLINED = 0
29    MATPLOTLIB_DEFAULT_PLOT_KARGS = {"marker": "+"}
30# RuntimeError to catch gtk "Cannot open display" error
31except (ImportError, RuntimeError):
32    plt = None
33    Line2D = None
34    MATPLOTLIB = 0
35    MATPLOTLIB_INLINED = 0
36    MATPLOTLIB_DEFAULT_PLOT_KARGS = dict()
37    log_loading.info("Can't import matplotlib. Won't be able to plot.")
38
39# PYX
40
41
42def _test_pyx():
43    # type: () -> bool
44    """Returns if PyX is correctly installed or not"""
45    try:
46        with open(os.devnull, 'wb') as devnull:
47            r = subprocess.check_call(["pdftex", "--version"],
48                                      stdout=devnull, stderr=subprocess.STDOUT)
49    except (subprocess.CalledProcessError, OSError):
50        return False
51    else:
52        return r == 0
53
54
55try:
56    import pyx  # noqa: F401
57    if _test_pyx():
58        PYX = 1
59    else:
60        log_loading.info("PyX dependencies are not installed ! Please install TexLive or MikTeX.")  # noqa: E501
61        PYX = 0
62except ImportError:
63    log_loading.info("Can't import PyX. Won't be able to use psdump() or pdfdump().")  # noqa: E501
64    PYX = 0
65