(self, prog=None, at_exit=True)
Remove the directories containing a program from the system's
        ``$PATH``. If ``GPGBase.binary`` is in a directory being removed, it
        is linked to :file:'./gpg' in the current directory.

        :param str prog: The program to remove from ``$PATH``.
        :param bool at_exit: Add the program back into the ``$PATH`` when the
                             Python interpreter exits, and delete any symlinks
                             to ``GPGBase.binary`` which were created.
Remove the directories containing a program from the system's
        ``$PATH``. If ``GPGBase.binary`` is in a directory being removed, it
        is linked to :file:'./gpg' in the current directory.
Remove the directories containing a program from the system s $PATH . If GPGBase . binary is in a directory being removed it is linked to : file : . / gpg in the current directory .
def __remove_path__(self, prog=None, at_exit=True):
        """Remove the directories containing a program from the system's
        ``$PATH``. If ``GPGBase.binary`` is in a directory being removed, it
        is linked to :file:'./gpg' in the current directory.

        :param str prog: The program to remove from ``$PATH``.
        :param bool at_exit: Add the program back into the ``$PATH`` when the
                             Python interpreter exits, and delete any symlinks
                             to ``GPGBase.binary`` which were created.
        """
        #: A list of ``$PATH`` entries which were removed to disable pinentry.
        self._removed_path_entries = []

        log.debug("Attempting to remove %s from system PATH" % str(prog))
        if (prog is None) or (not isinstance(prog, str)): return

        try:
            program = _util._which(prog)[0]
        except (OSError, IOError, IndexError) as err:
            log.err(str(err))
            log.err("Cannot find program '%s', not changing PATH." % prog)
            return

        ## __remove_path__ cannot be an @classmethod in GPGMeta, because
        ## the use_agent attribute must be set by the instance.
        if not self.use_agent:
            program_base = os.path.dirname(prog)
            gnupg_base = os.path.dirname(self.binary)

            ## symlink our gpg binary into $PWD if the path we are removing is
            ## the one which contains our gpg executable:
            new_gpg_location = os.path.join(os.getcwd(), 'gpg')
            if gnupg_base == program_base:
                os.symlink(self.binary, new_gpg_location)
                self.binary = new_gpg_location

            ## copy the original environment so that we can put it back later:
            env_copy = os.environ            ## this one should not be touched
            path_copy = os.environ.pop('PATH')
            log.debug("Created a copy of system PATH: %r" % path_copy)
            assert not os.environ.has_key('PATH'), "OS env kept $PATH anyway!"

            @staticmethod
            def remove_program_from_path(path, prog_base):
                """Remove all directories which contain a program from PATH.

                :param str path: The contents of the system environment's
                                 ``$PATH``.

                :param str prog_base: The directory portion of a program's
                                      location, without the trailing slash,
                                      and without the program name. For
                                      example, ``prog_base='/usr/bin'``.
                """
                paths = path.split(':')
                for directory in paths:
                    if directory == prog_base:
                        log.debug("Found directory with target program: %s"
                                  % directory)
                        path.remove(directory)
                        self._removed_path_entries.append(directory)
                log.debug("Deleted all found instance of %s." % directory)
                log.debug("PATH is now:%s%s" % (os.linesep, path))
                new_path = ':'.join([p for p in path])
                return new_path

            @staticmethod
            def update_path(environment, path):
                """Add paths to the string at ``os.environ['PATH']``.

                :param str environment: The environment mapping to update.
                :param list path: A list of strings to update the PATH with.
                """
                log.debug("Updating system path...")
                os.environ = environment
                new_path = ':'.join([p for p in path])
                old = ''
                if 'PATH' in os.environ:
                    new_path = ':'.join([os.environ['PATH'], new_path])
                os.environ.update({'PATH': new_path})
                log.debug("System $PATH: %s" % os.environ['PATH'])

            modified_path = remove_program_from_path(path_copy, program_base)
            update_path(env_copy, modified_path)

            ## register an _exithandler with the python interpreter:
            atexit.register(update_path, env_copy, path_copy)

            def remove_symlinked_binary(symlink):
                if os.path.islink(symlink):
                    os.unlink(symlink)
                    log.debug("Removed binary symlink '%s'" % symlink)
            atexit.register(remove_symlinked_binary, new_gpg_location)
def __remove_path__ ( self , prog = None , at_exit = True ) : #: A list of ``$PATH`` entries which were removed to disable pinentry. self . _removed_path_entries = [ ] log . debug ( "Attempting to remove %s from system PATH" % str ( prog ) ) if ( prog is None ) or ( not isinstance ( prog , str ) ) : return try : program = _util . _which ( prog ) [ 0 ] except ( OSError , IOError , IndexError ) as err : log . err ( str ( err ) ) log . err ( "Cannot find program '%s', not changing PATH." % prog ) return ## __remove_path__ cannot be an @classmethod in GPGMeta, because ## the use_agent attribute must be set by the instance. if not self . use_agent : program_base = os . path . dirname ( prog ) gnupg_base = os . path . dirname ( self . binary ) ## symlink our gpg binary into $PWD if the path we are removing is ## the one which contains our gpg executable: new_gpg_location = os . path . join ( os . getcwd ( ) , 'gpg' ) if gnupg_base == program_base : os . symlink ( self . binary , new_gpg_location ) self . binary = new_gpg_location ## copy the original environment so that we can put it back later: env_copy = os . environ ## this one should not be touched path_copy = os . environ . pop ( 'PATH' ) log . debug ( "Created a copy of system PATH: %r" % path_copy ) assert not os . environ . has_key ( 'PATH' ) , "OS env kept $PATH anyway!" @ staticmethod def remove_program_from_path ( path , prog_base ) : """Remove all directories which contain a program from PATH.

                :param str path: The contents of the system environment's
                                 ``$PATH``.

                :param str prog_base: The directory portion of a program's
                                      location, without the trailing slash,
                                      and without the program name. For
                                      example, ``prog_base='/usr/bin'``.
                """ paths = path . split ( ':' ) for directory in paths : if directory == prog_base : log . debug ( "Found directory with target program: %s" % directory ) path . remove ( directory ) self . _removed_path_entries . append ( directory ) log . debug ( "Deleted all found instance of %s." % directory ) log . debug ( "PATH is now:%s%s" % ( os . linesep , path ) ) new_path = ':' . join ( [ p for p in path ] ) return new_path @ staticmethod def update_path ( environment , path ) : """Add paths to the string at ``os.environ['PATH']``.

                :param str environment: The environment mapping to update.
                :param list path: A list of strings to update the PATH with.
                """ log . debug ( "Updating system path..." ) os . environ = environment new_path = ':' . join ( [ p for p in path ] ) old = '' if 'PATH' in os . environ : new_path = ':' . join ( [ os . environ [ 'PATH' ] , new_path ] ) os . environ . update ( { 'PATH' : new_path } ) log . debug ( "System $PATH: %s" % os . environ [ 'PATH' ] ) modified_path = remove_program_from_path ( path_copy , program_base ) update_path ( env_copy , modified_path ) ## register an _exithandler with the python interpreter: atexit . register ( update_path , env_copy , path_copy ) def remove_symlinked_binary ( symlink ) : if os . path . islink ( symlink ) : os . unlink ( symlink ) log . debug ( "Removed binary symlink '%s'" % symlink ) atexit . register ( remove_symlinked_binary , new_gpg_location )