Skip to content

Commit 027c3d0

Browse files
committed
Implemented new commands: list, show, uninstall & update; minor improvements to code
1 parent bddb923 commit 027c3d0

File tree

9 files changed

+191
-37
lines changed

9 files changed

+191
-37
lines changed

platformio/commands/install.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from platformio.platforms._base import PlatformFactory
77

88

9-
@command("run", short_help="Install new platforms")
9+
@command("install", short_help="Install new platforms")
1010
@argument("platform")
1111
@option('--with-package', multiple=True, metavar="<package>")
1212
@option('--without-package', multiple=True, metavar="<package>")

platformio/commands/list.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright (C) Ivan Kravets <[email protected]>
2+
# See LICENSE for details.
3+
4+
from click import command, echo, style
5+
6+
from platformio.pkgmanager import PackageManager
7+
8+
9+
@command("list", short_help="List installed platforms")
10+
def cli():
11+
12+
for name, pkgs in PackageManager.get_installed().iteritems():
13+
echo("{name:<20} with packages: {pkgs}".format(
14+
name=style(name, fg="cyan"),
15+
pkgs=", ".join(pkgs.keys())
16+
))

platformio/commands/show.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Copyright (C) Ivan Kravets <[email protected]>
2+
# See LICENSE for details.
3+
4+
from os.path import join
5+
6+
from click import argument, command, echo, style
7+
8+
from platformio.exception import PlatformNotInstalledYet
9+
from platformio.pkgmanager import PackageManager
10+
from platformio.platforms._base import PlatformFactory
11+
12+
13+
@command("show", short_help="Show details about an installed platforms")
14+
@argument("platform")
15+
def cli(platform):
16+
p = PlatformFactory().newPlatform(platform)
17+
if platform not in PackageManager.get_installed():
18+
raise PlatformNotInstalledYet(platform)
19+
20+
# print info about platform
21+
echo("{name:<20} - {info}".format(name=style(p.get_name(), fg="cyan"),
22+
info=p.get_short_info()))
23+
24+
pm = PackageManager(platform)
25+
for name, data in pm.get_installed(platform).iteritems():
26+
echo("----------")
27+
echo("Package: %s" % style(name, fg="yellow"))
28+
echo("Location: %s" % join(pm.get_platform_dir(), data['path']))
29+
echo("Version: %d" % int(data['version']))

platformio/commands/uninstall.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Copyright (C) Ivan Kravets <[email protected]>
2+
# See LICENSE for details.
3+
4+
from click import argument, command, secho
5+
6+
from platformio.exception import PlatformNotInstalledYet
7+
from platformio.pkgmanager import PackageManager
8+
from platformio.platforms._base import PlatformFactory
9+
10+
11+
@command("uninstall", short_help="Uninstall the platforms")
12+
@argument("platform")
13+
def cli(platform):
14+
15+
if platform not in PackageManager.get_installed():
16+
raise PlatformNotInstalledYet(platform)
17+
18+
p = PlatformFactory().newPlatform(platform)
19+
if p.uninstall():
20+
secho("The platform '%s' has been successfully "
21+
"uninstalled!" % platform, fg="green")

platformio/commands/update.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright (C) Ivan Kravets <[email protected]>
2+
# See LICENSE for details.
3+
4+
from click import command, echo, style
5+
6+
from platformio.pkgmanager import PackageManager
7+
from platformio.platforms._base import PlatformFactory
8+
9+
10+
@command("update", short_help="Update installed platforms")
11+
def cli():
12+
13+
for platform in PackageManager.get_installed().keys():
14+
echo("\nPlatform %s" % style(platform, fg="cyan"))
15+
echo("--------")
16+
p = PlatformFactory().newPlatform(platform)
17+
p.update()

platformio/exception.py

+6-5
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ class UnknownPlatform(PlatformioException):
1818
MESSAGE = "Unknown platform '%s'"
1919

2020

21+
class PlatformNotInstalledYet(PlatformioException):
22+
23+
MESSAGE = ("The platform '%s' has not been installed yet. "
24+
"Use `platformio install` command")
25+
26+
2127
class UnknownCLICommand(PlatformioException):
2228

2329
MESSAGE = "Unknown command '%s'"
@@ -33,11 +39,6 @@ class InvalidPackageVersion(PlatformioException):
3339
MESSAGE = "The package '%s' with version '%d' does not exist"
3440

