-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from datmo/tasks-command
Tasks command with master merged in
- Loading branch information
Showing
8 changed files
with
232 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
from __future__ import print_function | ||
from datmo.util.i18n import get as _ | ||
from datmo.cli.command.project import ProjectCommand | ||
from datmo.controller.task import TaskController | ||
from datmo.util.exceptions import ProjectNotInitializedException | ||
|
||
|
||
class TaskCommand(ProjectCommand): | ||
def __init__(self, home, cli_helper): | ||
super(TaskCommand, self).__init__(home, cli_helper) | ||
|
||
task_parser = self.subparsers.add_parser("task", help="Task module") | ||
subcommand_parsers = task_parser.add_subparsers(title="subcommands", dest="subcommand") | ||
|
||
# Task run arguments | ||
run = subcommand_parsers.add_parser("run", help="Run task") | ||
|
||
run.add_argument('--gpu', dest='gpu', action='store_true', | ||
help='Boolean if you want to train the Model leveraging GPUs') | ||
run.add_argument('--ports', nargs='*', dest='ports', type=str, help='Network port(s) to open during the Task') | ||
run.add_argument('--data', nargs='*', dest='data', type=str, help='Path for data to be used during the Task') | ||
run.add_argument('--dockerfile', dest='dockerfile', default='Dockerfile', nargs='?', type=str, | ||
help='Pass in the Dockerfile with which you want to build the environment') | ||
run.add_argument('--interactive', dest='interactive', action='store_true', | ||
help='Run the environment in interactive mode (keeps STDIN open)') | ||
run.add_argument("command", nargs='?', default=None) | ||
|
||
# Task list arguments | ||
ls = subcommand_parsers.add_parser("ls", help="List tasks") | ||
ls.add_argument('--running', dest='running', action='store_true', | ||
help='Boolean to filter for running Tasks') | ||
ls.add_argument('--all', dest='all', action='store_true', | ||
help='Boolean to filter all running/stopped Tasks') | ||
|
||
# Task stop arguments | ||
stop = subcommand_parsers.add_parser("stop", help="Stop tasks") | ||
stop.add_argument('--running', dest='running', action='store_true', | ||
help='Boolean to filter and stop running Tasks') | ||
stop.add_argument('--id', dest='id', default=None, type=str, help='Task ID to stop') | ||
|
||
self.snapshot_controller = TaskController(home=home, | ||
dal_driver=self.project_controller.dal_driver) | ||
if not self.project_controller.is_initialized: | ||
raise ProjectNotInitializedException(_("error", | ||
"cli.project", | ||
self.home)) | ||
|
||
def run(self, **kwargs): | ||
self.cli_helper.echo(_("info", "cli.task.run")) | ||
print("run", kwargs) | ||
|
||
def ls(self, **kwargs): | ||
print("ls", kwargs) | ||
|
||
def stop(self, **kwargs): | ||
print("stop", kwargs) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
""" | ||
Tests for TaskCommand | ||
""" | ||
from __future__ import division | ||
from __future__ import print_function | ||
from __future__ import unicode_literals | ||
|
||
# TODO: include builtin libraries for the appropriate Python | ||
# try: | ||
# import __builtin__ | ||
# except ImportError: | ||
# # Python 3 | ||
# import builtins as __builtin__ | ||
|
||
import os | ||
import shutil | ||
import tempfile | ||
|
||
from datmo.cli.driver.helper import Helper | ||
from datmo.cli.command.project import ProjectCommand | ||
from datmo.cli.command.task import TaskCommand | ||
from datmo.util.exceptions import ProjectNotInitializedException | ||
|
||
|
||
class TestTaskCommand(): | ||
def setup_class(self): | ||
test_datmo_dir = os.environ.get('TEST_DATMO_DIR', | ||
tempfile.gettempdir()) | ||
self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir) | ||
self.cli_helper = Helper() | ||
|
||
def teardown_class(self): | ||
shutil.rmtree(self.temp_dir) | ||
|
||
def __set_variables(self): | ||
self.init = ProjectCommand(self.temp_dir, self.cli_helper) | ||
self.init.parse([ | ||
"init", | ||
"--name", "foobar", | ||
"--description", "test model"]) | ||
self.init.execute() | ||
self.task = TaskCommand(self.temp_dir, self.cli_helper) | ||
|
||
def test_task_project_not_init(self): | ||
try: | ||
self.task = TaskCommand(self.temp_dir, self.cli_helper) | ||
except ProjectNotInitializedException: | ||
assert True | ||
|
||
def test_datmo_task_run(self): | ||
self.__set_variables() | ||
test_command = "python test.py" | ||
test_gpu = True | ||
test_ports = "8888:8888" | ||
test_data = "data" | ||
test_dockerfile = "Dockerfile" | ||
test_interactive = True | ||
|
||
self.task.parse([ | ||
"task", | ||
"run", | ||
"--gpu", | ||
"--ports", test_ports, | ||
"--data", test_data, | ||
"--dockerfile", test_dockerfile, | ||
"--interactive", | ||
test_command | ||
]) | ||
|
||
# test for desired side effects | ||
assert self.task.args.command == test_command | ||
assert self.task.args.gpu == test_gpu | ||
assert self.task.args.ports == [test_ports] | ||
assert self.task.args.data == [test_data] | ||
assert self.task.args.dockerfile == test_dockerfile | ||
assert self.task.args.interactive == test_interactive | ||
|
||
def test_datmo_task_run_invalid_arg(self): | ||
self.__set_variables() | ||
exception_thrown = False | ||
try: | ||
self.task.parse([ | ||
"task", | ||
"run" | ||
"--foobar", "foobar"]) | ||
except Exception: | ||
exception_thrown = True | ||
assert exception_thrown | ||
|
||
def test_datmo_task_ls(self): | ||
self.__set_variables() | ||
test_running = True | ||
test_all = True | ||
|
||
self.task.parse([ | ||
"task", | ||
"ls", | ||
"--running", | ||
"--all" | ||
]) | ||
|
||
# test for desired side effects | ||
assert self.task.args.running == test_running | ||
assert self.task.args.all == test_all | ||
|
||
def test_datmo_task_ls_invalid_arg(self): | ||
self.__set_variables() | ||
exception_thrown = False | ||
try: | ||
self.task.parse([ | ||
"task", | ||
"ls" | ||
"--foobar", "foobar"]) | ||
except Exception: | ||
exception_thrown = True | ||
assert exception_thrown | ||
|
||
def test_datmo_task_stop(self): | ||
self.__set_variables() | ||
test_running = True | ||
test_id = 'test_id' | ||
|
||
self.task.parse([ | ||
"task", | ||
"stop", | ||
"--running", | ||
"--id", test_id | ||
]) | ||
|
||
# test for desired side effects | ||
assert self.task.args.running == test_running | ||
assert self.task.args.id == test_id | ||
|
||
def test_datmo_task_stop_invalid_arg(self): | ||
self.__set_variables() | ||
exception_thrown = False | ||
try: | ||
self.task.parse([ | ||
"task", | ||
"stop" | ||
"--foobar", "foobar"]) | ||
except Exception: | ||
exception_thrown = True | ||
assert exception_thrown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters