Python subprocess.check_output() Examples

The following are code examples for showing how to use subprocess.check_output(). 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: fs_image   Author: facebookincubator   File: test_subvolume_garbage_collector.py    MIT License 10 votes vote down vote up
def setUp(self):
        # Mock out `sudo btrfs subvolume delete` for the garbage-collector,
        # so that the test doesn't require us to set up & clean up btrfs
        # volumes.  Everything else is easily tested in a tempdir.
        self._old_path = os.environ.pop('PATH', None)
        self.addCleanup(self._restore_path)
        fake_sudo_path = os.path.join(
            os.path.dirname(os.path.dirname(__file__)), 'fake_sudo/'
        )
        os.environ['PATH'] = f'{fake_sudo_path}:{self._old_path}'

        # Ensure the sudo override worked, so we don't mysteriously fail later.
        fake_sudo_file = os.path.join(fake_sudo_path, 'sudo')
        self.assertTrue(os.path.exists(fake_sudo_file), fake_sudo_file)
        self.assertEqual(
            b'MAGIC_SENTINEL',
            subprocess.check_output(['sudo', 'MAGIC_SENTINEL']),
        ) 
Example 2
Project: clikit   Author: sdispater   File: terminal.py    MIT License 8 votes vote down vote up
def _get_terminal_size_tput(self):
        # get terminal width
        # src: http://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window
        try:
            cols = int(
                subprocess.check_output(
                    shlex.split("tput cols"), stderr=subprocess.STDOUT
                )
            )
            rows = int(
                subprocess.check_output(
                    shlex.split("tput lines"), stderr=subprocess.STDOUT
                )
            )

            return (cols, rows)
        except:
            pass 
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: fs_image   Author: facebookincubator   File: test_toy_rpm_build_unittest.py    MIT License 6 votes vote down vote up
def test_built_files(self):
        # Files added as part of the rpmbuild_layer
        self.assertTrue(os.path.exists('/rpmbuild/SOURCES/toy_src_file'))
        self.assertTrue(os.path.exists('/rpmbuild/SPECS/specfile.spec'))

        # Built from rpmbuild
        rpm_path = b'/rpmbuild/RPMS/toy.rpm'
        self.assertTrue(os.path.exists(rpm_path))

        a = RpmMetadata.from_file(rpm_path)
        self.assertEqual(a.epoch, 0)
        self.assertEqual(a.version, '1.0')
        self.assertEqual(a.release, '1')

        # Check files contained in rpm
        files = subprocess.check_output(['rpm', '-qlp', rpm_path]).decode()
        self.assertEqual(files, '/usr/bin/toy_src_file\n') 
Example 5
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 6
Project: alfred-yubikey-otp   Author: robertoriv   File: util.py    MIT License 6 votes vote down vote up
def run_command(cmd, **kwargs):
    """Run a command and return the output.

    .. versionadded:: 1.31

    A thin wrapper around :func:`subprocess.check_output` that ensures
    all arguments are encoded to UTF-8 first.

    Args:
        cmd (list): Command arguments to pass to ``check_output``.
        **kwargs: Keyword arguments to pass to ``check_output``.

    Returns:
        str: Output returned by ``check_output``.

    """
    cmd = [utf8ify(s) for s in cmd]
    return subprocess.check_output(cmd, **kwargs) 
Example 7
Project: wechat-alfred-workflow   Author: TKkk-iOSer   File: util.py    MIT License 6 votes vote down vote up
def run_command(cmd, **kwargs):
    """Run a command and return the output.

    .. versionadded:: 1.31

    A thin wrapper around :func:`subprocess.check_output` that ensures
    all arguments are encoded to UTF-8 first.

    Args:
        cmd (list): Command arguments to pass to ``check_output``.
        **kwargs: Keyword arguments to pass to ``check_output``.

    Returns:
        str: Output returned by ``check_output``.
    """
    cmd = [utf8ify(s) for s in cmd]
    return subprocess.check_output(cmd, **kwargs) 
Example 8
Project: AutoDL   Author: tanguofu   File: setup.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def get_mpi_flags():
    show_command = os.environ.get('HOROVOD_MPICXX_SHOW', 'mpicxx -show')
    try:
        mpi_show_output = subprocess.check_output(
            shlex.split(show_command), universal_newlines=True).strip()
        mpi_show_args = shlex.split(mpi_show_output)
        if not mpi_show_args[0].startswith('-'):
            # Open MPI and MPICH print compiler name as a first word, skip it
            mpi_show_args = mpi_show_args[1:]
        # strip off compiler call portion and always escape each arg
        return ' '.join(
            ['"' + arg.replace('"', '"\'"\'"') + '"' for arg in mpi_show_args])
    except Exception:
        raise DistutilsPlatformError(
            '%s failed (see error below), is MPI in $PATH?\n'
            'Note: If your version of MPI has a custom command to show compilation flags, '
            'please specify it with the HOROVOD_MPICXX_SHOW environment variable.\n\n'
            '%s' % (show_command, traceback.format_exc())) 
Example 9
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def renew_ssl(force=False):
    outText = ''
    if not force:
        days_left = cert_daysleft()
        if days_left is None or days_left <= -9999 or days_left >= 30:
            return outText
        print >> sys.stderr, 'sdserver: Reloading after renewing SSL cert', days_left, 'days before expiry:', sliauth.iso_date(nosubsec=True)

    try:
        server_domain = Options['server_url'].split('//')[1].split(':')[0]
        cmd = ['certbot', 'certonly', '-n', '--webroot', '--config-dir', './certbot/config', '--logs-dir', './certbot/log', '--work-dir', './certbot/work', '-w', './acme-web', '-d', server_domain]

        print >> sys.stderr, 'Executing: '+' '.join(cmd)
        outText += 'Executing: '+' '.join(cmd) + '\n'
        certOutput = subprocess.check_output(cmd)

        print >> sys.stderr, 'Output:\n%s' % certOutput
        outText += 'Renewal status:\n'+certOutput
    except Exception, excp:
        errMsg = 'Error in renewal of SSL cert: '+str(excp)
        print >> sys.stderr, errMsg
        outText += errMsg + '\n' 
Example 10
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 11
Project: ups-utils   Author: Ricks-Lab   File: UPSmodule.py    GNU General Public License v3.0 6 votes vote down vote up
def check_snmp(self, tups=None):
        """ check if the IP address for the target UPS or active UPS if target is None, responds to snmp command.
        :param tups:  The target ups dictionary from list or None.
        :type tups: dict
        :return:  True if the given IP address responds, else False
        :rtype: bool
        """
        if not tups:
            tups = self.active_ups
        cmd_str = 'snmpget -v2c -c {} {} {}'.format(tups['snmp_community'], tups['ups_IP'], 'iso.3.6.1.2.1.1.1.0')

        try:
            snmp_output = subprocess.check_output(shlex.split(cmd_str), shell=False,
                                                  stderr=subprocess.DEVNULL).decode().split('\n')
            if env.ut_const.DEBUG: print(snmp_output)
        except:
            return False
        return True 
Example 12
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 13
Project: rnm   Author: alexjaw   File: interface.py    MIT License 6 votes vote down vote up
def get_ip(self):
        """Get IP for the interface, e.g. ifconfig eth0"""
        # OBS: several os commands have to be use '' and NOT ""
        cmd = ['ip', 'addr', 'show', self.iface]
        self.logger.debug('os cmd: {}'.format(cmd))
        try:
            iface_info = check_output(cmd).split()
        except OSError as e:
            self.logger.error('Failed with check_output: %s', e.args)
            ip = 'error'
        else:
            try:
                inet = iface_info[iface_info.index('inet') + 1]
            except ValueError as e:
                self.logger.debug('ValueError for inet: %s', e.args)
                ip = 'no IP - not connected'
            else:
                ip = inet.split('/')[0]

        self.logger.debug('ip = {}'.format(repr(ip)))
        return ip 
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: Ansible-Example-AB2018   Author: umit-ozturk   File: distro.py    MIT License 6 votes vote down vote up
def _lsb_release_info(self):
        """
        Get the information items from the lsb_release command output.

        Returns:
            A dictionary containing all information items.
        """
        if not self.include_lsb:
            return {}
        with open(os.devnull, 'w') as devnull:
            try:
                cmd = ('lsb_release', '-a')
                stdout = subprocess.check_output(cmd, stderr=devnull)
            except OSError:  # Command not found
                return {}
        content = stdout.decode(sys.getfilesystemencoding()).splitlines()
        return self._parse_lsb_release_content(content) 
Example 18
Project: spqrel_tools   Author: LCAS   File: execplan.py    MIT License 6 votes vote down vote up
def doExecPlan(memory_service, lu4r_value, asr_value):

    #print " *** DEBUG *** ASR_enable set to 0 !!!"
    memory_service.raiseEvent("ASR_enable","0")

    plan = LU4R_to_plan(lu4r_value, asr_value, memory_service)
    os.chdir(os.environ["PLAN_DIR"])
    cmd = 'cd ../plans; echo "'+plan+'" > '+gpsrtaskname+'.plan'
    os.system(cmd)

    cmd = 'cd ../plans; pnpgen_translator inline '+gpsrtaskname+'.plan' # ER ???
    os.system(cmd)

    try:
        task = subprocess.check_output('cat ./'+gpsrtaskname+'.plan', shell=True, cwd=os.environ["PLAN_DIR"])
        memory_service.raiseEvent('/gpsr/plan', task)
    except Exception as e:
        print e

    #print " *** DEBUG *** ASR_enable value = ", memory_service.getData("ASR_enable")

    #cmd = 'cd ../plans; ./run_plan.py --plan '+gpsrtaskname
    #os.system(cmd)

    #print " *** DEBUG *** ASR_enable value = ", memory_service.getData("ASR_enable") 
Example 19
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 20
Project: core   Author: lifemapper   File: backupRestoreUser.py    GNU General Public License v3.0 6 votes vote down vote up
def getColumns(dbcmd, fulltablename):
   getColumnsStmt = ' {} --command=\"\d {}\" | grep \'|\' | awk \'!print $1?\''.format(dbcmd, fulltablename)
   getColumnsStmt = getColumnsStmt.replace('!', '{')
   getColumnsStmt = getColumnsStmt.replace('?', '}')
   print 
   print getColumnsStmt
   print 
   columnStr = subprocess.check_output(getColumnsStmt, preexec_fn=SetPass, shell=True)
   cols = columnStr.split('\n')
   for str in ('Column', ''):
      try:
         cols.remove(str)
      except:
         print str
         pass
   return cols

# ............................................... 
Example 21
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_utils.py    Apache License 2.0 6 votes vote down vote up
def list_gpus():
    """Return a list of GPUs

    Returns
    -------
    list of int:
        If there are n GPUs, then return a list [0,1,...,n-1]. Otherwise returns
        [].
    """
    re = ''
    nvidia_smi = ['nvidia-smi', '/usr/bin/nvidia-smi', '/usr/local/nvidia/bin/nvidia-smi']
    for cmd in nvidia_smi:
        try:
            re = subprocess.check_output([cmd, "-L"], universal_newlines=True)
        except OSError:
            pass
    return range(len([i for i in re.split('\n') if 'GPU' in i])) 
Example 22
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 23
Project: factotum   Author: Denubis   File: newmap.py    GNU General Public License v3.0 5 votes vote down vote up
def newFactorioMap():
	FACTORIOPATH = getFactorioPath()

	mapFileExamplePath="%s/data/map-gen-settings.example.json" % (FACTORIOPATH)
	mapFilePath="%s/config/mapsettings.json" % (FACTORIOPATH)

	if not os.path.isfile(mapFilePath):		
		with codecs.open(mapFileExamplePath, 'r', encoding='utf-8') as map_file:
			mapJson = json.load(map_file)
	
			mapJson['starting_area'] = "very-high"

			for control in mapJson['autoplace_controls']:
				mapJson['autoplace_controls'][control]['size'] = "high"
				mapJson['autoplace_controls'][control]['richness'] = "very-high"
				mapJson['autoplace_controls'][control]['frequency'] = "low"

		with codecs.open(mapFilePath, 'w', encoding='utf-8') as map_file:
			json.dump(mapJson, map_file, indent=4)


	print(subprocess.check_output(
					["%s/bin/x64/factorio" % (FACTORIOPATH), 
					 "--create", "%s/saves/%s" % (FACTORIOPATH, 'Headless-{:%Y%m%d-%H%M%S}'.format(datetime.datetime.now())),
					 "--map-gen-settings", "%s/config/mapsettings.json" % (FACTORIOPATH)	]
					 ).decode("unicode_escape")) 
Example 24
Project: factotum   Author: Denubis   File: factotum.py    GNU General Public License v3.0 5 votes vote down vote up
def runFactorio(stdin=True):

	FACTORIOPATH = getFactorioPath()

	if not stdin:
		print("Interactive mode enabled. Server can only be quit with ctrl-C. /quit will only restart the server.")	
	phrase = generatePhrase(6)
	with open("/tmp/factorioRcon", "w") as phraseFile:
		phraseFile.write(phrase)

	with codecs.open("%s/config/settings.json" % (FACTORIOPATH), 'r+', encoding='utf-8') as settings_file:
		try:
			settingsJson = json.load(settings_file)
		except Exception as e:
			print("Problem with settings file.")
			print(e)
			sys.exit(1)


	if not filter(os.path.isfile, glob.glob('%s/saves/*.zip' % (FACTORIOPATH))):
		print("Cannot find a save file. Exiting.")
		sys.exit(1)

	nohup="nohup "

	if not stdin:
		print(nohup)
		nohup=""
	myprogram = {"cmd": "%s%s/bin/x64/factorio --rcon-port 27015 --rcon-password \"%s\" --start-server-load-latest --server-settings %s/config/settings.json" % (nohup, FACTORIOPATH, phrase, FACTORIOPATH) , "numprocesses": 1, "stop_timeout": 20, "close_child_stdin":stdin}

	arbiter = circus.get_arbiter([myprogram])
	try:
					
		arbiter.start()
	finally:
		os.remove("/tmp/factorioRcon")
		arbiter.stop()
		print(subprocess.check_output(['tail', '-13', '%s/factorio-current.log'%FACTORIOPATH]).decode("unicode_escape")) 
Example 25
Project: factotum   Author: Denubis   File: factotum.py    GNU General Public License v3.0 5 votes vote down vote up
def stop():
	"""Shortcut for factorio stop."""
	try:
		FACTORIOPATH = getFactorioPath()
		with open('/tmp/factorio.pid', 'r') as pidfile:
			pid = int(pidfile.read().strip())
			os.kill(pid, signal.SIGINT)
		print("SIGINT sent")
		time.sleep(10)
	except FileNotFoundError:
		print("Cannot find pid. Factorio has not been started.")		
	print(subprocess.check_output(['tail', '-13', '%s/factorio-current.log'%FACTORIOPATH]).decode("unicode_escape")) 
Example 26
Project: fs_image   Author: facebookincubator   File: nspawn_in_subvol.py    MIT License 5 votes vote down vote up
def _nspawn_version():
    '''
    We now care about the version of nspawn we are running.  The output of
    systemd-nspawn --version looks like:

    ```
    systemd 242 (v242-2.fb1)
    +PAM +AUDIT +SELINUX +IMA ...
    ```
    So we can get the major version as the second token of the first line.
    We hope that the output of systemd-nspawn --version is stable enough
    to keep parsing it like this.
    '''
    return int(subprocess.check_output([
        'systemd-nspawn', '--version']).split()[1]) 
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_unshare.py    MIT License 5 votes vote down vote up
def _check_ns_diff(self, unshare: Unshare, ns_diff: Iterable[str]):
        list_ns_cmd = [
            'readlink', *(f'/proc/self/ns/{name}' for name in _NS_FILES),
        ]
        in_ns, out_ns = [
            dict(
                ns_ino.split(':') for ns_ino in
                    subprocess.check_output(cmd).decode().strip().split('\n')
            ) for cmd in [
                list_ns_cmd, nsenter_as_root(unshare, *list_ns_cmd),
            ]
        ]
        for ns in ns_diff:
            self.assertNotEqual(in_ns.pop(ns), out_ns.pop(ns), ns)
        self.assertEqual(in_ns, out_ns) 
Example 29
Project: fs_image   Author: facebookincubator   File: test_volume_for_repo.py    MIT License 5 votes vote down vote up
def test_volume_repo(self):
        artifacts_dir = tempfile.mkdtemp(prefix='test_volume_repo')
        volume_dir = os.path.join(artifacts_dir, vfr.VOLUME_DIR)
        image_file = os.path.join(artifacts_dir, vfr.IMAGE_FILE)
        min_free_bytes = 250e6

        try:
            self.assertEqual(
                vfr.get_volume_for_current_repo(
                    min_free_bytes=min_free_bytes,
                    artifacts_dir=artifacts_dir,
                ),
                volume_dir,
            )
            self.assertGreaterEqual(
                os.stat(image_file).st_size, min_free_bytes,
            )

            fstype_and_avail = subprocess.check_output([
                'findmnt', '--noheadings', '--output', 'FSTYPE,AVAIL',
                '--bytes', volume_dir
            ])
            fstype, avail = fstype_and_avail.strip().split()
            self.assertEqual(b'btrfs', fstype)
            self.assertGreaterEqual(int(avail), min_free_bytes)
        finally:
            try:
                subprocess.call(['sudo', 'umount', volume_dir])
            except Exception:
                pass  # Might not have been mounted in case of an earlier error
            shutil.rmtree(artifacts_dir) 
Example 30
Project: fs_image   Author: facebookincubator   File: test_package_image.py    MIT License 5 votes vote down vote up
def _render_sendstream_path(path):
    if path.endswith('.zst'):
        data = subprocess.check_output(
            ['zstd', '--decompress', '--stdout', path]
        )
    else:
        with open(path, 'rb') as infile:
            data = infile.read()
    return render_sendstream(data) 
