Python sh.ErrorReturnCode_1() Examples

The following are 28 code examples of sh.ErrorReturnCode_1(). 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: bench_engine.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def get_cpu_mask(device):
    freq_list = []
    cpu_id = 0
    cpu_mask = ''
    while True:
        try:
            freq_list.append(
                int(device.exec_command(
                           "cat /sys/devices/system/cpu/cpu%d"
                           "/cpufreq/cpuinfo_max_freq" % cpu_id)))
        except (ValueError, sh.ErrorReturnCode_1):
            break
        else:
            cpu_id += 1
    for freq in freq_list:
        cpu_mask = '1' + cpu_mask if freq == max(freq_list) else '0' + cpu_mask
    return str(hex(int(cpu_mask, 2)))[2:], cpu_mask.count('1') 
Example #2
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 6 votes vote down vote up
def move_folder(src_path, dst_path, new_folder_name=None):
    """
    This helper function is aimed to copy a folder from a source path to a destination path,
     eventually renaming the folder to be moved. If it fails, it does it silently.

    :param src_path: absolute or relative source path
    :param dst_path: absolute or relative destination root path
    :param new_folder_name: new name for the source path's folder
    """
    src_path, dst_path = __expand_folders(src_path, dst_path)
    if new_folder_name is not None:
        dst_path = join(dst_path, new_folder_name).rstrip("/")
    try:
        if src_path != dst_path:
            sh.mv(src_path, dst_path)
    except sh.ErrorReturnCode_1:
        pass 
Example #3
Source File: sh_commands.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def adb_push_file(src_file, dst_dir, serialno, silent=False):
    if not os.path.isfile(src_file):
        print("Not file, skip pushing " + src_file)
        return
    src_checksum = bench_utils.file_checksum(src_file)
    dst_file = os.path.join(dst_dir, os.path.basename(src_file))
    stdout_buff = []
    try:
        sh.adb("-s", serialno, "shell", "md5sum", dst_file,
               _out=lambda line: stdout_buff.append(line))
    except sh.ErrorReturnCode_1:
        print("Push %s to %s" % (src_file, dst_dir))
        sh.adb("-s", serialno, "push", src_file, dst_dir)
    else:
        dst_checksum = stdout_buff[0].split()[0]
        if src_checksum == dst_checksum:
            if not silent:
                print("Equal checksum with %s and %s" % (src_file, dst_file))
        else:
            if not silent:
                print("Push %s to %s" % (src_file, dst_dir))
            sh.adb("-s", serialno, "push", src_file, dst_dir) 
Example #4
Source File: sh_commands.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def ssh_push_file(src_file, dst_dir, username, address, silent=False):
    if not os.path.isfile(src_file):
        print("Not file, skip pushing " + src_file)
        return
    src_checksum = bench_utils.file_checksum(src_file)
    dst_file = os.path.join(dst_dir, os.path.basename(src_file))
    stdout_buff = []
    try:
        sh.ssh('%s@%s' % (username, address), "md5sum", dst_file,
               _out=lambda line: stdout_buff.append(line))
    except sh.ErrorReturnCode_1:
        print("Scp %s to %s" % (src_file, dst_dir))
        sh.ssh('%s@%s' % (username, address), "mkdir -p %s" % dst_dir)
        sh.scp(src_file, '%s@%s:%s' % (username, address, dst_dir))
    else:
        dst_checksum = stdout_buff[0].split()[0]
        if src_checksum == dst_checksum:
            if not silent:
                print("Equal checksum with %s and %s" % (src_file, dst_file))
        else:
            if not silent:
                print("Scp %s to %s" % (src_file, dst_dir))
            sh.scp(src_file, '%s@%s:%s' % (username, address, dst_dir)) 
Example #5
Source File: device.py    From mobile-ai-bench with Apache License 2.0 6 votes vote down vote up
def pull(self, src_path, dst_path='.'):
        if self.system == SystemType.android:
            sh_commands.adb_pull(src_path, dst_path, self.address)
        elif self.system == SystemType.arm_linux:
            if os.path.isdir(dst_path):
                exist_file = dst_path + '/' + src_path.split('/')[-1]
                if os.path.exists(exist_file):
                    sh.rm('-rf', exist_file)
            elif os.path.exists(dst_path):
                sh.rm('-f', dst_path)
            try:
                sh.scp('-r',
                       '%s@%s:%s' % (self.username,
                                     self.address,
                                     src_path),
                       dst_path)
            except sh.ErrorReturnCode_1 as e:
                six.print_('Error msg {}'.format(e), file=sys.stderr)
                return 
