Improve support for 3-rd party platforms

This commit is contained in:
Ivan Kravets
2014-11-24 21:36:44 +02:00
parent b103dc01c0
commit cd2cc16fcf
6 changed files with 42 additions and 26 deletions

View File

@ -11,7 +11,7 @@ def cli():
installed_platforms = PlatformFactory.get_platforms( installed_platforms = PlatformFactory.get_platforms(
installed=True).keys() installed=True).keys()
sorted(installed_platforms) installed_platforms = sorted(installed_platforms)
for platform in installed_platforms: for platform in installed_platforms:
p = PlatformFactory().newPlatform(platform) p = PlatformFactory().newPlatform(platform)

View File

@ -11,7 +11,7 @@ def cli():
installed_platforms = PlatformFactory.get_platforms( installed_platforms = PlatformFactory.get_platforms(
installed=True).keys() installed=True).keys()
sorted(installed_platforms) installed_platforms = sorted(installed_platforms)
for platform in installed_platforms: for platform in installed_platforms:
echo("\nPlatform %s" % style(platform, fg="cyan")) echo("\nPlatform %s" % style(platform, fg="cyan"))

View File

@ -28,9 +28,6 @@ class AtmelavrPlatform(BasePlatform):
} }
} }
def get_name(self):
return "atmelavr"
def after_run(self, result): def after_run(self, result):
# fix STDERR "flash written" for avrdude # fix STDERR "flash written" for avrdude
if "flash written" in result['err']: if "flash written" in result['err']:

View File

@ -8,18 +8,35 @@ from os.path import isfile, join
from platformio.exception import (BuildScriptNotFound, PlatformNotInstalledYet, from platformio.exception import (BuildScriptNotFound, PlatformNotInstalledYet,
UnknownPackage, UnknownPlatform) UnknownPackage, UnknownPlatform)
from platformio.pkgmanager import PackageManager 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): class PlatformFactory(object):
@staticmethod
def get_clsname(name):
return "%sPlatform" % name.title()
@staticmethod @staticmethod
def get_platforms(installed=False): def get_platforms(installed=False):
platforms = {} platforms = {}
for p in listdir(join(get_source_dir(), "platforms")): for d in (get_home_dir(), get_source_dir()):
if p in ("__init__.py", "base.py") or not p.endswith(".py"): pdir = join(d, "platforms")
continue for p in listdir(pdir):
platforms[p[:-3]] = join(get_source_dir(), "platforms", p) 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: if not installed:
return platforms return platforms
@ -31,20 +48,28 @@ class PlatformFactory(object):
installed_platforms[name] = platforms[name] installed_platforms[name] = platforms[name]
return installed_platforms 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 @staticmethod
def newPlatform(name): def newPlatform(name):
platforms = PlatformFactory.get_platforms() platforms = PlatformFactory.get_platforms()
clsname = "%sPlatform" % name.title() if name not in platforms:
try:
assert name in platforms
mod = load_source(
"platformio.platforms.%s" % name, platforms[name])
except (AssertionError, ImportError):
raise UnknownPlatform(name) raise UnknownPlatform(name)
obj = getattr(mod, clsname)() _instance = getattr(
assert isinstance(obj, BasePlatform) PlatformFactory.load_module(name, platforms[name]),
return obj PlatformFactory.get_clsname(name)
)()
assert isinstance(_instance, BasePlatform)
return _instance
class BasePlatform(object): class BasePlatform(object):
@ -52,7 +77,7 @@ class BasePlatform(object):
PACKAGES = {} PACKAGES = {}
def get_name(self): def get_name(self):
raise NotImplementedError() return self.__class__.__name__[:-8].lower()
def get_build_script(self): def get_build_script(self):
builtin = join(get_source_dir(), "builder", "scripts", "%s.py" % builtin = join(get_source_dir(), "builder", "scripts", "%s.py" %

View File

@ -27,6 +27,3 @@ class Timsp430Platform(BasePlatform):
"default": True "default": True
} }
} }
def get_name(self):
return "timsp430"

View File

@ -27,6 +27,3 @@ class TitivaPlatform(BasePlatform):
"default": True "default": True
} }
} }
def get_name(self):
return "titiva"