Exemplo n.º 1
0
Arquivo: archive.py Projeto: nwbt/salt
    def _list_tar(name, cached, decompress_cmd):
        '''
        List the contents of a tar archive.
        '''
        try:
            with contextlib.closing(tarfile.open(cached)) as tar_archive:
                return [
                    x.name + '/' if x.isdir() else x.name
                    for x in tar_archive.getmembers()
                ]
        except tarfile.ReadError:
            if not salt.utils.which('tar'):
                raise CommandExecutionError('\'tar\' command not available')
            if decompress_cmd is not None:
                # Guard against shell injection
                try:
                    decompress_cmd = ' '.join(
                        [_quote(x) for x in shlex.split(decompress_cmd)]
                    )
                except AttributeError:
                    raise CommandExecutionError('Invalid CLI options')
            else:
                if salt.utils.which('xz') \
                        and __salt__['cmd.retcode'](['xz', '-l', cached],
                                                    python_shell=False,
                                                    ignore_retcode=True) == 0:
                    decompress_cmd = 'xz --decompress --stdout'

            if decompress_cmd:
                cmd = '{0} {1} | tar tf -'.format(decompress_cmd, _quote(cached))
                result = __salt__['cmd.run_all'](cmd, python_shell=True)
                if result['retcode'] != 0:
                    raise CommandExecutionError(
                        'Failed to decompress {0}'.format(name),
                        info={'error': result['stderr']}
                    )
                ret = []
                for line in salt.utils.itertools.split(result['stdout'], '\n'):
                    line = line.strip()
                    if line:
                        ret.append(line)
                return ret

        raise CommandExecutionError(
            'Unable to list contents of {0}. If this is an XZ-compressed tar '
            'archive, install XZ Utils to enable listing its contents. If it '
            'is compressed using something other than XZ, it may be necessary '
            'to specify CLI options to decompress the archive. See the '
            'documentation for details.'.format(name)
        )
Exemplo n.º 2
0
    def _list_tar(name, cached, decompress_cmd):
        '''
        List the contents of a tar archive.
        '''
        try:
            with contextlib.closing(tarfile.open(cached)) as tar_archive:
                return [
                    x.name + '/' if x.isdir() else x.name
                    for x in tar_archive.getmembers()
                ]
        except tarfile.ReadError:
            if not salt.utils.which('tar'):
                raise CommandExecutionError('\'tar\' command not available')
            if decompress_cmd is not None:
                # Guard against shell injection
                try:
                    decompress_cmd = ' '.join(
                        [_quote(x) for x in shlex.split(decompress_cmd)]
                    )
                except AttributeError:
                    raise CommandExecutionError('Invalid CLI options')
            else:
                if salt.utils.which('xz') \
                        and __salt__['cmd.retcode'](['xz', '-l', cached],
                                                    python_shell=False,
                                                    ignore_retcode=True) == 0:
                    decompress_cmd = 'xz --decompress --stdout'

            if decompress_cmd:
                cmd = '{0} {1} | tar tf -'.format(decompress_cmd, _quote(cached))
                result = __salt__['cmd.run_all'](cmd, python_shell=True)
                if result['retcode'] != 0:
                    raise CommandExecutionError(
                        'Failed to decompress {0}'.format(name),
                        info={'error': result['stderr']}
                    )
                ret = []
                for line in salt.utils.itertools.split(result['stdout'], '\n'):
                    line = line.strip()
                    if line:
                        ret.append(line)
                return ret

        raise CommandExecutionError(
            'Unable to list contents of {0}. If this is an XZ-compressed tar '
            'archive, install xz-utils to enable listing its contents. If it '
            'is compressed using something other than XZ, it may be necessary '
            'to specify CLI options to decompress the archive. See the '
            'documentation for details.'.format(name)
        )
Exemplo n.º 3
0
def quote(s):
    if os.name == 'nt' and os.sep == '\\':
        s = s.replace('"', '\\"')
        if re.search('\s', s):
            s = '"' + s + '"'
    else:
        s = _quote(s)
    return s
Exemplo n.º 4
0
def quote(s):
  if os.name == 'nt' and os.sep == '\\':
    s = s.replace('"', '\\"')
    if re.search('\s', s):
      s = '"' + s + '"'
  else:
    s = _quote(s)
  return s