Example 31
Project: fs_image   Author: facebookincubator   File: artifacts_dir.py    MIT License 5 votes vote down vote up
def _maybe_make_symlink_to_scratch(
    symlink_path, target_in_scratch, path_in_repo,
):
    '''
    IMPORTANT: This must be safe against races with other concurrent copies
    of `artifacts_dir.py`.
    '''
    scratch_bin = shutil.which('mkscratch')
    if scratch_bin is None:
        return symlink_path

    # If the decode ever fails, we should probably switch this entire
    # function to return `bytes`.
    target_path, _ = subprocess.check_output([
        scratch_bin, 'path', '--subdir', target_in_scratch, path_in_repo,
    ]).decode().rsplit('\n', 1)

    # Atomically ensure the desired symlink exists.
    try:
        os.symlink(target_path, symlink_path)
    except FileExistsError:
        pass

    # These two error conditions should never happen under normal usage, so
    # they are left as exceptions instead of auto-remediations.
    if not os.path.islink(symlink_path):
        raise RuntimeError(
            f'{symlink_path} is not a symlink. Clean up whatever is there '
            'and try again?'
        )
    real_target = os.path.realpath(symlink_path)
    if real_target != target_path:
        raise RuntimeError(
            f'{symlink_path} points at {real_target}, but should point '
            f'at {target_path}. Clean this up, and try again?'
        )

    return target_path 
Example 32
Project: fs_image   Author: facebookincubator   File: btrfs_loopback.py    MIT License 5 votes vote down vote up
def _mount_image_file(
    unshare: Optional[Unshare], file_path: bytes, mount_path: bytes,
) -> bytes:
    log.info(f'Mounting btrfs {file_path} at {mount_path}')
    # Explicitly set filesystem type to detect shenanigans.
    run_stdout_to_err(nsenter_as_root(
        unshare, 'mount', '-t', 'btrfs', '-o', 'loop,discard,nobarrier',
        file_path, mount_path,
    ), check=True)
    loop_dev = subprocess.check_output(nsenter_as_user(
        unshare, 'findmnt', '--noheadings', '--output', 'SOURCE',
        mount_path,
    )).rstrip(b'\n')
    # This increases the chances that --direct-io=on will succeed, since one
    # of the common failure modes is that the loopback's sector size is NOT
    # a multiple of the sector size of the underlying device (the devices
    # we've seen in production have sector sizes of 512, 1024, or 4096).
    if run_stdout_to_err([
        'sudo', 'losetup', '--sector-size=4096', loop_dev,
    ]).returncode != 0:
        log.error(
            f'Failed to set --sector-size=4096 for {loop_dev}, setting '
            'direct IO is more likely to fail.'
        )
    # This helps perf and avoids doubling our usage of buffer cache.
    # Also, when the image is on tmpfs, setting direct IO fails.
    if run_stdout_to_err([
        'sudo', 'losetup', '--direct-io=on', loop_dev,
    ]).returncode != 0:
        log.error(
            f'Could not enable --direct-io for {loop_dev}, expect worse '
            'performance.'
        )
    return loop_dev 
Example 33
Project: fs_image   Author: facebookincubator   File: subvolume_on_disk.py    MIT License 5 votes vote down vote up
def _btrfs_get_volume_props(subvolume_path):
    SNAPSHOTS = 'Snapshot(s)'
    props = {}
    # It's unfair to assume that the OS encoding is UTF-8, but our JSON
    # serialization kind of requires it, and Python3 makes it hyper-annoying
    # to work with bytestrings, so **shrug**.
    #
    # If this turns out to be a problem for a practical use case, we can add
    # `surrogateescape` all over the place, or even set
    # `PYTHONIOENCODING=utf-8:surrogateescape` in the environment.
    for l in subprocess.check_output([
        'sudo', 'btrfs', 'subvolume', 'show', subvolume_path,
    ]).decode('utf-8').split('\n')[1:]:  # Skip the header line
        if SNAPSHOTS in props:
            if l:  # Ignore the trailing empty line
                TABS = 4
                assert l[:TABS] == '\t' * TABS, 'Not a snapshot line' + repr(l)
                props[SNAPSHOTS].append(l[TABS:])
        else:
            k, v = l.strip().split(':', 1)
            k = k.rstrip(':')
            v = v.strip()
            if k == SNAPSHOTS:
                assert v == '', f'Should have nothing after ":" in: {l}'
                props[SNAPSHOTS] = []
            else:
                assert k not in props, f'{l} already had a value {props[k]}'
                if k.endswith(' UUID') and v == '-':
                    v = None
                props[k] = v
    return props 
Example 34
Project: fs_image   Author: facebookincubator   File: common.py    MIT License 5 votes vote down vote up
def _generate_file(
    temp_dir: str, generator: bytes, generator_args: List[str],
) -> str:
    # API design notes:
    #
    #  1) The generator takes an output directory, not a file, because we
    #     prefer not to have to hardcode the name of the output file in the
    #     TARGETS file -- that would make it more laborious to change the
    #     compression format for tarballs, e.g.  Instead, the generator
    #     prints the name of the created file to stdout.  This does not
    #     introduce nondeterminism for current consumers of `image.source`:
    #       - A tarball name cannot affect the result of its extraction.
    #       - `yum` ignores the RPM name, and reads metadata from its content.
    #       - `install_file` specifies the destination filename in `TARGETS`.
    #
    #     Since TARGETS already hardcodes a content hash, requiring the name
    #     would not be far-fetched, this approach just seemed cleaner.
    #
    #  2) `temp_dir` is last since this allows the use of inline scripts via
    #     `generator_args` with e.g. `/bin/bash`.
    #
    # Future: it would be best to sandbox the generator to limit its
    # filesystem writes.  At the moment, we trust rule authors not to abuse
    # this feature and write stuff outside the given directory.
    output_filename = subprocess.check_output([
        generator, *generator_args, temp_dir,
    ]).decode()
    assert output_filename.endswith('\n'), (generator, output_filename)
    output_filename = os.path.normpath(output_filename[:-1])
    assert (
        not output_filename.startswith('/')
        and not output_filename.startswith('../')
    ), output_filename
    return os.path.join(temp_dir, output_filename) 