Example #6
Source File: hyperparam_search_test.py    From keras-image-captioning with MIT License 6 votes vote down vote up
def test_execute(self, training_command):
        finished = []

        def done_callback(cmd, success, exit_code):
            finished.append(True)

        training_command._done_callback = done_callback

        if DRY_RUN:
            training_command._config_filepath = NOT_EXISTING_PATH
            running_command = training_command.execute()
            with pytest.raises(sh.ErrorReturnCode_1):
                running_command.wait()
        else:
            running_command = training_command.execute()
            running_command.wait()

        assert len(finished) == 1 and finished[0] 
Example #7
Source File: test_manager_misc.py    From cloudify-manager with Apache License 2.0 6 votes vote down vote up
def test_tmux_session(self):
        self.logger.info('Test list without tmux installed...')
        try:
            self.cfy.ssh(list_sessions=True)
        except sh.ErrorReturnCode_1 as ex:
            self.assertIn('tmux executable not found on manager', ex.stdout)

        self.logger.info('Installing tmux...')
        self.execute_on_manager('yum install tmux -y')

        self.logger.info('Test listing sessions when non are available..')
        output = self.cfy.ssh(list_sessions=True)
        self.assertIn('No sessions are available', output)

        self.logger.info('Test running ssh command...')
        content = 'yay'
        remote_path = '/tmp/ssh_test_output_file'
        self.cfy.ssh(command='echo {0} > {1}'.format(content, remote_path))
        self.assertEqual(content, self.read_manager_file(remote_path)) 
Example #8
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 6 votes vote down vote up
def move_files(src_path, dst_path, *files):
    """
    This helper function is aimed to move files from a source path to a destination path.

    :param src_path: absolute or relative source path
    :param dst_path: absolute or relative destination path
    :param files: tuples with the following format (source_filename, destination_filename)
    """
    src_path, dst_path = __expand_folders(src_path, dst_path)
    for f in files:
        if isinstance(f, tuple):
            src, dst = f
        elif isinstance(f, string_types):
            src, dst = 2 * [f]
        else:
            continue
        src, dst = join(src_path, src), join(dst_path, dst)
        try:
            if src != dst:
                sh.mv(src, dst)
        except sh.ErrorReturnCode_1:
            pass 
Example #9
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 #10
Source File: openbmc-autobump.py    From openbmc-tools with Apache License 2.0 6 votes vote down vote up
def find_candidate_recipes(meta, args):
    remote_fmt_args = (args.ssh_config_host, meta)
    remote = 'ssh://{}/openbmc/{}'.format(*remote_fmt_args)
    try:
        git_clone_or_reset(meta, remote, args)
    except sh.ErrorReturnCode as e:
        log('{}'.format(e), args)
        return []

    grep_args = ['-l', '-e', '_URI', '--and', '-e', 'github.com/openbmc']
    try:
        return git.grep(*grep_args, _cwd=meta).stdout.decode('utf-8').split()
    except sh.ErrorReturnCode_1:
        pass
    except sh.ErrorReturnCode as e:
        log('{}'.format(e), args)

    return [] 
Example #11
Source File: btrfs.py    From myaas with GNU Lesser General Public License v3.0 5 votes vote down vote up
def make_subvolume(self, name):
        path = os.path.join(self.mountpoint, name)
        try:
            sh.btrfs.subvolume.create(path)
        except sh.ErrorReturnCode_1 as e:
            stderr = e.stderr.decode('utf-8')
            if stderr.startswith("ERROR: can't access"):
                raise NoSuchFileOrDirectory(stderr)
            if stderr.startswith("ERROR: '{0}' exists".format(path)):
                raise TargetPathAlreadyExists(stderr)
            raise BtrfsError(stderr)

        return self.find_subvolume_by_name(name) 
Example #12
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 #13
Source File: btrfs.py    From myaas with GNU Lesser General Public License v3.0 5 votes vote down vote up
def delete_subvolume(self, name):
        path = os.path.join(self.mountpoint, name)
        try:
            sh.btrfs.subvolume.delete(path)
        except sh.ErrorReturnCode_1 as e:
            stderr = e.stderr.decode('utf-8')
            if 'No such file or directory' in stderr:
                return
            raise BtrfsError(stderr) 
Example #14
Source File: btrfs.py    From myaas with GNU Lesser General Public License v3.0 5 votes vote down vote up
def delete(self):
        for vol in self.subvolumes:
            vol.delete()

        try:
            sh.btrfs.subvolume.delete(self.path)
        except sh.ErrorReturnCode_1 as e:
            stderr = e.stderr.decode('utf-8')
            raise BtrfsError(stderr) 
Example #15
Source File: test_ansible_playbook.py    From molecule with MIT License 5 votes vote down vote up
def test_executes_catches_and_exits_return_code_with_stdout(
    patched_run_command, patched_logger_critical, _instance
):
    patched_run_command.side_effect = sh.ErrorReturnCode_1(
        sh.ansible_playbook, b"out", b"err"
    )
    with pytest.raises(SystemExit) as e:
        _instance.execute()

    assert 1 == e.value.code

    msg = "out"
    patched_logger_critical.assert_called_once_with(msg) 