3541

36-
class PackageInstalled(PlatformioException):
37-
38-
MESSAGE = "The package '%s' is installed already"
39-
40-
4142
class NonSystemPackage(PlatformioException):
4243

4344
MESSAGE = "The package '%s' is not available for your system '%s'"

platformio/pkgmanager.py

+73-14
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,33 @@
44
import json
55
from os import makedirs, remove
66
from os.path import isdir, isfile, join
7+
from shutil import rmtree
78

9+
from click import echo, secho, style
810
from requests import get
911

1012
from platformio import __pkgmanifesturl__
1113
from platformio.downloader import FileDownloader
1214
from platformio.exception import (InvalidPackageVersion, NonSystemPackage,
13-
PackageInstalled, UnknownPackage)
15+
UnknownPackage)
1416
from platformio.unpacker import FileUnpacker
1517
from platformio.util import get_home_dir, get_system
1618

1719

1820
class PackageManager(object):
1921

22+
DBFILE_PATH = join(get_home_dir(), "installed.json")
23+
2024
def __init__(self, platform_name):
2125
self._platform_name = platform_name
22-
self._platforms_dir = get_home_dir()
23-
self._dbfile = join(self._platforms_dir, "installed.json")
2426

2527
@staticmethod
2628
def get_manifest():
27-
return get(__pkgmanifesturl__).json()
29+
try:
30+
return PackageManager._cached_manifest
31+
except AttributeError:
32+
PackageManager._cached_manifest = get(__pkgmanifesturl__).json()
33+
return PackageManager._cached_manifest
2834

2935
@staticmethod
3036
def download(url, dest_dir, sha1=None):
@@ -38,22 +44,23 @@ def unpack(pkgpath, dest_dir):
3844
fu = FileUnpacker(pkgpath, dest_dir)
3945
return fu.start()
4046

41-
def get_installed(self):
47+
@staticmethod
48+
def get_installed(platform=None):
4249
data = {}
43-
if isfile(self._dbfile):
44-
with open(self._dbfile) as fp:
50+
if isfile(PackageManager.DBFILE_PATH):
51+
with open(PackageManager.DBFILE_PATH) as fp:
4552
data = json.load(fp)
46-
return data
53+
return data.get(platform, None) if platform else data
54+
55+
def get_platform_dir(self):
56+
return join(get_home_dir(), self._platform_name)
4757

4858
def is_installed(self, name):
4959
installed = self.get_installed()
5060
return (self._platform_name in installed and name in
5161
installed[self._platform_name])
5262

5363
def get_info(self, name, version=None):
54-
if self.is_installed(name):
55-
raise PackageInstalled(name)
56-
5764
manifest = self.get_manifest()
5865
if name not in manifest:
5966
raise UnknownPackage(name)
@@ -74,8 +81,14 @@ def get_info(self, name, version=None):
7481
return sorted(builds, key=lambda s: s['version'])[-1]
7582

7683
def install(self, name, path):
84+
echo("Installing %s package:" % style(name, fg="cyan"))
85+
86+
if self.is_installed(name):
87+
secho("Already installed", fg="yellow")
88+
return
89+
7790
info = self.get_info(name)
78-
pkg_dir = join(self._platforms_dir, self._platform_name, path)
91+
pkg_dir = join(self.get_platform_dir(), path)
7992
if not isdir(pkg_dir):
8093
makedirs(pkg_dir)
8194

@@ -85,13 +98,59 @@ def install(self, name, path):
8598
# remove archive
8699
remove(dlpath)
87100

