From cd2cc16fcfef812bd3fc8ce8df297c2f9b95140e Mon Sep 17 00:00:00 2001 From: Ivan Kravets Date: Mon, 24 Nov 2014 21:36:44 +0200 Subject: [PATCH] Improve support for 3-rd party platforms --- platformio/commands/list.py | 2 +- platformio/commands/update.py | 2 +- platformio/platforms/atmelavr.py | 3 -- platformio/platforms/base.py | 55 +++++++++++++++++++++++--------- platformio/platforms/timsp430.py | 3 -- platformio/platforms/titiva.py | 3 -- 6 files changed, 42 insertions(+), 26 deletions(-) diff --git a/platformio/commands/list.py b/platformio/commands/list.py index 7650164f..eba254ce 100644 --- a/platformio/commands/list.py +++ b/platformio/commands/list.py @@ -11,7 +11,7 @@ def cli(): installed_platforms = PlatformFactory.get_platforms( installed=True).keys() - sorted(installed_platforms) + installed_platforms = sorted(installed_platforms) for platform in installed_platforms: p = PlatformFactory().newPlatform(platform) diff --git a/platformio/commands/update.py b/platformio/commands/update.py index 8ccedb2f..285fc45d 100644 --- a/platformio/commands/update.py +++ b/platformio/commands/update.py @@ -11,7 +11,7 @@ def cli(): installed_platforms = PlatformFactory.get_platforms( installed=True).keys() - sorted(installed_platforms) + installed_platforms = sorted(installed_platforms) for platform in installed_platforms: echo("\nPlatform %s" % style(platform, fg="cyan")) diff --git a/platformio/platforms/atmelavr.py b/platformio/platforms/atmelavr.py index c0a9edc4..360b3d10 100644 --- a/platformio/platforms/atmelavr.py +++ b/platformio/platforms/atmelavr.py @@ -28,9 +28,6 @@ class AtmelavrPlatform(BasePlatform): } } - def get_name(self): - return "atmelavr" - def after_run(self, result): # fix STDERR "flash written" for avrdude if "flash written" in result['err']: diff --git a/platformio/platforms/base.py b/platformio/platforms/base.py index e95cc24d..20c99b01 100644 --- a/platformio/platforms/base.py +++ b/platformio/platforms/base.py @@ -8,18 +8,35 @@ from os.path import isfile, join from platformio.exception import (BuildScriptNotFound, PlatformNotInstalledYet, UnknownPackage, UnknownPlatform) from platformio.pkgmanager import PackageManager -from platformio.util import AppState, exec_command, get_source_dir +from platformio.util import (AppState, exec_command, get_home_dir, + get_source_dir) class PlatformFactory(object): + @staticmethod + def get_clsname(name): + return "%sPlatform" % name.title() + @staticmethod def get_platforms(installed=False): platforms = {} - for p in listdir(join(get_source_dir(), "platforms")): - if p in ("__init__.py", "base.py") or not p.endswith(".py"): - continue - platforms[p[:-3]] = join(get_source_dir(), "platforms", p) + for d in (get_home_dir(), get_source_dir()): + pdir = join(d, "platforms") + for p in listdir(pdir): + if p in ("__init__.py", "base.py") or not p.endswith(".py"): + continue + name = p[:-3] + path = join(pdir, p) + try: + isplatform = hasattr( + PlatformFactory.load_module(name, path), + PlatformFactory.get_clsname(name) + ) + if isplatform: + platforms[name] = path + except UnknownPlatform: + pass if not installed: return platforms @@ -31,20 +48,28 @@ class PlatformFactory(object): installed_platforms[name] = platforms[name] return installed_platforms + @staticmethod + def load_module(name, path): + module = None + try: + module = load_source( + "platformio.platforms.%s" % name, path) + except ImportError: + raise UnknownPlatform(name) + return module + @staticmethod def newPlatform(name): platforms = PlatformFactory.get_platforms() - clsname = "%sPlatform" % name.title() - try: - assert name in platforms - mod = load_source( - "platformio.platforms.%s" % name, platforms[name]) - except (AssertionError, ImportError): + if name not in platforms: raise UnknownPlatform(name) - obj = getattr(mod, clsname)() - assert isinstance(obj, BasePlatform) - return obj + _instance = getattr( + PlatformFactory.load_module(name, platforms[name]), + PlatformFactory.get_clsname(name) + )() + assert isinstance(_instance, BasePlatform) + return _instance class BasePlatform(object): @@ -52,7 +77,7 @@ class BasePlatform(object): PACKAGES = {} def get_name(self): - raise NotImplementedError() + return self.__class__.__name__[:-8].lower() def get_build_script(self): builtin = join(get_source_dir(), "builder", "scripts", "%s.py" % diff --git a/platformio/platforms/timsp430.py b/platformio/platforms/timsp430.py index 7cd799c3..51894687 100644 --- a/platformio/platforms/timsp430.py +++ b/platformio/platforms/timsp430.py @@ -27,6 +27,3 @@ class Timsp430Platform(BasePlatform): "default": True } } - - def get_name(self): - return "timsp430" diff --git a/platformio/platforms/titiva.py b/platformio/platforms/titiva.py index d2253114..2b34869f 100644 --- a/platformio/platforms/titiva.py +++ b/platformio/platforms/titiva.py @@ -27,6 +27,3 @@ class TitivaPlatform(BasePlatform): "default": True } } - - def get_name(self): - return "titiva"