diff --git a/HACKING.rst b/HACKING.rst index 2971d27b..d80b7aa1 100644 --- a/HACKING.rst +++ b/HACKING.rst @@ -44,3 +44,9 @@ Logs - [S373] LOG.debug never used for translations - [S374] You used a deprecated log level + +Importing json +-------------- + +- [S375] It's more preferable to use ``jsonutils`` from ``oslo_serialization`` + instead of ``json`` for operating with ``json`` objects. diff --git a/sahara/db/templates/api.py b/sahara/db/templates/api.py index 858b9b48..7398d0f1 100644 --- a/sahara/db/templates/api.py +++ b/sahara/db/templates/api.py @@ -13,12 +13,12 @@ # limitations under the License. import copy -import json import os import uuid import jsonschema from oslo_config import cfg +from oslo_serialization import jsonutils as json import six from sahara import conductor diff --git a/sahara/plugins/cdh/client/http_client.py b/sahara/plugins/cdh/client/http_client.py index c03f2243..925f20cb 100644 --- a/sahara/plugins/cdh/client/http_client.py +++ b/sahara/plugins/cdh/client/http_client.py @@ -23,13 +23,13 @@ # We also change some importings to use Sahara inherited classes. import cookielib -import json import posixpath import types import urllib import urllib2 from oslo_log import log as logging +from oslo_serialization import jsonutils as json import six from sahara.i18n import _LW diff --git a/sahara/plugins/cdh/client/resource.py b/sahara/plugins/cdh/client/resource.py index 5783d403..939d543b 100644 --- a/sahara/plugins/cdh/client/resource.py +++ b/sahara/plugins/cdh/client/resource.py @@ -22,12 +22,12 @@ # To satisfy the pep8 and python3 tests, we did some changes to the codes. # We also change some importings to use Sahara inherited classes. -import json import posixpath import socket import urllib2 from oslo_log import log as logging +from oslo_serialization import jsonutils as json import six from sahara import context diff --git a/sahara/plugins/cdh/client/services.py b/sahara/plugins/cdh/client/services.py index 6d31960a..528dfd85 100644 --- a/sahara/plugins/cdh/client/services.py +++ b/sahara/plugins/cdh/client/services.py @@ -22,8 +22,7 @@ # To satisfy the pep8 and python3 tests, we did some changes to the codes. # We also change some importings to use Sahara inherited classes. -import json - +from oslo_serialization import jsonutils as json import six from sahara.plugins.cdh.client import role_config_groups diff --git a/sahara/plugins/cdh/client/types.py b/sahara/plugins/cdh/client/types.py index d9148fe8..6dbfd878 100644 --- a/sahara/plugins/cdh/client/types.py +++ b/sahara/plugins/cdh/client/types.py @@ -24,9 +24,9 @@ import copy import datetime -import json import time +from oslo_serialization import jsonutils as json import six from sahara import context diff --git a/sahara/plugins/cdh/v5/config_helper.py b/sahara/plugins/cdh/v5/config_helper.py index f50c7efd..66e2ae4e 100644 --- a/sahara/plugins/cdh/v5/config_helper.py +++ b/sahara/plugins/cdh/v5/config_helper.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils as json from sahara.plugins import provisioning as p from sahara.utils import files as f diff --git a/sahara/plugins/cdh/v5/resources/cdh_config.py b/sahara/plugins/cdh/v5/resources/cdh_config.py index 6d150e20..86839724 100644 --- a/sahara/plugins/cdh/v5/resources/cdh_config.py +++ b/sahara/plugins/cdh/v5/resources/cdh_config.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils as json from sahara.plugins.cdh.client import api_client diff --git a/sahara/plugins/cdh/v5_3_0/config_helper.py b/sahara/plugins/cdh/v5_3_0/config_helper.py index 4f92cddf..145e59e6 100644 --- a/sahara/plugins/cdh/v5_3_0/config_helper.py +++ b/sahara/plugins/cdh/v5_3_0/config_helper.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils as json from sahara.plugins import provisioning as p from sahara.utils import files as f diff --git a/sahara/plugins/cdh/v5_3_0/resources/cdh_config.py b/sahara/plugins/cdh/v5_3_0/resources/cdh_config.py index 062d2473..b9fd0d88 100644 --- a/sahara/plugins/cdh/v5_3_0/resources/cdh_config.py +++ b/sahara/plugins/cdh/v5_3_0/resources/cdh_config.py @@ -13,7 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json +from oslo_serialization import jsonutils as json from sahara.plugins.cdh.client import api_client diff --git a/sahara/plugins/hdp/versions/version_1_3_2/versionhandler.py b/sahara/plugins/hdp/versions/version_1_3_2/versionhandler.py index c0d47a37..79627ac2 100644 --- a/sahara/plugins/hdp/versions/version_1_3_2/versionhandler.py +++ b/sahara/plugins/hdp/versions/version_1_3_2/versionhandler.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils as json import pkg_resources as pkg from sahara import context diff --git a/sahara/plugins/hdp/versions/version_2_0_6/versionhandler.py b/sahara/plugins/hdp/versions/version_2_0_6/versionhandler.py index f2c11599..c6fd94bc 100644 --- a/sahara/plugins/hdp/versions/version_2_0_6/versionhandler.py +++ b/sahara/plugins/hdp/versions/version_2_0_6/versionhandler.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils as json import pkg_resources as pkg import six diff --git a/sahara/plugins/mapr/base/base_node_manager.py b/sahara/plugins/mapr/base/base_node_manager.py index 13152e89..841e1625 100644 --- a/sahara/plugins/mapr/base/base_node_manager.py +++ b/sahara/plugins/mapr/base/base_node_manager.py @@ -13,10 +13,10 @@ # under the License. -import json import random from oslo_log import log as logging +from oslo_serialization import jsonutils as json from oslo_utils import timeutils from sahara import context diff --git a/sahara/plugins/mapr/domain/service.py b/sahara/plugins/mapr/domain/service.py index 9b260733..d8fac227 100644 --- a/sahara/plugins/mapr/domain/service.py +++ b/sahara/plugins/mapr/domain/service.py @@ -13,7 +13,7 @@ # under the License. -import json +from oslo_serialization import jsonutils as json from sahara import context import sahara.exceptions as e diff --git a/sahara/service/edp/oozie/oozie.py b/sahara/service/edp/oozie/oozie.py index b604facd..ea470e86 100644 --- a/sahara/service/edp/oozie/oozie.py +++ b/sahara/service/edp/oozie/oozie.py @@ -13,9 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json import re +from oslo_serialization import jsonutils as json from six.moves.urllib import parse as urlparse import sahara.exceptions as ex diff --git a/sahara/service/trusts.py b/sahara/service/trusts.py index 9647cfff..061d50c1 100644 --- a/sahara/service/trusts.py +++ b/sahara/service/trusts.py @@ -13,10 +13,9 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import cfg from oslo_log import log as logging +from oslo_serialization import jsonutils as json import six from sahara import conductor as c diff --git a/sahara/tests/integration/tests/gating/test_mapr_gating.py b/sahara/tests/integration/tests/gating/test_mapr_gating.py index 681646e9..c3154952 100644 --- a/sahara/tests/integration/tests/gating/test_mapr_gating.py +++ b/sahara/tests/integration/tests/gating/test_mapr_gating.py @@ -12,13 +12,13 @@ # License for the specific language governing permissions and limitations # under the License. -import json import random import string import time import uuid import fixtures +from oslo_serialization import jsonutils as json import six from sahara.tests.integration.tests import base as b diff --git a/sahara/tests/scenario/base.py b/sahara/tests/scenario/base.py index e612bec3..de8f14d9 100644 --- a/sahara/tests/scenario/base.py +++ b/sahara/tests/scenario/base.py @@ -16,7 +16,6 @@ from __future__ import print_function import functools import glob -import json import logging import os import sys @@ -24,6 +23,7 @@ import time import traceback import fixtures +from oslo_serialization import jsonutils as json from oslo_utils import timeutils import prettytable import six diff --git a/sahara/tests/unit/db/templates/test_update.py b/sahara/tests/unit/db/templates/test_update.py index d6e9bdeb..26a0c483 100644 --- a/sahara/tests/unit/db/templates/test_update.py +++ b/sahara/tests/unit/db/templates/test_update.py @@ -13,12 +13,12 @@ # limitations under the License. import copy -import json import tempfile import uuid import jsonschema import mock +from oslo_serialization import jsonutils as json from oslo_utils import uuidutils from sahara import context diff --git a/sahara/tests/unit/service/edp/test_json_api_examples.py b/sahara/tests/unit/service/edp/test_json_api_examples.py index 6991377b..45503b64 100644 --- a/sahara/tests/unit/service/edp/test_json_api_examples.py +++ b/sahara/tests/unit/service/edp/test_json_api_examples.py @@ -14,10 +14,10 @@ # limitations under the License. import itertools -import json import os import uuid +from oslo_serialization import jsonutils as json import testtools from sahara.service.validations.edp import data_source diff --git a/sahara/tests/unit/utils/test_hacking.py b/sahara/tests/unit/utils/test_hacking.py index 6251b542..d7387906 100644 --- a/sahara/tests/unit/utils/test_hacking.py +++ b/sahara/tests/unit/utils/test_hacking.py @@ -49,3 +49,18 @@ class HackingTestCase(testtools.TestCase): self.assertEqual(0, len(list(checks.dict_constructor_with_list_copy( " self._render_dict(xml, data_el, data.__dict__)")))) + + def test_use_jsonutils(self): + self.assertEqual(0, len(list(checks.use_jsonutils( + "import json # noqa", "path")))) + self.assertEqual(0, len(list(checks.use_jsonutils( + "from oslo_serialization import jsonutils as json", "path")))) + self.assertEqual(0, len(list(checks.use_jsonutils( + "import jsonschema", "path")))) + self.assertEqual(0, len(list(checks.use_jsonutils( + "import json", "sahara/openstack/common/utils.py")))) + + self.assertEqual(1, len(list(checks.use_jsonutils( + "import json", "path")))) + self.assertEqual(1, len(list(checks.use_jsonutils( + "import json as jsonutils", "path")))) diff --git a/sahara/utils/hacking/checks.py b/sahara/utils/hacking/checks.py index e6ba0d4f..c1d34030 100644 --- a/sahara/utils/hacking/checks.py +++ b/sahara/utils/hacking/checks.py @@ -13,6 +13,7 @@ # See the License for the specific language governing permissions and # limitations under the License. +import pep8 import re import tokenize @@ -97,6 +98,25 @@ def dict_constructor_with_list_copy(logical_line): 'constructor with a sequence of key-value pairs.') +def use_jsonutils(logical_line, filename): + """Check to prevent importing json in sahara code. + + S375 + """ + if pep8.noqa(logical_line): + return + ignore_dirs = ["sahara/openstack/common"] + for dir in ignore_dirs: + if dir in filename: + return + invalid_line = re.compile(r"(import\s+json)") + valid_line = re.compile(r"(import\s+jsonschema)") + if (re.match(invalid_line, logical_line) and + not re.match(valid_line, logical_line)): + yield(0, "S375: Use jsonutils from oslo_serialization instead" + " of json") + + def factory(register): register(import_db_only_in_conductor) register(hacking_no_author_attr) @@ -109,3 +129,4 @@ def factory(register): register(logging_checks.validate_log_translations) register(logging_checks.no_translate_debug_logs) register(logging_checks.accepted_log_levels) + register(use_jsonutils) diff --git a/sahara/utils/openstack/base.py b/sahara/utils/openstack/base.py index 304b9e2d..f48b6ae3 100644 --- a/sahara/utils/openstack/base.py +++ b/sahara/utils/openstack/base.py @@ -13,9 +13,8 @@ # See the License for the specific language governing permissions and # limitations under the License. -import json - from oslo_config import cfg +from oslo_serialization import jsonutils as json from six.moves.urllib import parse as urlparse from sahara import context