Skip to content

Commit 9641574

Browse files
authored
Add an env var to disable pkg_resources (#928)
* Add an env var to disable pkg_resources - in some cases, pkg_resources can be slow to both import and use - adds the OTIO_DISABLE_PKG_RESOURCE_PLUGINS environment variable to disable using this library for users that have those issues. - Even for users not experiencing those issues, this speeds up the import time of OTIO slightly. * add env variable doc page * Update docs/tutorials/otio-env-variables.md * Update docs/tutorials/otio-env-variables.md * clarified comment Co-authored-by: ssteinbach <[email protected]>
1 parent 846bfb1 commit 9641574

File tree

4 files changed

+46
-5
lines changed

4 files changed

+46
-5
lines changed

docs/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ Quick Start
2626
:maxdepth: 2
2727

2828
tutorials/quickstart
29+
tutorials/otio-env-variables
2930

3031
Tutorials
3132
------------

docs/tutorials/otio-env-variables.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Environment Variables
2+
3+
This document describes the environment variables that can be used to configure
4+
various aspects of OTIO.
5+
6+
## Plugin Configuration
7+
8+
These variables must be set _before_ the OpenTimelineIO python library is imported.
9+
10+
- `OTIO_PLUGIN_MANIFEST_PATH`: a ":" separated string with paths to .manifest.json files that contain OTIO plugin manifests. See: <a href="write-an-adapter.html" target="_blank">Tutorial on how to write an adapter plugin</a>.
11+
- `OTIO_DEFAULT_MEDIA_LINKER`: the name of the default media linker to use after reading a file, if "" then no media linker is automatically invoked.
12+
- `OTIO_DISABLE_PKG_RESOURCE_PLUGINS`: By default, OTIO will use the pkg_resource entry_points mechanism to discover plugins that have been installed into the current python environment. pkg_resources, however, can be slow in certain cases, so for users who wish to disable this behavior, this variable can be set to 1.
13+
14+
## Unit tests
15+
16+
These variables only impact unit tests.
17+
18+
- `OTIO_DISABLE_SHELLOUT_TESTS`: When running the unit tests, skip the console tests that run the otiocat program and check output through the shell. This is desirable in environments where running the commandline tests is not meaningful or problematic. Does not disable the tests that run through python calling mechanisms.

src/py-opentimelineio/opentimelineio/plugins/manifest.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,17 @@
2828
import logging
2929
import os
3030

31-
# on some python interpreters, pkg_resources is not available
32-
try:
33-
import pkg_resources
34-
except ImportError:
31+
# In some circumstances pkg_resources has bad performance characteristics.
32+
# Using the envirionment variable: $OTIO_DISABLE_PKG_RESOURCE_PLUGINS disables
33+
# OpenTimelineIO's import and of use of the pkg_resources module.
34+
if os.environ.get("OTIO_DISABLE_PKG_RESOURCE_PLUGINS", False):
3535
pkg_resources = None
36+
else:
37+
try:
38+
# on some python interpreters, pkg_resources is not available
39+
import pkg_resources
40+
except ImportError:
41+
pkg_resources = None
3642

3743
from .. import (
3844
core,

tests/test_plugin_detection.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import os
2727
import pkg_resources
2828
import sys
29+
import importlib
2930

3031
try:
3132
# Python 3.3 forward includes the mock module
@@ -75,7 +76,8 @@ def setUp(self):
7576
def tearDown(self):
7677
self.sys_patch.stop()
7778
self.entry_patcher.stop()
78-
del(sys.modules['otio_mockplugin'])
79+
if 'otio_mockplugin' in sys.modules:
80+
del(sys.modules['otio_mockplugin'])
7981

8082
def test_detect_plugin(self):
8183
"""This manifest uses the plugin_manifest function"""
@@ -98,6 +100,20 @@ def test_detect_plugin(self):
98100
for linker in man.media_linkers:
99101
self.assertIsInstance(linker, otio.media_linker.MediaLinker)
100102

103+
def test_pkg_resources_disabled(self):
104+
os.environ["OTIO_DISABLE_PKG_RESOURCE_PLUGINS"] = "1"
105+
importlib.reload(otio.plugins.manifest)
106+
107+
# detection of the environment variable happens on import, force a
108+
# reload to ensure that it is triggered
109+
with self.assertRaises(AssertionError):
110+
self.test_detect_plugin()
111+
112+
# remove the environment variable and reload again for usage in the
113+
# other tests
114+
del os.environ["OTIO_DISABLE_PKG_RESOURCE_PLUGINS"]
115+
importlib.reload(otio.plugins.manifest)
116+
101117
def test_detect_plugin_json_manifest(self):
102118
# Test detecting a plugin that rather than exposing the plugin_manifest
103119
# function, just simply has a plugin_manifest.json provided at the

0 commit comments

Comments
 (0)