Example #1
0
def main():
    cli_helper = Helper()

    # parse_args defaults to [1:] for args, but you need to
    # exclude the rest of the args too, or validation will fail
    # args = parser.parse_args(sys.argv[1:2])

    if len(sys.argv) > 1 and \
        sys.argv[1] in cli_helper.get_command_choices():
        command_name = sys.argv[1]
        if command_name == "init":
            command_name = "project"
        command_class = \
            cli_helper.get_command_class(command_name)
    else:
        command_class = BaseCommand

    try:
        command_instance = command_class(os.getcwd(), cli_helper)
    except TypeError as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
        sys.exit()

    try:
        command_instance.parse(sys.argv[1:])
    except CLIArgumentException as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
        sys.exit()

    try:
        command_instance.execute()
    except Exception as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
Example #2
0
 def setup_method(self):
     # provide mountable tmp directory for docker
     tempfile.tempdir = "/tmp" if not platform.system(
     ) == "Windows" else None
     test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
                                     tempfile.gettempdir())
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     self.orig_stdin = sys.stdin
     self.cli = Helper()
Example #3
0
def main():
    cli_helper = Helper()
    # Config is required to run first so it can
    # initialize/find datmo home directory (.datmo)
    # This is required for logging to place the logs in a
    # place for the user.
    config = Config()

    log = DatmoLogger.get_logger(__name__)
    log.info("handling command %s", config.home)

    # parse_args defaults to [1:] for args, but you need to
    # exclude the rest of the args too, or validation will fail
    # args = parser.parse_args(sys.argv[1:2])

    if len(sys.argv) > 1 and \
        sys.argv[1] in cli_helper.get_command_choices():
        command_name = sys.argv[1]
        if command_name == "init":
            command_name = "project"
        elif command_name == "version" or \
            command_name == "--version" or \
            command_name == "-v":
            command_name = "project"
            sys.argv[1] = "version"
        elif command_name == "status":
            command_name = "project"
            sys.argv[1] = "status"
        elif command_name == "cleanup":
            command_name = "project"
            sys.argv[1] = "cleanup"
        command_class = cli_helper.get_command_class(command_name)
    else:
        command_class = BaseCommand

    # instantiate the command class
    try:
        command_instance = command_class(os.getcwd(), cli_helper)
    except TypeError as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
        return 1

    # parse the command line arguments
    try:
        command_instance.parse(sys.argv[1:])
    except CLIArgumentException as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
        return 1

    try:
        command_instance.execute()
        return 0
    except Exception as ex:
        cli_helper.echo(__("error", "cli.general", str(ex)))
        return 1
Example #4
0
    def test_prompt(self):
        cli = Helper()
        test_message = 'foobar'
        with open(os.path.join(self.temp_dir, "test.txt"), "w") as f:
            f.write(to_unicode(test_message))

        with open(os.path.join(self.temp_dir, "test.txt"), "r") as f:
            sys.stdin = f
            i = cli.prompt("what is this test?")
            assert i == test_message
        os.remove(os.path.join(self.temp_dir, "test.txt"))
Example #5
0
 def setup_method(self):
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     Config().set_home(self.temp_dir)
     self.cli_helper = Helper()
     self.snapshot_dict = {
         "id": "test",
         "model_id": "my_model",
         "session_id": "my_session",
         "message": "my message",
         "code_id": "my_code_id",
         "environment_id": "my_environment_id",
         "file_collection_id": "my file collection",
         "config": {
             "test": 0.56
         },
         "stats": {
             "test": 0.34
         }
     }
     self.task_dict = {
         "id": "test",
         "model_id": "my_model",
         "session_id": "my_session",
         "command": "python test.py"
     }
Example #6
0
 def setup_class(self):
     # provide mountable tmp directory for docker
     tempfile.tempdir = '/tmp'
     test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
                                     tempfile.gettempdir())
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     self.cli_helper = Helper()
Example #7
0
 def setup_class(self):
     # provide mountable tmp directory for docker
     tempfile.tempdir = "/tmp" if not platform.system(
     ) == "Windows" else None
     test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
                                     tempfile.gettempdir())
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     self.cli = Helper()
     self.init = ProjectCommand(self.temp_dir, self.cli)
Example #8
0
    def test_prompt(self):
        cli = Helper()
        test_message = 'foobar'

        @cli.input(test_message)
        def dummy():
            return cli.prompt("what is this test?")

        i = dummy()
        assert i == test_message
Example #9
0
    def test_input(self):
        cli = Helper()
        test_msg = "test"

        @cli.input(test_msg)
        def dummy():
            return input("test")

        result = dummy()
        assert test_msg in result
