Skip to content

Commit

Permalink
Merge pull request #151 from datmo/object-viz
Browse files Browse the repository at this point in the history
Object visualizations to fix issue #139, #140
  • Loading branch information
asampat3090 committed May 22, 2018
2 parents 5ee889e + cc668b9 commit 2605879
Show file tree
Hide file tree
Showing 24 changed files with 998 additions and 135 deletions.
35 changes: 33 additions & 2 deletions datmo/cli/command/snapshot.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
from __future__ import print_function

import prettytable
import datetime

from datmo.core.util.i18n import get as __
from datmo.core.util.misc_functions import mutually_exclusive
from datmo.core.util.misc_functions import mutually_exclusive, prettify_datetime, format_table
from datmo.core.util.exceptions import (SnapshotCreateFromTaskArgs)
from datmo.cli.command.project import ProjectCommand
from datmo.core.controller.snapshot import SnapshotController
Expand Down Expand Up @@ -128,7 +129,7 @@ def ls(self, **kwargs):
for snapshot_obj in snapshot_objs:
t.add_row([
snapshot_obj.id,
snapshot_obj.created_at.strftime("%Y-%m-%d %H:%M:%S"),
prettify_datetime(snapshot_obj.created_at),
snapshot_obj.config, snapshot_obj.stats,
snapshot_obj.message, snapshot_obj.label,
snapshot_obj.code_id, snapshot_obj.environment_id,
Expand Down Expand Up @@ -159,3 +160,33 @@ def checkout(self, **kwargs):
self.cli_helper.echo(
__("info", "cli.snapshot.checkout.success", snapshot_id))
return self.snapshot_controller.checkout(snapshot_id)

def diff(self, **kwargs):
snapshot_id_1 = kwargs.get("id_1", None)
snapshot_id_2 = kwargs.get("id_2", None)
snapshot_obj_1 = self.snapshot_controller.get(snapshot_id_1)
snapshot_obj_2 = self.snapshot_controller.get(snapshot_id_2)
comparison_attributes = [
"id", "created_at", "message", "label", "code_id",
"environment_id", "file_collection_id"
]
table_data = [["Attributes", "Snapshot 1", "", "Snapshot 2"],
["", "", "", ""]]
for attribute in comparison_attributes:
value_1 = getattr(snapshot_obj_1, attribute) if getattr(
snapshot_obj_1, attribute) else "N/A"
value_2 = getattr(snapshot_obj_2, attribute) if getattr(
snapshot_obj_2, attribute) else "N/A"
if isinstance(value_1, datetime.datetime):
value_1 = prettify_datetime(value_1)
if isinstance(value_2, datetime.datetime):
value_2 = prettify_datetime(value_2)
table_data.append([attribute, value_1, "->", value_2])
self.cli_helper.echo(format_table(table_data))
return True

def inspect(self, **kwargs):
snapshot_id = kwargs.get("id", None)
snapshot_obj = self.snapshot_controller.get(snapshot_id)
self.cli_helper.echo(str(snapshot_obj))
return True
20 changes: 10 additions & 10 deletions datmo/cli/command/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
basestring = str

from datmo.core.util.i18n import get as __
from datmo.core.util.misc_functions import mutually_exclusive
from datmo.core.util.misc_functions import mutually_exclusive, prettify_datetime
from datmo.cli.command.project import ProjectCommand
from datmo.core.controller.task import TaskController
from datmo.core.util.exceptions import RequiredArgumentMissing
Expand All @@ -33,17 +33,17 @@ def run(self, **kwargs):
if kwargs['environment_definition_filepath']:
snapshot_dict["environment_definition_filepath"] =\
kwargs['environment_definition_filepath']
if not isinstance(kwargs['cmd'], list):
if platform.system() == "Windows":
kwargs['cmd'] = kwargs['cmd']
elif isinstance(kwargs['cmd'], basestring):
kwargs['cmd'] = shlex.split(kwargs['cmd'])

task_dict = {
"ports": kwargs['ports'],
"interactive": kwargs['interactive'],
"command": kwargs['cmd']
"interactive": kwargs['interactive']
}
if not isinstance(kwargs['cmd'], list):
if platform.system() == "Windows":
task_dict['command'] = kwargs['cmd']
elif isinstance(kwargs['cmd'], basestring):
task_dict['command_list'] = shlex.split(kwargs['cmd'])
else:
task_dict['command_list'] = kwargs['cmd']

# Create the task object)
task_obj = self.task_controller.create()
Expand Down Expand Up @@ -71,7 +71,7 @@ def ls(self, **kwargs):
t.add_row([
task_obj.id, task_obj.command, task_obj.status,
task_obj.results,
task_obj.created_at.strftime("%Y-%m-%d %H:%M:%S")
prettify_datetime(task_obj.created_at)
])
self.cli_helper.echo(t)

Expand Down
33 changes: 32 additions & 1 deletion datmo/cli/command/tests/test_snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,38 @@ def test_datmo_snapshot_checkout(self):
shutil.rmtree(datmo_tasks_dirpath)

# Test when optional parameters are not given
self.snapshot.parse(["snapshot", "checkout", "--id", snapshot_id])
self.snapshot.parse(["snapshot", "checkout", snapshot_id])

result = self.snapshot.execute()
assert result

def test_datmo_snapshot_diff(self):
self.__set_variables()
# Create snapshots to test
self.snapshot.parse(["snapshot", "create", "-m", "my test snapshot"])
snapshot_id_1 = self.snapshot.execute()

# Create another test file
self.filepath_3 = os.path.join(self.snapshot.home, "file3.txt")
with open(self.filepath_3, "w") as f:
f.write(to_unicode(str("test")))

self.snapshot.parse(["snapshot", "create", "-m", "my second snapshot"])
snapshot_id_2 = self.snapshot.execute()

# Test diff with the above two snapshots
self.snapshot.parse(["snapshot", "diff", snapshot_id_1, snapshot_id_2])

result = self.snapshot.execute()
assert result

def test_datmo_snapshot_inspect(self):
self.__set_variables()
# Create snapshot to test
self.snapshot.parse(["snapshot", "create", "-m", "my test snapshot"])
snapshot_id = self.snapshot.execute()
# Test diff with the above two snapshots
self.snapshot.parse(["snapshot", "inspect", snapshot_id])

result = self.snapshot.execute()
assert result
10 changes: 7 additions & 3 deletions datmo/cli/command/tests/test_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
from datmo.cli.command.project import ProjectCommand
from datmo.cli.command.task import TaskCommand
from datmo.core.entity.task import Task as CoreTask
from datmo.core.util.exceptions import ProjectNotInitialized, MutuallyExclusiveArguments, RequiredArgumentMissing
from datmo.core.util.exceptions import ProjectNotInitialized, MutuallyExclusiveArguments, RequiredArgumentMissing, SessionDoesNotExist
from datmo.core.util.misc_functions import pytest_docker_environment_failed_instantiation

# provide mountable tmp directory for docker
Expand Down Expand Up @@ -249,8 +249,12 @@ def test_task_ls(self):
# test for desired side effects
assert self.task.args.session_id == test_session_id

task_ls_command = self.task.execute()
assert task_ls_command == True
failed = False
try:
task_ls_command = self.task.execute()
except SessionDoesNotExist:
failed = True
assert failed

def test_task_ls_invalid_arg(self):
self.__set_variables()
Expand Down
12 changes: 10 additions & 2 deletions datmo/cli/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,16 @@ def get_datmo_parser():

snapshot_checkout = snapshot_subcommand_parsers.add_parser(
"checkout", help="checkout a snapshot by id")
snapshot_checkout.add_argument(
"--id", dest="id", default=None, help="snapshot id")
snapshot_checkout.add_argument("id", default=None, help="snapshot id")

snapshot_diff = snapshot_subcommand_parsers.add_parser(
"diff", help="view diff between 2 snapshots")
snapshot_diff.add_argument("id_1", default=None, help="snapshot id 1")
snapshot_diff.add_argument("id_2", default=None, help="snapshot id 2")

snapshot_inspect = snapshot_subcommand_parsers.add_parser(
"inspect", help="inspect a snapshot by id")
snapshot_inspect.add_argument("id", default=None, help="snapshot id")

# Task
task_parser = subparsers.add_parser("task", help="task module")
Expand Down
55 changes: 54 additions & 1 deletion datmo/core/controller/snapshot.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from datmo.core.util.json_store import JSONStore
from datmo.core.util.exceptions import (
FileIOError, RequiredArgumentMissing, ProjectNotInitialized,
SessionDoesNotExist, EntityNotFound, TaskNotComplete)
SessionDoesNotExist, EntityNotFound, TaskNotComplete, DoesNotExist)


class SnapshotController(BaseController):
Expand Down Expand Up @@ -312,6 +312,59 @@ def list(self,

return self.dal.snapshot.query(query, sort_key, sort_order)

def get(self, snapshot_id):
"""Get snapshot object and return
Parameters
----------
snapshot_id : str
id for the snapshot you would like to get
Returns
-------
datmo.core.entity.snapshot.Snapshot
core snapshot object
Raises
------
DoesNotExist
snapshot does not exist
"""
try:
return self.dal.snapshot.get_by_id(snapshot_id)
except EntityNotFound:
raise DoesNotExist()

def get_files(self, snapshot_id, mode="r"):
"""Get list of file objects for snapshot id.
Parameters
----------
snapshot_id : str
id for the snapshot you would like to get file objects for
mode : str
file open mode
(default is "r" to open file for read)
Returns
-------
list
list of python file objects
Raises
------
DoesNotExist
snapshot object does not exist
"""
try:
snapshot_obj = self.dal.snapshot.get_by_id(snapshot_id)
except EntityNotFound:
raise DoesNotExist()
file_collection_obj = self.dal.file_collection.get_by_id(
snapshot_obj.file_collection_id)
return self.file_driver.get_collection_files(
file_collection_obj.filehash, mode=mode)

def delete(self, snapshot_id):
if not snapshot_id:
raise RequiredArgumentMissing(
Expand Down

0 comments on commit 2605879

Please sign in to comment.