Skip to content

Instantly share code, notes, and snippets.

@milliams
Last active March 14, 2016 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milliams/818974e1d9135b24d7f8 to your computer and use it in GitHub Desktop.
Save milliams/818974e1d9135b24d7f8 to your computer and use it in GitHub Desktop.
A wrapper script to try to start Ganga with Python 2.7
#! /usr/bin/env python
"""
This script will launch Ganga with an appropriate interpreter
"""
from __future__ import print_function
import os
import re
import subprocess
import sys
try: # py3
from shlex import quote
except ImportError: # py2
from pipes import quote
def get_python(commands, version):
"""
Args:
commands: a list of python interpreters
version: a 2-element version tuple
Returns:
str: the first interpreter from ``commands`` that fulfils ``version``
"""
version_match = re.compile(r'(\d*)\.(\d*)\.(\d*)')
for command in commands:
try:
p = subprocess.Popen(' '.join([command, '--version']), shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout, stderr = p.communicate()
except OSError:
continue # Command not found
v = version_match.search(stdout)
if not v:
continue # Could not match version
if int(v.group(1)) == version[0] and int(v.group(2)) == version[1]:
return command
wanted_version = (2, 7) # The required version of Python to run
ganga_script = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'ganga') # The application entry_point
args = [quote(arg) for arg in sys.argv[1:]] # Arguments passed in
if sys.version_info[0] == wanted_version[0] and sys.version_info[1] == wanted_version[1]:
print('Launching Ganga with this interpreter')
execfile(ganga_script)
else:
pythons = (
'python', # Try this first to match defaults and virtualenvs
'python2.7', # If ``python`` is 2.6 this may exist
'scl enable python27 -- python', # Try a RedHat SCL
'/cvmfs/sft.cern.ch/lcg/external/Python/2.7.4/x86_64-slc6-gcc48-opt/bin/python',
)
python = get_python(pythons, wanted_version)
if python is None:
print('Could not find a suitable Python interpreter. Tried', pythons)
exit(1)
print('Launching Ganga with', python)
rc = subprocess.call(' '.join([python, ganga_script, args]), shell=True)
exit(rc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment