refactor: make safe_lxml an ordinary folder in openedx/core/lib (#25689)

This commit is contained in:
Kyle McCormick
2022-05-24 06:35:23 -04:00
committed by GitHub
parent 2ef25e3cfe
commit 25df9ca420
24 changed files with 58 additions and 39 deletions

View File

@@ -8,7 +8,7 @@ Taken from: https://celery.readthedocs.org/en/latest/django/first-steps-with-dja
import os
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()

View File

@@ -16,7 +16,7 @@ import pytest
from openedx.core.pytest_hooks import DeferPlugin
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs # isort:skip # lint-amnesty, pylint: disable=wrong-import-order
from openedx.core.lib.safe_lxml import defuse_xml_libs # isort:skip
defuse_xml_libs()

View File

@@ -12,7 +12,7 @@ isort:skip_file
"""
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()
import os # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position

View File

@@ -5,7 +5,7 @@
import pytest
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
# This import is needed for pytest plugin configuration, so please avoid deleting this during refactoring
from openedx.core.pytest_hooks import pytest_configure # pylint: disable=unused-import

View File

@@ -1,16 +0,0 @@
"""
Setup.py for safe_lxml.
"""
from setuptools import setup
setup(
name="safe_lxml",
version="1.0",
packages=["safe_lxml"],
install_requires=[
"lxml",
"defusedxml"
],
)

View File

@@ -1,4 +1,6 @@
"""Code run by pylint before running any tests."""
from safe_lxml import defuse_xml_libs
# Patch the xml libs before anything else.
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()

View File

@@ -21,7 +21,6 @@ root = Path('../..').abspath()
sys.path.insert(0, root)
sys.path.append(root / "docs/guides")
sys.path.append(root / "common/lib/capa")
sys.path.append(root / "common/lib/safe_lxml")
sys.path.append(root / "common/lib/xmodule")
# Use a settings module that allows all LMS and Studio code to be imported
@@ -223,7 +222,6 @@ autodoc_mock_imports = [
modules = {
'cms': 'cms',
'common/lib/capa/capa': 'common/lib/capa',
'common/lib/safe_lxml/safe_lxml': 'common/lib/safe_lxml',
'common/lib/xmodule/xmodule': 'common/lib/xmodule',
'lms': 'lms',
'openedx': 'openedx',

View File

@@ -9,5 +9,4 @@ out from edx-platform into separate packages at some point.
:maxdepth: 2
common/lib/capa/modules
common/lib/safe_lxml/modules
common/lib/xmodule/modules

View File

@@ -8,7 +8,7 @@ Taken from: https://celery.readthedocs.org/en/latest/django/first-steps-with-dja
import os
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()

View File

@@ -9,7 +9,7 @@ It exposes a module-level variable named ``application``. Django's
"""
# Patch the xml libs
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()
import os # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position

View File

@@ -6,7 +6,7 @@ It exposes a module-level variable named ``application``.
"""
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()
import os # lint-amnesty, pylint: disable=wrong-import-order, wrong-import-position

View File

@@ -17,7 +17,7 @@ from openedx.core.lib.logsettings import log_python_warnings
log_python_warnings()
# Patch the xml libs before anything else.
from safe_lxml import defuse_xml_libs
from openedx.core.lib.safe_lxml import defuse_xml_libs # isort:skip
defuse_xml_libs()
import importlib

View File

@@ -0,0 +1,7 @@
"""
Code run by pytest before running any tests in the safe_lxml directory.
"""
from openedx.core.lib.safe_lxml import defuse_xml_libs
defuse_xml_libs()

View File

@@ -5,6 +5,8 @@ It overrides some unsafe functions from lxml.etree with safer versions from defu
It also includes a safer XMLParser.
For processing xml always prefer this over using lxml.etree directly.
isort:skip_file
"""
# Names are imported into this module so that it can be a stand-in for

View File

@@ -1,4 +1,12 @@
"""Test that we have defused XML."""
"""
Test that we have defused XML.
For these tests, the defusing will happen in one or more of the `conftest.py`
files that runs at pytest startup calls `defuse_xml_libs()`.
In production, the defusing happens when the LMS or Studio `wsgi.py` files
call `defuse_xml_libs()`.
"""
import defusedxml

View File

@@ -14,7 +14,7 @@ from xblock.fields import NO_CACHE_VALUE, UNIQUE_ID, ScopeIds
from xblock.runtime import Runtime
from openedx.core.lib.xblock_builtin.xblock_discussion.xblock_discussion import DiscussionXBlock
from safe_lxml import etree # lint-amnesty, pylint: disable=wrong-import-order
from openedx.core.lib.safe_lxml import etree
def attribute_pair_repr(self):

View File

@@ -22,8 +22,6 @@
# via -r requirements/edx/local.in
-e git+https://github.com/edx/RateXBlock.git@2.0.1#egg=rate-xblock
# via -r requirements/edx/github.in
-e common/lib/safe_lxml
# via -r requirements/edx/local.in
-e common/lib/sandbox-packages
# via -r requirements/edx/local.in
-e openedx/core/lib/xblock_builtin/xblock_discussion

View File

@@ -22,8 +22,6 @@
# via -r requirements/edx/testing.txt
-e git+https://github.com/edx/RateXBlock.git@2.0.1#egg=rate-xblock
# via -r requirements/edx/testing.txt
-e common/lib/safe_lxml
# via -r requirements/edx/testing.txt
-e common/lib/sandbox-packages
# via -r requirements/edx/testing.txt
-e openedx/core/lib/xblock_builtin/xblock_discussion

View File

@@ -1,7 +1,6 @@
# Python libraries to install that are local to the edx-platform repo
-e .
-e common/lib/capa
-e common/lib/safe_lxml
-e common/lib/sandbox-packages
-e common/lib/xmodule

View File

@@ -22,8 +22,6 @@
# via -r requirements/edx/base.txt
-e git+https://github.com/edx/RateXBlock.git@2.0.1#egg=rate-xblock
# via -r requirements/edx/base.txt
-e common/lib/safe_lxml
# via -r requirements/edx/base.txt
-e common/lib/sandbox-packages
# via -r requirements/edx/base.txt
-e openedx/core/lib/xblock_builtin/xblock_discussion

27
safe_lxml/__init__.py Normal file
View File

@@ -0,0 +1,27 @@
"""
Temporary import path shim module.
Previously, the safe_lxml package was housed in common/lib/safe_lxml.
It was installed as its own Python project, so instead of its import path
being, as one would expect:
import common.lib.safe_lxml.safe_lxml
it was instead just:
import safe_lxml
To increase the sanity of edx-platform and simplify its tooling, we are
moving the safe_lxml package to openedx/core/lib (in tihs same repo) and
changing its import path to:
import openedx.core.lib.safe_lxml
In order to maintain backwards-compatibility with code using the
old import path for one release, we expose this compatibility module.
Jira ticket (public, but requires account): https://openedx.atlassian.net/browse/BOM-2583
Target removal for this shim module: by Olive.
"""
from openedx.core.lib.safe_lxml import * # pylint: disable=unused-wildcard-import,wrong-import-order

View File

@@ -38,7 +38,7 @@ exclude+='|^common/test/data/?.*$'
# * common/lib/xmodule -> EXCLUDE from check.
# * common/lib/xmodule/xmodule/modulestore -> INCLUDE in check.
exclude+='|^common/lib$'
exclude+='|^common/lib/(capa|safe_lxml|sandbox-packages|xmodule)$'
exclude+='|^common/lib/(capa|sandbox-packages|xmodule)$'
# Docs, scripts.
exclude+='|^docs/.*$'

View File

@@ -47,4 +47,3 @@ multi_line_output=3
skip=
envs
migrations
common/lib/safe_lxml/safe_lxml/etree.py