comparison venv/lib/python2.7/site-packages/planemo/cli.py @ 0:d67268158946 draft

planemo upload commit a3f181f5f126803c654b3a66dd4e83a48f7e203b
author bcclaywell
date Mon, 12 Oct 2015 17:43:33 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:d67268158946
1 import os
2 import sys
3
4 import click
5
6 # Hack to place Galaxy modules on PYTHONPATH - without
7 # actually having them on the real external PYTHONPATH.
8 import planemo_ext
9 planemo_ext_path = planemo_ext.__path__[0]
10 sys.path.append(os.path.join(planemo_ext_path))
11
12 from .io import error # noqa, import must happen after Galaxy hack
13 from .config import read_global_config # noqa, ditto
14 from planemo import __version__ # noqa, ditto
15
16
17 CONTEXT_SETTINGS = dict(auto_envvar_prefix='PLANEMO')
18 COMMAND_ALIASES = {
19 "l": "lint",
20 "t": "test",
21 "s": "serve",
22 }
23
24
25 class Context(object):
26
27 def __init__(self):
28 self.home = os.getcwd()
29 self._global_config = None
30 # Will be set by planemo CLI driver
31 self.verbose = False
32 self.planemo_config = None
33 self.planemo_directory = None
34
35 @property
36 def global_config(self):
37 if self._global_config is None:
38 self._global_config = read_global_config(self.planemo_config)
39 return self._global_config
40
41 def log(self, msg, *args):
42 """Logs a message to stderr."""
43 if args:
44 msg %= args
45 click.echo(msg, file=sys.stderr)
46
47 def vlog(self, msg, *args):
48 """Logs a message to stderr only if verbose is enabled."""
49 if self.verbose:
50 self.log(msg, *args)
51
52 @property
53 def workspace(self):
54 if not self.planemo_directory:
55 raise Exception("No planemo workspace defined.")
56 workspace = self.planemo_directory
57 if not os.path.exists(workspace):
58 os.makedirs(workspace)
59 if not os.path.isdir(workspace):
60 template = "Planemo workspace directory [%s] unavailable."
61 message = template % workspace
62 raise Exception(message)
63 return workspace
64
65
66 pass_context = click.make_pass_decorator(Context, ensure=True)
67 cmd_folder = os.path.abspath(os.path.join(os.path.dirname(__file__),
68 'commands'))
69
70
71 def list_cmds():
72 rv = []
73 for filename in os.listdir(cmd_folder):
74 if filename.endswith('.py') and \
75 filename.startswith('cmd_'):
76 rv.append(filename[len("cmd_"):-len(".py")])
77 rv.sort()
78 return rv
79
80
81 def name_to_command(name):
82 try:
83 if sys.version_info[0] == 2:
84 name = name.encode('ascii', 'replace')
85 mod_name = 'planemo.commands.cmd_' + name
86 mod = __import__(mod_name, None, None, ['cli'])
87 except ImportError as e:
88 error("Problem loading command %s, exception %s" % (name, e))
89 return
90 return mod.cli
91
92
93 class PlanemoCLI(click.MultiCommand):
94
95 def list_commands(self, ctx):
96 return list_cmds()
97
98 def get_command(self, ctx, name):
99 if name in COMMAND_ALIASES:
100 name = COMMAND_ALIASES[name]
101 return name_to_command(name)
102
103
104 @click.command(cls=PlanemoCLI, context_settings=CONTEXT_SETTINGS)
105 @click.version_option(__version__)
106 @click.option('-v', '--verbose', is_flag=True,
107 help='Enables verbose mode.')
108 @click.option('--config',
109 default="~/.planemo.yml",
110 envvar="PLANEMO_GLOBAL_CONFIG_PATH",
111 help="Planemo configuration YAML file.")
112 @click.option('--directory',
113 default="~/.planemo",
114 envvar="PLANEMO_GLOBAL_WORKSPACE",
115 help="Workspace for planemo.")
116 @pass_context
117 def planemo(ctx, config, directory, verbose):
118 """Utilities to assist with the development of Galaxy tools."""
119 ctx.verbose = verbose
120 ctx.planemo_config = os.path.expanduser(config)
121 ctx.planemo_directory = os.path.expanduser(directory)