2014-06-07 13:34:31 +03:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
|
|
|
from os.path import join
|
2014-06-13 20:47:02 +03:00
|
|
|
from shutil import rmtree
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-06-13 20:47:02 +03:00
|
|
|
from platformio.exception import UnknownPackage, UnknownPlatform
|
2014-06-07 13:34:31 +03:00
|
|
|
from platformio.pkgmanager import PackageManager
|
2014-06-13 20:47:02 +03:00
|
|
|
from platformio.util import exec_command, get_platforms, get_source_dir
|
2014-06-07 13:34:31 +03:00
|
|
|
|
|
|
|
|
|
|
|
class PlatformFactory(object):
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def newPlatform(name):
|
|
|
|
clsname = "%sPlatform" % name.title()
|
|
|
|
try:
|
2014-06-13 20:47:02 +03:00
|
|
|
assert name in get_platforms()
|
2014-06-07 13:34:31 +03:00
|
|
|
mod = __import__("platformio.platforms." + name.lower(),
|
|
|
|
None, None, [clsname])
|
2014-06-13 20:47:02 +03:00
|
|
|
except (AssertionError, ImportError):
|
2014-06-07 13:34:31 +03:00
|
|
|
raise UnknownPlatform(name)
|
|
|
|
|
|
|
|
obj = getattr(mod, clsname)()
|
|
|
|
assert isinstance(obj, BasePlatform)
|
|
|
|
return obj
|
|
|
|
|
|
|
|
|
|
|
|
class BasePlatform(object):
|
|
|
|
|
|
|
|
PACKAGES = {}
|
|
|
|
|
|
|
|
def get_name(self):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2014-06-12 23:29:47 +03:00
|
|
|
def get_short_info(self):
|
|
|
|
if self.__doc__:
|
|
|
|
doclines = [l.strip() for l in self.__doc__.splitlines()]
|
|
|
|
return " ".join(doclines).strip()
|
|
|
|
else:
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
2014-07-30 23:39:01 +03:00
|
|
|
def get_pkg_alias(self, pkgname):
|
|
|
|
return self.PACKAGES[pkgname].get("alias", None)
|
|
|
|
|
2014-07-30 23:08:36 +03:00
|
|
|
def pkg_aliases_to_names(self, aliases):
|
|
|
|
names = []
|
|
|
|
for alias in aliases:
|
|
|
|
name = alias
|
|
|
|
# lookup by packages alias
|
|
|
|
if name not in self.PACKAGES:
|
|
|
|
for _name, _opts in self.PACKAGES.items():
|
|
|
|
if _opts.get("alias", None) == alias:
|
|
|
|
name = _name
|
|
|
|
break
|
|
|
|
names.append(name)
|
|
|
|
return names
|
2014-07-30 22:40:11 +03:00
|
|
|
|
|
|
|
def install(self, with_packages, without_packages, skip_default_packages):
|
2014-07-30 23:08:36 +03:00
|
|
|
with_packages = set(self.pkg_aliases_to_names(with_packages))
|
|
|
|
without_packages = set(self.pkg_aliases_to_names(without_packages))
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-07-30 22:40:11 +03:00
|
|
|
upkgs = with_packages | without_packages
|
2014-06-07 13:34:31 +03:00
|
|
|
ppkgs = set(self.PACKAGES.keys())
|
2014-07-30 23:08:36 +03:00
|
|
|
if not upkgs.issubset(ppkgs):
|
|
|
|
raise UnknownPackage(", ".join(upkgs - ppkgs))
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-07-30 22:40:11 +03:00
|
|
|
requirements = []
|
|
|
|
for name, opts in self.PACKAGES.items():
|
2014-06-07 13:34:31 +03:00
|
|
|
if name in without_packages:
|
|
|
|
continue
|
2014-07-30 22:40:11 +03:00
|
|
|
elif (name in with_packages or (not skip_default_packages and
|
|
|
|
opts['default'])):
|
|
|
|
requirements.append((name, opts['path']))
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-07-30 22:40:11 +03:00
|
|
|
pm = PackageManager(self.get_name())
|
2014-06-13 20:47:02 +03:00
|
|
|
for (package, path) in requirements:
|
|
|
|
pm.install(package, path)
|
2014-07-30 22:40:11 +03:00
|
|
|
return len(requirements)
|
2014-06-13 20:47:02 +03:00
|
|
|
|
|
|
|
def uninstall(self):
|
|
|
|
platform = self.get_name()
|
|
|
|
pm = PackageManager(platform)
|
2014-06-07 13:34:31 +03:00
|
|
|
|
2014-07-30 22:40:11 +03:00
|
|
|
for package, data in pm.get_installed(platform).items():
|
2014-06-13 20:47:02 +03:00
|
|
|
pm.uninstall(package, data['path'])
|
|
|
|
|
|
|
|
pm.unregister_platform(platform)
|
|
|
|
rmtree(pm.get_platform_dir())
|
2014-06-07 13:34:31 +03:00
|
|
|
return True
|
|
|
|
|
2014-06-13 20:47:02 +03:00
|
|
|
def update(self):
|
|
|
|
platform = self.get_name()
|
|
|
|
pm = PackageManager(platform)
|
|
|
|
for package in pm.get_installed(platform).keys():
|
|
|
|
pm.update(package)
|
2014-06-07 13:34:31 +03:00
|
|
|
|
|
|
|
def run(self, variables, targets):
|
|
|
|
assert isinstance(variables, list)
|
|
|
|
assert isinstance(targets, list)
|
|
|
|
|
|
|
|
if "clean" in targets:
|
|
|
|
targets.remove("clean")
|
|
|
|
targets.append("-c")
|
|
|
|
|
|
|
|
result = exec_command([
|
|
|
|
"scons",
|
|
|
|
"-Q",
|
|
|
|
"-f", join(get_source_dir(), "builder", "main.py")
|
|
|
|
] + variables + targets)
|
|
|
|
|
|
|
|
return self.after_run(result)
|
2014-06-13 20:47:02 +03:00
|
|
|
|
|
|
|
def after_run(self, result): # pylint: disable=R0201
|
|
|
|
return result
|