From b05e99aed74c020b22e4dba628a5fa397ea10c35 Mon Sep 17 00:00:00 2001 From: Henke Adolfsson <catears@catears.se> Date: Mon, 12 Mar 2018 20:43:26 +0100 Subject: [PATCH] fix issue #40 Now there is a compile command for the cli --- kattcmd/commands/compile.py | 50 +++++++++++++++++++++++++++++++++++++ kattcmd/commands/latest.py | 4 +++ 2 files changed, 54 insertions(+) diff --git a/kattcmd/commands/compile.py b/kattcmd/commands/compile.py index e4000c5..bbf01e5 100644 --- a/kattcmd/commands/compile.py +++ b/kattcmd/commands/compile.py @@ -1,5 +1,6 @@ import os import shutil +import click def _GetBuildFolder(bus): @@ -121,3 +122,52 @@ def Init(bus): bus.provide('kattcmd:compile:python', CompilePython) bus.provide('kattcmd:compile:cpp-command', FindCppCompileCommand) + +def CLI(bus, parent): + + def OnCppCompiled(binary): + click.echo('Placed binary at {}'.format(binary)) + + def OnCppCompileFailed(compile_command): + click.secho('Could not compile with "{}"'.format(compile_command), fg='red') + + def OnPythonCompiled(paths): + paths = list(map(os.path.relpath, paths)) + click.echo('Copied to following paths {}'.format(paths)) + + def OnNoFiles(problemname): + click.secho('Could not find any "compilable" items for "{}"'.format(problemname), fg='red') + click.echo('Exiting') + exit(1) + + @parent.command() + @click.argument('name') + def compile(name): + '''Compile your solution to a problem. + + Will compile your solution and put it inside the build + folder. If you use an interpreted language then it will copy + it instead. + + This command is useful if you want to run the code yourself + against some specific input. + + ''' + bus.listen('kattcmd:compile:cpp-compiled', OnCppCompiled) + bus.listen('kattcmd:compile:cpp-compile-failed', OnCppCompileFailed) + bus.listen('kattcmd:compile:python-compiled', OnPythonCompiled) + bus.listen('kattcmd:latest:no-files', OnNoFiles) + + value = bus.call('kattcmd:latest', bus, name) + if not value: + click.secho('Could not determine what you worked on previously', fg='red') + click.echo('Exiting') + return None + + type, _ = value + if type == 'python': + bus.call('kattcmd:compile:python', bus, name) + elif type == 'cpp': + bus.call('kattcmd:compile:cpp', bus, name) + else: + raise ValueError('{} is not an implemented type'.format(type)) diff --git a/kattcmd/commands/latest.py b/kattcmd/commands/latest.py index b471b4b..1626252 100644 --- a/kattcmd/commands/latest.py +++ b/kattcmd/commands/latest.py @@ -32,6 +32,10 @@ and the associated files.''' home = bus.call('kattcmd:find-root', bus) problempath = os.path.join(home, 'kattis', problemname) + if not os.path.exists(problempath): + bus.call('kattcmd:latest:no-files', problemname) + return None + items = [os.path.join(problempath, item) for item in os.listdir(problempath)] with_date = [(os.path.getmtime(fpath), fpath) for fpath in items] latest_first = reversed(sorted(with_date)) -- GitLab