101+
def uninstall(self, name, path):
102+
echo("Uninstalling %s package: \t" % style(name, fg="cyan"),
103+
nl=False)
104+
rmtree(join(self.get_platform_dir(), path))
105+
self._unregister(name)
106+
echo("[%s]" % style("OK", fg="green"))
107+
108+
def update(self, name):
109+
echo("Updating %s package:" % style(name, fg="yellow"))
110+
111+
installed = self.get_installed(self._platform_name)
112+
current_version = installed[name]['version']
113+
latest_version = self.get_info(name)['version']
114+
115+
echo("Versions: Current=%d, Latest=%d \t " % (
116+
current_version, latest_version), nl=False)
117+
118+
if current_version == latest_version:
119+
echo("[%s]" % (style("Up-to-date", fg="green")))
120+
return True
121+
else:
122+
echo("[%s]" % (style("Out-of-date", fg="red")))
123+
124+
self.uninstall(name, installed[name]['path'])
125+
self.install(name, installed[name]['path'])
126+
127+
def register_platform(self, name):
128+
data = self.get_installed()
129+
if name not in data:
130+
data[name] = {}
131+
self._update_db(data)
132+
return data
133+
134+
def unregister_platform(self, name):
135+
data = self.get_installed()
136+
del data[name]
137+
self._update_db(data)
138+
88139
def _register(self, name, version, path):
89140
data = self.get_installed()
90141
if self._platform_name not in data:
91-
data[self._platform_name] = {}
142+
data = self.register_platform(self._platform_name)
92143
data[self._platform_name][name] = {
93144
"version": version,
94145
"path": path
95146
}
96-
with open(self._dbfile, "w") as fp:
147+
self._update_db(data)
148+
149+
def _unregister(self, name):
150+
data = self.get_installed()
151+
del data[self._platform_name][name]
152+
self._update_db(data)
153+
154+
def _update_db(self, data):
155+
with open(self.DBFILE_PATH, "w") as fp:
97156
json.dump(data, fp)

platformio/platforms/_base.py

+25-14
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
# See LICENSE for details.
33

44
from os.path import join
5+
from shutil import rmtree
56

6-
from click import echo, secho, style
7-
8-
from platformio.exception import (PackageInstalled, UnknownPackage,
9-
UnknownPlatform)
7+
from platformio.exception import UnknownPackage, UnknownPlatform
108
from platformio.pkgmanager import PackageManager
11-
from platformio.util import exec_command, get_source_dir
9+
from platformio.util import exec_command, get_platforms, get_source_dir
1210

1311

1412
class PlatformFactory(object):
@@ -17,9 +15,10 @@ class PlatformFactory(object):
1715
def newPlatform(name):
1816
clsname = "%sPlatform" % name.title()
1917
try:
18+
assert name in get_platforms()
2019
mod = __import__("platformio.platforms." + name.lower(),
2120
None, None, [clsname])
22-
except ImportError:
21+
except (AssertionError, ImportError):
2322
raise UnknownPlatform(name)
2423

2524
obj = getattr(mod, clsname)()
@@ -56,17 +55,26 @@ def install(self, with_packages, without_packages):
5655
elif name in with_packages or opts["default"]:
5756
requirements.append((name, opts["path"]))
5857

59-
for (name, path) in requirements:
60-
echo("Installing %s package:" % style(name, fg="cyan"))
61-
try:
62-
pm.install(name, path)
63-
except PackageInstalled:
64-
secho("Already installed", fg="yellow")
58+
for (package, path) in requirements:
59+
pm.install(package, path)
60+
return True
61+
62+
def uninstall(self):
63+
platform = self.get_name()
64+
pm = PackageManager(platform)
6565

66+
for package, data in pm.get_installed(platform).iteritems():
67+
pm.uninstall(package, data['path'])
68+
69+
pm.unregister_platform(platform)
70+
rmtree(pm.get_platform_dir())
6671
return True
6772

68-
def after_run(self, result): # pylint: disable=R0201
69-
return result
73+
def update(self):
74+
platform = self.get_name()
75+
pm = PackageManager(platform)
76+
for package in pm.get_installed(platform).keys():
77+
pm.update(package)
7078

7179
def run(self, variables, targets):
7280
assert isinstance(variables, list)
@@ -83,3 +91,6 @@ def run(self, variables, targets):
8391
] + variables + targets)
8492

8593
return self.after_run(result)
94+
95+
def after_run(self, result): # pylint: disable=R0201
96+
return result

platformio/projectconftpl.ini

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# Copyright (C) Ivan Kravets <[email protected]>
22
# See LICENSE for details.
33

4-
# Please uncomment (remove "#" sign from the beginning of the line) any
5-
# environments which are fit to your project
4+
# Please uncomment (remove "#" sign from the beginning of the line)
5+
# some of the environments which fit to your project.
66
#
7-
# And replace all values that match with "%..._HERE%" by real data
7+
# And replace all values that match with "%..._HERE%" by real data.
88

99

1010
# Simple and base environment

0 commit comments

Comments
 (0)