Dealing with python version

Andrei Kulakov ak at silmarill.org
Fri Mar 29 04:54:25 EST 2002


In article <a7vfo5$13p$1 at panix1.panix.com>, Aahz wrote:
> In article <slrnaa5hul.7ve.ak at ak.silmarill.org>,
> Andrei Kulakov  <ak at silmarill.org> wrote:
>>
>>IOW, this should be automated. Hadn't anyone done this yet? Is there
>>some particular reason why this is hard to implement?
> 
> I would say it's the opposite: it's so easy to do that nobody has
> bothered creating a generic solution.
>
Hmm.. I cooked up a script just now that does that, and it came out at
47 lines. Am I missing an easier way to do this? Here it is:


#!/usr/bin/env python

import os, sys, string

script = "test.py"      # script to run
version = (2,2)         # python version to use (or higher)
use_ver = None

possible_versions = ((1,5), (1,6), (2,0), (2,1), (2,2))

if version not in possible_versions:
    print "Error: Invalid python version specified."
    sys.exit()

def str_ver(version):
    """Return version converted to a string."""
    s = '%d'*len(version)
    s = string.join(s, '.')
    return s % version

def exists(version):
    """Return 1 if version is present on system, 0 if not."""
    return not os.system("which python%s > /dev/null" % 
        str_ver(version))

if exists(version):
    use_ver = version
else:
    usable_versions = []
    for v in possible_versions:
        if v >= version:
            usable_versions.append(v)
    available_versions = []
    for v in usable_versions:
        if exists(v):
            available_versions.append(v)
    if available_versions:
        use_ver = max(available_versions)
    else:
        print """
        Error: No suitable python version is available on 
        this system. Please install python version %s or higher.""" % \
            str_ver(version)

if use_ver:
    args = string.join(sys.argv[1:], ' ')
    os.system("python%s %s %s" % (str_ver(use_ver), script, args))


-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: cy.silmarill.org



More information about the Python-list mailing list