Python sh.Command() Examples

The following are 30 code examples of sh.Command(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module sh , or try the search function .
Example #1
Source File: process.py    From flyingcloud with Apache License 2.0 7 votes vote down vote up
def run_command_using_sh(cmd, env, kwargs):
    "install the sh module in the system Python to have better debugging of CbCommon module installation (pip install sh)"
    import sh
    command_stdout = ''
    command_stderr = ''
    current_working_directory = kwargs.get('cwd')
    executable_file = cmd[0]
    command_args = cmd[1:]
    command_runner = sh.Command(executable_file)
    try:
        output = command_runner(*command_args, _cwd=current_working_directory, _env=env, _out=command_stdout)
        retcode = output.exit_code
    except sh.ErrorReturnCode as e:
        print("sh e.stderr:{}".format(e.stderr))
        retcode = 1
        command_stderr = e.stderr
        print("command STDOUT:{}".format(command_stdout))
        print("command STDOUT:{}".format(command_stderr))
    return command_stderr, command_stdout, retcode 
Example #2
Source File: ruby.py    From packman with Apache License 2.0 6 votes vote down vote up
def get_gems(self, gems, dir, rbenv=False):
        """downloads a list of ruby gems

        :param list gems: gems to download
        :param string dir: directory to download gems to
        """
        gem = sh.Command('{0}/bin/gem'.format(rbenv)) \
            if rbenv else sh.Command('gem')
        for gem in gems:
            lgr.debug('Downloading gem {0}'.format(gem))
            # TODO: (TEST) add support for ruby in different environments
            o = gem.install('--no-ri', '--no-rdoc', '--install-dir',
                            dir, gem, _iter=True)
            try:
                # this is where the command is actually executed
                for line in o:
                    lgr.debug(line)
            except:
                sys.exit(codes.mapping['failed_to_download_gem']) 
Example #3
Source File: snapshot.py    From scyllabackup with MIT License 6 votes vote down vote up
def __init__(self, scylla_data_dir, db_path, storage_obj,
                 nodetool_path='/usr/bin/nodetool',
                 cqlsh_path='/usr/bin/cqlsh',
                 cqlsh_host='127.0.0.1',
                 cqlsh_port='9042',
                 prefix='scyllabackup',
                 max_workers=4):
        self.scylla_data_dir = scylla_data_dir
        self.db = DB(db_path)
        self.db_path = db_path
        self.nodetool = Command(nodetool_path)
        self.cqlsh = Command(cqlsh_path).bake(cqlsh_host, cqlsh_port)
        self._upload_queue = gevent.queue.JoinableQueue()
        self._download_queue = gevent.queue.JoinableQueue()
        self._delete_queue = gevent.queue.JoinableQueue()
        self._verify_queue = gevent.queue.JoinableQueue()
        self._storage = storage_obj
        self._prefix = prefix
        self.db_key = self._prefix + '/' + os.path.basename(self.db_path)
        self.max_workers = max_workers 
Example #4
Source File: test_repositories.py    From DLRN with Apache License 2.0 6 votes vote down vote up
def test_clone_fallback_var(self, sh_mock):
        config = configparser.RawConfigParser()
        config.read("projects.ini")
        config.set('DEFAULT', 'fallback_to_master', '1')
        config.set('DEFAULT', 'nonfallback_branches', '^foo-')
        self.config = ConfigOptions(config)
        with mock.patch.object(sh.Command, '__call__') as new_mock:
            new_mock.side_effect = _aux_sh
            result = repositories.refreshrepo('url', 'path', branch='bar')
            self.assertEqual(result, ['master', 'None'])
            expected = [mock.call('url', 'path'),
                        mock.call('origin'),
                        mock.call('-f', 'bar'),
                        mock.call('master'),
                        mock.call('--hard', 'origin/master'),
                        mock.call('--pretty=format:%H %ct', '-1', '.')]
            self.assertEqual(new_mock.call_args_list, expected) 
Example #5
Source File: conftest.py    From scyllabackup with MIT License 6 votes vote down vote up
def snapshotter_object(docker_compose_file, docker_compose_project_name,
                       scylla_data_symlink, sql_db_file, storage_client,
                       docker_service_name):

    scylla_docker_cmd = ['-f', docker_compose_file, '-p',
                         docker_compose_project_name, 'exec',
                         '-T', docker_service_name]

    nodetool_cmd = scylla_docker_cmd + ['nodetool']
    cqlsh_cmd = scylla_docker_cmd + ['cqlsh']

    obj = Snapshot(scylla_data_symlink + '/data/',
                   sql_db_file,
                   storage_obj=storage_client,
                   nodetool_path='docker-compose',
                   cqlsh_path='docker-compose')

    # override cli tools for docker
    obj.nodetool = sh.Command('docker-compose').bake(*nodetool_cmd)
    obj.cqlsh = sh.Command('docker-compose').bake(*cqlsh_cmd)

    return obj 
Example #6
Source File: testBench.py    From V-pipe with Apache License 2.0 6 votes vote down vote up
def mafft(infile, outfile, max_iter=1000, thrd=4, mafft='mafft'):
    "Use MAFFT to obtain the multiple sequence alignment"
    # --nuc sequences are nucleotide
    # --localpair pairwise alignments
    # --maxiterate number of iterative refinement
    cmd = sh.Command(mafft)
    cmd = cmd.bake('--nuc')
    cmd = cmd.bake('--preservecase')
    cmd = cmd.bake('--maxiterate', max_iter)
    cmd = cmd.bake('--localpair')
    cmd = cmd.bake('--thread', thrd)
    cmd = cmd.bake(infile)
    cmd = cmd.bake(_out=outfile)

    print(cmd)
    cmd() 
Example #7
Source File: test_repositories.py    From DLRN with Apache License 2.0 6 votes vote down vote up
def test_clone_no_fallback(self, sh_mock):
        config = configparser.RawConfigParser()
        config.read("projects.ini")
        config.set('DEFAULT', 'fallback_to_master', '0')
        self.config = ConfigOptions(config)
        # We need to redefine the mock object again, to use a side effect
        # that will fail in the git checkout call. A bit convoluted, but
        # it works
        with mock.patch.object(sh.Command, '__call__') as new_mock:
            new_mock.side_effect = _aux_sh
            self.assertRaises(sh.ErrorReturnCode_1, repositories.refreshrepo,
                              'url', 'path', branch='branch')
            expected = [mock.call('url', 'path'),
                        mock.call('origin'),
                        mock.call('-f', 'branch')]
            self.assertEqual(new_mock.call_args_list, expected) 
Example #8
Source File: python.py    From packman with Apache License 2.0 6 votes vote down vote up
def check_module_installed(self, name, venv=False):
        """checks to see that a module is installed

        :param string name: module to check for
        :param string venv: (optional) if ommited, will use system python
         else, will use `venv` (for virtualenvs and such)
        """
        pip = sh.Command('{0}/bin/pip'.format(venv)) \
            if venv else sh.Command('pip')
        lgr.debug('Checking whether {0} is installed'.format(name))
        installed_modules = pip.freeze()
        if re.search(r'{0}'.format(name.lower()) + '==',
                     str(installed_modules).lower()):
            lgr.debug('Module {0} is installed'.format(name))
            return True
        else:
            lgr.debug('Module {0} is not installed'.format(name))
            return False

    # TODO: (FEAT) support virtualenv --relocate OR
    # TODO: (FEAT) support whack http://mike.zwobble.org/2013/09/relocatable-python-virtualenvs-using-whack/ # NOQA 
Example #9
Source File: python.py    From packman with Apache License 2.0 6 votes vote down vote up
def get_modules(self, modules, dir, venv=False, timeout='45'):
        """downloads python modules

        :param list modules: python modules to download
        :param string dir: dir to download modules to
        :param string venv: (optional) if ommited, will use system python
         else, will use `dir` (for virtualenvs and such)
        """
        pip = sh.Command('{0}/bin/pip'.format(venv)) \
            if venv else sh.Command('pip')
        for module in modules:
            # returns a stream of the command
            o = pip.install(
                '--no-use-wheel', '--download', dir, module, _iter=True)
            try:
                # this is where the command is actually executed
                for line in o:
                    lgr.debug(line)
            except:
                sys.exit(codes.mapping['failed_to_download_module']) 
Example #10
Source File: python.py    From packman with Apache License 2.0 6 votes vote down vote up
def install(self, modules, venv=False, sources_path=False, timeout='45'):
        """pip installs a list of modules

        :param list modules: python modules to ``pip install``
        :param string venv: (optional) if ommited, will use system python
         else, will use `venv` (for virtualenvs and such)
        """
        # this allows us to use the 'virtualenv' feature.
        venv_path = os.path.join(sources_path, venv) if sources_path else venv
        pip = sh.Command('{0}/bin/pip'.format(venv_path)) \
            if venv else sh.Command('pip')
        for module in modules:
            lgr.debug('Installing module {0}'.format(module))
            o = pip.install('--default-timeout', timeout, module, _iter=True)
            try:
                for line in o:
                    lgr.debug(line)
                if not self.check_module_installed(module, venv_path):
                    lgr.error('Module {0} could not be installed.'.format(
                        module))
                    sys.exit(codes.mapping['module_could_not_be_installed'])
            except:
                lgr.error('Module {0} could not be installed.'.format(module))
                sys.exit(codes.mapping['module_could_not_be_installed']) 
Example #11
Source File: fits_utils.py    From rgz_rcnn with MIT License 6 votes vote down vote up
def fits2png(fits_dir, png_dir):
    """
    Convert fits to png files based on the D1 method
    """
    # cmd_tpl = '%s -cmap Cool'\
    #     ' -zoom to fit -scale log -scale mode minmax -export %s -exit'
    cmd_tpl = '%s -cmap gist_heat -cmap value 0.684039 0'\
        ' -zoom to fit -scale log -scale mode minmax -export %s -exit'
    from sh import Command
    ds9 = Command(ds9_path)
    #fits = '/Users/chen/gitrepos/ml/rgz_rcnn/data/EMU_GAMA23/split_fits/30arcmin/gama_linmos_corrected_clipped0-0.fits'
    #png = '/Users/chen/gitrepos/ml/rgz_rcnn/data/EMU_GAMA23/split_png/30arcmin/gama_linmos_corrected_clipped0-0.png'
    for fits in os.listdir(fits_dir):
        if (fits.endswith('.fits')):
            png = fits.replace('.fits', '.png')
            cmd = cmd_tpl % (osp.join(fits_dir, fits), osp.join(png_dir, png))
            ds9(*(cmd.split())) 
Example #12
Source File: base.py    From flyingcloud with Apache License 2.0 6 votes vote down vote up
def ecr_get_login(self, namespace, aws_ecr_region):
        """AWS EC2 Container Registry needs a 12-hour token."""
        aws_cli_path = os.path.join(os.getenv("VIRTUAL_ENV"), "bin", "aws")
        command = ["ecr", "get-login", "--region", aws_ecr_region]

        # As of Docker v17.12, --email is no longer accepted for docker login.
        # https://github.com/aws/aws-cli/issues/1926
        import awscli
        from distutils.version import LooseVersion
        if LooseVersion(awscli.__version__) >= LooseVersion("1.11.91"):
            command += ["--no-include-email"]

        with io.BytesIO() as output:
            namespace.logger.info("Running: %r", command)
            sh.Command(aws_cli_path)(*command, _out=output)
            docker_login_cmdline = output.getvalue()
        return self.parse_docker_login(docker_login_cmdline.split()) 
Example #13
Source File: testinfra.py    From molecule with MIT License 6 votes vote down vote up
def bake(self):
        """
        Bake a ``testinfra`` command so it's ready to execute and returns None.

        :return: None
        """
        options = self.options
        verbose_flag = util.verbose_flag(options)
        args = verbose_flag + self.additional_files_or_dirs

        self._testinfra_command = sh.Command("pytest").bake(
            options,
            self._tests,
            *args,
            _cwd=self._config.scenario.directory,
            _env=self.env,
            _out=LOG.out,
            _err=LOG.error
        ) 
Example #14
Source File: test_testinfra.py    From molecule with MIT License 6 votes vote down vote up
def test_bake(_patched_testinfra_get_tests, inventory_file, _instance):
    tests_directory = _instance._config.verifier.directory
    file1_file = os.path.join(tests_directory, "file1.py")

    os.mkdir(tests_directory)
    util.write_file(file1_file, "")

    _instance.bake()
    x = [
        str(sh.Command("pytest")),
        "--ansible-inventory={}".format(inventory_file),
        "--connection=ansible",
        "-v",
        "--foo=bar",
        "foo.py",
        "bar.py",
        "-p",
        "no:cacheprovider",
        file1_file,
    ]
    result = str(_instance._testinfra_command).split()

    assert sorted(x) == sorted(result) 
Example #15
Source File: util.py    From gilt with MIT License 5 votes vote down vote up
def build_sh_cmd(cmd, cwd=None):
    """Build a `sh.Command` from a string.

    :param cmd: String with the command to convert.
    :param cwd: Optional path to use as working directory.
    :return: `sh.Command`
    """
    args = cmd.split()
    return getattr(sh, args[0]).bake(_cwd=cwd, *args[1:]) 
Example #16
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_clone_no_fallback_var(self, sh_mock):
        config = configparser.RawConfigParser()
        config.read("projects.ini")
        config.set('DEFAULT', 'fallback_to_master', '1')
        config.set('DEFAULT', 'nonfallback_branches', '^foo-')
        self.config = ConfigOptions(config)
        with mock.patch.object(sh.Command, '__call__') as new_mock:
            new_mock.side_effect = _aux_sh
            self.assertRaises(sh.ErrorReturnCode_1, repositories.refreshrepo,
                              'url', 'path', branch='foo-bar')
            expected = [mock.call('url', 'path'),
                        mock.call('origin'),
                        mock.call('-f', 'foo-bar')]
            self.assertEqual(new_mock.call_args_list, expected) 
Example #17
Source File: util.py    From gilt with MIT License 5 votes vote down vote up
def run_command(cmd, debug=False):
    """Execute the given command and return None.

    :param cmd: A `sh.Command` object to execute.
    :param debug: An optional bool to toggle debug output.
    :return: None
    """
    if debug:
        msg = "  PWD: {}".format(os.getcwd())
        print_warn(msg)
        msg = "  COMMAND: {}".format(cmd)
        print_warn(msg)
    cmd() 
Example #18
Source File: build.py    From declaracad with GNU General Public License v3.0 5 votes vote down vote up
def make_installer(cfg):
    """ """
    print("Building installer...")
    build_dir = 'build/{name}-{version}'.format(**cfg)
    cfg.update({'build_dir': build_dir})
    install_dir = '{build_dir}/usr/share/{name}'.format(**cfg)
    desktop_dir = '{build_dir}/usr/share/applications/'.format(**cfg)
    cfg.update({'install_dir': install_dir,
                'desktop_dir': desktop_dir})

    os.makedirs(build_dir)

    with cd(build_dir):
        os.makedirs('DEBIAN')

        #: Write control
        with open('DEBIAN/control', 'w') as f:
            f.write(CONTROL_TEMPLATE.format(**cfg))

    #: Write
    os.makedirs(install_dir)
    print(sh.cp('-R', glob('build/exe.linux-x86_64-3.5/*'), install_dir))

    #: Make a simlink to /usr/local/bin
    #print(sh.ln('-sf', '{install_dir}/{name}'.format(**cfg),
    #            '{install_dir}/usr/local/bin/{name}'.format(**cfg)))

    #: Make a desktop icon /usr/share/applications
    os.makedirs(desktop_dir)
    print(sh.cp('{name}/res/declaracad.desktop'.format(**cfg), desktop_dir))

    #: Prepare
    try:
        print(sh.chown('-R', 'root:root', build_dir))
    except:
        pass

    #: Build it
    deb = sh.Command('dpkg-deb')
    print(deb('--build', build_dir)) 
Example #19
Source File: base.py    From ATX with Apache License 2.0 5 votes vote down vote up
def exec_cmd(*cmds, **kwargs):
    '''
    @arguments env=None, timeout=3
    may raise Error
    '''
    env = os.environ.copy()
    env.update(kwargs.get('env', {}))
    envcopy = {}
    for key in env: 
        try:
            envcopy[key] = str(env[key]).encode('utf-8') # fix encoding
        except:
            print('IGNORE BAD ENV KEY: ' + repr(key))
    env = envcopy

    timeout = kwargs.get('timeout', 120)
    shell = kwargs.get('shell', False)
    try:
        import sh
        # log.debug('RUN(timeout=%ds): %s'%(timeout, ' '.join(cmds)))
        if shell:
            cmds = list(cmds)
            cmds[:0] = ['bash', '-c']
        c = sh.Command(cmds[0])
        try:
            r = c(*cmds[1:], _err_to_out=True, _out=sys.stdout, _env=env, _timeout=timeout)
        except:
            # log.error('EXEC_CMD error, cmd: %s'%(' '.join(cmds)))
            raise
    except ImportError:
        # log.debug('RUN(timeout=XX): %s'%(' '.join(cmds)))
        if shell:
            cmds = ' '.join(cmds)
        r = subprocess.Popen(cmds, env=env, stdout=sys.stdout, stderr=sys.stderr, shell=shell)
        return r.wait()
    return 0 
Example #20
Source File: test_remote_debug.py    From enaml-native with MIT License 5 votes vote down vote up
def test_remote_debug():
    #sh.pip('install tornado --user'.split())
    enaml_native = sh.Command('enaml-native')
    enaml_native('start', '--remote-debugging', _bg=True)

    #: Add
    sys.path.append('src/apps/')
    sys.path.append('src/')

    #: Init remote nativehooks implementation
    from enamlnative.core import remotehooks
    remotehooks.init()
    main() 
Example #21
Source File: simBench.py    From V-pipe with Apache License 2.0 5 votes vote down vote up
def sim_reads(art, haplotype_seq, coverage, read_len, fragment_mean,
              fragment_sd, outprefix='', paired=True, highQ=True,
              num_reads=False, seed=None, verbose=False):
    # NOTE: art_illumina additional interesting options: -na (no aln file)
    cmd = sh.Command(art)
    cmd = cmd.bake('-i', haplotype_seq)
    if num_reads:
        cmd = cmd.bake('-c', coverage)
    else:
        cmd = cmd.bake('-f', coverage)
    cmd = cmd.bake('-l', read_len)
    cmd = cmd.bake('-o', outprefix)
    cmd = cmd.bake('-ef')   # generate error free reads
    cmd = cmd.bake('-sam')  # generate SAM alignment file
    # use 'M' instead of '=/X' in the CIGAR for alignment match/mismatch
    cmd = cmd.bake('-M')
    # the MSv3 profile seems to generate poorer quality reads
    # cmd = cmd.bake('-ss', 'MSv3')

    if paired:
        cmd = cmd.bake('-p')
        cmd = cmd.bake('-m', fragment_mean)
        cmd = cmd.bake('-s', fragment_sd)

    if highQ:
        cmd = cmd.bake('-qL', 28)
        # ngshmmalign range '!' (=0) and 'I' (=41).
        cmd = cmd.bake('-qU', 40)
        cmd = cmd.bake('-qs', 5)
        cmd = cmd.bake('-qs2', 5)

    if seed is not None:
        cmd = cmd.bake('-rs', seed)

    if verbose:
        print(cmd)
    cmd() 
Example #22
Source File: update-puppet-uc.py    From rdoinfo with Apache License 2.0 5 votes vote down vote up
def update_puppet_uc():
    if os.path.exists(os.path.join(".", "modules")):
        shutil.rmtree("./modules")

    info = rdoinfo.parse_info_file('rdo.yml')

    puppet_info = []
    for package in info['packages']:
        if package['name'].startswith('puppet'):
            puppet_info.append([package['name'], package['upstream']])

    for package in puppet_info:
        url = package[1]
        if 'openstack' in url:  # Do not bump OpenStack modules
            continue
        module = package[0]
        gitpath = os.path.join("modules", module)
        sh.git.clone(url, gitpath)
        git = sh.git.bake(_cwd=gitpath, _tty_out=False)
        try:
            rev_list = str(git('rev-list', '--tags', '--max-count=1')).strip()
            tag = str(git.describe('--tags', rev_list)).strip()
            with open('upper-constraints.txt', 'a') as fp:
                fp.write("%s===%s\n" % (module, tag))
        except Exception:
            continue
        shutil.rmtree(gitpath)

    update_uc = sh.Command('./update-uc.py')
    update_uc(UC_RELEASE) 
Example #23
Source File: packer.py    From python-packer with Apache License 2.0 5 votes vote down vote up
def __init__(self, packerfile, exc=None, only=None, vars=None,
                 var_file=None, exec_path=DEFAULT_PACKER_PATH, out_iter=None,
                 err_iter=None):
        """
        :param string packerfile: Path to Packer template file
        :param list exc: List of builders to exclude
        :param list only: List of builders to include
        :param dict vars: key=value pairs of template variables
        :param string var_file: Path to variables file
        :param string exec_path: Path to Packer executable
        """
        self.packerfile = self._validate_argtype(packerfile, str)
        self.var_file = var_file
        if not os.path.isfile(self.packerfile):
            raise OSError('packerfile not found at path: {0}'.format(
                self.packerfile))
        self.exc = self._validate_argtype(exc or [], list)
        self.only = self._validate_argtype(only or [], list)
        self.vars = self._validate_argtype(vars or {}, dict)

        kwargs = dict()
        if out_iter is not None:
            kwargs["_out"] = out_iter
            kwargs["_out_bufsize"] = 1
        if err_iter is not None:
            kwargs["_err"] = err_iter
            kwargs["_out_bufsize"] = 1

        self.packer = sh.Command(exec_path)
        self.packer = self.packer.bake(**kwargs) 
Example #24
Source File: test_cli.py    From scyllabackup with MIT License 5 votes vote down vote up
def test_cli_restore_snapshot(docker_compose_file, docker_compose_project_name,
                              restore_snapshotter, cli_common_args,
                              scylla_restore_dir, cli_restore_mapping_file):

    cli = parse_args(['restore'] + cli_common_args +
                     ['--restore-path', scylla_restore_dir,
                      '--restore-mapping-file', cli_restore_mapping_file])
    cli.snapshotter = restore_snapshotter

    docker_compose_args = ['-f', docker_compose_file, '-p',
                           docker_compose_project_name]
    docker_compose = sh.Command('docker-compose').bake(*docker_compose_args)
    docker_compose('stop', 'scylla_restore')
    cli.func(cli)
    docker_compose('start', 'scylla_restore')
    wait_for_port = (docker_compose('port', 'scylla_restore', '9042').
                     split(":")[1])
    # Wait for the scylla restore container to start
    for i in range(30):
        if is_open('127.0.0.1', wait_for_port):
            break

    out = docker_compose('exec', '-T', 'scylla_restore', 'cqlsh',
                         '-e', 'select * from excelsior.test;')
    data_lines = [line for line in out.splitlines() if 'static' in line]
    assert len(data_lines) is 2 
Example #25
Source File: test_snapshot.py    From scyllabackup with MIT License 5 votes vote down vote up
def test_restore_snapshot(docker_compose_file, docker_compose_project_name,
                          restore_snapshotter, scylla_restore_dir):
    restore_mapping = (restore_snapshotter.
                       restore_snapshot_mapping(scylla_restore_dir,
                                                'excelsior'))

    docker_compose_args = ['-f', docker_compose_file, '-p',
                           docker_compose_project_name]
    docker_compose = sh.Command('docker-compose').bake(*docker_compose_args)

    # NOTE: Restore should fail as scylla is still up
    with pytest.raises(SystemExit) as exit_scylla_still_up:
        restore_snapshotter.restore_snapshot(scylla_restore_dir,
                                             restore_mapping)
    assert exit_scylla_still_up.value.code == 3

    docker_compose('stop', 'scylla_restore')
    restore_snapshotter.restore_snapshot(scylla_restore_dir, restore_mapping)

    # NOTE: Restore will fail second time as the table directory is not empty
    with pytest.raises(SystemExit) as exit_non_empty_dir:
        restore_snapshotter.restore_snapshot(scylla_restore_dir,
                                             restore_mapping)
    assert exit_non_empty_dir.value.code == 2

    docker_compose('start', 'scylla_restore')
    wait_for_port = (docker_compose('port', 'scylla_restore', '9042').
                     split(":")[1])
    # Wait for the scylla restore container to start
    for i in range(30):
        if is_open('127.0.0.1', wait_for_port):
            break

    out = docker_compose('exec', '-T', 'scylla_restore', 'cqlsh',
                         '-e', 'select * from excelsior.test;')
    data_lines = [line for line in out.splitlines() if 'static' in line]
    assert len(data_lines) is 3 
Example #26
Source File: snapshot.py    From scyllabackup with MIT License 5 votes vote down vote up
def log_shell_exception_and_exit(e):
    """Utility function to log exception for a non zero sh.Command exit and
    exit with error code 2

    :param e: A sh.Command exception for non zero exit code
    :returns: Nothing
    :rtype: None


    """
    logger.error("Command failed with exit code '{0}': {1}".
                 format(e.exit_code, e.__dict__))
    sys.exit(2) 
Example #27
Source File: test_cli.py    From subtitle-downloader with GNU General Public License v3.0 5 votes vote down vote up
def test_run_script():
    print("test_run_script")

    executable = os.path.realpath("../bin/run.sh")

    sh.Command(executable)                          # should do nothing; default behavior is to look in
                                                    # the current directory, which in this case should
                                                    # not have any video files.
    sh.Command(executable)("--help")                # should print help message.

    buf1 = io.StringIO()
    sh.Command(executable)("--list", _out=buf1)     # should list available languages.

    buf2 = io.StringIO()
    sh.Command(executable)("--list", "-a",
                           _out=buf2)               # output should be the same as previous command
                                                    # since --list is a flag, it should take priority.
    assert buf1.getvalue() == buf2.getvalue(), "--list is a flag; it should override any other functionality"

    for file in VIDEO_FILES:
        sh.Command(executable)("-i", "en", file)    # should work except for square brackets in directory

    sh.Command(executable)("-v", VIDEO_DIRECTORY)   # test video directory

    # gather a list of subtitle files created before removing them
    sub_files = glob.glob(VIDEO_DIRECTORY + "*.srt")
    sh.Command("rm")("-rf", sub_files)

    # change directories to the path containing the video files
    current_path = os.path.realpath(__file__)
    sh.cd(os.path.abspath(VIDEO_DIRECTORY))

    # run vanilla command and ensure that the result is the same as before
    subprocess.run([executable])
    sub_files2 = glob.glob(VIDEO_DIRECTORY + "*.srt")

    try:
        assert sub_files == sub_files2
    except AssertionError:
        print("Error: given no arguments, script should search the current directory")
        sh.cd(os.path.dirname(current_path)) 
Example #28
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def test_clone_no_fallback_default(self, sh_mock):
        config = configparser.RawConfigParser()
        config.read("projects.ini")
        config.set('DEFAULT', 'fallback_to_master', '1')
        self.config = ConfigOptions(config)
        with mock.patch.object(sh.Command, '__call__') as new_mock:
            new_mock.side_effect = _aux_sh
            self.assertRaises(sh.ErrorReturnCode_1, repositories.refreshrepo,
                              'url', 'path', branch='rpm-master')
            expected = [mock.call('url', 'path'),
                        mock.call('origin'),
                        mock.call('-f', 'rpm-master')]
            self.assertEqual(new_mock.call_args_list, expected) 
Example #29
Source File: runtime_server.py    From stackhut with Apache License 2.0 5 votes vote down vote up
def run_command(req_id, cmd, stdin=''):
    try:
        cmd_run = sh.Command(cmd)
        output = cmd_run(_in=stdin)
    except sh.ErrorReturnCode as e:
        raise rpc.NonZeroExitError(e.exit_code, e.stderr)
    return output 
Example #30
Source File: rpc.py    From stackhut with Apache License 2.0 5 votes vote down vote up
def __init__(self, backend, shim_cmd):
        self.contract = contract_from_file(CONTRACTFILE)
        self.backend = backend

        # setup fifos
        os.mkfifo(REQ_FIFO)
        os.mkfifo(RESP_FIFO)

        # run the shim
        cmd = sh.Command(shim_cmd[0])
        self.p = cmd(shim_cmd[1:], _bg=True, _out=lambda x: log.debug("Runner - {}".format(x.rstrip())),
                     _err=lambda x: log.error("Runner - {}".format(x.rstrip())))