Example #10
0
    def setup_class(self):
        # provide mountable tmp directory for docker
        tempfile.tempdir = '/tmp'
        test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
                                        tempfile.gettempdir())
        self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
        self.cli_helper = Helper()
        self.session_command = SessionCommand(self.temp_dir, self.cli_helper)

        init = ProjectCommand(self.temp_dir, self.cli_helper)
        init.parse(["init", "--name", "foobar", "--description", "test model"])
        init.execute()
Example #11
0
 def setup_method(self):
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     Config().set_home(self.temp_dir)
     self.cli_helper = Helper()
Example #12
0
class TestHelper():
    # https://stackoverflow.com/questions/35851323/pytest-how-to-test-a-function-with-input-call/36377194

    def setup_method(self):
        # provide mountable tmp directory for docker
        tempfile.tempdir = "/tmp" if not platform.system(
        ) == "Windows" else None
        test_datmo_dir = os.environ.get('TEST_DATMO_DIR',
                                        tempfile.gettempdir())
        self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
        self.orig_stdin = sys.stdin
        self.cli = Helper()

    def teardown_method(self):
        sys.stdin = self.orig_stdin

    def test_init(self):
        assert self.cli != None

    def test_input(self):
        test_input = "test"

        @self.cli.input(test_input)
        def dummy():
            return input("test prompt")

        result = dummy()
        assert test_input in result

    def test_prompt(self):
        test_input = "foobar"

        @self.cli.input(test_input)
        def dummy():
            return self.cli.prompt("what is this test?")

        i = dummy()
        assert i == test_input

        # Test with default input true
        test_input = ""
        default_input = "hello"

        @self.cli.input(test_input)
        def dummy():
            return self.cli.prompt("what is this test?", default=default_input)

        i = dummy()
        assert i == default_input

        # Test with false input and no default given
        test_input = ""

        @self.cli.input(test_input)
        def dummy():
            return self.cli.prompt("what is this test?")

        i = dummy()
        assert i == None

        # TODO: figure out how to replace "print" with a testable function
        # https://stackoverflow.com/questions/4219717/how-to-assert-output-with-nosetest-unittest-in-python
        # assert cli.prompt(test_message)

    def test_print_items(self):
        # Test without download in print_format="table"
        header_list = ["foo", "bar"]
        item_dict_list = [{
            "foo": "yo",
            "bar": "hello"
        }, {
            "foo": "cool",
            "bat": "there"
        }]
        result = self.cli.print_items(header_list=header_list,
                                      item_dict_list=item_dict_list)
        assert "foo" in result
        assert "bar" in result
        assert "yo" in result
        assert "hello" in result
        assert "cool" in result
        assert "there" not in result
        assert "N/A" in result
        # Test without download in print_format="csv"
        result = self.cli.print_items(header_list=header_list,
                                      item_dict_list=item_dict_list,
                                      print_format="csv")
        assert result == "foo,bar%syo,hello%scool,N/A%s" % (
            os.linesep, os.linesep, os.linesep)
        assert "there" not in result
        # Test without download in random print_format
        result = self.cli.print_items(header_list=header_list,
                                      item_dict_list=item_dict_list,
                                      print_format="not_valid")
        assert not result
        # Test with output_path given (not absolute)
        test_path = "testprint"
        test_full_path = os.path.join(os.getcwd(), test_path)
        result = self.cli.print_items(header_list=header_list,
                                      item_dict_list=item_dict_list,
                                      output_path=test_path)
        assert result
        assert os.path.isfile(test_full_path)
        assert result in open(test_full_path, "r").read()
        os.remove(test_full_path)
        # Test with output_path given (absolute)
        test_full_path = os.path.join(self.temp_dir, "testprint")
        result = self.cli.print_items(header_list=header_list,
                                      item_dict_list=item_dict_list,
                                      output_path=test_full_path)
        assert result
        assert os.path.isfile(test_full_path)
        assert result in open(test_full_path, "r").read()

    def test_prompt_bool(self):
        # Test if true for truthy inputs
        test_message_list = [
            'true', '1', 't', 'y', 'yes', 'yeah', 'yup', 'certainly', 'uh-huh'
        ]
        for test_message in test_message_list:

            @self.cli.input(test_message)
            def dummy():
                return self.cli.prompt_bool("are you sure about this test?")

            i = dummy()
            assert i == True

        # Test if false for non-truthy inputs
        test_message = 'n'

        @self.cli.input(test_message)
        def dummy():
            return self.cli.prompt_bool("are you sure about this test?")

        i = dummy()
        assert i == False

    def test_prompt_validator(self):
        def validate_y(val):
            return True if val == "y" else False

        # Test success
        test_input = "y"

        @self.cli.input(test_input)
        def dummy():
            return self.cli.prompt_validator("is this a y?", validate_y)

        i = dummy()
        assert i == test_input
        # Test success invalid (2 tries) - TODO: fix
        # test_input = "n"
        # @self.cli.input(test_input + "\n")
        # @self.cli.input(test_input + "\n")
        # def dummy():
        #     return self.cli.prompt_validator("is this a y?", validate_y, tries=2)
        # i = dummy()
        # assert i == test_input
        # Test failure (not valid function)
        validate_func = 2
        failed = False
        try:
            self.cli.prompt_validator("y", validate_func)
        except ArgumentError:
            failed = True
        assert failed

    def test_get_command_class(self):
        # Test success
        for command in ["project", "snapshot", "session", "environment"]:
            result = self.cli.get_command_class(command)
            if command == "project":
                assert result == ProjectCommand
            elif command == "snapshot":
                assert result == SnapshotCommand
            elif command == "session":
                assert result == SessionCommand
            elif command == "environment":
                assert result == EnvironmentCommand
            else:
                assert False

    def test_get_command_choices(self):
        # assert same as output
        assert self.cli.get_command_choices() == [
            "init", "version", "--version", "-v", "status", "cleanup",
            "snapshot", "session", "notebook", "jupyterlab", "terminal",
            "rstudio", "environment", "run", "rerun", "stop", "delete", "ls"
        ]
