Commit 4eb87491 authored by André Anjos's avatar André Anjos 💬

Closes #24

parent d167e6e7
This diff is collapsed.
......@@ -30,7 +30,7 @@
Usage:
%(prog)s [--verbose ...] [--prefix=<path>] [--cache=<path>] [--user=<user>]
[--platform=<url>] [--token=<token>] [--color=<theme>]
[--platform=<url>] [--token=<token>]
[--test-mode] <command> [<args>...]
%(prog)s (--help | -h)
%(prog)s (--version | -V)
......@@ -40,14 +40,20 @@ Options:
-h, --help Show this screen
-v, --verbose Increases the verbosity (may appear multiple times)
-V, --version Show version
-p, --prefix=<path> Prefix of your local data [default: .]
-c, --cache=<path> Cache prefix, otherwise defaults to `<prefix>/cache'
-o, --color=<theme> The color theme to use for colored messages (choose
between `dark', for dark backgrounds or `light', for
lighther ones - the default is `none')
-t, --token=<token> User token for server operations
-u, --user=<user> The user name on the remote platform
-p, --prefix=<path> Overrides the prefix of your local data. If not set use
the value from your RC file
[default: %(prefix)s]
-c, --cache=<path> Overrides the cache prefix. If not set, use the value
from your RC file, otherwise defaults to
`<prefix>/%(cache)s'
-t, --token=<token> Overrides the user token for server operations. If not
set, use the value from your RC file. There are no
defaults for this option.
-u, --user=<user> Overrides the user name on the remote platform. If not
set, use the value from your RC file.
[default: %(user)s]
-m, --platform=<url> The URL of the BEAT platform to access
[default: %(platform)s]
-T, --test-mode Assume test mode and doesn't setup the logging module
......@@ -65,6 +71,7 @@ Commands:
See 'beat <command> --help' for more information on a specific command.
N.B.: The values of options "prefix", "cache" "user", "token", "platform" and
"""
import os
......@@ -76,7 +83,6 @@ from ..config import Configuration
import difflib
import logging
import colorlog
# defines our own logging level for extra information to be printed
......@@ -98,17 +104,21 @@ def main(user_input=None):
else:
arguments = sys.argv[1:]
from ..config import DEFAULTS
prog = os.path.basename(sys.argv[0])
completions = dict(
prog=prog,
version=__version__,
)
**DEFAULTS,
)
args = docopt(
__doc__ % completions,
argv=arguments,
options_first=True,
version='BEAT command-line swiss army knife v%s' % __version__,
)
)
try:
......@@ -150,23 +160,12 @@ def main(user_input=None):
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.INFO) # default level
format_str = "%(log_color)s%(message)s"
format_str = "%(message)s"
if args['--verbose'] >= 2:
format_str = "%(log_color)s[%(asctime)s - %(name)s]%(reset)s %(levelname)s: %(message)s"
# Some coloring
formatter = colorlog.ColoredFormatter(format_str,
log_colors={
'DEBUG': config.color_debug,
'EXTRA': config.color_info,
'INFO': config.color_info,
'WARNING': config.color_warn,
'ERROR': config.color_error,
'CRITICAL': config.color_critical,
},
style='%',
datefmt="%d/%b/%Y %H:%M:%S"
)
format_str = "[%(asctime)s - %(name)s]%(reset)s %(levelname)s: %(message)s"
formatter = logging.Formatter(format_str, style='%',
datefmt="%d/%b/%Y %H:%M:%S")
console_handler.setFormatter(formatter)
logger.addHandler(console_handler)
......
......@@ -33,6 +33,7 @@ import tempfile
import shutil
import subprocess
import pkg_resources
import contextlib
import six.moves.urllib as urllib
......@@ -95,3 +96,14 @@ def setup_package():
def teardown_package():
shutil.rmtree(prefix_folder)
@contextlib.contextmanager
def temp_cwd():
tempdir = tempfile.mkdtemp(prefix=__name__, suffix='.cwd')
curdir = os.getcwd()
os.chdir(tempdir)
try: yield tempdir
finally:
os.chdir(curdir)
shutil.rmtree(tempdir)
......@@ -33,7 +33,7 @@ import os
import shutil
import json
from . import platform, disconnected, prefix, tmp_prefix, user, token
from . import platform, disconnected, prefix, tmp_prefix, user, token, temp_cwd
from ..common import Selector
from ..scripts.beat import main
from beat.core.test.utils import slow, cleanup, skipif
......@@ -47,15 +47,16 @@ def call(*args, **kwargs):
use_platform = kwargs.get('platform', platform)
use_cache = kwargs.get('cache', 'cache')
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--cache=%s' % use_cache,
'--test-mode',
'algorithm',
) + args)
with temp_cwd():
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--cache=%s' % use_cache,
'--test-mode',
'algorithm',
) + args)
@slow
......
......@@ -32,7 +32,7 @@ import os
import nose.tools
from . import prefix, tmp_prefix
from . import prefix, tmp_prefix, temp_cwd
from .utils import index_experiment_dbs
from ..scripts.beat import main
......@@ -45,11 +45,12 @@ def call(*args, **kwargs):
use_prefix = kwargs.get('prefix', prefix)
return main((
'--prefix=%s' % use_prefix,
'--cache=%s' % tmp_prefix,
'--test-mode',
) + args)
with temp_cwd():
return main((
'--prefix=%s' % use_prefix,
'--cache=%s' % tmp_prefix,
'--test-mode',
) + args)
def setup_module():
......
......@@ -34,7 +34,7 @@ import nose.tools
from nose.tools import assert_raises
import simplejson
from . import tmp_prefix
from . import tmp_prefix, temp_cwd
from ..scripts.beat import main
from beat.core.test.utils import cleanup
from .. import config
......@@ -52,11 +52,12 @@ def call(*args, **kwargs):
def test_config_list():
nose.tools.eq_(call('config', 'list'), 0)
nose.tools.eq_(call('config', 'show'), 0)
def test_config_cache():
cache_dir = 'cache'
c = config.Configuration({'--cache': cache_dir})
nose.tools.eq_(c.cache, os.path.join(c.path, cache_dir))
......@@ -66,48 +67,42 @@ def test_config_cache():
@nose.tools.with_setup(teardown=cleanup)
def test_config_save():
nose.tools.eq_(call('config', 'save'), 0)
config = os.path.join(tmp_prefix, '.beat', 'config.json')
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents
@nose.tools.with_setup(teardown=cleanup)
def test_set_token():
def test_set_local_token():
token_value = '123456abcdef'
nose.tools.eq_(call('config', 'set', 'token', token_value), 0)
config = os.path.join(tmp_prefix, '.beat', 'config.json')
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents['token'] == token_value
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', 'token', token_value), 0)
config = os.path.join(d, '.beatrc')
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents['token'] == token_value
@nose.tools.with_setup(teardown=cleanup)
def test_set_atnt_db():
def test_set_local_atnt_db():
db_config = 'database/atnt'
db_path = './atnt_db'
nose.tools.eq_(call('config', 'set', db_config, db_path), 0)
config = os.path.join(tmp_prefix, '.beat', 'config.json')
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents[db_config] == db_path
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', db_config, db_path), 0)
config = os.path.join(d, '.beatrc')
assert os.path.exists(config)
with open(config, 'rt') as f: contents = simplejson.load(f)
assert contents[db_config] == db_path
@nose.tools.with_setup(teardown=cleanup)
def test_set_get_atnt_db():
def test_set_get_local_atnt_db():
db_config = 'database/atnt'
db_path = './atnt_db'
nose.tools.eq_(call('config', 'set', db_config, db_path), 0)
nose.tools.eq_(call('config', 'get', db_config), 0)
with temp_cwd() as d:
nose.tools.eq_(call('config', 'set', '--local', db_config, db_path), 0)
nose.tools.eq_(call('config', 'get', db_config), 0)
@nose.tools.with_setup(teardown=cleanup)
def test_set_bad_config_key():
db_config = 'fail'
with assert_raises(SystemExit) as c:
call('config', 'set', db_config, db_config)
with assert_raises(SystemExit) as c, temp_cwd() as d:
call('config', 'set', '--local', db_config, db_config)
assert c.exception.code == 1
......@@ -122,13 +117,3 @@ def test_get_bad_config_key():
@nose.tools.with_setup(teardown=cleanup)
def test_get_token():
nose.tools.eq_(call('config', 'get', 'token'), 0)
@nose.tools.with_setup(teardown=cleanup)
def test_color_list():
nose.tools.eq_(call('config', 'color', '--list'), 0)
@nose.tools.with_setup(teardown=cleanup)
def test_color_set():
nose.tools.eq_(call('config', 'color', 'dark'), 0)
......@@ -30,7 +30,7 @@
import nose.tools
from . import platform, disconnected, prefix, tmp_prefix, user, token
from . import platform, disconnected, prefix, tmp_prefix, user, token, temp_cwd
from ..scripts.beat import main
from beat.core.test.utils import slow, cleanup, skipif
......@@ -47,14 +47,15 @@ def call(*args, **kwargs):
use_prefix = kwargs.get('prefix', prefix)
use_platform = kwargs.get('platform', platform)
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--test-mode',
'database',
) + args)
with temp_cwd():
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--test-mode',
'database',
) + args)
@slow
......
......@@ -32,7 +32,7 @@ import os
import sys
import nose.tools
from . import platform, disconnected, prefix, tmp_prefix, user, token
from . import platform, disconnected, prefix, tmp_prefix, user, token, temp_cwd
from .utils import index_experiment_dbs
from ..common import Selector
from ..scripts.beat import main
......@@ -52,15 +52,16 @@ def call(*args, **kwargs):
use_platform = kwargs.get('platform', platform)
use_cache = kwargs.get('cache', 'cache')
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--cache=%s' % use_cache,
'--test-mode',
'experiment',
) + args)
with temp_cwd():
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--cache=%s' % use_cache,
'--test-mode',
'experiment',
) + args)
@slow
......
......@@ -31,7 +31,7 @@
import os
import nose.tools
from . import platform, disconnected, prefix, tmp_prefix, user, token
from . import platform, disconnected, prefix, tmp_prefix, user, token, temp_cwd
from ..common import Selector, dot_diagram
from ..scripts.beat import main
from beat.core.test.utils import slow, cleanup, skipif
......@@ -44,14 +44,15 @@ def call(*args, **kwargs):
use_prefix = kwargs.get('prefix', prefix)
use_platform = kwargs.get('platform', platform)
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--test-mode',
'toolchain',
) + args)
with temp_cwd():
return main((
'--platform=%s' % use_platform,
'--user=%s' % user,
'--token=%s' % token,
'--prefix=%s' % use_prefix,
'--test-mode',
'toolchain',
) + args)
@slow
......
......@@ -28,14 +28,14 @@ requirements:
- beat.backend.python
- beat.core
- termcolor
- colorlog
- oset
- bob.extension
- docopt
test:
requires:
- bob-devel {{ bob_devel }}.*
- beat-devel {{ beat_devel }}.*
- bob.extension
- nose
- coverage
- sphinx
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment