Python six.moves.shlex_quote() Examples

The following are 30 code examples of six.moves.shlex_quote(). 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 six.moves , or try the search function .
Example #1
Source File: command.py    From pyinfra with MIT License 6 votes vote down vote up
def _get_all_bits(self, bit_accessor):
        all_bits = []

        for bit in self.bits:
            quote = False
            if isinstance(bit, QuoteString):
                quote = True
                bit = bit.object

            if isinstance(bit, StringCommand):
                bit = bit_accessor(bit)

            if quote:
                bit = shlex_quote(bit)

            all_bits.append(bit)

        return all_bits 
Example #2
Source File: _project_spec.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def get_entry_point(self, entry_point):
        if entry_point in self._entry_points:
            return self._entry_points[entry_point]
        _, file_extension = os.path.splitext(entry_point)
        ext_to_cmd = {".py": "python", ".sh": os.environ.get("SHELL", "bash")}
        if file_extension in ext_to_cmd:
            command = "%s %s" % (ext_to_cmd[file_extension], shlex_quote(entry_point))
            if not is_string_type(command):
                command = command.encode("utf-8")
            return EntryPoint(name=entry_point, parameters={}, command=command)
        elif file_extension == ".R":
            command = "Rscript -e \"mlflow::mlflow_source('%s')\" --args" % shlex_quote(entry_point)
            return EntryPoint(name=entry_point, parameters={}, command=command)
        raise ExecutionException("Could not find {0} among entry points {1} or interpret {0} as a "
                                 "runnable script. Supported script file extensions: "
                                 "{2}".format(entry_point, list(self._entry_points.keys()),
                                              list(ext_to_cmd.keys()))) 
Example #3
Source File: test_entry_point.py    From mlflow with Apache License 2.0 6 votes vote down vote up
def test_entry_point_compute_command():
    """
    Tests that EntryPoint correctly computes the command to execute in order to run the entry point.
    """
    project = load_project()
    entry_point = project.get_entry_point("greeter")
    with TempDir() as tmp:
        storage_dir = tmp.path()
        command = entry_point.compute_command({"name": "friend", "excitement": 10}, storage_dir)
        assert command == "python greeter.py hi friend --excitement 10"
        with pytest.raises(ExecutionException):
            entry_point.compute_command({}, storage_dir)
        # Test shell escaping
        name_value = "friend; echo 'hi'"
        command = entry_point.compute_command({"name": name_value}, storage_dir)
        assert command == "python greeter.py %s %s" % (shlex_quote("hi"), shlex_quote(name_value)) 
Example #4
Source File: __main__.py    From awsmfa with Apache License 2.0 6 votes vote down vote up
def print_env_vars(credentials, target_profile):
    aws_access_key_id = shlex_quote(credentials.get(
        target_profile, 'aws_access_key_id'))
    aws_secret_access_key = shlex_quote(credentials.get(
        target_profile, 'aws_secret_access_key'))
    aws_session_token = shlex_quote(credentials.get(
        target_profile, 'aws_session_token'))
    print("AWS_ACCESS_KEY_ID=%s; export AWS_ACCESS_KEY_ID;" %
          shlex_quote(aws_access_key_id))
    print("AWS_SECRET_ACCESS_KEY=%s; export AWS_SECRET_ACCESS_KEY;" %
          shlex_quote(aws_secret_access_key))
    print("AWS_SESSION_TOKEN=%s; export AWS_SESSION_TOKEN;" %
          shlex_quote(aws_session_token))
    # for backwards compatibility with older Boto
    print("AWS_SECURITY_TOKEN=%s; export AWS_SECURITY_TOKEN;" %
          shlex_quote(aws_session_token)) 
Example #5
Source File: sed.py    From hpc-container-maker with Apache License 2.0 6 votes vote down vote up
def sed_step(self, file=None, in_place=True, patterns=[]):
        """Generate sed command line string"""

        if not file:
            logging.error('file is not defined')
            return ''

        if not patterns:
            logging.error('patterns is not defined')
            return ''

        # Copy so not to modify the member variable
        opts = list(self.sed_opts)

        if in_place:
            opts.append('-i')

        opt_string = ' '.join(opts)

        quoted_patterns = ['-e {}'.format(shlex_quote(patterns[0]))]
        quoted_patterns.extend('        -e {}'.format(shlex_quote(x)) for x in patterns[1:])
        quoted_pattern_string = ' \\\n'.join(quoted_patterns)

        return 'sed {0} {1} {2}'.format(opt_string, quoted_pattern_string, file) 
Example #6
Source File: backend.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _str_optional(s):
    return "NULL" if s is None else "'{}'".format(shlex_quote(str(s))) 
Example #7
Source File: core.py    From Live-feed-object-device-identification-using-Tensorflow-and-OpenCV with Apache License 2.0 5 votes vote down vote up
def get_nondefault_flags_as_str():
  """Returns flags as a string that can be passed as command line arguments.

  E.g., returns: "--batch_size=256 --use_synthetic_data" for the following code
  block:

  ```
  flags.FLAGS.batch_size = 256
  flags.FLAGS.use_synthetic_data = True
  print(get_nondefault_flags_as_str())
  ```

  Only flags with nondefault values are returned, as passing default flags as
  command line arguments has no effect.

  Returns:
    A string with the flags, that can be passed as command line arguments to a
    program to use the flags.
  """
  nondefault_flags = _get_nondefault_flags_as_dict()
  flag_strings = []
  for name, value in sorted(nondefault_flags.items()):
    if isinstance(value, bool):
      flag_str = '--{}'.format(name) if value else '--no{}'.format(name)
    elif isinstance(value, list):
      flag_str = '--{}={}'.format(name, ','.join(value))
    else:
      flag_str = '--{}={}'.format(name, value)
    flag_strings.append(flag_str)
  return ' '.join(shlex_quote(flag_str) for flag_str in flag_strings) 
Example #8
Source File: train.py    From human-rl with MIT License 5 votes vote down vote up
def new_cmd(session, name, cmd, mode, logdir, shell):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join(shlex_quote(str(v)) for v in cmd)
        if name != "htop":
            cmd += " > {} 2>&1".format(os.path.join(logdir, "logs", "{}.txt".format(name)))
    if mode == 'tmux':
        return name, "tmux send-keys -t {}:{} {} Enter".format(session, name, shlex_quote(cmd))
    elif mode == 'child':
        return name, "{} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(
            cmd, logdir, session, name, logdir)
    elif mode == 'nohup':
        return name, "nohup {} -c {} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(
            shell, shlex_quote(cmd), logdir, session, name, logdir) 
Example #9
Source File: train.py    From FeatureControlHRL with MIT License 5 votes vote down vote up
def new_cmd(session, name, cmd, mode, logdir, shell):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join(shlex_quote(str(v)) for v in cmd)
    if mode == 'tmux':
        return name, "tmux send-keys -t {}:{} {} Enter".format(session, name, shlex_quote(cmd))
    elif mode == 'child':
        return name, "{} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(cmd, logdir, session, name, logdir)
    elif mode == 'nohup':
        return name, "nohup {} -c {} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(shell, shlex_quote(cmd), logdir, session, name, logdir) 
Example #10
Source File: train.py    From FeatureControlHRL-Tensorflow with MIT License 5 votes vote down vote up
def new_cmd(session, name, cmd, mode, logdir, shell):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join(shlex_quote(str(v)) for v in cmd)
    if mode == 'tmux':
        return name, "tmux send-keys -t {}:{} {} Enter".format(session, name, shlex_quote(cmd))
    elif mode == 'child':
        return name, "{} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(cmd, logdir, session, name, logdir)
    elif mode == 'nohup':
        return name, "nohup {} -c {} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(shell, shlex_quote(cmd), logdir, session, name, logdir) 
Example #11
Source File: kubernetes.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _get_run_command(entrypoint_command):
    formatted_command = []
    for cmd in entrypoint_command:
        formatted_command.extend([quote(s) for s in split(cmd)])
    return formatted_command 
Example #12
Source File: train.py    From human-rl with MIT License 5 votes vote down vote up
def new_cmd(session, name, cmd, mode, logdir, shell):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join(shlex_quote(str(v)) for v in cmd)
        if name != "htop":
            cmd += " > {} 2>&1".format(os.path.join(logdir, "logs", "{}.txt".format(name)))
    if mode == 'tmux':
        return name, "tmux send-keys -t {}:{} {} Enter".format(session, name, shlex_quote(cmd))
    elif mode == 'child':
        return name, "{} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(
            cmd, logdir, session, name, logdir)
    elif mode == 'nohup':
        return name, "nohup {} -c {} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(
            shell, shlex_quote(cmd), logdir, session, name, logdir) 
Example #13
Source File: _project_spec.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _sanitize_param_dict(param_dict):
        return {str(key): shlex_quote(str(value)) for key, value in param_dict.items()} 
Example #14
Source File: databricks.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _get_cluster_mlflow_run_cmd(project_dir, run_id, entry_point, parameters):
    mlflow_run_arr = list(map(shlex_quote, ["mlflow", "run", project_dir,
                                            "--entry-point", entry_point]))
    if run_id:
        mlflow_run_arr.extend(["-c", json.dumps({_MLFLOW_LOCAL_BACKEND_RUN_ID_CONFIG: run_id})])
    if parameters:
        for key, value in parameters.items():
            mlflow_run_arr.extend(["-P", "%s=%s" % (key, value)])
    return mlflow_run_arr 
Example #15
Source File: databricks.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def _get_databricks_run_cmd(dbfs_fuse_tar_uri, run_id, entry_point, parameters):
    """
    Generate MLflow CLI command to run on Databricks cluster in order to launch a run on Databricks.
    """
    # Strip ".gz" and ".tar" file extensions from base filename of the tarfile
    tar_hash = posixpath.splitext(posixpath.splitext(posixpath.basename(dbfs_fuse_tar_uri))[0])[0]
    container_tar_path = posixpath.abspath(posixpath.join(DB_TARFILE_BASE,
                                           posixpath.basename(dbfs_fuse_tar_uri)))
    project_dir = posixpath.join(DB_PROJECTS_BASE, tar_hash)

    mlflow_run_arr = _get_cluster_mlflow_run_cmd(project_dir, run_id, entry_point, parameters)
    mlflow_run_cmd = " ".join([shlex_quote(elem) for elem in mlflow_run_arr])
    shell_command = textwrap.dedent("""
    export PATH=$PATH:$DB_HOME/python/bin &&
    mlflow --version &&
    # Make local directories in the container into which to copy/extract the tarred project
    mkdir -p {tarfile_base} {projects_base} &&
    # Rsync from DBFS FUSE to avoid copying archive into local filesystem if it already exists
    rsync -a -v --ignore-existing {dbfs_fuse_tar_path} {tarfile_base} &&
    # Extract project into a temporary directory. We don't extract directly into the desired
    # directory as tar extraction isn't guaranteed to be atomic
    cd $(mktemp -d) &&
    tar --no-same-owner -xzvf {container_tar_path} &&
    # Atomically move the extracted project into the desired directory
    mv -T {tarfile_archive_name} {work_dir} &&
    {mlflow_run}
    """.format(tarfile_base=DB_TARFILE_BASE, projects_base=DB_PROJECTS_BASE,
               dbfs_fuse_tar_path=dbfs_fuse_tar_uri, container_tar_path=container_tar_path,
               tarfile_archive_name=DB_TARFILE_ARCHIVE_NAME, work_dir=project_dir,
               mlflow_run=mlflow_run_cmd))
    return ["bash", "-c", shell_command] 
Example #16
Source File: backend.py    From mlflow with Apache License 2.0 5 votes vote down vote up
def serve(self, model_uri, port, host):
        """
        Generate R model locally.
        """
        model_path = _download_artifact_from_uri(model_uri)
        command = "mlflow::mlflow_rfunc_serve('{0}', port = {1}, host = '{2}')".format(
            shlex_quote(model_path), port, host)
        _execute(command) 
Example #17
Source File: train.py    From feudal_networks with MIT License 5 votes vote down vote up
def new_cmd(session, name, cmd, mode, logdir, shell):
    if isinstance(cmd, (list, tuple)):
        cmd = " ".join(shlex_quote(str(v)) for v in cmd)
    if mode == 'tmux':
        return name, "tmux send-keys -t {}:{} {} Enter".format(session, name, shlex_quote(cmd))
    elif mode == 'child':
        return name, "{} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(cmd, logdir, session, name, logdir)
    elif mode == 'nohup':
        return name, "nohup {} -c {} >{}/{}.{}.out 2>&1 & echo kill $! >>{}/kill.sh".format(shell, shlex_quote(cmd), logdir, session, name, logdir) 
Example #18
Source File: core.py    From models with Apache License 2.0 5 votes vote down vote up
def get_nondefault_flags_as_str():
  """Returns flags as a string that can be passed as command line arguments.

  E.g., returns: "--batch_size=256 --use_synthetic_data" for the following code
  block:

  ```
  flags.FLAGS.batch_size = 256
  flags.FLAGS.use_synthetic_data = True
  print(get_nondefault_flags_as_str())
  ```

  Only flags with nondefault values are returned, as passing default flags as
  command line arguments has no effect.

  Returns:
    A string with the flags, that can be passed as command line arguments to a
    program to use the flags.
  """
  nondefault_flags = _get_nondefault_flags_as_dict()
  flag_strings = []
  for name, value in sorted(nondefault_flags.items()):
    if isinstance(value, bool):
      flag_str = '--{}'.format(name) if value else '--no{}'.format(name)
    elif isinstance(value, list):
      flag_str = '--{}={}'.format(name, ','.join(value))
    else:
      flag_str = '--{}={}'.format(name, value)
    flag_strings.append(flag_str)
  return ' '.join(shlex_quote(flag_str) for flag_str in flag_strings) 
Example #19
Source File: __init__.py    From guildai with Apache License 2.0 5 votes vote down vote up
def _as_suffix(self):
        return " as %s" % q(self.named_as) if self.named_as else "" 
Example #20
Source File: python_script.py    From guildai with Apache License 2.0 5 votes vote down vote up
def _exec_attr(self):
        return "${python_exe} -um guild.op_main %s ${flag_args}" % shlex_quote(
            self._script_module()
        ) 
Example #21
Source File: parser.py    From dockerfile-parse with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _add_instruction(self, instruction, value):
        """
        :param instruction: instruction name to be added
        :param value: instruction value
        """
        if instruction in ('LABEL', 'ENV', 'ARG') and len(value) == 2:
            new_line = instruction + ' ' + '='.join(map(quote, value)) + '\n'
        else:
            new_line = '{0} {1}\n'.format(instruction, value)
        if new_line:
            lines = self.lines
            if not lines[len(lines) - 1].endswith('\n'):
                new_line = '\n' + new_line
            lines += new_line
            self.lines = lines 
Example #22
Source File: stack.py    From fabricio with MIT License 5 votes vote down vote up
def save_new_settings(self, configuration, image):
        self.rotate_sentinel_images()

        labels = [(self.configuration_label, b64encode(configuration).decode())]
        try:
            digests = self._get_digests(self.images)
            digests_bucket = json.dumps(digests, sort_keys=True)
            digests_bucket = b64encode(digests_bucket.encode()).decode()
            labels.append((self.digests_label, digests_bucket))
        except fabricio.host_errors:
            pass

        dockerfile = (
            'FROM {image}\n'
            'LABEL {labels}\n'
        ).format(
            image=image or 'scratch',
            labels=' '.join(itertools.starmap('{0}={1}'.format, labels)),
        )
        build_command = 'echo {dockerfile} | docker build --tag {tag} -'.format(
            dockerfile=shlex_quote(dockerfile),
            tag=self.current_settings_tag,
        )

        try:
            fabricio.run(build_command)
        except fabricio.host_errors as error:
            fabricio.log(
                'WARNING: {error}'.format(error=error),
                output=sys.stderr,
                color=colors.red,
            ) 
Example #23
Source File: service.py    From fabricio with MIT License 5 votes vote down vote up
def get_current_values(self, *args, **kwargs):
        values = super(PlacementPrefOption, self).get_current_values(*args, **kwargs)  # noqa
        for value in values:
            yield ','.join(
                '{strategy}={descriptor}'.format(
                    strategy=strategy.lower(),
                    descriptor=shlex_quote(descriptor[strategy + 'Descriptor'])
                )
                for strategy, descriptor in value.items()
            ) 
Example #24
Source File: utils.py    From fabricio with MIT License 5 votes vote down vote up
def make_option(self, option, value=None):
        option = '--' + option
        if value is not None:
            option += '=' + shlex_quote(six.text_type(value))
        return option 
