Search code examples
pythonpipgentoo

What is the function of this python script(python-exec tool)?


Recently I happened a strange problem.

My OS is Gentoo. I install pip and layman, but the binary file in /usr/bin: /usr/bin/pip and /usr/bin/layman, are all softlink to /usr/bin/python-exec.

% ll /usr/bin/{pip,layman}
lrwxrwxrwx 1 root root 11 Sep 18 23:51 /usr/bin/layman -> python-exec
lrwxrwxrwx 1 root root 11 Aug 16 08:14 /usr/bin/pip -> python-exec

The content of /usr/bin/python-exec:

#!/usr/bin/python2.7
# EASY-INSTALL-ENTRY-SCRIPT: 'Pygments==1.6','console_scripts','pygmentize'
__requires__ = 'Pygments==1.6'
import sys
from pkg_resources import load_entry_point

sys.exit(
   load_entry_point('Pygments==1.6', 'console_scripts', 'pygmentize')()
) 

I found this file belongs to dev-python/python-exec-0.3.1:

% equery belongs python-exec
 * Searching for python-exec ... 
dev-python/python-exec-0.3.1 (/usr/bin/python-exec)

and this package is :

*  dev-python/python-exec
      Latest version available: 0.3.1
      Latest version installed: 0.3.1
      Size of files: 72 kB
      Homepage:      https://bitbucket.org/mgorny/python-exec/
      Description:   Python script wrapper
      License:       BSD

I don't know what is the function of the /usr/bin/python-exec script?

and why the /usr/bin/pip and /usr/bin.layman will softlink to this script?

now if I want to use pip to install package or layman to manage overlays, I should use /usr/bin/pip-python2.7 and layman-python2.7.


Solution

  • I read the help information of python-exec tool.

    I think it is a pygment wapper. There are Formatters, Filters, and so on.

    So If I use:

    $ python-exec -l python -f html -o /tmp/test.file
    #this is the input#
    print 'hello world'
    ....
    

    it will write the input to /tmp/test.file, and use pygments to colorful the code.

    But why the pip and layman will softlink to it, I still don't know.

    Append:

    I have found the reason:

    for some reason, some another application overwrite the /usr/bin/python-exec. (I think maybe is pygments.)

    The right content is :

    #!/usr/bin/python-exec-c                                                                                                                             
    # vim:fileencoding=utf-8:ft=python                                               
    # (c) 2012 Michał Górny                                                          
    # Released under the terms of the 2-clause BSD license.                          
    #                                                                                
    # This is not the script you are looking for. This is just a wrapper.            
    # The actual scripts of this application were installed with -python*,           
    # -pypy* or -jython* suffixes. You are most likely looking for one               
    # of those.                                                                      
    
    from __future__ import with_statement                                            
    import os, os.path, sys                                                          
    
    try:                                                                             
        from epython import EPYTHON                                                  
    except ImportError:                                                              
        EPYTHON = os.path.basename(sys.executable)                                   
        if '' and EPYTHON.endswith(''):                                              
            EPYTHON = EPYTHON[:-len('')]                                             
    
    # In the loop:                                                                   
    # sys.argv[0] keeps the 'bare' name                                              
    # __file__ keeps the 'full' name
    
    while True:                                                                      
        __file__ = sys.argv[0] + '-' + EPYTHON                                       
    
        try:                                                                         
            kwargs = {}                                                              
            if sys.version_info[0] >= 3:                                             
                import tokenize                                                      
    
                # need to provide encoding                                           
                with open(__file__, 'rb') as f:                                      
                    kwargs['encoding'] = tokenize.detect_encoding(f.readline)[0]     
    
            with open(__file__, 'r', **kwargs) as f:                                 
                data = f.read()                                                      
        except IOError:                                                              
            # follow symlinks (if supported)                                         
            try:                                                                     
                sys.argv[0] = os.path.join(os.path.dirname(sys.argv[0]),             
                        os.readlink(sys.argv[0]))                                    
            except (OSError, AttributeError):                                        
                # no more symlinks? then it's time to fail.                          
                sys.stderr.write('This Python implementation (%s) is not supported by the script.\n'
                        % EPYTHON)                                                   
                sys.exit(127)
        else:                                                                        
            break                                                                    
    
    sys.argv[0] = __file__                                                           
    exec(data)
    

    and many binary files, such as pip, layman will all softlink to it, it is a simple wrapper.