提交 7d6054ba 编写于 作者: W wuzewu

update commands

上级 c492de97
...@@ -12,7 +12,6 @@ ...@@ -12,7 +12,6 @@
# See the License for the specific language governing permissions and # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
from . import hub
from . import download from . import download
from . import run from . import run
from . import show from . import show
...@@ -22,3 +21,4 @@ from . import install ...@@ -22,3 +21,4 @@ from . import install
from . import uninstall from . import uninstall
from . import search from . import search
from . import help from . import help
from . import hub
...@@ -16,9 +16,10 @@ from __future__ import absolute_import ...@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.arg_helper import add_argument, print_arguments from paddle_hub.tools.arg_helper import add_argument, print_arguments
from paddle_hub.tools.logger import logger
import argparse import argparse
ENTRY = "hub"
class BaseCommand: class BaseCommand:
command_dict = {} command_dict = {}
...@@ -39,12 +40,14 @@ class BaseCommand: ...@@ -39,12 +40,14 @@ class BaseCommand:
assert not hasattr( assert not hasattr(
self.__class__, self.__class__,
'_instance'), 'Please use `instance()` to get Command object!' '_instance'), 'Please use `instance()` to get Command object!'
self.parser = argparse.ArgumentParser(description=__doc__)
self.args = None self.args = None
self.name = name self.name = name
self.show_in_help = True self.show_in_help = True
self.description = "" self.description = ""
def help(self):
self.parser.print_help()
def add_arg(self, argument, type="str", default=None, help=None): def add_arg(self, argument, type="str", default=None, help=None):
add_argument( add_argument(
argument=argument, argument=argument,
......
...@@ -16,10 +16,11 @@ from __future__ import absolute_import ...@@ -16,10 +16,11 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils from paddle_hub.tools import utils
from paddle_hub.tools.downloader import default_downloader from paddle_hub.tools.downloader import default_downloader
from paddle_hub.hub_server import default_hub_server from paddle_hub.hub_server import default_hub_server
import argparse
class DownloadCommand(BaseCommand): class DownloadCommand(BaseCommand):
...@@ -29,15 +30,21 @@ class DownloadCommand(BaseCommand): ...@@ -29,15 +30,21 @@ class DownloadCommand(BaseCommand):
super(DownloadCommand, self).__init__(name) super(DownloadCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Download a paddle hub module." self.description = "Download a paddle hub module."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s [options]',
add_help=False)
# yapf: disable # yapf: disable
self.add_arg('--output_path', str, ".", "path to save the module, default in current directory" ) self.add_arg('--output_path', str, ".", "path to save the module" )
self.add_arg('--uncompress', bool, False, "uncompress the download package or not" ) self.add_arg('--uncompress', bool, False, "uncompress the download package or not" )
# yapf: enable # yapf: enable
def help(self):
self.parser.print_help()
def exec(self, argv): def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0] module_name = argv[0]
module_version = None if "==" not in module_name else module_name.split( module_version = None if "==" not in module_name else module_name.split(
"==")[1] "==")[1]
...@@ -55,7 +62,7 @@ class DownloadCommand(BaseCommand): ...@@ -55,7 +62,7 @@ class DownloadCommand(BaseCommand):
if module_version: if module_version:
tips += " with version %s" % module_version tips += " with version %s" % module_version
print(tips) print(tips)
return return True
self.print_args() self.print_args()
if self.args.uncompress: if self.args.uncompress:
...@@ -65,7 +72,7 @@ class DownloadCommand(BaseCommand): ...@@ -65,7 +72,7 @@ class DownloadCommand(BaseCommand):
result, tips, file = default_downloader.download_file( result, tips, file = default_downloader.download_file(
url=url, save_path=self.args.output_path) url=url, save_path=self.args.output_path)
print(tips) print(tips)
return result return True
command = DownloadCommand.instance() command = DownloadCommand.instance()
...@@ -42,6 +42,7 @@ class HelpCommand(BaseCommand): ...@@ -42,6 +42,7 @@ class HelpCommand(BaseCommand):
help_text += " %-15s\t\t%s\n" % (command.name, command.description) help_text += " %-15s\t\t%s\n" % (command.name, command.description)
print(help_text) print(help_text)
return True
command = HelpCommand.instance() command = HelpCommand.instance()
...@@ -31,22 +31,24 @@ class HubCommand(BaseCommand): ...@@ -31,22 +31,24 @@ class HubCommand(BaseCommand):
def __init__(self, name): def __init__(self, name):
super(HubCommand, self).__init__(name) super(HubCommand, self).__init__(name)
self.show_in_help = False self.show_in_help = False
# yapf: disable
self.add_arg('command', str, None, "command to run" )
# yapf: enable
def exec(self, argv): def exec(self, argv):
args = self.parser.parse_args(argv[1:2]) if not argv:
help.command.exec(argv)
if not args.command in BaseCommand.command_dict: exit(1)
logger.critical("command %s not supported!" % args.command) return False
sub_command = argv[0]
if not sub_command in BaseCommand.command_dict:
print("ERROR: unknown command '%s'" % sub_command)
help.command.exec(argv)
exit(1) exit(1)
return False
command = BaseCommand.command_dict[args.command] command = BaseCommand.command_dict[sub_command]
command.exec(argv[2:]) return command.exec(argv[1:])
command = HubCommand.instance() command = HubCommand.instance()
if __name__ == "__main__": if __name__ == "__main__":
command.exec(sys.argv) command.exec(sys.argv[1:])
...@@ -16,9 +16,10 @@ from __future__ import absolute_import ...@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils from paddle_hub.tools import utils
from paddle_hub.module.manager import default_module_manager from paddle_hub.module.manager import default_module_manager
import argparse
class InstallCommand(BaseCommand): class InstallCommand(BaseCommand):
...@@ -28,12 +29,18 @@ class InstallCommand(BaseCommand): ...@@ -28,12 +29,18 @@ class InstallCommand(BaseCommand):
super(InstallCommand, self).__init__(name) super(InstallCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Install the specify module to current environment." self.description = "Install the specify module to current environment."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
#TODO(wuzewu): add --upgrade option #TODO(wuzewu): add --upgrade option
def help(self):
self.parser.print_help()
def exec(self, argv): def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0] module_name = argv[0]
module_version = None if "==" not in module_name else module_name.split( module_version = None if "==" not in module_name else module_name.split(
"==")[1] "==")[1]
...@@ -42,7 +49,7 @@ class InstallCommand(BaseCommand): ...@@ -42,7 +49,7 @@ class InstallCommand(BaseCommand):
result, tips, module_dir = default_module_manager.install_module( result, tips, module_dir = default_module_manager.install_module(
module_name=module_name, module_version=module_version) module_name=module_name, module_version=module_version)
print(tips) print(tips)
return result return True
command = InstallCommand.instance() command = InstallCommand.instance()
...@@ -38,6 +38,7 @@ class ListCommand(BaseCommand): ...@@ -38,6 +38,7 @@ class ListCommand(BaseCommand):
for module_name, module_dir in all_modules.items(): for module_name, module_dir in all_modules.items():
list_text += " %-20s\t\t%s\n" % (module_name, module_dir) list_text += " %-20s\t\t%s\n" % (module_name, module_dir)
print(list_text) print(list_text)
return True
command = ListCommand.instance() command = ListCommand.instance()
...@@ -16,11 +16,14 @@ from __future__ import absolute_import ...@@ -16,11 +16,14 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
import paddle_hub as hub
from paddle_hub.data.reader import csv_reader, yaml_reader from paddle_hub.data.reader import csv_reader, yaml_reader
from paddle_hub.module.manager import default_module_manager
from paddle_hub.tools import utils from paddle_hub.tools import utils
from paddle_hub.tools.arg_helper import add_argument, print_arguments from paddle_hub.tools.arg_helper import add_argument, print_arguments
import paddle_hub as hub
import argparse
import os
class RunCommand(BaseCommand): class RunCommand(BaseCommand):
...@@ -30,51 +33,59 @@ class RunCommand(BaseCommand): ...@@ -30,51 +33,59 @@ class RunCommand(BaseCommand):
super(RunCommand, self).__init__(name) super(RunCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Run the specify module" self.description = "Run the specify module"
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module>' % (ENTRY, name),
usage='%(prog)s [options]')
# yapf: disable # yapf: disable
self.add_arg('--config', str, None, "config file in yaml format" ) self.add_arg('--config', str, None, "config file in yaml format" )
self.add_arg('--dataset', str, None, "dataset be used" ) self.add_arg('--dataset', str, None, "dataset be used" )
self.add_arg('--module', str, None, "module to run" )
self.add_arg('--signature', str, None, "signature to run" ) self.add_arg('--signature', str, None, "signature to run" )
# yapf: enable # yapf: enable
def _check_module(self):
if not self.args.module:
logger.critical("lack of module")
self.help()
exit(1)
def _check_dataset(self): def _check_dataset(self):
if not self.args.dataset: if not self.args.dataset:
logger.critical("lack of dataset file") print("Error! Lack of dataset file")
self.help() self.help()
exit(1) exit(1)
if not utils.is_csv_file(self.args.dataset): if not utils.is_csv_file(self.args.dataset):
logger.critical("dataset file should in csv format") print("Error! Dataset file should in csv format")
self.help() self.help()
exit(1) exit(1)
def _check_config(self): def _check_config(self):
if not self.args.config: if not self.args.config:
logger.critical("lack of config file") print("Error! Lack of config file")
self.help() self.help()
exit(1) exit(1)
if not utils.is_yaml_file(self.args.config): if not utils.is_yaml_file(self.args.config):
logger.critical("config file should in yaml format") print("Error! Config file should in yaml format")
self.help() self.help()
exit(1) exit(1)
def help(self):
self.parser.print_help()
def exec(self, argv): def exec(self, argv):
self.args = self.parser.parse_args(argv) if not argv:
self.print_args() print("ERROR: Please specify a key\n")
self.help()
self._check_module() return False
module_name = argv[0]
self.args = self.parser.parse_args(argv[1:])
self._check_dataset() self._check_dataset()
self._check_config() self._check_config()
module = hub.Module(module_dir=self.args.module) module_dir = default_module_manager.search_module(module_name)
if not module_dir:
if os.path.exists(module_name):
module_dir = module_name
else:
print("Install Module %s" % module_name)
result, tips, module_dir = default_module_manager.install_module(
module_name)
print(tips)
if not result:
return False
module = hub.Module(module_dir=module_dir)
yaml_config = yaml_reader.read(self.args.config) yaml_config = yaml_reader.read(self.args.config)
if not self.args.signature: if not self.args.signature:
......
...@@ -16,9 +16,10 @@ from __future__ import absolute_import ...@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils from paddle_hub.tools import utils
from paddle_hub.hub_server import default_hub_server from paddle_hub.hub_server import default_hub_server
import argparse
class SearchCommand(BaseCommand): class SearchCommand(BaseCommand):
...@@ -28,8 +29,18 @@ class SearchCommand(BaseCommand): ...@@ -28,8 +29,18 @@ class SearchCommand(BaseCommand):
super(SearchCommand, self).__init__(name) super(SearchCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Search a paddle hub module with keyword." self.description = "Search a paddle hub module with keyword."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <key>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
def exec(self, argv): def exec(self, argv):
if not argv:
print("ERROR: Please specify a key\n")
self.help()
return False
module_name = argv[0] module_name = argv[0]
module_list = default_hub_server.search_module(module_name) module_list = default_hub_server.search_module(module_name)
text = "\n" text = "\n"
...@@ -38,6 +49,7 @@ class SearchCommand(BaseCommand): ...@@ -38,6 +49,7 @@ class SearchCommand(BaseCommand):
for module_name, module_version in module_list: for module_name, module_version in module_list:
text += " %-20s\t\t%s\n" % (module_name, module_version) text += " %-20s\t\t%s\n" % (module_name, module_version)
print(text) print(text)
return True
command = SearchCommand.instance() command = SearchCommand.instance()
...@@ -16,10 +16,11 @@ from __future__ import absolute_import ...@@ -16,10 +16,11 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.module.manager import default_module_manager from paddle_hub.module.manager import default_module_manager
from paddle_hub.module.module import Module from paddle_hub.module.module import Module
import os import os
import argparse
class ShowCommand(BaseCommand): class ShowCommand(BaseCommand):
...@@ -29,8 +30,18 @@ class ShowCommand(BaseCommand): ...@@ -29,8 +30,18 @@ class ShowCommand(BaseCommand):
super(ShowCommand, self).__init__(name) super(ShowCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Show the specify module's info" self.description = "Show the specify module's info"
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name/module_dir>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
def exec(self, argv): def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0] module_name = argv[0]
cwd = os.getcwd() cwd = os.getcwd()
...@@ -38,7 +49,7 @@ class ShowCommand(BaseCommand): ...@@ -38,7 +49,7 @@ class ShowCommand(BaseCommand):
module_dir = os.path.join(cwd, module_dir = os.path.join(cwd,
module_name) if not module_dir else module_dir module_name) if not module_dir else module_dir
if not module_dir or not os.path.exists(module_dir): if not module_dir or not os.path.exists(module_dir):
return return True
module = Module(module_dir=module_dir) module = Module(module_dir=module_dir)
show_text = "Name:%s\n" % module.name show_text = "Name:%s\n" % module.name
...@@ -50,6 +61,7 @@ class ShowCommand(BaseCommand): ...@@ -50,6 +61,7 @@ class ShowCommand(BaseCommand):
show_text += "Location:%s\n" % module_dir show_text += "Location:%s\n" % module_dir
#TODO(wuzewu): add more signature info #TODO(wuzewu): add more signature info
print(show_text) print(show_text)
return True
command = ShowCommand.instance() command = ShowCommand.instance()
...@@ -16,9 +16,10 @@ from __future__ import absolute_import ...@@ -16,9 +16,10 @@ from __future__ import absolute_import
from __future__ import division from __future__ import division
from __future__ import print_function from __future__ import print_function
from paddle_hub.tools.logger import logger from paddle_hub.tools.logger import logger
from paddle_hub.commands.base_command import BaseCommand from paddle_hub.commands.base_command import BaseCommand, ENTRY
from paddle_hub.tools import utils from paddle_hub.tools import utils
from paddle_hub.module.manager import default_module_manager from paddle_hub.module.manager import default_module_manager
import argparse
class UninstallCommand(BaseCommand): class UninstallCommand(BaseCommand):
...@@ -28,13 +29,22 @@ class UninstallCommand(BaseCommand): ...@@ -28,13 +29,22 @@ class UninstallCommand(BaseCommand):
super(UninstallCommand, self).__init__(name) super(UninstallCommand, self).__init__(name)
self.show_in_help = True self.show_in_help = True
self.description = "Uninstall the specify module from current environment." self.description = "Uninstall the specify module from current environment."
self.parser = self.parser = argparse.ArgumentParser(
description=self.__class__.__doc__,
prog='%s %s <module_name>' % (ENTRY, name),
usage='%(prog)s',
add_help=False)
def exec(self, argv): def exec(self, argv):
if not argv:
print("ERROR: Please specify a module\n")
self.help()
return False
module_name = argv[0] module_name = argv[0]
result, tips = default_module_manager.uninstall_module( result, tips = default_module_manager.uninstall_module(
module_name=module_name) module_name=module_name)
print(tips) print(tips)
return result return True
command = UninstallCommand.instance() command = UninstallCommand.instance()
...@@ -30,6 +30,7 @@ class VersionCommand(BaseCommand): ...@@ -30,6 +30,7 @@ class VersionCommand(BaseCommand):
def exec(self, argv): def exec(self, argv):
print("hub %s" % version.hub_version) print("hub %s" % version.hub_version)
return True
command = VersionCommand.instance() command = VersionCommand.instance()
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册