Exemple #1
0
def open_fd(*args, **kwargs):
    """
    Wraps os.fdopen to use it inside a with statement that auto closes the
    generated file descriptor after the with block closes


    Parameters
    ----------------------------------------------------------------------------
    :param args, kwargs:
        arguments passed to os.fdopen


    :return: TextIOWrapper
    ----------------------------------------------------------------------------
        open file object connected to the file descriptor
    """
    fp = os_fdopen(*args, **kwargs)
    try:
        yield fp
    finally:
        fp.close()
Exemple #2
0
def open_fd(*args, **kwargs):
    """
    Wraps os.fdopen to use it inside a with statement that auto closes the
    generated file descriptor after the with block closes


    Parameters
    ----------------------------------------------------------------------------
    :param args, kwargs:
        arguments passed to os.fdopen


    :return: TextIOWrapper
    ----------------------------------------------------------------------------
        open file object connected to the file descriptor
    """
    fp = os_fdopen(*args, **kwargs)
    try:
        yield fp
    finally:
        fp.close()
Exemple #3
0
def silence(fd: "File descripter"):
    """ silence(fd) -> Silence any output from fd.

    """

    from os import dup as os_dup
    from os import pipe as os_pipe
    from os import dup2 as os_dup2
    from os import close as os_close
    from os import fdopen as os_fdopen

    # Backup the file
    old_fd = fd

    # Flush the file so it can be silenced properly.
    fd.flush()

    # Create a duplicate of fd
    new_fd = os_dup(fd.fileno())

    # Create a pipe to write to.
    read, write = os_pipe()

    # Set the write to the fd filenumber
    os_dup2(write, fd.fileno())

    # Close the pipe.
    os_close(write)
    os_close(read)

    # Set fd to the new fd
    fd = os_fdopen(new_fd, "w")

    try:
        # Run the commands in the 'with' statement.
        yield
    finally:
        # Return the fd back to its original state.
        os_dup2(fd.fileno(), old_fd.fileno())
        fd = old_fd
Exemple #4
0
def silence(fd):
    """ silence(fd) -> Silence any output from fd.

    """

    from os import dup as os_dup
    from os import pipe as os_pipe
    from os import dup2 as os_dup2
    from os import close as os_close
    from os import fdopen as os_fdopen

    # Backup the file
    old_fd = fd

    # Flush the file so it can be silenced properly.
    fd.flush()

    # Create a duplicate of fd
    new_fd = os_dup(fd.fileno())

    # Create a pipe to write to.
    read, write = os_pipe()

    # Set the write to the fd filenumber
    os_dup2(write, fd.fileno())

    # Close the pipe.
    os_close(write)
    os_close(read)

    # Set fd to the new fd
    fd = os_fdopen(new_fd, 'w')

    try:
        # Run the commands in the 'with' statement.
        yield
    finally:
        # Return the fd back to its original state.
        os_dup2(fd.fileno(), old_fd.fileno())
        fd = old_fd
    def save_combined_data(self,
                           pdf_data=None,
                           js_data=None,
                           pdf_path=None,
                           save_path=None):
        ## See: https://security.openstack.org/guidelines/dg_using-temporary-files-securely.html
        ##  for where the following is inspired from
        fd, tmp_path = mkstemp()
        try:
            ## Write to a tempfile, note 'b' is there to avoid warnings during write process
            with os_fdopen(fd, 'wb') as tmp:
                pdf_data.addJS(js_data)
                pdf_data.write(tmp)

            ## Figure out where to save the temp file for the calling process
            if self.clobber is True:
                save_path = pdf_path
                copyfile(tmp_path, save_path)
                message = 'overwrote exsisting: '
            else:
                if save_path is None:
                    save_path = path_join(dirname(tmp_path),
                                          basename(pdf_path))
                copyfile(tmp_path, save_path)
                message = 'enhanced file path: '

        finally:
            ## Clean up and return saved file path or throw error messages about failures
            if path_exists(tmp_path) is True:
                os_remove(tmp_path)
            else:
                error('Unable to remove temp path: {0}'.format(tmp_path))

            if path_exists(save_path) is True:
                if self.verbose and self.verbose > 0:
                    notice(message + save_path)
                return save_path
            else:
                error('Unable to make save path: {0}'.format(save_path))
def open_fd(*args, **kwargs):
    fp = os_fdopen(*args, **kwargs)
    try:
        yield fp
    finally:
        fp.close()
def open_fd(*args, **kwargs):
    fp = os_fdopen(*args, **kwargs)
    try:
        yield fp
    finally:
        fp.close()