Python subprocess32.check_call() Examples

The following are 15 code examples of subprocess32.check_call(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module subprocess32 , or try the search function .
Example #1
Source File: instaclone.py    From instaclone with Apache License 2.0 6 votes vote down vote up
def _rmtree_fast(path, ignore_errors=False):
  """
  Delete a file or directory. Uses rsync to delete directories, which
  is among the fastest ways possible to delete large numbers of
  files. Note it can remove some read-only files.

  slashroot.in/which-is-the-fastest-method-to-delete-files-in-linux
  """
  if ignore_errors and not os.path.exists(path):
    return
  if os.path.isdir(path) and not os.path.islink(path):
    with temp_output_dir("empty.", always_clean=True) as empty_dir:
      popenargs = ["rsync", "-r", "--delete", empty_dir + '/', path]
      subprocess.check_call(popenargs)
    os.rmdir(path)
  else:
    os.unlink(path) 
Example #2
Source File: archives.py    From instaclone with Apache License 2.0 6 votes vote down vote up
def _autodetect_unzip_command():
  unzip_cmd = None
  unzip_output = None
  try:
    unzip_output = subprocess.check_output(["unzip", "-v"])
    unzip_cmd = "unzip -q $ARCHIVE"
  except subprocess.CalledProcessError as e:
    pass

  # On MacOS Yosemite, unzip does not support Zip64, but ditto is available.
  # See: https://github.com/vivlabs/instaclone/issues/1
  if not unzip_cmd or not unzip_output or unzip_output.find("ZIP64_SUPPORT") < 0:
    log.debug("did not find 'unzip' with Zip64 support; trying ditto")
    try:
      # ditto has no simple flag to check its version and exit with 0 status code.
      subprocess.check_call(["ditto", "-c", "/dev/null", tempfile.mktemp()])
      unzip_cmd = "ditto -x -k $ARCHIVE ."
    except subprocess.CalledProcessError as e:
      log.debug("did not find ditto")

  if not unzip_cmd:
    raise ArchiveError("Archive handling requires 'unzip' or 'ditto' in path")

  log.debug("unzip command: %s", unzip_cmd)
  return unzip_cmd 
Example #3
Source File: instaclone.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def _upload_file(command_template, local_path, remote_loc):
  popenargs = shell_expand_to_popen(command_template,
                                    dict_merge(os.environ,
                                               {"REMOTE": remote_loc,
                                                "LOCAL": local_path}))
  log.info("uploading: %s", " ".join(popenargs))
  # TODO: Find a way to support force here (e.g. add or remove -f to s4cmd)
  subprocess.check_call(popenargs, stdout=SHELL_OUTPUT, stderr=SHELL_OUTPUT,
                        stdin=DEV_NULL) 
Example #4
Source File: instaclone.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def _download_file(command_template, remote_loc, local_path):
  with atomic_output_file(local_path, make_parents=True) as temp_target:
    popenargs = shell_expand_to_popen(command_template,
                                      dict_merge(os.environ,
                                                 {"REMOTE": remote_loc,
                                                  "LOCAL": temp_target}))
    log.info("downloading: %s", " ".join(popenargs))
    # TODO: Find a way to support force here.
    subprocess.check_call(popenargs, stdout=SHELL_OUTPUT, stderr=SHELL_OUTPUT,
                          stdin=DEV_NULL) 
Example #5
Source File: instaclone.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def _rsync_dir(source_dir, target_dir, chmod=None):
  """
  Use rsync to clone source_dir to target_dir.
  Preserves the original owners and permissions.
  As an optimization, rsync also allows perms to be partly modified
  as files are synced.
  """
  popenargs = ["rsync", "-a", "--delete"]
  if chmod:
    popenargs.append("--chmod=%s" % chmod)
  popenargs.append(source_dir.rstrip('/') + '/')
  popenargs.append(target_dir)
  log.info("using rsync for faster copy")
  log.debug("rsync: %r" % popenargs)
  subprocess.check_call(popenargs) 
Example #6
Source File: archives.py    From instaclone with Apache License 2.0 5 votes vote down vote up
def unzip_dir(source_archive, target_dir):
  popenargs = shell_expand_to_popen(_autodetect_unzip_command(), {"ARCHIVE": source_archive, "DIR": target_dir})
  cd_to = target_dir
  log.debug("using cwd: %s", cd_to)
  log.info("decompress: %s", " ".join(popenargs))
  subprocess.check_call(popenargs, cwd=cd_to, stdout=SHELL_OUTPUT, stderr=SHELL_OUTPUT, stdin=DEV_NULL) 
Example #7
Source File: conftest.py    From lain with MIT License 5 votes vote down vote up
def up_node1():
    subproc.check_call(['vagrant', 'destroy', '-f', 'node1'])
    subproc.check_call(['vagrant', 'up', 'node1', '--no-provision'])
    yield "node1 is ready"

    print("Destroying node1...")
    subproc.call(['vagrant', 'destroy', '-f', 'node1'])
    print("Node1 is destroyed.") 
Example #8
Source File: conftest.py    From lain with MIT License 5 votes vote down vote up
def up_node2():
    subproc.check_call(['vagrant', 'destroy', '-f', 'node2'])
    subproc.check_call(['vagrant', 'up', 'node2'])
    yield "node2 is ready"

    print("Destroying node2...")
    subproc.call(['vagrant', 'destroy', '-f', 'node2'])
    print("Node2 is destroyed.") 
Example #9
Source File: conftest.py    From lain with MIT License 5 votes vote down vote up
def up_node3():
    subproc.check_call(['vagrant', 'destroy', '-f', 'node3'])
    subproc.check_call(['vagrant', 'up', 'node3'])
    yield "node3 is ready"

    print("Destroying node3...")
    subproc.call(['vagrant', 'destroy', '-f', 'node3'])
    print("Node3 is destroyed.") 
Example #10
Source File: conftest.py    From lain with MIT License 5 votes vote down vote up
def bootstrap(up_node1):
    subproc.check_call([
        'vagrant', 'ssh', 'node1', '-c',
        'sudo /vagrant/bootstrap --pypi-mirror -m https://l2ohopf9.mirror.aliyuncs.com -r docker.io/laincloud --vip={}'.
        format(CONFIG.vip)
    ]) 
Example #11
Source File: conftest.py    From lain with MIT License 5 votes vote down vote up
def prepare_demo_images(bootstrap):
    subproc.check_call([
        'vagrant', 'ssh', 'node1', '-c',
        'sudo sh /vagrant/bootstrap_test/prepare_demo_images.sh'
    ]) 
Example #12
Source File: strif.py    From strif with Apache License 2.0 5 votes vote down vote up
def chmod_native(path, mode_expression, recursive=False):
  """
  This is ugly and will only work on POSIX, but the built-in Python os.chmod support
  is very minimal, and neither supports fast recursive chmod nor "+X" type expressions,
  both of which are slow for large trees. So just shell out.
  """
  popenargs = ["chmod"]
  if recursive:
    popenargs.append("-R")
  popenargs.append(mode_expression)
  popenargs.append(path)
  subprocess.check_call(popenargs) 
Example #13
Source File: subproc.py    From treadmill with Apache License 2.0 5 votes vote down vote up
def check_call(cmdline, environ=(), runas=None, **kwargs):
    """Runs command wrapping subprocess.check_call.

    :param cmdline:
        Command to run
    :type cmdline:
        ``list``
    :param environ:
        *optional* Environ variable to set prior to running the command
    :type environ:
        ``dict``
    :param runas:
        *optional* Run as user.
    :type runas:
        ``str``
    """
    _LOGGER.debug('check_call environ: %r, runas: %r, %r',
                  environ, runas, cmdline)

    args = _alias_command(cmdline)
    if runas:
        s6_setguid = _resolve('s6_setuidgid')
        args = s6_setguid + [runas] + args

    # Setup a copy of the environ with the provided overrides
    cmd_environ = dict(os.environ.items())
    cmd_environ.update(environ)

    try:
        rc = subprocess.check_call(args, close_fds=_CLOSE_FDS, env=cmd_environ,
                                   **kwargs)
        _LOGGER.debug('Finished, rc: %d', rc)
        return rc
    except CalledProcessError as exc:
        _LOGGER.error('Command failed: rc:%d', exc.returncode)
        raise 
Example #14
Source File: export_remote.py    From osm2vectortiles with MIT License 5 votes vote down vote up
def handle_message(tm2source, bucket, s3_url, body):
    msg = json.loads(body.decode('utf-8'))
    task_id = msg['id']
    mbtiles_file = task_id + '.mbtiles'

    source = 'tmsource://' + os.path.abspath(tm2source)
    sink = 'mbtiles://' + os.path.abspath(mbtiles_file)

    tilelive_cmd = []
    if msg['type'] == 'pyramid':
        tilelive_cmd = render_pyramid(msg, source, sink)
    elif msg['type'] == 'list':
        tilelive_cmd = render_list(msg, source, sink)
    else:
        raise ValueError("Message must be either of type pyramid or list")

    render_timeout = int(os.getenv('RENDER_TIMEOUT', 5 * 60))
    _, render_time = timing(subprocess.check_call, tilelive_cmd,
                            timeout=render_timeout)
    print('Render MBTiles: {}'.format(naturaltime(render_time)))

    _, optimize_time = timing(optimize_mbtiles, mbtiles_file)
    print('Optimize MBTiles: {}'.format(naturaltime(optimize_time)))

    _, upload_time = timing(upload_mbtiles, bucket, mbtiles_file)
    print('Upload MBTiles : {}'.format(naturaltime(upload_time)))

    download_link = s3_url(mbtiles_file)
    print('Uploaded {} to {}'.format(
        naturalsize(os.path.getsize(mbtiles_file)),
        download_link
    ))

    os.remove(mbtiles_file)

    return create_result_message(task_id, download_link, msg) 
Example #15
Source File: solvers.py    From MatchingMarkets.py with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
def actualSolve(self, lp):
        """Solve a well formulated lp problem"""
        if not self.executable(self.path):
            raise PulpSolverError("PuLP: cannot execute "+self.path)

        # TODO: should we use tempfile instead?
        if not self.keepFiles:
            pid = os.getpid()
            tmpLp = os.path.join(self.tmpDir, "%d-pulp.lp" % pid)
            tmpSol = os.path.join(self.tmpDir, "%d-pulp.sol" % pid)
        else:
            tmpLp = lp.name + "-pulp.lp"
            tmpSol = lp.name + "-pulp.sol"

        lp.writeLP(tmpLp)
        proc = [
            'scip', '-c', 'read "%s"' % tmpLp, '-c', 'optimize',
            '-c', 'write solution "%s"' % tmpSol, '-c', 'quit'
        ]
        proc.extend(self.options)
        if not self.msg:
            proc.append('-q')

        self.solution_time = clock()
        subprocess.check_call(proc, stdout=sys.stdout, stderr=sys.stderr)
        self.solution_time += clock()

        if not os.path.exists(tmpSol):
            raise PulpSolverError("PuLP: Error while executing "+self.path)

        lp.status, values = self.readsol(tmpSol)

        # Make sure to add back in any 0-valued variables SCIP leaves out.
        finalVals = {}
        for v in lp.variables():
            finalVals[v.name] = values.get(v.name, 0.0)

        lp.assignVarsVals(finalVals)

        if not self.keepFiles:
            for f in (tmpLp, tmpSol):
                try:
                    os.remove(f)
                except:
                    pass

        return lp.status