Example #25
Source File: annotate.py    From hpc-container-maker with Apache License 2.0 5 votes vote down vote up
def add_annotation(self, key, value):
        if isinstance(self.base_annotation, string_types):
            key = 'hpccm.' + self.base_annotation + '.' + key
        elif self.base_annotation:
            key = 'hpccm.' + self.__class__.__name__ + '.' + key

        self.__labels[key] = shlex_quote(str(value)) 
Example #26
Source File: install.py    From commcare-cloud with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def run(self, args, unknown_args):
        env = os.environ.copy()
        put_virtualenv_bin_on_the_path()
        if not os.path.exists(ANSIBLE_ROLES_PATH):
            os.makedirs(ANSIBLE_ROLES_PATH)
        
        if not os.path.exists(ANSIBLE_COLLECTIONS_PATHS):
            os.makedirs(ANSIBLE_COLLECTIONS_PATHS)

        env['ANSIBLE_ROLES_PATH'] = ANSIBLE_ROLES_PATH
        env['ANSIBLE_COLLECTIONS_PATHS'] = ANSIBLE_COLLECTIONS_PATHS
        cmd_roles_parts = ['ansible-galaxy', 'install', '-f', '-r', os.path.join(ANSIBLE_DIR, 'requirements.yml')]
        cmd_collection_parts = ['ansible-galaxy', 'collection', 'install', '-f', '-r', os.path.join(ANSIBLE_DIR, 'requirements.yml')]
        
        for cmd_parts in (cmd_roles_parts, cmd_collection_parts):
            cmd = ' '.join(shlex_quote(arg) for arg in cmd_parts)
            print_command(cmd)
            p = subprocess.Popen(cmd, stdin=subprocess.PIPE, shell=True, env=env)
            p.communicate()

        puts(color_notice("To finish first-time installation, run `manage-commcare-cloud configure`".format()))
        return p.returncode 
Example #27
Source File: core.py    From models with Apache License 2.0 5 votes vote down vote up
def get_nondefault_flags_as_str():
  """Returns flags as a string that can be passed as command line arguments.

  E.g., returns: "--batch_size=256 --use_synthetic_data" for the following code
  block:

  ```
  flags.FLAGS.batch_size = 256
  flags.FLAGS.use_synthetic_data = True
  print(get_nondefault_flags_as_str())
  ```

  Only flags with nondefault values are returned, as passing default flags as
  command line arguments has no effect.

  Returns:
    A string with the flags, that can be passed as command line arguments to a
    program to use the flags.
  """
  nondefault_flags = _get_nondefault_flags_as_dict()
  flag_strings = []
  for name, value in sorted(nondefault_flags.items()):
    if isinstance(value, bool):
      flag_str = '--{}'.format(name) if value else '--no{}'.format(name)
    elif isinstance(value, list):
      flag_str = '--{}={}'.format(name, ','.join(value))
    else:
      flag_str = '--{}={}'.format(name, value)
    flag_strings.append(flag_str)
  return ' '.join(shlex_quote(flag_str) for flag_str in flag_strings) 
Example #28
Source File: tools.py    From Dallinger with MIT License 5 votes vote down vote up
def set(self, key, value):
        """Configure an app key/value pair"""
        cmd = [
            "heroku",
            "config:set",
            "{}={}".format(key, quote(str(value))),
            "--app",
            self.name,
        ]
        if self._is_sensitive_key(key):
            self._run_quiet(cmd)
        else:
            self._run(cmd) 
Example #29
Source File: tools.py    From Dallinger with MIT License 5 votes vote down vote up
def set_multiple(self, **kwargs):
        """Configure multiple app key/value pairs"""
        quiet = False
        if not kwargs:
            return
        cmd = ["heroku", "config:set"]
        for k in sorted(kwargs):
            cmd.append("{}={}".format(k, quote(str(kwargs[k]))))
            if self._is_sensitive_key(k):
                quiet = True
        cmd.extend(["--app", self.name])
        if quiet:
            self._run_quiet(cmd)
        else:
            self._run(cmd) 
Example #30
Source File: fab.py    From commcare-cloud with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def exec_fab_command(env_name, *extra_args):
    cmd_parts = (
        'fab', '-f', FABFILE,
        env_name,
    ) + tuple(extra_args)
    cmd = ' '.join(shlex_quote(arg) for arg in cmd_parts)
    print_command(cmd)
    return subprocess.call(cmd_parts)