Dynamically load refactored dev-platform module

This commit is contained in:
Ivan Kravets
2022-05-18 16:16:51 +03:00
parent 1585b829be
commit 45fcb40a5c
3 changed files with 12 additions and 37 deletions

View File

@ -1,13 +0,0 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

View File

@ -1,16 +0,0 @@
# Copyright (c) 2014-present PlatformIO <contact@platformio.org>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Backward compatibility with legacy dev-platforms
from platformio.platform.base import PlatformBase # pylint: disable=unused-import

View File

@ -14,11 +14,12 @@
import os
import re
import sys
from platformio import fs
from platformio.compat import load_python_module
from platformio.package.meta import PackageItem
from platformio.platform.base import PlatformBase
from platformio.platform import base
from platformio.platform.exception import UnknownPlatform
@ -28,15 +29,18 @@ class PlatformFactory(object):
name = re.sub(r"[^\da-z\_]+", "", name, flags=re.I)
return "%sPlatform" % name.lower().capitalize()
@staticmethod
def load_module(name, path):
@classmethod
def load_platform_module(cls, name, path, by_fallback=False):
try:
return load_python_module("platformio.platform.%s" % name, path)
except ImportError:
except ImportError as exc:
if "platformio.managers" in str(exc) and not by_fallback:
sys.modules["platformio.managers.platform"] = base
return cls.load_platform_module(name, path, by_fallback=True)
raise UnknownPlatform(name)
@classmethod
def new(cls, pkg_or_spec, autoinstall=False) -> PlatformBase:
def new(cls, pkg_or_spec, autoinstall=False) -> base.PlatformBase:
# pylint: disable=import-outside-toplevel
from platformio.package.manager.platform import PlatformPackageManager
@ -72,16 +76,16 @@ class PlatformFactory(object):
platform_cls = None
if os.path.isfile(os.path.join(platform_dir, "platform.py")):
platform_cls = getattr(
cls.load_module(
cls.load_platform_module(
platform_name, os.path.join(platform_dir, "platform.py")
),
cls.get_clsname(platform_name),
)
else:
platform_cls = type(
str(cls.get_clsname(platform_name)), (PlatformBase,), {}
str(cls.get_clsname(platform_name)), (base.PlatformBase,), {}
)
_instance = platform_cls(os.path.join(platform_dir, "platform.json"))
assert isinstance(_instance, PlatformBase)
assert isinstance(_instance, base.PlatformBase)
return _instance