Skip to content

Commit

Permalink
Merge pull request #3 from datmo/tasks-command
Browse files Browse the repository at this point in the history
Tasks command with master merged in
  • Loading branch information
asampat3090 committed Apr 12, 2018
2 parents 9ece01f + 4dbcfb7 commit 0816c2b
Show file tree
Hide file tree
Showing 8 changed files with 232 additions and 18 deletions.
3 changes: 0 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,6 @@ python:
- '3.5'
- '3.6'
sudo: required
dist:
- precise
- trusty
install:
- pip install pytest pytest-cov
- pip install coveralls
Expand Down
7 changes: 4 additions & 3 deletions datmo/cli/command/base.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from datmo.util.i18n import get as _
from datmo.cli.driver.parser import Parser
from datmo.util.exceptions import ClassMethodNotFound

Expand All @@ -22,8 +23,6 @@ def __init__(self, home, cli_helper):
command:
""")
# self.parser.add_argument("-v", "--verbose", action="store_true",
# help="run in verbose mode")
self.subparsers = self.parser.add_subparsers(title="commands", dest="command")

def parse(self, args):
Expand Down Expand Up @@ -56,6 +55,8 @@ def execute(self):
del command_args["subcommand"]

if method is None:
raise ClassMethodNotFound("Method %s.%s not found" % (self.args.command, method))
raise ClassMethodNotFound(_("error",
"cli.general.method.not_found",
(self.args.command, method)))

method(**command_args)
9 changes: 4 additions & 5 deletions datmo/cli/command/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
class SnapshotCommand(ProjectCommand):
def __init__(self, home, cli_helper):
super(SnapshotCommand, self).__init__(home, cli_helper)

snapshot_parser = self.subparsers.add_parser("snapshot", help="Snapshot module")
# dest="subcommand" argument will populate a "subcommand" property with the subparsers name
# example "subcommand"="create" or "subcommand"="ls"
snapshot_parser = self.subparsers.add_parser("snapshot", help="Snapshot module")
subcommand_parsers = snapshot_parser.add_subparsers(title="subcommands", dest="subcommand")

create = subcommand_parsers.add_parser("create", help="Create snapshot")
Expand Down Expand Up @@ -78,9 +77,9 @@ def __init__(self, home, cli_helper):
self.snapshot_controller = SnapshotController(home=home,
dal_driver=self.project_controller.dal_driver)
if not self.project_controller.is_initialized:
raise ProjectNotInitializedException("exception.cli.snapshot", {
"exception": "No project found in the current directory"
})
raise ProjectNotInitializedException(_("error",
"cli.project",
self.home))

def create(self, **kwargs):
self.cli_helper.echo(_("info", "cli.snapshot.create"))
Expand Down
63 changes: 63 additions & 0 deletions datmo/cli/command/task.py
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)







16 changes: 13 additions & 3 deletions datmo/cli/command/test/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from datmo.cli.driver.helper import Helper
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.snapshot import SnapshotCommand
from datmo.util.exceptions import ProjectNotInitializedException


class TestSnapshot():
Expand All @@ -26,6 +27,11 @@ def setup_class(self):
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",
Expand All @@ -34,11 +40,14 @@ def setup_class(self):
self.init.execute()
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper)

def teardown_class(self):
shutil.rmtree(self.temp_dir)
def test_snapshot_project_not_init(self):
try:
self.snapshot = SnapshotCommand(self.temp_dir, self.cli_helper)
except ProjectNotInitializedException:
assert True

def test_datmo_snapshot_create(self):

self.__set_variables()
test_message = "this is a test message"
test_label = "test label"
test_session_id = "test_session_id"
Expand Down Expand Up @@ -82,6 +91,7 @@ def test_datmo_snapshot_create(self):


def test_datmo_snapshot_create_invalid_arg(self):
self.__set_variables()
exception_thrown = False
try:
self.snapshot.parse([
Expand Down
144 changes: 144 additions & 0 deletions datmo/cli/command/test/test_task.py
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
6 changes: 4 additions & 2 deletions datmo/util/lang/en.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"cli.general.str.test" : "%s",
"cli.general.dict.test" : "{foo} - {bar}",
"cli.general.tuple.test" : "%s, %s",
"cli.snapshot.create": "Creating a new snapshot"
"cli.snapshot.create": "Creating a new snapshot",
"cli.task.run": "Running a new task"
},
"warn": {
"cli.general.internet": "Internet connectivity doesn't exist",
Expand All @@ -19,6 +20,8 @@
},
"error": {
"cli.general": "An exception occurred: %s",
"cli.general.method.not_found": "Method %s.%s not found",
"cli.project": "No project found in the current directory: %s",
"util.misc_functions.get_filehash": "Filepath does not point to a valid file: %s",
"controller.code.driver.git.__init__.dne": "File path does not exist: %s",
"controller.code.driver.git.__init__.giterror": "Error in git: %s",
Expand All @@ -38,7 +41,6 @@
"controller.code.driver.git.stash_apply": "Error in git stash_apply: %s",
"controller.code.driver.git.latest_commit": "Error in git latest commit: %s",
"controller.code.driver.git.reset": "Error in git reset: %s",
"controller.code.driver.git.get_absolute_git_dir": "Error in git getting git dir: %s",
"controller.code.driver.git.check_git_work_tree": "Error in git check work tree: %s",
"controller.code.driver.git.remote": "Error in git remote -- mode: %s, origin: %s, git_url: %s -- %s",
"controller.code.driver.git.get_remote_url": "Error in git get remote url: %s",
Expand Down
2 changes: 0 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@
],
install_requires=[
"future==0.16.0",
"six==1.11.0",
"Click==6.7",
"enum34==1.1.6",
"sh==1.12.11",
"glob2==0.5",
Expand Down

0 comments on commit 0816c2b

Please sign in to comment.