예제 #1
0
 def __init__(self):
     self.version = get_tbears_version()
     self._create_parser()
     self.cmdServer = CommandServer(self.subparsers)
     self.cmdScore = CommandScore(self.subparsers)
     self.cmdUtil = CommandUtil(self.subparsers)
     self.cmdWallet = CommandWallet(self.subparsers)
예제 #2
0
class Command(object):
    def __init__(self):
        self.version = get_tbears_version()
        self._create_parser()
        self.cmdServer = CommandServer(self.subparsers)
        self.cmdScore = CommandScore(self.subparsers)
        self.cmdUtil = CommandUtil(self.subparsers)
        self.cmdWallet = CommandWallet(self.subparsers)

    def _create_parser(self):
        parser = argparse.ArgumentParser(
            prog='tbears', description=f'tbears v{self.version} arguments')
        parser.add_argument('-d',
                            '--debug',
                            help='Debug mode',
                            action='store_true')
        subparsers = parser.add_subparsers(
            title='Available commands',
            metavar='command',
            description=f'If you want to see help message of commands, '
            f'use "tbears command -h"')
        subparsers.required = True
        subparsers.dest = 'command'

        self.parser = parser
        self.subparsers = subparsers

        return parser

    def run(self, sys_args) -> Optional:
        # sys_args is list of commands splitted by space
        # e.g. tbears deploy project -k keystore => ['deploy', 'project', '-k', 'keystore']
        try:
            # parse_args return the populated namespace
            # e.g. Namespace (command='deploy', config=None, keyStore='keystore' ...)
            args = self.parser.parse_args(args=sys_args)
            if self.cmdServer.check_command(args.command):
                result = self.cmdServer.run(args)
            elif self.cmdScore.check_command(args.command):
                result = self.cmdScore.run(args)
            elif self.cmdUtil.check_command(args.command):
                result = self.cmdUtil.run(args)
            elif self.cmdWallet.check_command(args.command):
                result = self.cmdWallet.run(args)
        except TBearsBaseException as e:
            print(f"{e}")
            return e.code.value
        except Exception as e:
            print(f"Exception: {e}")
            return TBearsExceptionCode.COMMAND_ERROR.value
        else:
            return result
예제 #3
0
    def test_check_deploy_necessary_args(self):
        # # Deploy essential check
        # No project directory
        os.mkdir(self.project)
        cmd = f'deploy {self.project}'
        parsed = self.parser.parse_args(cmd.split())
        shutil.rmtree(self.project)
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        os.mkdir(self.project)

        # # Deploy to zip

        # Without keystore option
        cmd = f'deploy {self.project} -t zip'
        parsed = self.parser.parse_args(cmd.split())
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        # Keystore file does not exist
        no_keystore = './keystore_not_exist'
        cmd = f'deploy {self.project} -t zip -k {no_keystore}'
        self.touch(no_keystore)
        parsed = self.parser.parse_args(cmd.split())
        os.remove(no_keystore)
        self.assertRaises(SystemExit, self.parser.parse_args, vars(parsed))

        # Invaild password value
        # Even though input invaild password, _check_deploy method should return password
        # (this method doesn't check password value)
        cmd = f'deploy {self.project} -t zip -k {self.keystore}'
        user_input_password = "******"
        expected_password = "******"
        parsed = self.parser.parse_args(cmd.split())
        self.assertEqual(
            CommandScore._check_deploy(vars(parsed), user_input_password),
            expected_password)

        # # Deploy to tbears

        # Correct command (when deploy to tbears, return value from _check_deploy method should None)
        cmd = f'deploy {self.project} -t tbears'
        parsed = self.parser.parse_args(cmd.split())
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        # Deploy tbears SCORE to remote(doesn't check actual -uri value)
        cmd = f'deploy {self.project} -t tbears -u http://1.2.3.4:9000/api/v3'
        parsed = self.parser.parse_args(cmd.split())
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        # Insufficient argument
        cmd = f'deploy {self.project} -m update'
        parsed = self.parser.parse_args(cmd.split())
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        shutil.rmtree(self.project)
예제 #4
0
    def test_check_deploy_necessary_args(self):
        # # Deploy essential check
        # No project directory
        cmd = f'deploy {self.project}'
        self.assertRaises(SystemExit, self.parser.parse_args, cmd.split())

        # Keystore file does not exist
        no_keystore = './keystore_not_exist'
        cmd = f'deploy {self.project} -k {no_keystore}'
        self.assertRaises(SystemExit, self.parser.parse_args, cmd.split())

        conf = self.cmd.cmdUtil.get_init_args(project=self.project,
                                              score_class=self.project_class)
        self.cmd.cmdUtil.init(conf)

        # Invalid password value
        # Even though input invalid password, _check_deploy method should return password
        # (this method doesn't check password value)
        cmd = f'deploy {self.project} -k {self.keystore}'
        user_input_password = "******"
        expected_password = "******"
        parsed = self.parser.parse_args(cmd.split())
        self.assertEqual(
            CommandScore._check_deploy(vars(parsed), user_input_password),
            expected_password)

        # Insufficient argument
        cmd = f'deploy {self.project} -m update'
        parsed = self.parser.parse_args(cmd.split())
        self.assertRaises(TBearsCommandException, CommandScore._check_deploy,
                          vars(parsed))

        shutil.rmtree(self.project)
예제 #5
0
class Command(object):
    def __init__(self):
        self.version = get_tbears_version()
        self._create_parser()
        self.cmdServer = CommandServer(self.subparsers)
        self.cmdScore = CommandScore(self.subparsers)
        self.cmdUtil = CommandUtil(self.subparsers)
        self.cmdWallet = CommandWallet(self.subparsers)

    def _create_parser(self):
        parser = TbearsParser(prog='tbears',
                              description=f'tbears v{self.version} arguments')
        parser.add_argument('-v',
                            '--verbose',
                            help='Verbose mode',
                            action='store_true')
        subparsers = parser.add_subparsers(
            title='Available commands',
            metavar='command',
            description=f'If you want to see help message of commands, '
            f'use "tbears command -h"')
        subparsers.required = True
        subparsers.dest = 'command'

        self.parser = parser
        self.subparsers = subparsers

        return parser

    def run(self, sys_args) -> Optional:
        # sys_args is list of commands splitted by space
        # e.g. tbears deploy project -k keystore => ['deploy', 'project', '-k', 'keystore']
        try:
            # parse_args return the populated namespace
            # e.g. Namespace (command='deploy', config=None, keyStore='keystore' ...)
            args = self.parser.parse_args(args=sys_args)
            self._init_logger(args=args)
            if self.cmdServer.check_command(args.command):
                result = self.cmdServer.run(args)
            elif self.cmdScore.check_command(args.command):
                result = self.cmdScore.run(args)
            elif self.cmdUtil.check_command(args.command):
                result = self.cmdUtil.run(args)
            elif self.cmdWallet.check_command(args.command):
                result = self.cmdWallet.run(args)
        except TBearsBaseException as e:
            print(f"{e}")
            return e.code.value
        except Exception as e:
            print(f"Exception: {e}")
            return TBearsExceptionCode.COMMAND_ERROR.value
        else:
            return result

    def _init_logger(self, args):
        from iconcommons.icon_config import IconConfig

        if args.verbose:
            conf = IconConfig(None, tbears_server_config)
            conf.load()
            if 'console' not in conf['log']['outputType']:
                conf['log'][
                    'outputType'] = conf['log']['outputType'] + "|console"
            Logger.load_config(conf)