# Path: servi/command.py
# def process_and_run_command_line(command_line=None):
#         servi_parser, sub_parsers = setup_parsers()
# 
#         commands = load_plugins(sub_parsers)
# 
#         args, extra_args = parse_args(commands, servi_parser, command_line)
# 
#         log_level = arg_to_log_level(args.verbose)
#         retval = False
#         with use_log_level(log_level):
#             if args.template_dir:
#                 c.TMPL_DIR_SITE = args.template_dir
#                 warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
#                     c.TMPL_DIR_SITE))
# 
#             if not args.command:
#                 error('\n***** Error ******\nNo command on command line.\n')
#                 servi_parser.print_help()
#                 raise ServiError('No command line')
# 
#             if args.command not in commands:
#                 raise ServiError('Unknown command: {0}'.format(args.command))
# 
#             special = getattr(commands[args.command], "special", {})
# 
#             if not special.get("skip_init", False):
#                 master_dir = c.find_master_dir(os.getcwd())
#                 set_master_dir(master_dir)
#                 load_user_config()
# 
#             try:
#                 info('Servi - Running: {0}\n'.format(args.command))
#                 if c.MASTER_DIR:
#                     debug('Master Directory: {0}'
#                           .format(os.path.abspath(c.MASTER_DIR)))
#                 debug('Template Directory: {0}'
#                       .format(os.path.abspath(c.TMPL_DIR_SITE)))
# 
#                 retval = args.command_func(args, extra_args)
#             except (ForceError, ServiError):
#                 raise
# 
# 
#         return retval


import json
import yaml
from tests.fixtures import *
from servi.command import process_and_run_command_line as servi_run
    try:
        o1 = json.loads(text1.strip('\n\s"'))
        o2 = json.loads(text2.strip('\n\s"'))
    except ValueError:
        return False

    return o1 == o2


@pytest.mark.wip
def test_inventory(mock_homedir, setup_init):
    with open(c.SERVIFILE_GLOBAL_FULL, 'w') as fp:
        fp.write(BAD_REC)
    assert not servi_run('inventory')

    with open(c.SERVIFILE_GLOBAL_FULL, 'w') as fp:
        fp.write(GOOD_REC)

    with open(pathfor(c.SERVIFILE, c.MASTER), 'w') as fp:
        fp.write(SERVIFILE_LOCAL)

    assert not servi_run('inventory')
    good_o = yaml.load(GOOD_REC)['HOSTS']
    servifile_o = yaml.load(SERVIFILE_LOCAL)['HOSTS']
    good_o = c.deep_update(good_o, servifile_o)

    good_j = json.dumps(good_o)
    assert equal_jsons(servi_run('inventory --list'), good_j)
    assert equal_jsons(servi_run('inventory --host prod'),
                       json.dumps(good_o["prod"]))

# Path: servi/command.py
# def process_and_run_command_line(command_line=None):
#         servi_parser, sub_parsers = setup_parsers()
# 
#         commands = load_plugins(sub_parsers)
# 
#         args, extra_args = parse_args(commands, servi_parser, command_line)
# 
#         log_level = arg_to_log_level(args.verbose)
#         retval = False
#         with use_log_level(log_level):
#             if args.template_dir:
#                 c.TMPL_DIR_SITE = args.template_dir
#                 warn('*** Just set TMPL_DIR_SITE to |{0}|'.format(
#                     c.TMPL_DIR_SITE))
# 
#             if not args.command:
#                 error('\n***** Error ******\nNo command on command line.\n')
#                 servi_parser.print_help()
#                 raise ServiError('No command line')
# 
#             if args.command not in commands:
#                 raise ServiError('Unknown command: {0}'.format(args.command))
# 
#             special = getattr(commands[args.command], "special", {})
# 
#             if not special.get("skip_init", False):
#                 master_dir = c.find_master_dir(os.getcwd())
#                 set_master_dir(master_dir)
#                 load_user_config()
# 
#             try:
#                 info('Servi - Running: {0}\n'.format(args.command))
#                 if c.MASTER_DIR:
#                     debug('Master Directory: {0}'
#                           .format(os.path.abspath(c.MASTER_DIR)))
#                 debug('Template Directory: {0}'
#                       .format(os.path.abspath(c.TMPL_DIR_SITE)))
# 
#                 retval = args.command_func(args, extra_args)
#             except (ForceError, ServiError):
#                 raise
# 
# 
#         return retval


# Path: tests/test_inventory.py
import json
import yaml
from tests.fixtures import *
from servi.command import process_and_run_command_line as servi_run

    try:
        o1 = json.loads(text1.strip('\n\s"'))
        o2 = json.loads(text2.strip('\n\s"'))
    except ValueError:
        return False

    return o1 == o2


@pytest.mark.wip
def test_inventory(mock_homedir, setup_init):
    with open(c.SERVIFILE_GLOBAL_FULL, 'w') as fp:
        fp.write(BAD_REC)
    assert not servi_run('inventory')

    with open(c.SERVIFILE_GLOBAL_FULL, 'w') as fp:
        fp.write(GOOD_REC)

    with open(pathfor(c.SERVIFILE, c.MASTER), 'w') as fp:
        fp.write(SERVIFILE_LOCAL)

    assert not servi_run('inventory')
    good_o = yaml.load(GOOD_REC)['HOSTS']
    servifile_o = yaml.load(SERVIFILE_LOCAL)['HOSTS']
    good_o = c.deep_update(good_o, servifile_o)

    good_j = json.dumps(good_o)
    assert equal_jsons(servi_run('inventory --list'), good_j)
    assert equal_jsons(servi_run('inventory --host prod'),
                       json.dumps(good_o["prod"]))

    assert equal_jsons(servi_run('inventory --host MISSING'), json.dumps('{}'))