File: get-python-defs

package info (click to toggle)
pyxine 0.1alpha2-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 852 kB
  • ctags: 2,017
  • sloc: ansic: 7,512; python: 2,224; cpp: 1,810; makefile: 160; sh: 40
file content (113 lines) | stat: -rwxr-xr-x 3,565 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python

# $Id: get-python-defs,v 1.1.1.1 2003/02/08 00:42:18 dairiki Exp $
#
# Copyright (C) 2003  Geoffrey T. Dairiki <dairiki@dairiki.org>
#
# This file is part of Pyxine, Python bindings for xine.
#
# Pyxine is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# Pyxine is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

"""Find the path to the python executable (version 2 or better).

Usage:
    python-config [options] [<variable name> [...]]

Options:
    --alternate-python-names=<alternates>
        A comma separted list of alternate names under which
        Python 2.x interpreters might be found.
        (Default is 'python2'.)

If no variable names are listed on the command line, all defined
variables are extracted.
"""    

import sys, os, getopt, string

    
class Main:
    # Alternate names under which to find python interpreters.
    # (Default value, may be changed in parse_args().)
    alternates = ['python2']

    # Config variables to extract.
    # (Default value, may be changed in parse_args().)
    requested_vars = []
    
    def __init__(self):
        self.parse_args()
        self.ensure_compatible_python()
        config = self.get_config()
        vars = self.requested_vars or config.keys()
        vars.sort()
        for name in vars:
            print "%-14s = %s" % (name, config[name])
        
    def parse_args(self):
        """Parse command-line args.
        """
        opts, vars = getopt.getopt(sys.argv[1:], '',
                                   ['alternate-python-names='])

        alternates = []

        for opt, val in opts:
            if opt == '--alternate-python-names':
                alternates.extend(string.split(val, ','))

        if alternates:
            self.alternates = alternates
        if vars:
            self.requested_vars = vars
        

    def check_python_version(self):
        """Raise exception if we're not running under python 2.x
        """
        if sys.version_info[0] < 2:
            raise Exception, 'Bad python version'

    def ensure_compatible_python(self):
        try:
            self.check_python_version()
        except Exception:
            alternates = self.alternates
            while alternates:
                # doesn't return upon success
                self.try_exec_self(alternates.pop(0))
            print "Can't find python (version 2.x)"
            sys.exit(1)
            
    def try_exec_self(self, python):
        """Try running ourself using an alternate python.
        """
        args = [python, sys.argv[0]]
        args.extend(['--alternate-python-names',
                     string.join(self.alternates, ',')])
        args.extend(self.requested_vars)

        try:
            os.execvp(python, args)
        except OSError:
            pass

    def get_config(self):
        from distutils.sysconfig import get_config_vars
        config = get_config_vars()
        config['PYTHON'] = sys.executable
        return config

Main()