Python subprocess.CalledProcessError() Examples

The following are code examples for showing how to use subprocess.CalledProcessError(). They are from open source Python projects. You can vote up the examples you like or vote down the ones you don't like.

Example 1
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 8 votes vote down vote up
def import_ovpn(self):
        """
        Renames and imports the ovpn to the Network Manager
        Cleans up the temporary files
        """
        try:
            self.statusbar.showMessage("Importing Connection...")
            self.repaint()
            self.connection_name = self.generate_connection_name()
            ovpn_file = self.connection_name + '.ovpn'
            path = os.path.join(self.config_path, ovpn_file) #changes name from default
            shutil.copy(self.ovpn_path, path)
            os.remove(self.ovpn_path)
            output = subprocess.run(['nmcli', 'connection', 'import', 'type', 'openvpn', 'file', path])
            output.check_returncode()
            os.remove(path)

        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Importing VPN configuration") 
Example 2
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 7 votes vote down vote up
def enable_ipv6(self):
        """
        Re-enables ipv6 system wide
        """
        if self.sudo_password:
            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0', '&&', 'sysctl', '-w', 'net.ipv6.conf.default.disable_ipv6=0'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
            except subprocess.CalledProcessError:
                self.statusbar.showMessage("ERROR: disabling IPV6 failed", 2000)
        else:
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to enable IPV6. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()

            if self.sudo_password:
                try:
                    p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                    p2 = subprocess.Popen(['sudo', '-S', 'sysctl', '-w', 'net.ipv6.conf.all.disable_ipv6=0', '&&', 'sysctl', '-w', 'net.ipv6.conf.default.disable_ipv6=0'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                    p1.stdout.close()
                    p2.stdout.close()
                except subprocess.CalledProcessError:
                    self.statusbar.showMessage("ERROR: Enabling IPV6 failed", 2000) 
Example 3
Project: incubator-spot   Author: apache   File: gti.py    Apache License 2.0 7 votes vote down vote up
def _call_gti(self, command, num_values):
        try:
            response_json = check_output(command, shell=True)
            result_dict = json.loads(response_json[0:len(response_json) - 1])
            responses = result_dict['a']
            return responses

        except CalledProcessError as e:
            self._logger.error("Error calling McAfee GTI client in gti module: " + e.output)
            error_resp = [{self.REP_KEY: self.DEFAULT_REP}] * num_values
            return error_resp

        except ValueError as e:
            self._logger.error("Error reading JSON response in gti module: " + e.message)
            error_resp = [{self.REP_KEY: self.DEFAULT_REP}] * num_values
            return error_resp 
Example 4
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 votes vote down vote up
def add_secrets(self):
        """
        Adds the username and Password to the configuration
        """
        try:
            self.statusbar.showMessage("Adding Secrets...", 1000)
            self.repaint()
            password_flag = subprocess.run(['nmcli', 'connection', 'modify', self.connection_name, '+vpn.data', 'password-flags=0'])
            password_flag.check_returncode()
            secrets = subprocess.run(['nmcli', 'connection', 'modify', self.connection_name, '+vpn.secrets', 'password='+self.password])
            secrets.check_returncode()
            user_secret = subprocess.run(['nmcli', 'connection', 'modify', self.connection_name, '+vpn.data', 'username='+self.username])
            user_secret.check_returncode()
            disable_ipv6 = subprocess.run(['nmcli', 'connection', 'modify', self.connection_name, '+ipv6.method', 'ignore'])
            disable_ipv6.check_returncode()
            password_flag = subprocess.run(['nmcli', 'connection', 'modify', self.connection_name, '+vpn.data', 'password-flags=0'])
            password_flag.check_returncode()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Secrets could not be added", 2000) 
Example 5
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 6 votes vote down vote up
def get_interfaces(self):
        """
        Gets current Network interfaces

        :return: List of Network interfaces
        """
        try:
            output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,DEVICE', 'device', 'status'], stdout=subprocess.PIPE)
            output.check_returncode()

            lines = output.stdout.decode('utf-8').split('\n')
            interfaces = []

            for line in lines:
                if line:
                    elements = line.strip().split(':')

                    if (elements[0] == 'wifi') or (elements[0] == 'ethernet'):
                        interfaces.append(elements[1])

            return interfaces

        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR Fetching interfaces") 
Example 6
Project: fs_image   Author: facebookincubator   File: test_unshare.py    MIT License 6 votes vote down vote up
def test_pid_namespace_dead_keepalive(self):
        with Unshare([Namespace.PID]) as unshare:
            self._check_ns_diff(unshare, {'pid'})

            good_echo = nsenter_as_user(unshare, 'echo')
            subprocess.check_call(good_echo)  # Will fail once the NS is dead

            proc, _ = self._popen_sleep_forever(unshare)
            time.sleep(2)  # Leave some time for `sleep` to exit erroneously
            self.assertEqual(None, proc.poll())  # Sleeps forever

            self._kill_keepalive(unshare)

            self.assertEqual(-signal.SIGKILL, proc.wait())  # The NS is dead

            # The `echo` command that worked above no longer works.
            with self.assertRaises(subprocess.CalledProcessError):
                subprocess.check_call(good_echo) 
Example 7
Project: fs_image   Author: facebookincubator   File: test_nspawn_in_subvol.py    MIT License 6 votes vote down vote up
def test_bindmount_ro(self):
        with tempfile.TemporaryDirectory() as tmpdir:
            with self.assertRaises(subprocess.CalledProcessError):
                ret = self._nspawn_in('host', [
                    '--user',
                    'root',
                    '--bindmount-ro',
                    tmpdir, '/tmp',
                    '--',
                    'touch',
                    '/tmp/testfile',
                ])
                self.assertEqual(
                    "touch: cannot touch '/tmp/testfile': " +
                        "Read-only file system",
                    ret.stdout,
                ) 
Example 8
Project: leapp-repository   Author: oamg   File: library.py    Apache License 2.0 6 votes vote down vote up
def _get_cmd_output(cmd, delim, expected_len):
    ''' Verify if command exists and return output '''
    if not any(os.access(os.path.join(path, cmd[0]), os.X_OK) for path in os.environ['PATH'].split(os.pathsep)):
        api.current_logger().warning("'%s': command not found" % cmd[0])
        raise StopIteration

    try:
        # FIXME: Will keep call to subprocess until our stdlib supports "env" parameter
        # when there is any fd except 0,1,2 open, lvm closes the fd and prints a warning.
        # In our case /dev/urandom has other fd opened, probably for caching purposes.
        output = subprocess.check_output(cmd, env={'LVM_SUPPRESS_FD_WARNINGS': '1', 'PATH': os.environ['PATH']})

    except subprocess.CalledProcessError as e:
        api.current_logger().debug("Command '%s' return non-zero exit status: %s" % (" ".join(cmd), e.returncode))
        raise StopIteration

    for entry in output.split('\n'):
        entry = entry.strip()
        if not entry:
            continue

        data = entry.split(delim)
        data.extend([''] * (expected_len - len(data)))

        yield data 
Example 9
Project: godot-mono-builds   Author: godotengine   File: os_utils.py    MIT License 6 votes vote down vote up
def run_command(command, args=[], cwd=None, env=None, name='command'):
    def cmd_args_to_str(cmd_args):
        return ' '.join([arg if not ' ' in arg else '"%s"' % arg for arg in cmd_args])

    assert isinstance(command, str) and isinstance(args, list)
    args = [command] + args

    check_call_args = {}
    if cwd is not None:
        check_call_args['cwd'] = cwd
    if env is not None:
        check_call_args['env'] = env

    import subprocess
    try:
        print('Running command \'%s\': %s' % (name, subprocess.list2cmdline(args)))
        subprocess.check_call(args, **check_call_args)
        print('Command \'%s\' completed successfully' % name)
    except subprocess.CalledProcessError as e:
        raise BuildError('\'%s\' exited with error code: %s' % (name, e.returncode)) 
Example 10
Project: ieml   Author: IEMLdev   File: word_to_latex.py    GNU General Public License v3.0 6 votes vote down vote up
def compile_latex(latex_str):
    old_cwd = os.getcwd()

    with tempfile.TemporaryDirectory() as tempdir:
        path = os.path.join(tempdir, 'output')

        logger.error(path)
        doc = Document(path, data=[dumps_list([latex_str], escape=False)], geometry_options='landscape')

        doc.packages.append(Package('xcolor', ['dvipsnames','table']))
        try:
            doc.generate_pdf(clean_tex=False, silent=False)
            doc.generate_tex()
        except subprocess.CalledProcessError as e:
            os.chdir(old_cwd)  # because pylatex change it but doesnt restore it
            raise e

        with open(path + '.pdf', 'rb') as fp:
            return fp.read() 
Example 11
Project: django-click   Author: GaretJax   File: conftest.py    MIT License 6 votes vote down vote up
def manage():
    def call(*args, **kwargs):
        ignore_errors = kwargs.pop('ignore_errors', False)
        assert not kwargs
        cmd = [
            sys.executable,
            os.path.join(os.path.dirname(__file__), 'testprj', 'manage.py'),
        ] + list(args)
        try:
            return subprocess.check_output(cmd, stderr=subprocess.STDOUT)
        except subprocess.CalledProcessError as e:
            if not ignore_errors:
                raise
            return e.output

    return call 
Example 12
Project: django-click   Author: GaretJax   File: test_adapter.py    MIT License 6 votes vote down vote up
def test_django_traceback(manage):
    try:
        manage('errcmd')
    except subprocess.CalledProcessError as e:
        assert e.output == b'CommandError: Raised error description\n'
        assert e.returncode == 1
    else:
        assert False  # NOCOV

    try:
        manage('errcmd', '--traceback')
    except subprocess.CalledProcessError as e:
        lines = e.output.splitlines()
        assert lines[0] == b'Traceback (most recent call last):'
        for line in lines[1:-1]:
            assert line.startswith(b'  ')
        # Use `.endswith()` because of differences between CPython and pypy
        assert lines[-1].endswith(b'CommandError: Raised error description')
        assert e.returncode == 1
    else:
        assert False  # NOCOV 
Example 13
Project: speedrun   Author: inferno-pytorch   File: core.py    Apache License 2.0 6 votes vote down vote up
def update_git_revision(self, overwrite=False):
        """
        Updates the configuration with a 'git_rev' field with the current HEAD revision.

        Parameters
        ----------
        overwrite : bool
            If a 'git_rev' field already exists, Whether to overwrite it.

        Returns
        -------
            BaseExperiment
        """
        try:
            gitcmd = ["git", "rev-parse", "--verify", "HEAD"]
            gitrev = subprocess.check_output(gitcmd).decode('latin1').strip()
        except subprocess.CalledProcessError:
            gitrev = "none"
        if not overwrite and self.get('git_rev', None) is not None:
            # Git rev already in config and we're not overwriting, so...
            pass
        else:
            self.set("git_rev", gitrev)
        return self 
Example 14
Project: neural-fingerprinting   Author: StephanZheng   File: validate_submission_lib.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _verify_docker_image_size(self, image_name):
    """Verifies size of Docker image.

    Args:
      image_name: name of the Docker image.

    Returns:
      True if image size is withing the limits, False otherwise.
    """
    shell_call(['docker', 'pull', image_name])
    try:
      image_size = subprocess.check_output(
          ['docker', 'inspect', '--format={{.Size}}', image_name]).strip()
      image_size = int(image_size) if PY3 else long(image_size)
    except (ValueError, subprocess.CalledProcessError) as e:
      logging.error('Failed to determine docker image size: %s', e)
      return False
    logging.info('Size of docker image %s is %d', image_name, image_size)
    if image_size > MAX_DOCKER_IMAGE_SIZE:
      logging.error('Image size exceeds limit %d', MAX_DOCKER_IMAGE_SIZE)
    return image_size <= MAX_DOCKER_IMAGE_SIZE 
Example 15
Project: neural-fingerprinting   Author: StephanZheng   File: validate_and_copy_submissions.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def run(self):
    """Runs validation of all submissions."""
    cmd = ['gsutil', 'ls', os.path.join(self.source_dir, '**')]
    try:
      files_list = subprocess.check_output(cmd).split('\n')
    except subprocess.CalledProcessError:
      logging.error('Can''t read source directory')
    all_submissions = [
        s for s in files_list
        if s.endswith('.zip') or s.endswith('.tar') or s.endswith('.tar.gz')
    ]
    for submission_path in all_submissions:
      self.validate_and_copy_one_submission(submission_path)
    self.stats.log_stats()
    self.save_id_to_path_mapping()
    if self.containers_file:
      with open(self.containers_file, 'w') as f:
        f.write('\n'.join(sorted(self.list_of_containers))) 
Example 16
Project: neural-fingerprinting   Author: StephanZheng   File: validate_submission_lib.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def _verify_docker_image_size(self, image_name):
    """Verifies size of Docker image.

    Args:
      image_name: name of the Docker image.

    Returns:
      True if image size is within the limits, False otherwise.
    """
    shell_call(['docker', 'pull', image_name])
    try:
      image_size = subprocess.check_output(
          ['docker', 'inspect', '--format={{.Size}}', image_name]).strip()
      image_size = int(image_size) if PY3 else long(image_size)
    except (ValueError, subprocess.CalledProcessError) as e:
      logging.error('Failed to determine docker image size: %s', e)
      return False
    logging.info('Size of docker image %s is %d', image_name, image_size)
    if image_size > MAX_DOCKER_IMAGE_SIZE:
      logging.error('Image size exceeds limit %d', MAX_DOCKER_IMAGE_SIZE)
    return image_size <= MAX_DOCKER_IMAGE_SIZE 
Example 17
Project: redrum   Author: Evidlo   File: redrum.py    MIT License 6 votes vote down vote up
def set_wallpaper(config, image):

    print("Applying wallpaper")

    # download image to `image`
    try:
        response = requests.get(image['link'])
        if response.status_code == 200:
            with open(config.image_file, 'wb') as f:
                f.write(response.content)
        else:
            logging.error("Got response {} when downloading image.".format(reponse.status_code))
    except ConnectionError:
        logging.error("Connection error")
        sys.exit()

    try:
        subprocess.check_output(config.wallpaper_command.format(image_file=config.image_file), shell=True)
    except subprocess.CalledProcessError as e:
        logger.error("Command `{}` failed with status {}".format(e.cmd, e.returncode))
        sys.exit()


# save date, options, seen images and images to cache 
Example 18
Project: core   Author: lifemapper   File: makeflow_debugger.py    GNU General Public License v3.0 6 votes vote down vote up
def runMakeflow(dagFn, pre_options, ws, log, name=None, logFile=None):
    """
    @summary: Run a makeflow and return a boolean indicating if it was successful
    @param dagFn: The file path to the Makeflow DAG file
    @param pre_options: Makeflow options from the script arguments (unclean)
    @param ws: The workspace were the Makeflow shoudl run
    @param log: LmLogger instance to be used for logging
    @param name: The name of the Makeflow (for use with a catalog server)
    @param logFile: The file location for Makeflow to log
    """
    options = processMfOptions(pre_options, ws, log, name=name, logFile=logFile)
    mfCmd = getMakeflowCommand(dagFn, options)
    
    try:
        cmdOutput = subprocess.check_output(mfCmd, shell=True)
        success = True
    except subprocess.CalledProcessError, e:
        log.debug("Process failed")
        log.debug(e.output)
        success = False 
Example 19
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: docker_cache.py    Apache License 2.0 6 votes vote down vote up
def delete_local_docker_cache(docker_tag):
    """
    Delete the local docker cache for the entire docker image chain
    :param docker_tag: Docker tag
    :return: None
    """
    history_cmd = ['docker', 'history', '-q', docker_tag]

    try:
        image_ids_b = subprocess.check_output(history_cmd)
        image_ids_str = image_ids_b.decode('utf-8').strip()
        layer_ids = [id.strip() for id in image_ids_str.split('\n') if id != '<missing>']

        delete_cmd = ['docker', 'image', 'rm', '--force']
        delete_cmd.extend(layer_ids)
        subprocess.check_call(delete_cmd)
    except subprocess.CalledProcessError as error:
        # Could be caused by the image not being present
        logging.debug('Error during local cache deletion %s', error) 
Example 20
Project: helloworld   Author: pip-uninstaller-python   File: learning.py    GNU General Public License v2.0 6 votes vote down vote up
def do_POST(self):
        self.close_connection = True
        if self.path != '/run':
            return self.send_error(400)
        print('Prepare code...')
        body = self.rfile.read(int(self.headers['Content-length']))
        qs = parse.parse_qs(body.decode('utf-8'))
        if not 'code' in qs:
            return self.send_error(400)
        code = qs['code'][0]
        r = dict()
        try:
            fpath = write_py(get_name(), code)
            print('Execute: %s %s' % (EXEC, fpath))
            r['output'] = decode(subprocess.check_output([EXEC, fpath], stderr=subprocess.STDOUT, timeout=5))
        except subprocess.CalledProcessError as e:
            r = dict(error='Exception', output=decode(e.output))
        except subprocess.TimeoutExpired as e:
            r = dict(error='Timeout', output='执行超时')
        except subprocess.CalledProcessError as e:
            r = dict(error='Error', output='执行错误')
        print('Execute done.')
        self._sendHttpHeader()
        self._sendHttpBody(r) 
Example 21
Project: advent-of-code-2018   Author: badouralix   File: rust.py    MIT License 6 votes vote down vote up
def __init__(self, file):
        DEVNULL = open(os.devnull, 'wb')
        SubmissionWrapper.__init__(self)
        tmpdir = tempfile.TemporaryDirectory(prefix="aoc")
        tmpdir.cleanup()
        try:
            subprocess.check_output(
                ["cargo", "test", "--bin", file.replace('/', '-')[:-3]], stderr=DEVNULL).decode()
        except subprocess.CalledProcessError as e:
            raise CompilationError(e.output)

        e = subprocess.Popen(["cargo", "build", "--release", "--bin",
                              file.replace('/', '-')[:-3]],
                             env={**os.environ, "CARGO_TARGET_DIR": tmpdir.name},
                             stdout=DEVNULL,
                             stderr=DEVNULL).wait()
        if e > 0:
            raise CompilationError("Could not compile " + file)
        self.executable = tmpdir.name + "/release/" + file.replace('/', '-')[:-3] 
Example 22
Project: InsightAgent   Author: insightfinder   File: getlogs_spark.py    Apache License 2.0 6 votes vote down vote up
def check_project(project_name):
    if 'token' in if_config_vars and len(if_config_vars['token']) != 0:
        logger.debug(project_name)
        try:
            # check for existing project
            check_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/getprojectstatus')
            output_check_project = subprocess.check_output('curl "' + check_url + '?userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&projectList=%5B%7B%22projectName%22%3A%22' + project_name + '%22%2C%22customerName%22%3A%22' + if_config_vars['user_name'] + '%22%2C%22projectType%22%3A%22CUSTOM%22%7D%5D&tzOffset=-14400000"', shell=True)
            # create project if no existing project
            if project_name not in output_check_project:
                logger.debug('creating project')
                create_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/add-custom-project')
                output_create_project = subprocess.check_output('no_proxy= curl -d "userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&projectName=' + project_name + '&instanceType=PrivateCloud&projectCloudType=PrivateCloud&dataType=' + get_data_type_from_project_type() + '&samplingInterval=' + str(if_config_vars['sampling_interval'] / 60) +  '&samplingIntervalInSeconds=' + str(if_config_vars['sampling_interval']) + '&zone=&email=&access-key=&secrete-key=&insightAgentType=' + get_insight_agent_type_from_project_type() + '" -H "Content-Type: application/x-www-form-urlencoded" -X POST ' + create_url + '?tzOffset=-18000000', shell=True)
            # set project name to proposed name
            if_config_vars['project_name'] = project_name
            # try to add new project to system
            if 'system_name' in if_config_vars and len(if_config_vars['system_name']) != 0:
                system_url = urlparse.urljoin(if_config_vars['if_url'], '/api/v1/projects/update')
                output_update_project = subprocess.check_output('no_proxy= curl -d "userName=' + if_config_vars['user_name'] + '&token=' + if_config_vars['token'] + '&operation=updateprojsettings&projectName=' + project_name + '&systemName=' + if_config_vars['system_name'] + '" -H "Content-Type: application/x-www-form-urlencoded" -X POST ' + system_url + '?tzOffset=-18000000', shell=True)
        except subprocess.CalledProcessError as e:
            logger.error('Unable to create project for ' + project_name + '. Data will be sent to ' + if_config_vars['project_name']) 
Example 23
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def randomize_mac(self):
        """
        Takes down network interface and brings it back with a new MAC Address
        """
        try:
            self.statusbar.showMessage("Randomizing MAC Address", 2000)
            self.repaint()
            output = subprocess.run(['nmcli', '--mode', 'tabular', '--terse', '--fields', 'TYPE,UUID', 'connection', 'show', '--active'], stdout=subprocess.PIPE)
            output.check_returncode()
            lines = output.stdout.decode('utf-8').split('\n')

            for line in lines:
                if line:
                    elements = line.strip().split(':')
                    uuid = elements[1]
                    connection_type = elements[0]
                    if type != 'vpn':
                        subprocess.run(['nmcli', 'connection', 'down', uuid])
                        subprocess.run(['nmcli', 'connection', 'modify', '--temporary', uuid, connection_type+'.cloned-mac-address', 'random'])
                        subprocess.run(['nmcli', 'connection', 'up', uuid])

            self.statusbar.showMessage("Random MAC Address assigned", 2000)
            self.write_conf()
            self.repaint()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Randomizer failed", 2000)
            self.repaint() 
Example 24
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def enable_connection(self):
        """
        Enable vpn connection in NetworkManager
        """
        try:
            self.statusbar.showMessage("Connecting...", 1000)
            self.repaint()
            connection = subprocess.run(['nmcli', 'connection', 'up', self.connection_name])
            connection.check_returncode()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Connection Failed", 2000) 
Example 25
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def disable_connection(self):
        """
        Disconnect vpn connection in NetworkManager
        """
        try:
            self.statusbar.showMessage("Disconnecting...", 1000)
            self.repaint()
            connection = subprocess.run(['nmcli', 'connection', 'down', self.connection_name])
            connection.check_returncode()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Disconnection Failed", 2000) 
Example 26
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 5 votes vote down vote up
def remove_connection(self):
        """
        Remove connection from network manager
        """
        try:
            connection = subprocess.run(['nmcli', 'connection', 'delete', self.connection_name])
            connection.check_returncode()
        except subprocess.CalledProcessError:
            self.statusbar.showMessage("ERROR: Failed to remove Connection", 2000) 
Example 27
Project: fs_image   Author: facebookincubator   File: rpm_metadata.py    MIT License 5 votes vote down vote up
def _repo_query(
        self,
        db_path: Path,
        package_name: str,
        package_path: Path,
    ) -> "RpmMetadata":
        query_args = [
            "rpm",
            "--query",
            "--queryformat",
            "'%{NAME}:%{epochnum}:%{VERSION}:%{RELEASE}'",
        ]

        if db_path and package_name and (package_path is None):
            query_args += ["--dbpath", db_path, package_name]
        elif package_path and (db_path is None and package_name is None):
            query_args += ["--package", package_path]
        else:
            raise ValueError(
                "Must pass only (--dbpath and --package_name) or --package"
            )

        try:
            result = subprocess.check_output(
                query_args,
                stderr=subprocess.PIPE,
            ).decode().strip("'\"")
        except subprocess.CalledProcessError as e:
            raise RuntimeError(f"Error querying RPM: {e.stdout}, {e.stderr}")

        n, e, v, r = result.split(":")
        return RpmMetadata(name=n, epoch=int(e), version=v, release=r)


# Returns  1 if the version of a is newer than b
# Returns  0 if the versions match
# Returns -1 if the version of a is older than b
#
# Referenced from:
# github.com/rpm-software-management/yum/blob/master/rpmUtils/miscutils.py 
Example 28
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_from_snapshot.py    MIT License 5 votes vote down vote up
def test_fail_to_write_to_protected_path(self):
        # Nothing fails with no specified protection, or with /meta:
        for p in [[], ['meta/']]:
            with self._install(protected_paths=p):
                pass
        with self.assertRaises(subprocess.CalledProcessError) as ctx:
            with self._install(protected_paths=['usr/share/rpm_test/']):
                pass
        with self.assertRaises(subprocess.CalledProcessError) as ctx:
            with self._install(protected_paths=[
                'usr/share/rpm_test/milk.txt'
            ]):
                pass
        # It was none other than `yum install` that failed.
        self.assertEqual(_INSTALL_ARGS, ctx.exception.cmd[-len(_INSTALL_ARGS):]) 
Example 29
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 5 votes vote down vote up
def check_popen_returncode(proc: subprocess.Popen):
    if proc.returncode != 0:  # pragma: no cover
        # Providing a meaningful coverage test for this is annoying, so I just
        # tested manually:
        #   >>> import subprocess
        #   >>> raise subprocess.CalledProcessError(returncode=5, cmd=['a'])
        #   Traceback (most recent call last):
        #     File "<stdin>", line 1, in <module>
        #   subprocess.CalledProcessError: Command '['a']' returned non-zero
        #   exit status 5.
        raise subprocess.CalledProcessError(
            returncode=proc.returncode, cmd=proc.args,
        ) 
Example 30
Project: fs_image   Author: facebookincubator   File: test_fs_utils.py    MIT License 5 votes vote down vote up
def test_open_for_read_decompress(self):
        # The goal is that our stream should be bigger than any buffers
        # involved (so we get to test edge effects), but not so big that the
        # test takes more than 1-2 seconds.
        n_bytes = 12 << 20  # 12MiB
        my_line = b'kitteh' * 700 + b'\n'  # ~ 4KiB
        for compress, ext in [('gzip', 'gz'), ('zstd', 'zst')]:
            filename = 'kitteh.' + ext
            with temp_dir() as td, open(td / filename, 'wb') as outf:
                with subprocess.Popen(
                    [compress, '-'], stdin=subprocess.PIPE, stdout=outf,
                ) as proc:
                    for _ in range(n_bytes // len(my_line)):
                        proc.stdin.write(my_line)
                check_popen_returncode(proc)

                with open_for_read_decompress(td / filename) as infile:
                    for l in infile:
                        self.assertEqual(my_line, l)

        # Test uncompressed
        with temp_dir() as td:
            with open(td / 'kitteh', 'wb') as outfile:
                outfile.write(my_line + b'meow')
            with open_for_read_decompress(td / 'kitteh') as infile:
                self.assertEqual(my_line + b'meow', infile.read())

        # Test decompression error
        with temp_dir() as td:
            with open(td / 'kitteh.gz', 'wb') as outfile:
                outfile.write(my_line)
            with self.assertRaises(subprocess.CalledProcessError), \
                    open_for_read_decompress(td / 'kitteh.gz') as infile:
                infile.read() 
Example 31
Project: fs_image   Author: facebookincubator   File: test_install_file.py    MIT License 5 votes vote down vote up
def test_install_file_command(self):
        with TempSubvolumes(sys.argv[0]) as temp_subvolumes, \
                tempfile.NamedTemporaryFile() as empty_tf:
            subvol = temp_subvolumes.create('tar-sv')
            subvol.run_as_root(['mkdir', subvol.path('d')])

            _install_file_item(
                from_target='t', source={'source': empty_tf.name},
                dest='/d/empty',
            ).build(subvol, DUMMY_LAYER_OPTS)
            self.assertEqual(
                ['(Dir)', {'d': ['(Dir)', {'empty': ['(File m444)']}]}],
                render_subvol(subvol),
            )

            # Fail to write to a nonexistent dir
            with self.assertRaises(subprocess.CalledProcessError):
                _install_file_item(
                    from_target='t', source={'source': empty_tf.name},
                    dest='/no_dir/empty',
                ).build(subvol, DUMMY_LAYER_OPTS)

            # Running a second copy to the same destination. This just
            # overwrites the previous file, because we have a build-time
            # check for this, and a run-time check would add overhead.
            _install_file_item(
                from_target='t', source={'source': empty_tf.name},
                dest='/d/empty',
                # A non-default mode & owner shows that the file was
                # overwritten, and also exercises HasStatOptions.
                mode='u+rw', user_group='12:34',
            ).build(subvol, DUMMY_LAYER_OPTS)
            self.assertEqual(
                ['(Dir)', {'d': ['(Dir)', {'empty': ['(File m600 o12:34)']}]}],
                render_subvol(subvol),
            ) 
Example 32
Project: leapp-repository   Author: oamg   File: install_actor_deps.py    Apache License 2.0 5 votes vote down vote up
def install(path):
    cmd = "make -f {} install-deps".format(path)
    try:
        check_call(cmd, shell=True)
    except CalledProcessError as e:
        error(str(e) + '\n', e.returncode) 
Example 33
Project: incubator-spot   Author: apache   File: utils.py    Apache License 2.0 5 votes vote down vote up
def remove_kafka_topic(cls,zk,topic,logger):
        rm_kafka_topic = "kafka-topics --delete --zookeeper {0} --topic {1}".format(zk,topic)
        try:
            logger.info("SPOT.Utils: Executing: {0}".format(rm_kafka_topic))
            subprocess.call(rm_kafka_topic,shell=True)

        except subprocess.CalledProcessError as e:
            logger.error("SPOT.Utils: There was an error executing: {0}".format(e.cmd))
            sys.exit(1) 
Example 34
Project: incubator-spot   Author: apache   File: utils.py    Apache License 2.0 5 votes vote down vote up
def execute_cmd(cls,command,logger):

        try:
            logger.info("SPOT.Utils: Executing: {0}".format(command))
            subprocess.call(command,shell=True)

        except subprocess.CalledProcessError as e:
            logger.error("SPOT.Utils: There was an error executing: {0}".format(e.cmd))
            sys.exit(1) 
Example 35
Project: incubator-spot   Author: apache   File: utilities.py    Apache License 2.0 5 votes vote down vote up
def execute_cmd(cls,command,logger):

        try:
            logger.info("SPOT.Utils: Executing: {0}".format(command))
            subprocess.call(command,shell=True)

        except subprocess.CalledProcessError as e:
            logger.error("SPOT.Utils: There was an error executing: {0}".format(e.cmd))
            sys.exit(1) 
Example 36
Project: incubator-spot   Author: apache   File: utilities.py    Apache License 2.0 5 votes vote down vote up
def execute_hive_cmd(cls,command,logger):

        try:
            logger.info("SPOT.Utils: Executing hive command: hive -e \"{0}\"".format(command))
            subprocess.call("hive -e \"{0}\"".format(command),shell=True)

        except subprocess.CalledProcessError as e:
            logger.error("SPOT.Utils: There was an error executing: {0}".format(e.cmd))
            sys.exit(1) 
Example 37
Project: ubittool   Author: carlosperate   File: test_system_cli.py    MIT License 5 votes vote down vote up
def _run_cli_cmd(cmd_list):
    """Run a shell command and return the output.

    :param cmd_list: A list of strings that make up the command to execute.
    """
    try:
        return subprocess.check_output(cmd_list, stderr=subprocess.STDOUT)
    except subprocess.CalledProcessError as e:
        return e.output 
Example 38
Project: pyblish-win   Author: pyblish   File: dist.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def exe(src, dst):
    """Create installer using Inno Setup

    Arguments:
        src (str): Path to bundle, e.g. /build
        dst (str): Output directory in which to compile installer, e.g. /dist

    """

    print("Creating installer..")
    try:
        iscc = subprocess.check_output(["where", "iscc"]).strip()
    except subprocess.CalledProcessError:
        print("Could not find Inno Setup")
        return 1

    setup = os.path.join(os.getcwd(), "setup.iss")

    print("Compiling \"%s\" using \"%s\"" % (setup, iscc))
    BUILD = os.environ.get("APPVEYOR_BUILD_NUMBER", "0")
    VERSION = os.path.join(__file__, "..", "..", "lib", "pyblish", "VERSION")
    with open(VERSION) as f:
        VERSION = f.read()

    subprocess.call([iscc,
                     "/dMyVersion=%s-build%s" % (VERSION, BUILD),
                     "/dMyOutputDir=%s" % dst,
                     setup])

    print("Successfully created installer") 
Example 39
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_check_call_nonzero(self):
        # check_call() function with non-zero return code
        with self.assertRaises(subprocess.CalledProcessError) as c:
            subprocess.check_call([sys.executable, "-c",
                                   "import sys; sys.exit(47)"])
        self.assertEqual(c.exception.returncode, 47) 
Example 40
Project: pyblish-win   Author: pyblish   File: test_subprocess.py    GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_check_output_nonzero(self):
        # check_call() function with non-zero return code
        with self.assertRaises(subprocess.CalledProcessError) as c:
            subprocess.check_output(
                    [sys.executable, "-c", "import sys; sys.exit(5)"])
        self.assertEqual(c.exception.returncode, 5) 
Example 41
Project: arm_now   Author: nongiach   File: utils.py    MIT License 5 votes vote down vote up
def which(filename, **kwargs):
    try:
        subprocess.check_output(["which", filename])
        return True
    except subprocess.CalledProcessError:
        if distribution() in kwargs:
            print(kwargs[distribution()])
        else:
            print(kwargs["ubuntu"])
        return False 
Example 42
Project: arm_now   Author: nongiach   File: filesystem.py    MIT License 5 votes vote down vote up
def rm(self, filename):
        def e2rm_warning(_exception):
            porange("WARNING: e2rm file already suppressed")
        with exall(subprocess.check_call, subprocess.CalledProcessError, e2rm_warning):
            subprocess.check_call(["e2rm", self.rootfs + ":" + filename]) 
Example 43
Project: arm_now   Author: nongiach   File: filesystem.py    MIT License 5 votes vote down vote up
def check(self):
        try:
            print(" Checking the filesystem ".center(80, "+"))
            subprocess.check_call(["e2fsck", "-vfy", self.rootfs])
        except subprocess.CalledProcessError as e:
            print(e)
            if str(e).find("returned non-zero exit status 1."):
                porange("It's ok but next time poweroff") 
Example 44
Project: logging-test-case   Author: chadrosenquist   File: loggingtestcase_test.py    MIT License 5 votes vote down vote up
def run_test(command):
    """Runs a test case and returns the output."""
    new_command = 'python -m unittest ' + command
    new_command = new_command.split()
    try:
        output = subprocess.check_output(new_command,
                                         stderr=subprocess.STDOUT,
                                         universal_newlines=True)
    except subprocess.CalledProcessError as error:
        output = error.output

    return output 
Example 45
Project: kvmd   Author: pikvm   File: __init__.py    GNU General Public License v3.0 5 votes vote down vote up
def _remount(path: str, rw: bool) -> None:
    mode = ("rw" if rw else "ro")
    _log(f"Remouning {path} to {mode.upper()}-mode ...")
    try:
        subprocess.check_call([_MOUNT_PATH, "--options", f"remount,{mode}", path])
    except subprocess.CalledProcessError as err:
        raise SystemExit(f"Can't remount: {err}") 
Example 46
Project: django-click   Author: GaretJax   File: test_adapter.py    MIT License 5 votes vote down vote up
def test_django_pythonpath(manage):
    with pytest.raises(subprocess.CalledProcessError):
        manage('pathcmd')

    manage('pathcmd', '--pythonpath',
           os.path.join(os.path.dirname(__file__), 'testdir')) == b'1' 
Example 47
Project: git-deployment-scripts   Author: codechick   File: deployment.py    MIT License 5 votes vote down vote up
def __stash_modules_changes(self, op='save'):
        exit_code = 0

        if op not in ('save', 'pop'):
            raise ValueError("Bad argument: %s" % op)

        try:
            if op == 'save':
                self.__print('Stashing all the modules...')
                cmd = self.__cmd_prefix + "submodule foreach 'git stash save --all';"
                exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)
            else:
                self.__print('Reapplying stash to the modules...')
                cmd = self.__cmd_prefix + "submodule foreach 'git stash pop ||:'"
                exit_code = subprocess.call(cmd, stderr=subprocess.STDOUT, shell=True)
        except subprocess.CalledProcessError as e:
            self.__print('An error occoured and stashing has terminated unexpectedly.', 'ERR')
            ret = e.output
            exit_code = 1

        if exit_code == 0:
            self.__print('Done')
        else:
            self.__print('Stashing met some errors. Proceding anyway', 'ERR')

#        self.__output_history.append({'op': 'stash_modules_changes', 'out': ret})

        if exit_code == 0:
            return True
        else:
            return False 
Example 48
Project: neural-fingerprinting   Author: StephanZheng   File: worker.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def shell_call(command, **kwargs):
  """Calls shell command with argument substitution.

  Args:
    command: command represented as a list. Each element of the list is one
      token of the comman. For example "cp a b" becomes ['cp', 'a', 'b']
      If any element of the list looks like '${NAME}' then it will be replaced
      by value from **kwargs with key 'NAME'.
    **kwargs: dictionary with argument substitution

  Returns:
    output of the command

  Raises:
    subprocess.CalledProcessError if command return value is not zero

  This function is useful when you need to do variable substitution prior
  running the command. Below are few examples of how it works:

    shell_call(['cp', 'a', 'b'], a='asd') calls command 'cp a b'

    shell_call(['cp', '${a}', 'b'], a='asd') calls command 'cp asd b',
    '${a}; was replaced with 'asd' before calling the command
  """
  command = list(command)
  for i in range(len(command)):
    m = CMD_VARIABLE_RE.match(command[i])
    if m:
      var_id = m.group(1)
      if var_id in kwargs:
        command[i] = kwargs[var_id]
  logging.debug('Executing shell command: %s', ' '.join(command))
  return subprocess.check_output(command) 
Example 49
Project: neural-fingerprinting   Author: StephanZheng   File: worker.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def sudo_remove_dirtree(dir_name):
  """Removes directory tree as a superuser.

  Args:
    dir_name: name of the directory to remove.

  This function is necessary to cleanup directories created from inside a
  Docker, since they usually written as a root, thus have to be removed as a
  root.
  """
  try:
    subprocess.check_output(['sudo', 'rm', '-rf', dir_name])
  except subprocess.CalledProcessError as e:
    raise WorkerError('Can''t remove directory {0}'.format(dir_name), e) 
Example 50
Project: Ansible-Example-AB2018   Author: umit-ozturk   File: configuration.py    MIT License 5 votes vote down vote up
def open_in_editor(self, options, args):
        editor = self._determine_editor(options)

        fname = self.configuration.get_file_to_edit()
        if fname is None:
            raise PipError("Could not determine appropriate file.")

        try:
            subprocess.check_call([editor, fname])
        except subprocess.CalledProcessError as e:
            raise PipError(
                "Editor Subprocess exited with exit code {}"
                .format(e.returncode)
            ) 
Example 51
Project: friendly-telegram   Author: friendly-telegram   File: updater.py    GNU Affero General Public License v3.0 5 votes vote down vote up
def req_common(self):
        # Now we have downloaded new code, install requirements
        logger.debug("Installing new requirements...")
        try:
            subprocess.run([sys.executable, "-m", "pip", "install", "-r",
                            os.path.join(os.path.dirname(utils.get_base_dir()), "requirements.txt"), "--user"])
        except subprocess.CalledProcessError:
            logger.exception("Req install failed") 
Example 52
Project: 2015-SpaceInvaders-Bot-Python   Author: EntelectChallenge   File: ez_setup.py    MIT License 5 votes vote down vote up
def _clean_check(cmd, target):
    """
    Run the command to download target.

    If the command fails, clean up before re-raising the error.
    """
    try:
        subprocess.check_call(cmd)
    except subprocess.CalledProcessError:
        if os.access(target, os.F_OK):
            os.unlink(target)
        raise 
Example 53
Project: flasky   Author: RoseOu   File: test_svn.py    MIT License 5 votes vote down vote up
def _do_svn_check():
    try:
        subprocess.check_call(["svn", "--version"],
                              shell=(sys.platform == 'win32'))
        return True
    except (OSError, subprocess.CalledProcessError):
        return False 
Example 54
Project: core   Author: lifemapper   File: makeflow_debugger.py    GNU General Public License v3.0 5 votes vote down vote up
def cleanupMakeflow(mfDag, workspace, cleanupVal, success, log):
    """
    @summary: Clean up a Makeflow
    """
    if cleanupVal in [-1, 1, 2] or (cleanupVal == 0 and success):
        cleanCmd = getMakeflowCleanCommand(mfDag, workspace)
        try:
            subprocess.check_call(cleanCmd, shell=True)
        except subprocess.CalledProcessError, e:
            log.debug('Could not clean up Makeflow:')
            log.debug(str(e)) 
Example 55
Project: cmus-osx   Author: PhilipTrauner   File: util.py    MIT License 5 votes vote down vote up
def get_cmus_instances() -> Optional[List[int]]:
    try:
        return [
            int(pid)
            for pid in check_output(["pgrep", "-x", "cmus"]).decode().split("\n")
            if pid != ""
        ]
    except CalledProcessError:
        return None


# https://gist.github.com/walkermatt/2871026#gistcomment-2280711 
Example 56
Project: cmus-osx   Author: PhilipTrauner   File: media_keys.py    MIT License 5 votes vote down vote up
def get_cmus_instances() -> Optional[List[int]]:
    try:
        return [
            int(pid)
            for pid in check_output(["pgrep", "-x", "cmus"]).decode().split("\n")
            if pid != ""
        ]
    except CalledProcessError:
        return None 
Example 57
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: mxdoc.py    Apache License 2.0 5 votes vote down vote up
def _run_cmd(cmds):
    """Run commands, raise exception if failed"""
    if not isinstance(cmds, str):
        cmds = "".join(cmds)
    print("Execute \"%s\"" % cmds)
    try:
        subprocess.check_call(cmds, shell=True)
    except subprocess.CalledProcessError as err:
        print(err)
        raise err 
Example 58
Project: myhoard   Author: aiven   File: version.py    Apache License 2.0 5 votes vote down vote up
def get_project_version(version_file):
    version_file = os.path.join(os.path.dirname(os.path.realpath(__file__)), version_file)
    try:
        module = imp.load_source("verfile", version_file)
        file_ver = module.__version__
    except IOError:
        file_ver = None

    os.chdir(os.path.dirname(__file__) or ".")
    try:
        git_out = subprocess.check_output(["git", "describe", "--always"],
                                          stderr=getattr(subprocess, "DEVNULL", None))
    except (OSError, subprocess.CalledProcessError):
        pass
    else:
        git_ver = git_out.splitlines()[0].strip().decode("utf-8")
        if "." not in git_ver:
            git_ver = "0.0.1-0-unknown-{}".format(git_ver)
        if save_version(git_ver, file_ver, version_file):
            return git_ver

    makefile = os.path.join(os.path.dirname(__file__), "Makefile")
    if os.path.exists(makefile):
        with open(makefile, "r") as fp:
            lines = fp.readlines()
        short_ver = [line.split("=", 1)[1].strip() for line in lines if line.startswith("short_ver")][0]
        if save_version(short_ver, file_ver, version_file):
            return short_ver

    if not file_ver:
        raise Exception("version not available from git or from file {!r}".format(version_file))

    return file_ver 
Example 59
Project: OpenFermion-Cirq   Author: quantumlib   File: shell_tools_test.py    Apache License 2.0 5 votes vote down vote up
def test_run_cmd_raise_on_fail():
    assert run_cmd('true') == (None, None, 0)
    assert run_cmd('true', raise_on_fail=False) == (None, None, 0)

    with pytest.raises(subprocess.CalledProcessError):
        run_cmd('false')
    assert run_cmd('false', raise_on_fail=False) == (None, None, 1) 
Example 60
Project: OpenFermion-Cirq   Author: quantumlib   File: shell_tools_test.py    Apache License 2.0 5 votes vote down vote up
def test_run_shell_raise_on_fail():
    assert run_shell('true') == (None, None, 0)
    assert run_shell('true', raise_on_fail=False) == (None, None, 0)

    with pytest.raises(subprocess.CalledProcessError):
        run_shell('false')
    assert run_shell('false', raise_on_fail=False) == (None, None, 1) 
Example 61
Project: OpenFermion-Cirq   Author: quantumlib   File: shell_tools_test.py    Apache License 2.0 5 votes vote down vote up
def test_output_of():
    assert shell_tools.output_of('true') == ''
    with pytest.raises(subprocess.CalledProcessError):
        _ = shell_tools.output_of('false')
    assert shell_tools.output_of('echo', 'test') == 'test'
    assert shell_tools.output_of('pwd', cwd='/tmp') in ['/tmp', '/private/tmp'] 
Example 62
Project: OpenFermion-Cirq   Author: quantumlib   File: shell_tools.py    Apache License 2.0 5 votes vote down vote up
def output_of(*cmd: Optional[str], **kwargs) -> str:
    """Invokes a subprocess and returns its output as a string.

    Args:
        cmd: Components of the command to execute, e.g. ["echo", "dog"].
        **kwargs: Extra arguments for asyncio.create_subprocess_shell, such as
            a cwd (current working directory) argument.

    Returns:
        A (captured output, captured error output, return code) triplet. The
        captured outputs will be None if the out or err parameters were not set
        to an instance of TeeCapture.

    Raises:
         subprocess.CalledProcessError: The process returned a non-zero error
            code and raise_on_fail was set.
    """
    result = cast(str, run_cmd(*cmd,
                               log_run_to_stderr=False,
                               out=TeeCapture(),
                               **kwargs).out)

    # Strip final newline.
    if result.endswith('\n'):
        result = result[:-1]

    return result 
Example 63
Project: Stegall   Author: mrT4ntr4   File: steghide.py    MIT License 5 votes vote down vote up
def launch(arg2):
	yn1 = 'y'
	while yn1 == 'y':
		print stg.color.OKBLUE	+  "Enter the Password to demystify the image" +stg.color.END
		Pass = raw_input()
		
		try:
			p = sb.Popen([prog,'extract','-sf',arg2,'-p',Pass,'-f'], stdout=sb.PIPE, stdin=sb.PIPE, stderr=sb.STDOUT) 
			output = p.communicate()[0]
			outlist = str(output).split()
			catter = outlist[4][1:-2] 
			try: 
				with open(catter,'r') as fl:
					print stg.color.OKBLUE + "=======================================\n"
					print stg.color.OKGREEN + fl.read() + stg.color.OKBLUE + "\n=======================================" + stg.color.END
				break
			except IOError:
					print stg.color.FAIL + "	 Wrong Password :( 		"  + stg.color.END
					while (yn1!='y' or yn1!='n'):
						yn1 = raw_input('Do you want to enter password once more (y/n)')
						if yn1=='y':
							break
						elif yn1=='n':
							scrk(arg2)
							exit
						else :
							print("Please enter a vaild choice")
						continue	

		except sb.CalledProcessError as e:
			print stg.color.FAIL + "Error Hit", e.output 
Example 64
Project: sic   Author: Yanixos   File: distro.py    GNU General Public License v3.0 5 votes vote down vote up
def _get_lsb_release_info(self):
        """
        Get the information items from the lsb_release command output.

        Returns:
            A dictionary containing all information items.
        """
        cmd = 'lsb_release -a'
        process = subprocess.Popen(
            cmd,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        stdout, stderr = stdout.decode('utf-8'), stderr.decode('utf-8')
        code = process.returncode
        if code == 0:
            content = stdout.splitlines()
            return self._parse_lsb_release_content(content)
        elif code == 127:  # Command not found
            return {}
        else:
            if sys.version_info[:2] >= (3, 5):
                raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
            elif sys.version_info[:2] >= (2, 7):
                raise subprocess.CalledProcessError(code, cmd, stdout)
            elif sys.version_info[:2] == (2, 6):
                raise subprocess.CalledProcessError(code, cmd) 
Example 65
Project: sic   Author: Yanixos   File: test_tagopt.py    GNU General Public License v3.0 5 votes vote down vote up
def test_plat_name_ext(temp_ext_pkg):
    try:
        subprocess.check_call(
            [sys.executable, 'setup.py', 'bdist_wheel', '--plat-name=testplat.arch'],
            cwd=str(temp_ext_pkg))
    except subprocess.CalledProcessError:
        pytest.skip("Cannot compile C Extensions")
    dist_dir = temp_ext_pkg.join('dist')
    assert dist_dir.check(dir=1)
    wheels = dist_dir.listdir()
    assert len(wheels) == 1
    assert wheels[0].basename.endswith('-testplat_arch.whl')
    assert wheels[0].ext == '.whl' 
Example 66
Project: sic   Author: Yanixos   File: test_tagopt.py    GNU General Public License v3.0 5 votes vote down vote up
def test_plat_name_ext_in_setupcfg(temp_ext_pkg):
    temp_ext_pkg.join('setup.cfg').write('[bdist_wheel]\nplat_name=testplat.arch')
    try:
        subprocess.check_call(
            [sys.executable, 'setup.py', 'bdist_wheel'],
            cwd=str(temp_ext_pkg))
    except subprocess.CalledProcessError:
        pytest.skip("Cannot compile C Extensions")
    dist_dir = temp_ext_pkg.join('dist')
    assert dist_dir.check(dir=1)
    wheels = dist_dir.listdir()
    assert len(wheels) == 1
    assert wheels[0].basename.endswith('-testplat_arch.whl')
    assert wheels[0].ext == '.whl' 
Example 67
Project: PyOptiX   Author: ozen   File: setup.py    MIT License 5 votes vote down vote up
def check_call_sudo_if_fails(cmd):
    try:
        return check_call(cmd)
    except CalledProcessError as e:
        return check_call(['sudo'] + cmd) 
Example 68
Project: PyOptiX   Author: ozen   File: setup.py    MIT License 5 votes vote down vote up
def check_output_sudo_if_fails(cmd):
    try:
        return check_output(cmd)
    except CalledProcessError as e:
        return check_output(['sudo'] + cmd) 
Example 69
Project: bigquerylayers   Author: smandaric   File: _cloud_sdk.py    GNU General Public License v3.0 5 votes vote down vote up
def get_project_id():
    """Gets the project ID from the Cloud SDK.

    Returns:
        Optional[str]: The project ID.
    """
    if os.name == 'nt':
        command = _CLOUD_SDK_WINDOWS_COMMAND
    else:
        command = _CLOUD_SDK_POSIX_COMMAND

    try:
        output = subprocess.check_output(
            (command,) + _CLOUD_SDK_CONFIG_COMMAND,
            stderr=subprocess.STDOUT)
    except (subprocess.CalledProcessError, OSError, IOError):
        return None

    try:
        configuration = json.loads(output.decode('utf-8'))
    except ValueError:
        return None

    try:
        return configuration['configuration']['properties']['core']['project']
    except KeyError:
        return None 
Example 70
Project: AshsSDK   Author: thehappydinoa   File: distro.py    MIT License 5 votes vote down vote up
def _get_lsb_release_info(self):
        """
        Get the information items from the lsb_release command output.

        Returns:
            A dictionary containing all information items.
        """
        cmd = 'lsb_release -a'
        process = subprocess.Popen(
            cmd,
            shell=True,
            stdout=subprocess.PIPE,
            stderr=subprocess.PIPE)
        stdout, stderr = process.communicate()
        stdout, stderr = stdout.decode('utf-8'), stderr.decode('utf-8')
        code = process.returncode
        if code == 0:
            content = stdout.splitlines()
            return self._parse_lsb_release_content(content)
        elif code == 127:  # Command not found
            return {}
        else:
            if sys.version_info[:2] >= (3, 5):
                raise subprocess.CalledProcessError(code, cmd, stdout, stderr)
            elif sys.version_info[:2] >= (2, 7):
                raise subprocess.CalledProcessError(code, cmd, stdout)
            elif sys.version_info[:2] == (2, 6):
                raise subprocess.CalledProcessError(code, cmd) 
Example 71
Project: AshsSDK   Author: thehappydinoa   File: test_tagopt.py    MIT License 5 votes vote down vote up
def test_plat_name_ext(temp_ext_pkg):
    try:
        subprocess.check_call(
            [sys.executable, 'setup.py', 'bdist_wheel', '--plat-name=testplat.arch'],
            cwd=str(temp_ext_pkg))
    except subprocess.CalledProcessError:
        pytest.skip("Cannot compile C Extensions")
    dist_dir = temp_ext_pkg.join('dist')
    assert dist_dir.check(dir=1)
    wheels = dist_dir.listdir()
    assert len(wheels) == 1
    assert wheels[0].basename.endswith('-testplat_arch.whl')
    assert wheels[0].ext == '.whl' 
Example 72
Project: AshsSDK   Author: thehappydinoa   File: test_tagopt.py    MIT License 5 votes vote down vote up
def test_plat_name_ext_in_setupcfg(temp_ext_pkg):
    temp_ext_pkg.join('setup.cfg').write('[bdist_wheel]\nplat_name=testplat.arch')
    try:
        subprocess.check_call(
            [sys.executable, 'setup.py', 'bdist_wheel'],
            cwd=str(temp_ext_pkg))
    except subprocess.CalledProcessError:
        pytest.skip("Cannot compile C Extensions")
    dist_dir = temp_ext_pkg.join('dist')
    assert dist_dir.check(dir=1)
    wheels = dist_dir.listdir()
    assert len(wheels) == 1
    assert wheels[0].basename.endswith('-testplat_arch.whl')
    assert wheels[0].ext == '.whl' 
Example 73
Project: NordVPN-NetworkManager-Gui   Author: vfosterm   File: nord_nm_gui.py    GNU General Public License v3.0 4 votes vote down vote up
def disable_kill_switch(self):
        """
        Enables or disables Killswitch depending on UI state
        Called everytime the Killswitch button is pressed
        """
        if not self.killswitch_btn.isChecked() and not self.sudo_password and self.config.getboolean('SETTINGS', 'kill_switch'):
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to remove the kill switch script from the Network Manager directory. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()

            if not self.sudo_password:  # dialog was canceled
                self.killswitch_btn.setChecked(False)
                return False
            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.statusbar.showMessage('Kill switch disabled', 2000)
                self.repaint()
                self.config['SETTINGS']['kill_switch'] = 'False'
                self.write_conf()

            except subprocess.CalledProcessError:
                self.statusbar.showMessage('ERROR disabling kill switch', 2000)
                self.repaint()

        elif not self.killswitch_btn.isChecked() and self.sudo_password and self.config.getboolean('SETTINGS', 'kill_switch'):

            try:
                p1 = subprocess.Popen(['echo', self.sudo_password], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
                p2 = subprocess.Popen(['sudo', '-S', 'rm', self.network_manager_path + 'kill_switch'], stdin=p1.stdout, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
                p1.stdout.close()
                p2.stdout.close()
                self.statusbar.showMessage('Kill switch disabled', 2000)
                self.repaint()
                self.config['SETTINGS']['kill_switch'] = 'False'
                self.write_conf()

            except subprocess.CalledProcessError:
                self.statusbar.showMessage('ERROR disabling kill switch', 2000)
                self.repaint()

        elif self.killswitch_btn.isChecked() and self.get_active_vpn() and self.sudo_password:
            self.set_kill_switch()

        elif self.killswitch_btn.isChecked() and self.get_active_vpn() and not self.sudo_password:
            self.sudo_dialog = self.get_sudo()
            self.sudo_dialog.text_label.setText("<html><head/><body><p>VPN Network Manager requires <span style=\" font-weight:600;\">sudo</span> permissions in order to move the kill switch script to the Network Manager directory. Please input the <span style=\" font-weight:600;\">sudo</span> Password or run the program with elevated priveledges.</p></body></html>")
            self.sudo_dialog.exec_()
            if self.sudo_password:
                self.set_kill_switch()
            else:
                self.killswitch_btn.setChecked(False)
                return False 
Example 74
Project: fs_image   Author: facebookincubator   File: test_yum_dnf_from_snapshot.py    MIT License 4 votes vote down vote up
def test_verify_contents_of_install_from_snapshot(self):
        milk = {
            'milk.txt': 'milk 2.71 8\n',
            'post.txt': 'stuff\n',  # From `milk-2.71` post-install
        }
        with self._install(protected_paths=['meta/']) as install_root:
            self._check_installed_content(install_root, {
                **milk,
                'carrot.txt': 'carrot 2 rc0\n',
            })

        # Version-locking carrot causes a non-latest version to be installed
        with self._install(
            protected_paths=['meta/'],
            version_lock=['0\trpm-test-carrot\t1\tlockme\tx86_64'],
        ) as install_root:
            self._check_installed_content(install_root, {
                **milk,
                'carrot.txt': 'carrot 1 lockme\n',
            })

        def _install_nonexistent():
            return self._install(
                protected_paths=['meta/'],
                version_lock=['0\trpm-test-carrot\t3333\tnonesuch\tx86_64'],
            )

        if self._YUM_DNF == YumDnf.yum:
            # For `yum`, we'd actually want this to fail loudly instead of
            # failing to install the requested package, but this is what it
            # does now, and it'd take some effort to make it otherwise (it's
            # easier to do this error-checking in `RpmActionItem` anyway)
            with _install_nonexistent() as install_root:
                self._check_installed_content(install_root, milk)
        elif self._YUM_DNF == YumDnf.dnf:
            # Unlike `yum`, `dnf` actually fails with:
            #   Error: Unable to find a match: rpm-test-carrot
            with self.assertRaises(subprocess.CalledProcessError):
                with _install_nonexistent():
                    pass
        else:
            raise NotImplementedError(self._YUM_DNF) 
Example 75
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: build.py    Apache License 2.0 4 votes vote down vote up
def build_docker(platform: str, docker_binary: str, registry: str, num_retries: int, use_cache: bool) -> str:
    """
    Build a container for the given platform
    :param platform: Platform
    :param docker_binary: docker binary to use (docker/nvidia-docker)
    :param registry: Dockerhub registry name
    :param num_retries: Number of retries to build the docker image
    :param use_cache: will pass cache_from to docker to use the previously pulled tag
    :return: Id of the top level image
    """
    tag = get_docker_tag(platform=platform, registry=registry)
    logging.info("Building docker container tagged '%s' with %s", tag, docker_binary)
    #
    # We add a user with the same group as the executing non-root user so files created in the
    # container match permissions of the local user. Same for the group.
    #
    # These variables are used in the docker files to create user and group with these ids.
    # see: docker/install/ubuntu_adduser.sh
    #
    # cache-from is needed so we use the cached images tagged from the remote via
    # docker pull see: docker_cache.load_docker_cache
    #
    # This also prevents using local layers for caching: https://github.com/moby/moby/issues/33002
    # So to use local caching, we should omit the cache-from by using --no-dockerhub-cache argument to this
    # script.
    #
    # This doesn't work with multi head docker files.
    #
    cmd = [docker_binary, "build",
           "-f", get_dockerfile(platform),
           "--build-arg", "USER_ID={}".format(os.getuid()),
           "--build-arg", "GROUP_ID={}".format(os.getgid())]
    if use_cache:
        cmd.extend(["--cache-from", tag])
    cmd.extend(["-t", tag, get_dockerfiles_path()])

    @retry(subprocess.CalledProcessError, tries=num_retries)
    def run_cmd():
        logging.info("Running command: '%s'", ' '.join(cmd))
        check_call(cmd)

    run_cmd()
    # Get image id by reading the tag. It's guaranteed (except race condition) that the tag exists. Otherwise, the
    # check_call would have failed
    image_id = _get_local_image_id(docker_binary=docker_binary, docker_tag=tag)
    if not image_id:
        raise FileNotFoundError('Unable to find docker image id matching with {}'.format(tag))
    return image_id 
Example 76
Project: OpenFermion-Cirq   Author: quantumlib   File: shell_tools.py    Apache License 2.0 4 votes vote down vote up
def run_shell(cmd: str,
              out: Optional[Union[TeeCapture, IO[str]]] = sys.stdout,
              err: Optional[Union[TeeCapture, IO[str]]] = sys.stderr,
              raise_on_fail: bool = True,
              log_run_to_stderr: bool = True,
              **kwargs
              ) -> CommandOutput:
    """Invokes a shell command and waits for it to finish.

    Args:
        cmd: The command line string to execute, e.g. "echo dog | cat > file".
        out: Where to write the process' stdout. Defaults to sys.stdout. Can be
            anything accepted by print's 'file' parameter, or None if the
            output should be dropped, or a TeeCapture instance. If a TeeCapture
            instance is given, the first element of the returned tuple will be
            the captured output.
        err: Where to write the process' stderr. Defaults to sys.stderr. Can be
            anything accepted by print's 'file' parameter, or None if the
            output should be dropped, or a TeeCapture instance. If a TeeCapture
            instance is given, the second element of the returned tuple will be
            the captured error output.
        raise_on_fail: If the process returns a non-zero error code
            and this flag is set, a CalledProcessError will be raised.
            Otherwise the return code is the third element of the returned
            tuple.
        log_run_to_stderr: Determines whether the fact that this shell command
            was executed is logged to sys.stderr or not.
        **kwargs: Extra arguments for asyncio.create_subprocess_shell, such as
            a cwd (current working directory) argument.

    Returns:
        A (captured output, captured error output, return code) triplet. The
        captured outputs will be None if the out or err parameters were not set
        to an instance of TeeCapture.

    Raises:
         subprocess.CalledProcessError: The process returned a non-zero error
            code and raise_on_fail was set.
    """
    if log_run_to_stderr:
        print('shell:', cmd, file=sys.stderr)
    result = asyncio.get_event_loop().run_until_complete(
        _async_wait_for_process(
            asyncio.create_subprocess_shell(
                cmd,
                stdout=asyncio.subprocess.PIPE,
                stderr=asyncio.subprocess.PIPE,
                **kwargs),
            out,
            err))
    if raise_on_fail and result[2]:
        raise subprocess.CalledProcessError(result[2], cmd)
    return result 
Example 77
Project: PyOptiX   Author: ozen   File: compiler.py    MIT License 4 votes vote down vote up
def compile(cls, source_path, output_ptx_name=None):
        if output_ptx_name is None:
            output_ptx_name = cls.get_ptx_name(source_path)

        if not os.path.isdir(cls.output_path):
            raise RuntimeError('Compiler.output_path is not a directory.')

        output_ptx_path = os.path.join(cls.output_path, output_ptx_name)
        is_compiled = True

        if cls.is_compile_required(source_path, output_ptx_path):
            if os.path.exists(output_ptx_path):
                os.remove(output_ptx_path)

            logger.info("Compiling {0}".format(source_path))
            bash_command = cls.nvcc_path + " "
            bash_command += " ".join(cls._compile_args)
            bash_command += " ".join(cls.extra_compile_args)
            bash_command += " -ptx"
            if cls.use_fast_math:
                bash_command += " --use_fast_math"
            for include_path in cls._program_directories:
                if os.path.exists(include_path):
                    bash_command += " -I=" + include_path
            bash_command += " " + source_path
            bash_command += " -o=" + output_ptx_path
            logger.debug("Executing: {0}".format(bash_command))
            try:
                check_call(shlex.split(bash_command))
            except CalledProcessError as e:
                logger.error(e)

            if not os.path.exists(output_ptx_path):
                logger.error("Could not compile {0}".format(source_path))
                raise RuntimeError("Could not compile {0}".format(source_path))
            else:
                os.chmod(output_ptx_path, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH)

        else:
            logger.debug("No compiling required for {0}".format(source_path))
            is_compiled = False

        return output_ptx_path, is_compiled 
Example 78
Project: face_rekognition   Author: cnidus   File: IcnsImagePlugin.py    GNU General Public License v3.0 4 votes vote down vote up
def _save(im, fp, filename):
    """
    Saves the image as a series of PNG files,
    that are then converted to a .icns file
    using the macOS command line utility 'iconutil'.

    macOS only.
    """
    if hasattr(fp, "flush"):
        fp.flush()

    # create the temporary set of pngs
    iconset = tempfile.mkdtemp('.iconset')
    last_w = None
    last_im = None
    for w in [16, 32, 128, 256, 512]:
        prefix = 'icon_{}x{}'.format(w, w)

        if last_w == w:
            im_scaled = last_im
        else:
            im_scaled = im.resize((w, w), Image.LANCZOS)
        im_scaled.save(os.path.join(iconset, prefix+'.png'))

        im_scaled = im.resize((w*2, w*2), Image.LANCZOS)
        im_scaled.save(os.path.join(iconset, prefix+'@2x.png'))
        last_im = im_scaled

    # iconutil -c icns -o {} {}
    from subprocess import Popen, PIPE, CalledProcessError

    convert_cmd = ["iconutil", "-c", "icns", "-o", filename, iconset]
    with open(os.devnull, 'wb') as devnull:
        convert_proc = Popen(convert_cmd, stdout=PIPE, stderr=devnull)

    convert_proc.stdout.close()

    retcode = convert_proc.wait()

    # remove the temporary files
    shutil.rmtree(iconset)

    if retcode:
        raise CalledProcessError(retcode, convert_cmd) 
Example 79
Project: face_rekognition   Author: cnidus   File: GifImagePlugin.py    GNU General Public License v3.0 4 votes vote down vote up
def _save_netpbm(im, fp, filename):

    # Unused by default.
    # To use, uncomment the register_save call at the end of the file.
    #
    # If you need real GIF compression and/or RGB quantization, you
    # can use the external NETPBM/PBMPLUS utilities.  See comments
    # below for information on how to enable this.

    import os
    from subprocess import Popen, check_call, PIPE, CalledProcessError
    file = im._dump()

    with open(filename, 'wb') as f:
        if im.mode != "RGB":
            with open(os.devnull, 'wb') as devnull:
                check_call(["ppmtogif", file], stdout=f, stderr=devnull)
        else:
            # Pipe ppmquant output into ppmtogif
            # "ppmquant 256 %s | ppmtogif > %s" % (file, filename)
            quant_cmd = ["ppmquant", "256", file]
            togif_cmd = ["ppmtogif"]
            with open(os.devnull, 'wb') as devnull:
                quant_proc = Popen(quant_cmd, stdout=PIPE, stderr=devnull)
                togif_proc = Popen(togif_cmd, stdin=quant_proc.stdout,
                                   stdout=f, stderr=devnull)

            # Allow ppmquant to receive SIGPIPE if ppmtogif exits
            quant_proc.stdout.close()

            retcode = quant_proc.wait()
            if retcode:
                raise CalledProcessError(retcode, quant_cmd)

            retcode = togif_proc.wait()
            if retcode:
                raise CalledProcessError(retcode, togif_cmd)

    try:
        os.unlink(file)
    except OSError:
        pass


# Force optimization so that we can test performance against
# cases where it took lots of memory and time previously. 
Example 80
Project: GCN-SeA   Author: sumanbanerjee1   File: bleu.py    Apache License 2.0 4 votes vote down vote up
def moses_multi_bleu(hypotheses, references, lowercase=False):
  """Calculate the bleu score for hypotheses and references
  using the MOSES ulti-bleu.perl script.

  Args:
    hypotheses: A numpy array of strings where each string is a single example.
    references: A numpy array of strings where each string is a single example.
    lowercase: If true, pass the "-lc" flag to the multi-bleu script

  Returns:
    The BLEU score as a float32 value.
  """

  if np.size(hypotheses) == 0:
    return np.float32(0.0)

  # Get MOSES multi-bleu script
  try:
    multi_bleu_path, _ = urllib.request.urlretrieve(
        "https://raw.githubusercontent.com/moses-smt/mosesdecoder/"
        "master/scripts/generic/multi-bleu.perl")
    os.chmod(multi_bleu_path, 0o755)
  except: #pylint: disable=W0702
    tf.logging.info("Unable to fetch multi-bleu.perl script, using local.")
    metrics_dir = os.path.dirname(os.path.realpath(__file__))
    bin_dir = os.path.abspath(os.path.join(metrics_dir, "..", "..", "bin"))
    multi_bleu_path = os.path.join(bin_dir, "tools/multi-bleu.perl")

  # Dump hypotheses and references to tempfiles
  hypothesis_file = tempfile.NamedTemporaryFile()
  hypothesis_file.write("\n".join(hypotheses).encode("utf-8"))
  hypothesis_file.write(b"\n")
  hypothesis_file.flush()
  reference_file = tempfile.NamedTemporaryFile()
  reference_file.write("\n".join(references).encode("utf-8"))
  reference_file.write(b"\n")
  reference_file.flush()

  # Calculate BLEU using multi-bleu script
  with open(hypothesis_file.name, "r") as read_pred:
    bleu_cmd = [multi_bleu_path]
    if lowercase:
      bleu_cmd += ["-lc"]
    bleu_cmd += [reference_file.name]
    try:
      bleu_out = subprocess.check_output(
          bleu_cmd, stdin=read_pred, stderr=subprocess.STDOUT)
      bleu_out = bleu_out.decode("utf-8")
      bleu_score = re.search(r"BLEU = (.+?),", bleu_out).group(1)
      bleu_score = float(bleu_score)
    except subprocess.CalledProcessError as error:
      if error.output is not None:
        tf.logging.warning("multi-bleu.perl script returned non-zero exit code")
        tf.logging.warning(error.output)
      bleu_score = np.float32(0.0)

  # Close temp files
  hypothesis_file.close()
  reference_file.close()

  return np.float32(bleu_score)