Example #13
0
def main():
    cli_helper = Helper()
    # Config is required to run first so it can
    # initialize/find datmo home directory (.datmo)
    # This is required for logging to place the logs in a
    # place for the user.
    config = Config()
    config.set_home(os.getcwd())

    log = DatmoLogger.get_logger(__name__)
    log.info("handling command %s", config.home)

    # parse_args defaults to [1:] for args, but you need to
    # exclude the rest of the args too, or validation will fail
    # args = parser.parse_args(sys.argv[1:2])
    if len(sys.argv) > 1 and \
        sys.argv[1] in cli_helper.get_command_choices():
        command_name = sys.argv[1]
        # commands in project.py
        if command_name == "init":
            command_name = "project"
        elif command_name == "version" or \
            command_name == "--version" or \
            command_name == "-v":
            command_name = "project"
            sys.argv[1] = "version"
        elif command_name == "status":
            command_name = "project"
            sys.argv[1] = "status"
        elif command_name == "cleanup":
            command_name = "project"
            sys.argv[1] = "cleanup"
        # commands in workspace.py
        elif command_name in ["notebook", "jupyterlab", "terminal", "rstudio"]:
            sys.argv[1] = command_name
            command_name = "workspace"
        # commands in run.py
        elif command_name == "rerun":
            command_name = "run"
            sys.argv[1] = "rerun"
        elif command_name == "run":
            if len(sys.argv) == 2:
                command_name = "run"
                sys.argv.append("--help")
            else:
                command_name = "run"
        elif command_name == "stop":  # stop command in run.py
            if len(sys.argv) == 2:
                command_name = "run"
                sys.argv.append("--help")
            else:
                command_name = "run"
        elif command_name == "ls":  # ls command in run.py
            command_name = "run"
        elif command_name == "delete":  # delete command in run.py
            command_name = "run"
        command_class = cli_helper.get_command_class(command_name)
    elif len(sys.argv) == 1:
        command_name = "datmo_command"
        command_class = cli_helper.get_command_class(command_name)
    else:
        command_class = BaseCommand

    # instantiate the command class
    try:
        command_instance = command_class(cli_helper)
    except TypeError as ex:
        cli_helper.echo(__("error", "cli.general", "%s %s" % (type(ex), ex)))
        return 1

    # parse the command line arguments
    try:
        command_instance.parse(sys.argv[1:])
    except CLIArgumentError as ex:
        cli_helper.echo(__("error", "cli.general", "%s %s" % (type(ex), ex)))
        return 1

    try:
        command_instance.execute()
        return 0
    except Exception as ex:
        cli_helper.echo(__("error", "cli.general", "%s %s" % (type(ex), ex)))
        return 1
Example #14
0
 def setup_method(self):
     self.temp_dir = tempfile.mkdtemp(dir=test_datmo_dir)
     Config().set_home(self.temp_dir)
     self.cli_helper = Helper()
     self.project_command = ProjectCommand(self.cli_helper)
Example #15
0
 def test_init(self):
     cli = Helper()
     assert cli != None