Exemplo n.º 5
0
def format_command(args):
    r"""
    Return a list of argument strings as one shell-escaped command.

    >>> import shlex
    >>> shlex.split(format_command(["echo", "Hello, World!", "'quoted'"]))
    ['echo', 'Hello, World!', "'quoted'"]
    """

    return " ".join(_quote(arg) for arg in args)
Exemplo n.º 6
0
def format_command(args):
    r"""
    Return a list of argument strings as one shell-escaped command.

    >>> import shlex
    >>> shlex.split(format_command(["echo", "Hello, World!", "'quoted'"]))
    ['echo', 'Hello, World!', "'quoted'"]
    """

    return " ".join(_quote(arg) for arg in args)
Exemplo n.º 7
0
def create_git_hooks(configfile_path, githooks_dir):
    config = ConfigParser()
    if os.path.isfile(configfile_path):
        config.read(configfile_path)
        for section in config.sections():
            hook = config[section]
            if 'command' in hook:
                command = hook['command']
                githook_file = os.path.join(githooks_dir, section)
                with open(githook_file, 'wb') as file:
                    file.write(hook_template.format(section=section, command=repr(_quote(command))).encode())
                    st = os.stat(githook_file)
                    os.chmod(githook_file, st.st_mode | stat.S_IEXEC)
                print('{} hook successfully created for running "{}"'.format(section, command))
    else:
        message = '''
        Unable to find the ".githooks.ini" configuration file.
        Please, create it and try again.
        '''
        print(message)
        sys.exit(1)
Exemplo n.º 8
0
    def _list_tar(name, cached, decompress_cmd, failhard=False):
        '''
        List the contents of a tar archive.
        '''
        dirs = []
        files = []
        links = []
        try:
            open_kwargs = {'name': cached} \
                if not isinstance(cached, subprocess.Popen) \
                else {'fileobj': cached.stdout, 'mode': 'r|'}
            with contextlib.closing(
                    tarfile.open(**open_kwargs)) as tar_archive:
                for member in tar_archive.getmembers():
                    if member.issym():
                        links.append(member.name)
                    elif member.isdir():
                        dirs.append(member.name + '/')
                    else:
                        files.append(member.name)
            return dirs, files, links

        except tarfile.ReadError:
            if failhard:
                if isinstance(cached, subprocess.Popen):
                    stderr = cached.communicate()[1]
                    if cached.returncode != 0:
                        raise CommandExecutionError(
                            'Failed to decompress {0}'.format(name),
                            info={'error': stderr})
            else:
                if not salt.utils.path.which('tar'):
                    raise CommandExecutionError(
                        '\'tar\' command not available')
                if decompress_cmd is not None:
                    # Guard against shell injection
                    try:
                        decompress_cmd = ' '.join(
                            [_quote(x) for x in shlex.split(decompress_cmd)])
                    except AttributeError:
                        raise CommandExecutionError('Invalid CLI options')
                else:
                    if salt.utils.path.which('xz') \
                            and __salt__['cmd.retcode'](['xz', '-t', cached],
                                                        python_shell=False,
                                                        ignore_retcode=True) == 0:
                        decompress_cmd = 'xz --decompress --stdout'

                if decompress_cmd:
                    decompressed = subprocess.Popen('{0} {1}'.format(
                        decompress_cmd, _quote(cached)),
                                                    shell=True,
                                                    stdout=subprocess.PIPE,
                                                    stderr=subprocess.PIPE)
                    return _list_tar(name, decompressed, None, True)

        raise CommandExecutionError(
            'Unable to list contents of {0}. If this is an XZ-compressed tar '
            'archive, install XZ Utils to enable listing its contents. If it '
            'is compressed using something other than XZ, it may be necessary '
            'to specify CLI options to decompress the archive. See the '
            'documentation for details.'.format(name))
Exemplo n.º 9
0
    def _list_tar(name, cached, decompress_cmd, failhard=False):
        '''
        List the contents of a tar archive.
        '''
        dirs = []
        files = []
        links = []
        try:
            with contextlib.closing(tarfile.open(cached)) as tar_archive:
                for member in tar_archive.getmembers():
                    if member.issym():
                        links.append(member.name)
                    elif member.isdir():
                        dirs.append(member.name + '/')
                    else:
                        files.append(member.name)
            return dirs, files, links

        except tarfile.ReadError:
            if not failhard:
                if not salt.utils.which('tar'):
                    raise CommandExecutionError(
                        '\'tar\' command not available')
                if decompress_cmd is not None:
                    # Guard against shell injection
                    try:
                        decompress_cmd = ' '.join(
                            [_quote(x) for x in shlex.split(decompress_cmd)])
                    except AttributeError:
                        raise CommandExecutionError('Invalid CLI options')
                else:
                    if salt.utils.which('xz') \
                            and __salt__['cmd.retcode'](['xz', '-t', cached],
                                                        python_shell=False,
                                                        ignore_retcode=True) == 0:
                        decompress_cmd = 'xz --decompress --stdout'

                if decompress_cmd:
                    fd, decompressed = tempfile.mkstemp()
                    os.close(fd)
                    try:
                        cmd = '{0} {1} > {2}'.format(decompress_cmd,
                                                     _quote(cached),
                                                     _quote(decompressed))
                        result = __salt__['cmd.run_all'](cmd,
                                                         python_shell=True)
                        if result['retcode'] != 0:
                            raise CommandExecutionError(
                                'Failed to decompress {0}'.format(name),
                                info={'error': result['stderr']})
                        return _list_tar(name, decompressed, None, True)
                    finally:
                        try:
                            os.remove(decompressed)
                        except OSError as exc:
                            if exc.errno != errno.ENOENT:
                                log.warning(
                                    'Failed to remove intermediate '
                                    'decompressed archive %s: %s',
                                    decompressed, exc.__str__())

        raise CommandExecutionError(
            'Unable to list contents of {0}. If this is an XZ-compressed tar '
            'archive, install XZ Utils to enable listing its contents. If it '
            'is compressed using something other than XZ, it may be necessary '
            'to specify CLI options to decompress the archive. See the '
            'documentation for details.'.format(name))
Exemplo n.º 10
0
    def _list_tar(name, cached, decompress_cmd, failhard=False):
        """
        List the contents of a tar archive.
        """
        dirs = []
        files = []
        links = []
        try:
            open_kwargs = (
                {"name": cached}
                if not isinstance(cached, subprocess.Popen)
                else {"fileobj": cached.stdout, "mode": "r|"}
            )
            with contextlib.closing(tarfile.open(**open_kwargs)) as tar_archive:
                for member in tar_archive.getmembers():
                    _member = salt.utils.data.decode(member.name)
                    if member.issym():
                        links.append(_member)
                    elif member.isdir():
                        dirs.append(_member + "/")
                    else:
                        files.append(_member)
            return dirs, files, links

        except tarfile.ReadError:
            if failhard:
                if isinstance(cached, subprocess.Popen):
                    stderr = cached.communicate()[1]
                    if cached.returncode != 0:
                        raise CommandExecutionError(
                            "Failed to decompress {0}".format(name),
                            info={"error": stderr},
                        )
            else:
                if not salt.utils.path.which("tar"):
                    raise CommandExecutionError("'tar' command not available")
                if decompress_cmd is not None:
                    # Guard against shell injection
                    try:
                        decompress_cmd = " ".join(
                            [_quote(x) for x in shlex.split(decompress_cmd)]
                        )
                    except AttributeError:
                        raise CommandExecutionError("Invalid CLI options")
                else:
                    if (
                        salt.utils.path.which("xz")
                        and __salt__["cmd.retcode"](
                            ["xz", "-t", cached],
                            python_shell=False,
                            ignore_retcode=True,
                        )
                        == 0
                    ):
                        decompress_cmd = "xz --decompress --stdout"

                if decompress_cmd:
                    decompressed = subprocess.Popen(
                        "{0} {1}".format(decompress_cmd, _quote(cached)),
                        shell=True,
                        stdout=subprocess.PIPE,
                        stderr=subprocess.PIPE,
                    )
                    return _list_tar(name, decompressed, None, True)

        raise CommandExecutionError(
            "Unable to list contents of {0}. If this is an XZ-compressed tar "
            "archive, install XZ Utils to enable listing its contents. If it "
            "is compressed using something other than XZ, it may be necessary "
            "to specify CLI options to decompress the archive. See the "
            "documentation for details.".format(name)
        )
Exemplo n.º 11
0
def shelljoin(argv):
    return ' '.join(_quote(a) for a in argv)
Exemplo n.º 12
0
 def strcmd(self):
     """Get the stringified cmd"""
     return ' '.join(_quote(cmdpart) for cmdpart in self.cmd)