コード例 #1
0
ファイル: utils.py プロジェクト: M3TIOR/kaldi-active-grammar
class ExternalProcess(object):

    shell = ush.Shell(raise_on_error=True)

    fstcompile = shell(os.path.join(exec_dir, 'fstcompile'))
    fstarcsort = shell(os.path.join(exec_dir, 'fstarcsort'))
    fstaddselfloops = shell(os.path.join(exec_dir, 'fstaddselfloops'))
    fstinfo = shell(os.path.join(exec_dir, 'fstinfo'))
    compile_graph = shell(os.path.join(exec_dir, 'compile-graph'))
    compile_graph_agf = shell(os.path.join(exec_dir, 'compile-graph-agf'))
    compile_graph_agf_debug = shell(
        os.path.join(exec_dir, 'compile-graph-agf-debug'))

    make_lexicon_fst = shell([
        sys.executable,
        os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kaldi',
                     'make_lexicon_fst%s.py' % ('_py2' if PY2 else ''))
    ])

    @staticmethod
    def get_formatter(format_kwargs):
        return lambda *args: [arg.format(**format_kwargs) for arg in args]

    @staticmethod
    def get_debug_stderr_kwargs(log):
        return (dict() if log.isEnabledFor(logging.DEBUG) else dict(
            stderr=six.BytesIO()))
コード例 #2
0
class ExternalProcess(object):

    shell = ush.Shell(raise_on_error=True)

    fstcompile = shell(os.path.join(exec_dir, 'fstcompile'))
    fstarcsort = shell(os.path.join(exec_dir, 'fstarcsort'))
    fstaddselfloops = shell(os.path.join(exec_dir, 'fstaddselfloops'))
    fstinfo = shell(os.path.join(exec_dir, 'fstinfo'))
    # compile_graph = shell(os.path.join(exec_dir, 'compile-graph'))
    compile_graph_agf = shell(os.path.join(exec_dir, 'compile-graph-agf'))
    # compile_graph_agf_debug = shell(os.path.join(exec_dir, 'compile-graph-agf-debug'))

    make_lexicon_fst = shell([sys.executable, os.path.join(os.path.dirname(os.path.abspath(__file__)), 'kaldi', 'make_lexicon_fst%s.py' % ('_py2' if PY2 else ''))])

    @staticmethod
    def get_formatter(format_kwargs):
        return lambda *args: [arg.format(**format_kwargs) for arg in args]

    @staticmethod
    def get_debug_stderr_kwargs(log):
        return (dict() if log.isEnabledFor(logging.DEBUG) else dict(stderr=six.BytesIO()))

    @staticmethod
    def execute_command_safely(commands, log):
        """ Executes given `ush` command, redirecting stderr appropriately: either logging, or storing to output upon error. """
        stderr = six.BytesIO()
        for command in commands.commands:
            command.opts['stderr'] = stderr
        try:
            result = commands()
        except Exception as e:
            log.error("Error running command. Printing stderr as follows...\n%s", stderr.getvalue().decode('utf-8'))
            raise e
        return result
コード例 #3
0
ファイル: helper.py プロジェクト: tarruda/python-ush
def commands(*names):
    argvs = []
    for name in names:
        script = os.path.join(TEST_BIN_DIR, '{0}.py'.format(name))
        argvs.append([sys.executable, script])
    return sh(*argvs)


def s(obj):
    """Helper to normalize linefeeds in strings."""
    if isinstance(obj, bytes):
        return obj.replace(b'\n', os.linesep.encode())
    else:
        return obj.replace('\n', os.linesep)


ush.Shell().export_as_module('sh', full_name=True)
import sh
for name in [
        'cat', 'env', 'fold', 'head', 'repeat', 'sha256sum', 'errmd5', 'pargs',
        'pwd'
]:
    script = os.path.join(TEST_BIN_DIR, '{0}.py'.format(name))
    alias_dict = {name: [sys.executable, script]}
    sh.alias(**alias_dict)
from sh import (cat, echo, env, fold, head, repeat, sha256sum, errmd5, pargs,
                pwd)

STDOUT = ush.STDOUT
PIPE = ush.PIPE