Example #16
Source File: test_testinfra.py    From molecule with MIT License 5 votes vote down vote up
def test_executes_catches_and_exits_return_code(
    patched_run_command, _patched_testinfra_get_tests, _instance
):
    patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.pytest, b"", b"")
    with pytest.raises(SystemExit) as e:
        _instance.execute()

    assert 1 == e.value.code 
Example #17
Source File: test_shell.py    From molecule with MIT License 5 votes vote down vote up
def test_executes_catches_and_exits_return_code(patched_run_command, _instance):
    patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.ls, b"", b"")
    with pytest.raises(SystemExit) as e:
        _instance.execute()

    assert 1 == e.value.code 
Example #18
Source File: test_gilt.py    From molecule with MIT License 5 votes vote down vote up
def test_executes_catches_and_exits_return_code(
    patched_run_command, _patched_gilt_has_requirements_file, _instance
):
    patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.gilt, b"", b"")
    with pytest.raises(SystemExit) as e:
        _instance.execute()

    assert 1 == e.value.code 
Example #19
Source File: test_collections.py    From molecule with MIT License 5 votes vote down vote up
def test_executes_catches_and_exits_return_code(
    patched_run_command, _patched_ansible_galaxy_has_requirements_file, _instance
):
    patched_run_command.side_effect = sh.ErrorReturnCode_1(sh.ansible_galaxy, b"", b"")
    with pytest.raises(SystemExit) as e:
        _instance.execute()

    assert 1 == e.value.code 
Example #20
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_folder(path):
    """
    This helper function is aimed to remove an entire folder. If the folder does not exist,
     it fails silently.

    :param path: absolute or relative source path
    """
    path = __expand_folders(path)
    try:
        sh.rm('-r', path)
    except sh.ErrorReturnCode_1:
        pass 
Example #21
Source File: helpers.py    From rpl-attacks with GNU Affero General Public License v3.0 5 votes vote down vote up
def remove_files(path, *files):
    """
    This helper function is aimed to remove specified files. If a file does not exist,
     it fails silently.

    :param path: absolute or relative source path
    :param files: filenames of files to be removed
    """
    path = __expand_folders(path)
    for file in files:
        try:
            sh.rm(join(path, file))
        except sh.ErrorReturnCode_1:
            pass 
Example #22
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 #23
Source File: test_repositories.py    From DLRN with Apache License 2.0 5 votes vote down vote up
def _aux_sh(*args):
    call = args[0]
    if call == '-f':
        raise sh.ErrorReturnCode_1('blabla'.encode(), ''.encode(),
                                   ''.encode())
    return 
Example #24
Source File: qualcomm_adb_device.py    From mobile-ai-bench with Apache License 2.0 5 votes vote down vote up
def _support_dev_dsp(self):
        support_dev_dsp = False
        try:
            output = self.exec_command(
                "ls /system/vendor/lib/rfsa/adsp/libhexagon_nn_skel.so")  # noqa
        except sh.ErrorReturnCode_1:
            print("libhexagon_nn_skel.so does not exists, QualcommAdbDevice Skip DSP.")  # noqa
        else:
            if "No such file or directory" in output:
                print("libhexagon_nn_skel.so does not exists, QualcommAdbDevice Skip DSP.")  # noqa
            else:
                support_dev_dsp = True
        return support_dev_dsp 
Example #25
Source File: ssh_device.py    From mobile-ai-bench with Apache License 2.0 5 votes vote down vote up
def pull(self, src_path, dst_path='.'):
        if os.path.isdir(dst_path):
            exist_file = dst_path + '/' + src_path.split('/')[-1]
            if os.path.exists(exist_file):
                sh.rm('-rf', exist_file)
        elif os.path.exists(dst_path):
            sh.rm('-f', dst_path)
        try:
            sh.scp('-r',
                   '%s@%s:%s' % (self.username, self.address, src_path),
                   dst_path)
        except sh.ErrorReturnCode_1 as e:
            six.print_('Error msg {}'.format(e), file=sys.stderr)
            return 
