Implemented multiple CLI commands

This commit is contained in:
Ivan Kravets
2014-06-03 21:27:36 +03:00
parent fc7430de94
commit 1d0fea592a
5 changed files with 73 additions and 71 deletions

View File

@@ -1,82 +1,41 @@
# Copyright (C) Ivan Kravets <me@ikravets.com> # Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details. # See LICENSE for details.
from os import listdir
from os.path import join
from sys import exit from sys import exit
import click from click import command, MultiCommand, version_option
from clint.textui import colored, indent, puts
from platformio.util import get_project_config, run_builder from platformio import __version__
from platformio.util import get_source_dir
class PlatformioCLI(MultiCommand):
def list_commands(self, ctx):
cmds = []
for filename in listdir(join(get_source_dir(), "commands")):
if filename.startswith("__init__"):
continue
if filename.endswith(".py"):
cmds.append(filename[:-3])
cmds.sort()
return cmds
def get_command(self, ctx, name):
mod = __import__("platformio.commands." + name, None, None, ["cli"])
return mod.cli
@command(cls=PlatformioCLI)
@version_option(__version__)
def cli():
pass
@click.group()
def main(): def main():
pass cli()
@main.group()
def init():
""" Initialize new platformio based project """
pass
@main.group()
def install():
""" Install new platforms """
pass
@main.group()
def list():
""" List installed platforms """
pass
@main.command()
@click.option("--environment", "-e", multiple=True)
@click.option("--target", "-t", multiple=True)
def run(environment, target):
"""Process project environments """
config = get_project_config()
for section in config.sections():
if section[:4] != "env:":
continue
envname = section[4:]
if environment and envname not in environment:
puts("Skipped %s environment" % colored.yellow(envname))
continue
puts("Processing %s environment:" % colored.cyan(envname))
variables = ["%s=%s" % (o.upper(), v) for o, v in config.items(section)
if o != "targets"]
variables.append("PIOENV=" + envname)
envtargets = []
if target:
envtargets = [t for t in target]
elif config.has_option(section, "targets"):
envtargets = config.get(section, "targets").split()
result = run_builder(variables, envtargets)
# print result
with indent(4, quote=colored.white(".")):
puts(colored.green(result['out']))
puts(colored.red(result['err']))
@main.group()
def search():
""" Search for new platforms """
pass
@main.group()
def show():
""" Show information about installed platforms """
pass
if __name__ == "__main__": if __name__ == "__main__":

View File

@@ -0,0 +1,2 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.

View File

@@ -0,0 +1,36 @@
# Copyright (C) Ivan Kravets <me@ikravets.com>
# See LICENSE for details.
from click import command, echo, option, style
from platformio.util import get_project_config, run_builder, textindent
@command("run", short_help="Process project environments")
@option("--environment", "-e", multiple=True)
@option("--target", "-t", multiple=True)
def cli(environment, target):
config = get_project_config()
for section in config.sections():
if section[:4] != "env:":
continue
envname = section[4:]
if environment and envname not in environment:
echo("Skipped %s environment" % style(envname, fg="yellow"))
continue
echo("Processing %s environment:" % style(envname, fg="cyan"))
variables = ["%s=%s" % (o.upper(), v) for o, v in config.items(section)
if o != "targets"]
variables.append("PIOENV=" + envname)
envtargets = []
if target:
envtargets = [t for t in target]
elif config.has_option(section, "targets"):
envtargets = config.get(section, "targets").split()
result = run_builder(variables, envtargets)
echo(textindent(style(result['out'], fg="green"), ". "))
echo(textindent(style(result['err'], fg="red"), ". "))

View File

@@ -5,6 +5,7 @@ from os import getcwd
from os.path import dirname, expanduser, isfile, join, realpath from os.path import dirname, expanduser, isfile, join, realpath
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
from sys import exit from sys import exit
from textwrap import fill
try: try:
from configparser import ConfigParser from configparser import ConfigParser
@@ -39,6 +40,11 @@ def get_project_config():
return get_project_config._cache return get_project_config._cache
def textindent(text, quote):
return fill(text, initial_indent=quote,
subsequent_indent=quote, width=120)
def exec_command(args): def exec_command(args):
p = Popen(args, stdout=PIPE, stderr=PIPE) p = Popen(args, stdout=PIPE, stderr=PIPE)
out, err = p.communicate() out, err = p.communicate()

View File

@@ -17,7 +17,6 @@ setup(
license=__license__, license=__license__,
install_requires=[ install_requires=[
"click", "click",
"clint",
"pyserial", "pyserial",
# "SCons" # "SCons"
], ],