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

Use lazy import for Matplotlib and move cache to runpath #1148

Merged
Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Use lazy import for Matplotlib and move cache to runpath.
13 changes: 9 additions & 4 deletions testplan/exporters/testing/pdf/renderers/entries/baseUtils.py
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we rename this file to maybe utils.py in this pr as well?

Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,9 @@
import tempfile
import uuid

import matplotlib
from reportlab.lib.units import inch
from reportlab.platypus import Image

matplotlib.use("Agg")
import matplotlib.pyplot as plot


def export_plot_to_image(graph_plot):
"""Convert a MatPlot plot into an image readable in the pdf."""
Expand Down Expand Up @@ -81,6 +77,11 @@ def get_matlib_plot(source):
Call the appropriate plotting function based on
whether a graph or chart is being plotted.
"""
import matplotlib

matplotlib.use("Agg")
import matplotlib.pyplot as plot
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this line can be removed?


graph_type = source["graph_type"]

valid_graph_types = [
Expand All @@ -106,6 +107,8 @@ def plot_graph(source, graph_type):
Create a MatPlot plot for any graph requiring
axis (and can therefore use the get_xy_coords function.)
"""
import matplotlib.pyplot as plot

data = source["graph_data"]
graph_options = source["graph_options"]
series_options = source["series_options"]
Expand Down Expand Up @@ -170,6 +173,8 @@ def plot_graph(source, graph_type):

def plot_chart(source, graph_type):
"""Create a MatPlot plot for any chart not requiring axes."""
import matplotlib.pyplot as plot

data = source["graph_data"]

for entry in data:
Expand Down
5 changes: 5 additions & 0 deletions testplan/runnable/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,6 +1072,11 @@ def make_runpath_dirs(self):
)
makedirs(self.resource_monitor_server_file_path)

if not os.environ.get("MPLCONFIGDIR"):
os.environ["MPLCONFIGDIR"] = os.path.join(
self._runpath, "matplotlib"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

does matplotlib complain if the dir pointed by MPLCONFIGDIR doesn't exist?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it tries to create that.

This sets the path to the runpath, if the user didn't set the env var. This way there will be no warning, and the dir is only created when the matplotlib import executes. If I remove this default setting, there will be a warning when matplotlib is imported (and the default location is read-only).

)

def _start_resource_monitor(self):
"""Start resource monitor server and client"""
if self.cfg.resource_monitor:
Expand Down