Example #26
Source File: search.py    From magpie with MIT License 5 votes vote down vote up
def get(self):
        query = unquote(self.get_argument('q', ''))
        if query == '':
            self.redirect('/')
        try:
            results = str(grep('-R', '--exclude-dir', '.git', query,
                               self.settings.repo))
        except ErrorReturnCode_1 as e:
            results = ''

        try:
            results += str(find(self.settings.repo, '-type', 'f', '-name',
                                '*' + query + '*', '-not', '(', '-path',
                                '%s/%s/*' % (self.settings.repo, '.git') ))
        except ErrorReturnCode_1 as e:
            pass

        results = results.replace(self.settings.repo, '').split('\n')[:-1]
        formatted_results = []
        for result in results:
            if 'Binary file' in result or result == '':
                continue

            # TODO this doesn't play well with colons in filenames
            stuff = result.split(':')
            filename = stuff[0]
            if path.basename(filename).startswith('.'):
                filename = path.join(path.dirname(filename),
                                     path.basename(filename)[1:])
            string = ''.join(stuff[1:])
            string = self.highlight(string, query)
            formatted_results.append({'filename': filename, 'string': string})
        self.render('search.html', query=query, results=formatted_results) 
Example #27
Source File: note.py    From magpie with MIT License 4 votes vote down vote up
def _edit(self, notebook_name, note_name, note_contents=None,
              confirmed=False, toggle=-1, note_name_rename=None):

        notebook_enc = self.encode_name(notebook_name)
        note_enc = self.encode_name(note_name)
        path = join(self.settings.repo, notebook_enc, note_enc)
        if note_name_rename:
            rename_note_enc = self.encode_name(note_name_rename)
            rename_path = join(self.settings.repo, notebook_enc, rename_note_enc)
        if not confirmed:
            note_contents = open(path).read()
            self.render('note.html', notebook_name=notebook_name,
                        note_name=note_name, note_contents=note_contents,
                        edit=True, autosave=self.settings['autosave'], 
                        autosave_interval=self.settings['autosave_interval'],
                        wysiwyg=self.settings['wysiwyg'])
        else:
            if toggle > -1:
                f = open(path)
                tmp = []
                search_string = r'^(\s*?)(\[.\])\s(.*)$'
                index = 0
                for line in f.readlines():
                    regex = search(search_string, line)
                    if regex is not None:
                        if int(index) == int(toggle):
                            old = regex.group(2)
                            if old == '[x]':
                                new = '[ ]'
                            else:
                                new = '[x]'
                            line = "%s%s %s\n" % \
                            (regex.group(1), new, regex.group(3))
                        index = index + 1
                    tmp.append(line)
                f.close()
                note_contents = ''.join(tmp)

            f = open(path, 'w')
            f.write(note_contents.encode('utf8'))
            f.close()

            self.application.git.add(path)
            try:
                if note_contents == '':
                    message = 'creating %s' % path
                else:
                    message = 'updating %s' % path
                self.application.git.commit('-m', message)
            except ErrorReturnCode_1 as e:
                if 'nothing to commit' not in e.message:
                    raise
            if note_name_rename and note_name_rename != note_name:
                # rename note
                message = 'moving %s to %s' % (path, rename_path)
                # TODO: don't force; do something more graceful
                self.application.git.mv('-f', path, rename_path)
                self.application.git.commit('-m', message)
                note_enc = rename_note_enc
            self.redirect(note_enc.replace('#', '%23')) 
Example #28
Source File: builder.py    From stackhut with Apache License 2.0 4 votes vote down vote up
def setup_machine(self):
        """
        Setup the StackHut Docker Machine, creating if necessary
        Can override if user creates DM with name 'stackhut' externally
        """
        def docker_machine_state():
            try:
                state = str(sh.docker_machine.status(self.machine_name))
                if state.startswith('Running'):
                    return DockerMachineState.RUNNING
                elif state.startswith('Stopped') or state.startswith('Saved'):
                    return DockerMachineState.STOPPED
                else:
                    return DockerMachineState.UNKNOWN
            except sh.ErrorReturnCode_1:
                return DockerMachineState.NOTEXIST

        state = docker_machine_state()

        if state == DockerMachineState.NOTEXIST:
            log.info("StackHut Docker Machine not found, creating for first time, please wait...")
            # creating machine also starts it
            with toolkit_utils.Spinner():
                sh.docker_machine.create("--driver", "virtualbox", self.machine_name)
            state = docker_machine_state()
        elif state == DockerMachineState.STOPPED:
            log.info("Starting StackHut Docker Machine, please wait...")
            with toolkit_utils.Spinner():
                sh.docker_machine.start(self.machine_name)
            state = docker_machine_state()

        if state != DockerMachineState.RUNNING:
            raise RuntimeError("Couldn't start StackHut's Docker Machine")

        # setup machine env vars
        out = str(sh.docker_machine.env('--shell', "bash", self.machine_name))
        kwargs1 = [x.split("export ")[1].split('=')
                   for x in out.splitlines()
                   if x.startswith("export")]
        kwargs = {k: v.strip('\'"') for [k, v] in kwargs1}
        os.environ.update(kwargs)

        # get docker-machine ip - using default
        ip = str(sh.docker_machine.ip(self.machine_name)).strip()
        return ip