Example 35
Project: leapp-repository   Author: oamg   File: run_pytest.py    Apache License 2.0 5 votes vote down vote up
def snactor_register(path):
    """ Snactor registers all repos in @path.
    """
    cmd = "snactor repo find --path {PATH}".format(PATH=path)
    try:
        logger.info(" Registering leapp repositories. This may take a while.")
        return subprocess.check_output(cmd, shell=True)
    except OSError as exc:
        sys.stderr.write(str(exc) + '\n')
        return None 
Example 36
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 37
Project: phrydy   Author: Josef-Friedrich   File: test_phrydy_debug.py    MIT License 5 votes vote down vote up
def test_cli(self):
        output = subprocess.check_output(('phrydy-debug', '--help'))
        self.assertIn('usage: phrydy-debug', str(output))

        output = subprocess.check_output(
            ('phrydy-debug', os.path.join('test', 'files', 'full.mp3'))
        )
        output = str(output)
        self.assertIn('Raw mutagen values', output)
        self.assertIn('COMM:iTunPGAP:eng                : 0', output)
        self.assertIn('TCMP                             : 1', output)
        self.assertIn('Values provided by the class: MediaFile', output)
        self.assertIn('title            : full', 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_output(self):
        # check_output() function with zero return code
        output = subprocess.check_output(
                [sys.executable, "-c", "print 'BDFL'"])
        self.assertIn('BDFL', output) 
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: 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_stderr(self):
        # check_output() function stderr redirected to stdout
        output = subprocess.check_output(
                [sys.executable, "-c", "import sys; sys.stderr.write('BDFL')"],
                stderr=subprocess.STDOUT)
        self.assertIn('BDFL', output) 
Example 42
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_stdout_arg(self):
        # check_output() function stderr redirected to stdout
        with self.assertRaises(ValueError) as c:
            output = subprocess.check_output(
                    [sys.executable, "-c", "print 'will not be run'"],
                    stdout=sys.stdout)
            self.fail("Expected ValueError when stdout arg supplied.")
        self.assertIn('stdout', c.exception.args[0]) 
Example 43
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 44
Project: azure-cred-manager   Author: gregohardy   File: core.py    Apache License 2.0 5 votes vote down vote up
def shell(self, cmd):
        try:
            return subprocess.check_output(cmd.split(), universal_newlines=True)
        except Exception as e:
            raise Exception("Failed to execute the shell command [{0}] as [{1}]".format(cmd, str(e))) 
Example 45
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 46
Project: earlgrey   Author: icon-project   File: test_multiprocess.py    Apache License 2.0 5 votes vote down vote up
def available_credential(username, password):
    try:
        subprocess.check_output(["rabbitmqctl", "authenticate_user", username, password])
        result = subprocess.check_output(["rabbitmqctl", "list_user_permissions", username])
        return b"/	.*	.*	.*" in result
    except Exception as e:
        traceback.print_exc()
        return False 
Example 47
Project: gpu-mux   Author: google   File: gpumux.py    Apache License 2.0 5 votes vote down vote up
def get_gpus():
    cmd_outut = subprocess.check_output(['nvidia-smi', '--list-gpus']).decode()
    gpus = collections.OrderedDict()
    for x in cmd_outut.split('\n'):
        if not x:
            continue
        expr = R_GPU.match(x)
        gpus[int(expr.group('id'))] = expr.group('model')

    gpus = apply_gpu_preferences(gpus)
    print('GPUs available %d' % len(gpus))
    for k, v in gpus.items():
        print('%-2d %s' % (k, v))
    return gpus 
Example 48
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exec_cmd(cmd_name, options=[], arg_list=[]):
    # Execute command and return empty list on success or list of output/error messages
    cmd = [cmd_name] + options + arg_list
    print >> sys.stderr, 'exec_cmd:', ' '.join(cmd)
    try:
        output = subprocess.check_output(cmd)
    except Exception, excp:
        output = str(excp)
        print >> sys.stderr, 'ERROR:exec_cmd', cmd_name, output 
Example 49
Project: slidoc   Author: mitotic   File: sdserver.py    BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def cert_daysleft():
    if not Options['ssl_options'] or not Options['ssl_options']['certfile'].startswith('certbot/'):
        return None
    try:
        cmd = ['openssl', 'x509', '-enddate', '-noout', '-in', Options['ssl_options']['certfile']]
        expdatestr = subprocess.check_output(cmd).split('=')[-1].strip()
        expdate = datetime.datetime.strptime(expdatestr,'%b %d %H:%M:%S %Y %Z')
        return (expdate - datetime.datetime.today()).days
    except Exception, excp:
        print >> sys.stderr, 'sdserver/cert_daysleft: ERROR: '+' '.join(cmd) + ': '+ str(excp)
        return -9999 
Example 50
Project: Py-Utils   Author: LonamiWebs   File: adb.py    MIT License 5 votes vote down vote up
def run_adb_command(command):
    """Runs the given command (arguments) through adb"""
    return str(check_output('./adb {}'.format(command), shell=True), encoding='utf-8') 
Example 51
Project: reroils-data-legacy   Author: rero   File: test_examples_app.py    GNU General Public License v2.0 5 votes vote down vote up
def example_app():
    """Example app fixture."""
    current_dir = os.getcwd()

    # Go to example directory
    project_dir = dirname(dirname(abspath(__file__)))
    exampleapp_dir = join(project_dir, 'examples')
    os.chdir(exampleapp_dir)

    # Setup application
    assert subprocess.call('./app-setup.sh', shell=True) == 0

    # Setup fixtures
    assert subprocess.call('./app-fixtures.sh', shell=True) == 0

    # Start example app
    webapp = subprocess.Popen(
        'FLASK_APP=app.py flask run --debugger -p 5001',
        stdout=subprocess.PIPE, preexec_fn=os.setsid, shell=True)
    time.sleep(10)
    yield webapp

    # Stop server
    os.killpg(webapp.pid, signal.SIGTERM)

    # Tear down example app
    subprocess.call('./app-teardown.sh', shell=True)

    # Return to the original directory
    os.chdir(current_dir)


# def test_example_app_role_admin(example_app):
#     """Test example app."""
#     cmd = 'curl http://0.0.0.0:5001/'
#     output = subprocess.check_output(cmd, shell=True)
#     assert b'Welcome to REROILS-DATA' in output 
Example 52
Project: mmdetection   Author: open-mmlab   File: publish_model.py    Apache License 2.0 5 votes vote down vote up
def process_checkpoint(in_file, out_file):
    checkpoint = torch.load(in_file, map_location='cpu')
    # remove optimizer for smaller file size
    if 'optimizer' in checkpoint:
        del checkpoint['optimizer']
    # if it is necessary to remove some sensitive data in checkpoint['meta'],
    # add the code here.
    torch.save(checkpoint, out_file)
    sha = subprocess.check_output(['sha256sum', out_file]).decode()
    final_file = out_file.rstrip('.pth') + '-{}.pth'.format(sha[:8])
    subprocess.Popen(['mv', out_file, final_file]) 
Example 53
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 54
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 55
Project: openplotter   Author: openplotter   File: add_i2c.py    GNU General Public License v2.0 5 votes vote down vote up
def onCheckAddresses(self, e):
		addresses = ''
		try:
			addresses = subprocess.check_output(['i2cdetect', '-y', '1'])
		except: 
			addresses = subprocess.check_output(['i2cdetect', '-y', '2'])
		wx.MessageBox(addresses, _('Detected I2C addresses'), wx.OK | wx.ICON_INFORMATION) 
Example 56
Project: openplotter   Author: openplotter   File: SDR_AIS_fine_cal.py    GNU General Public License v2.0 5 votes vote down vote up
def calculate(self,e):
			self.SetStatusText(_('Working...'))
			self.output.SetValue('')
			if self.option=='c': 
				
				try: output=subprocess.check_output(['kal', '-c', self.channel, '-g', self.gain, '-e', self.ppm])
				except: output=_('error')
			if self.option=='b': 
				band=self.conf.get('AIS-SDR', 'band')
				try: output=subprocess.check_output(['kal', '-s', band, '-g', self.gain, '-e', self.ppm])
				except: output=_('error')
			self.output.SetValue(output)
			self.SetStatusText(_('Finished')) 
Example 57
Project: openplotter   Author: openplotter   File: test_sms.py    GNU General Public License v2.0 5 votes vote down vote up
def calculate(self,e):
			self.output.SetValue('')
			output=''
			if self.option=='i': 
				self.SetStatusText(_('Connecting...'))
				try:
					output=subprocess.check_output(['gammu', 'identify'])
				except: output= _('Error opening device. Check settings.')	
			if self.option=='t':
				self.SetStatusText(_('Sending...'))
				try:
					sm = gammu.StateMachine()
					sm.ReadConfig()
					sm.Init()
					netinfo = sm.GetNetworkInfo()
					output+= 'Network name: %s' % netinfo['NetworkName']
					output+= '\nNetwork code: %s' % netinfo['NetworkCode']
					output+= '\nLAC: %s' % netinfo['LAC']
					output+= '\nCID: %s' % netinfo['CID']
					message = {
						'Text': self.text_sms, 
						'SMSC': {'Location': 1},
						'Number': self.phone,
					}
					sm.SendSMS(message)
					output+='\nSMS "'+self.text_sms+_('" sent succesfully to ').decode('utf8')+self.phone
				except Exception,e: output= str(e)

			self.output.SetValue(output)
			self.SetStatusText(_('Finished')) 
Example 58
Project: spqrel_tools   Author: LCAS   File: spqrel_ui.py    MIT License 5 votes vote down vote up
def _translate_plan(self, plan_name):
        try:
            print "translate plan in %s" % self.__plan_dir
            print check_output(
                ['pnpgen_translator',
                 'inline', plan_name + '.plan'],
                cwd=self.__plan_dir
            )
            print "translated plan"
        except Exception as e:
            print "failed translating plan: %s" % str(e) 
Example 59
Project: spqrel_tools   Author: LCAS   File: spqrel_ui.py    MIT License 5 votes vote down vote up
def _start_plan(self, plan_name):
        self._ensure_py_stopped()
        try:
            print "start plan in %s" % self.__plan_dir
            print check_output(
                ['./run_plan.py',
                 '--plan', plan_name],
                cwd=self.__plan_dir
            )
            print "started plan"
        except Exception as e:
            print "failed starting plan: %s" % str(e) 
Example 60
Project: core   Author: lifemapper   File: backupRestoreUser.py    GNU General Public License v3.0 5 votes vote down vote up
def __call__(self):
      hostname = subprocess.check_output('hostname').strip()
      if hostname == 'hera':
         os.environ['PGPASSWORD'] = 'jetsons'
      else:
         # more /opt/lifemapper/rocks/etc/users | grep admin | awk '{print $2}'
         fname = os.path.join(APP_PATH, 'rocks/etc/users')
         cmd = 'cat {} | grep admin | awk \'!print $2?\''.format(fname)
         cmd = cmd.replace('!', '{')
         cmd = cmd.replace('?', '}')
         pswd = subprocess.check_output(cmd).strip()
         os.environ['PGPASSWORD'] = pswd

# ............................................... 
Example 61
Project: core   Author: lifemapper   File: backupRestoreUser.py    GNU General Public License v3.0 5 votes vote down vote up
def getTimestamp(dateonly=False):
   '''
   @note: options must be valid options to the linux 'date' command
   '''
   args = ['date']
   if dateonly:
      args.append('+%F')
   timestamp = subprocess.check_output(args).strip()
   return timestamp
   
# ............................................... 
Example 62
Project: core   Author: lifemapper   File: backupRestoreUser.py    GNU General Public License v3.0 5 votes vote down vote up
def getActiveUsers(dbuser, dbname, dbschema, ignoreUsers, ignoreUserPrefixes, 
                   days=None):
   dbcmd = 'psql --username={} --dbname={} '.format(dbuser, dbname)
   pastdate = mx.DateTime.gmt().mjd - days
   if dbname == MAL_STORE:
      queryStmt = ('select distinct(m.userid) from {0}.model m, {0}.scenario s '.format(dbschema)+
                   'WHERE m.scenarioid = s.scenarioid ')
      if days != None:
         queryStmt += ' AND statusmodtime > {} '.format(pastdate)
   else:
      queryStmt = ('select distinct(userid) from {0}.bucket '.format(dbschema))
      if days != None:
         queryStmt += ' WHERE statusmodtime > {} '.format(pastdate)
         
   cmd = '\"COPY ({}) TO STDOUT \"'.format(queryStmt)
   fullStmt = ' {} --command={}'.format(dbcmd, cmd)
   backupuserStr = subprocess.check_output(fullStmt, preexec_fn=SetPass, shell=True)
   
   backupusers = backupuserStr.split('\n')
   activeUsers = []
   for usr in backupusers:
      if not ignoreUser(usr, ignoreUsers, ignoreUserPrefixes):
         activeUsers.append(usr)
   print 'Active Users: ', str(activeUsers)
   return activeUsers

# ............................................... 
Example 63
Project: core   Author: lifemapper   File: backupRestoreUser.py    GNU General Public License v3.0 5 votes vote down vote up
def getUsersToBackup(backupChoice, dbuser, dbname, dbschema, 
                     ignoreUsers=[], ignoreUserPrefixes=[],
                     onlyUsers=[], days=None):
   hostname = subprocess.check_output('hostname').strip()
   if hostname == 'hera':
      datapath = '/share/data/archive'
   else:
      earl = EarlJr()
      datapath = earl.createArchiveDataPath()
      
   if backupChoice not in ('users', 'all'):
      backupusers = [backupChoice]
   elif backupChoice == 'users':
      ignoreUsers.extend([PUBLIC_USER, DEFAULT_POST_USER])
      if len(onlyUsers) > 0:
         backupusers = onlyUsers
      else: 
         backupusers = getActiveUsers(dbuser, dbname, dbschema, ignoreUsers, 
                                      ignoreUserPrefixes, days=180)
   elif backupChoice == 'all':
      backupusers = getActiveUsers(dbuser, dbname, dbschema, ignoreUsers, 
                                   ignoreUserPrefixes, days=None)

   return datapath, backupusers

# ............................................... 
Example 64
Project: extractors   Author: pudo-attic   File: tesseract.py    MIT License 5 votes vote down vote up
def _extract_image_page(pdf_file, page, languages=None):
    # This is a somewhat hacky way of working around some of the formats
    # and compression mechanisms not supported in pdfminer. It will
    # generate an image based on the given page in the PDF and then OCR
    # that.
    args = [PDFTOPPM_BIN, pdf_file, '-singlefile', '-gray', '-f', str(page)]
    output = subprocess.check_output(args)
    return extract_image_data(output, languages=languages) 
Example 65
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 66
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 67
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    Apache License 2.0 5 votes vote down vote up
def generate_hosts_file(num_nodes, workers_file, args_workers_file):
    f = open(workers_file, 'w')
    output = subprocess.check_output(['head', '-n', str(num_nodes), args_workers_file])
    f.write(output)
    f.close()
    return 
Example 68
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: benchmark.py    Apache License 2.0 5 votes vote down vote up
def stop_old_processes(hosts_file, prog_name):
    stop_args = ['python', '../../tools/kill-mxnet.py', hosts_file, 'python', prog_name]
    stop_args_str = ' '.join(stop_args)
    LOGGER.info('killing old remote processes\n %s', stop_args_str)
    stop = subprocess.check_output(stop_args, stderr=subprocess.STDOUT)
    LOGGER.debug(stop)
    time.sleep(1) 
Example 69
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: util.py    Apache License 2.0 5 votes vote down vote up
def get_gpus():
    """
    return a list of GPUs
    """
    try:
        re = subprocess.check_output(["nvidia-smi", "-L"], universal_newlines=True)
    except OSError:
        return []
    return range(len([i for i in re.split('\n') if 'GPU' in i])) 
Example 70
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: util.py    Apache License 2.0 5 votes vote down vote up
def get_gpus():
    """
    return a list of GPUs
    """
    try:
        re = subprocess.check_output(["nvidia-smi", "-L"], universal_newlines=True)
    except OSError:
        return []
    return range(len([i for i in re.split('\n') if 'GPU' in i])) 
Example 71
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: dot.py    Apache License 2.0 5 votes vote down vote up
def _line_count(path):
    return int(subprocess.check_output('wc -l {}'.format(path), shell=True).split()[0]) 
Example 72
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: test_measure.py    Apache License 2.0 5 votes vote down vote up
def get_gpus():
    try:
        re = subprocess.check_output(["nvidia-smi", "-L"], universal_newlines=True)
    except OSError:
        return ''
    gpus = [i for i in re.split('\n') if 'GPU' in i]
    return ','.join([str(i) for i in range(len(gpus))]) 
Example 73
Project: dynamic-training-with-apache-mxnet-on-aws   Author: awslabs   File: build.py    Apache License 2.0 5 votes vote down vote up
def _get_local_image_id(docker_binary, docker_tag):
    """
    Get the image id of the local docker layer with the passed tag
    :param docker_tag: docker tag
    :return: Image id as string or None if tag does not exist
    """
    cmd = [docker_binary, "images", "-q", docker_tag]
    image_id_b = check_output(cmd)
    image_id = image_id_b.decode('utf-8').strip()
    if not image_id:
        raise RuntimeError('Unable to find docker image id matching with tag {}'.format(docker_tag))
    return image_id 
Example 74
Project: Adeept_PiCar-B   Author: adeept   File: server.py    GNU General Public License v3.0 5 votes vote down vote up
def get_ram():
    try:
        s = subprocess.check_output(['free','-m'])
        lines = s.split('\n') 
        return ( int(lines[1].split()[1]), int(lines[2].split()[3]) )
    except:
        return 0 
Example 75
Project: Adeept_PiCar-B   Author: adeept   File: server.py    GNU General Public License v3.0 5 votes vote down vote up
def get_temperature():
    try:
        s = subprocess.check_output(['/opt/vc/bin/vcgencmd','measure_temp'])
        return float(s.split('=')[1][:-3])
    except:
        return 0 
Example 76
Project: myhoard   Author: aiven   File: util.py    Apache License 2.0 5 votes vote down vote up
def detect_running_process_id(command):
    """Find a process with matching command owned by the same user as current process and return
    its pid. Returns None if no such process is found."""
    # This is mainly used in tests. Actual use should rely on systemd and if non-Linux operating systems
    # are supported later whatever service management interface those other operating systems provide
    output = subprocess.check_output(["ps", "-x", "--cols", "1000", "-o", "pid,command"])
    # We don't expect to have non-ASCII characters, convert using ISO-8859-1, which should always work without raising
    output = output.decode("ISO-8859-1")
    regex = re.compile(r"^\s*\d+\s+{}".format(re.escape(command)))
    ids = [int(line.strip().split()[0]) for line in output.splitlines() if regex.match(line)]
    if not ids or len(ids) > 1:
        return None
    return ids[0] 
Example 77
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 78
Project: myhoard   Author: aiven   File: test_util.py    Apache License 2.0 5 votes vote down vote up
def test_detect_running_process_id():
    pytest_id = myhoard_util.detect_running_process_id("python3 -m pytest")
    coverage_id = myhoard_util.detect_running_process_id("python3 -m coverage")
    if pytest_id is None and coverage_id is None:
        output = subprocess.check_output(["ps", "-x", "--cols", "1000", "-o", "pid,command"])
        raise AssertionError(f"None of the commands included in ps output {output!r}")
    assert myhoard_util.detect_running_process_id("certainlynosuchprocesscurrentlyrunning") is None 
Example 79
Project: fs_image   Author: facebookincubator   File: test_unshare.py    MIT License 4 votes vote down vote up
def test_mount_namespace(self):
        try:
            sleep_pid = None
            with tempfile.TemporaryDirectory() as mnt_src, \
                    tempfile.TemporaryDirectory() as mnt_dest1, \
                    tempfile.TemporaryDirectory() as mnt_dest2:
                with open(os.path.join(mnt_src, 'cypa'), 'w') as outfile:
                    outfile.write('kvoh')

                def check_mnt_dest(mnt_dest: str):
                    cypa = os.path.join(mnt_dest, 'cypa')
                    # The outer NS cannot see the mount
                    self.assertFalse(os.path.exists(cypa))
                    # But we can read it from inside the namespace
                    self.assertEqual(b'kvoh', subprocess.check_output(
                        nsenter_as_user(unshare, 'cat', cypa),
                    ))

                with Unshare([Namespace.MOUNT]) as unshare:
                    # Without a PID namespace, this will outlive the
                    # __exit__ -- in fact, this process would leak but for
                    # our `finally`.
                    proc, sleep_pid = self._popen_sleep_forever(unshare)

                    subprocess.check_call(nsenter_as_root(
                        unshare, 'mount', mnt_src, mnt_dest1, '-o', 'bind',
                    ))
                    check_mnt_dest(mnt_dest1)

                    # Mount namespaces remain usable after the keepalive dies
                    self._kill_keepalive(unshare)

                    # We can make a second mount inside the namespace
                    subprocess.check_call(nsenter_as_root(
                        unshare, 'mount', mnt_src, mnt_dest2, '-o', 'bind',
                    ))
                    check_mnt_dest(mnt_dest2)
                    check_mnt_dest(mnt_dest1)  # The old mount is still good

                # Outside the context, nsenter cannot work. There's no way
                # to test the mounts are gone since we don't have any handle
                # by which to access them.  That's the point.
                with self.assertRaisesRegex(
                    RuntimeError, 'Must nsenter from inside an Unshare',
                ):
                    check_mnt_dest(mnt_dest1)

            time.sleep(2)  # Give some time for `sleep` to exit erroneously
            self.assertIs(None, proc.poll())  # Processes leak
        finally:
            # Ensure we don't leak the `sleep infinity` -- since it was
            # started via `sudo`, `subprocess` cannot kill it automatically.
            if sleep_pid:
                if proc.poll() is None:
                    os.kill(sleep_pid, signal.SIGTERM)
                proc.wait() 
Example 80
Project: mmdetection   Author: open-mmlab   File: collect_env.py    Apache License 2.0 4 votes vote down vote up
def collect_env():
    env_info = {}
    env_info['sys.platform'] = sys.platform
    env_info['Python'] = sys.version.replace('\n', '')

    cuda_available = torch.cuda.is_available()
    env_info['CUDA available'] = cuda_available

    if cuda_available:
        from torch.utils.cpp_extension import CUDA_HOME
        env_info['CUDA_HOME'] = CUDA_HOME

        if CUDA_HOME is not None and osp.isdir(CUDA_HOME):
            try:
                nvcc = osp.join(CUDA_HOME, 'bin/nvcc')
                nvcc = subprocess.check_output(
                    '"{}" -V | tail -n1'.format(nvcc), shell=True)
                nvcc = nvcc.decode('utf-8').strip()
            except subprocess.SubprocessError:
                nvcc = 'Not Available'
            env_info['NVCC'] = nvcc

        devices = defaultdict(list)
        for k in range(torch.cuda.device_count()):
            devices[torch.cuda.get_device_name(k)].append(str(k))
        for name, devids in devices.items():
            env_info['GPU ' + ','.join(devids)] = name

    gcc = subprocess.check_output('gcc --version | head -n1', shell=True)
    gcc = gcc.decode('utf-8').strip()
    env_info['GCC'] = gcc

    env_info['PyTorch'] = torch.__version__
    env_info['PyTorch compiling details'] = torch.__config__.show()

    env_info['TorchVision'] = torchvision.__version__

    env_info['OpenCV'] = cv2.__version__

    env_info['MMCV'] = mmcv.__version__
    env_info['MMDetection'] = mmdet.__version__
    env_info['MMDetection Compiler'] = get_compiler_version()
    env_info['MMDetection CUDA Compiler'] = get_compiling_cuda_version()

    for name, val in env_info.items():
        print('{}: {}'.format(name, val))