Commit 721a517652174daeb2b4f11dd7b79bb8589e0549

Authored by Jürgen Knödlseder
1 parent 551763bd

Make csinfo compatible to Python 2.x and Python 3.x

Showing 1 changed file with 48 additions and 21 deletions
cscripts/csinfo.py
... ... @@ -20,7 +20,18 @@
20 20 # ==========================================================================
21 21 import sys
22 22 import os
23   -import commands
  23 +
  24 +
  25 +def get_command_output(cmd):
  26 + """Utility function to get command output on old and new Python versions.
  27 + """
  28 + try:
  29 + from subprocess import getoutput
  30 + except ImportError:
  31 + from commands import getoutput
  32 +
  33 + return getoutput(cmd)
  34 +
24 35  
25 36 # Copied and edited a bit from the `doc/source/reference_manual/index.rst` file
26 37 # The list there and here needs to be manually updated and kept in sync.
... ... @@ -116,52 +127,68 @@ def csinfo_setup_check():
116 127 ctools_python = True
117 128  
118 129 if all_ok:
119   - print('\n Your Gammalib / ctools setup is OK.')
  130 + print('\n ===> Your Gammalib / ctools setup is OK.')
120 131 else:
121   - print('\n WARNING: Your Gammalib / ctools setup is NOT OK!\n')
  132 + print('\n ===> WARNING: Your Gammalib / ctools setup is NOT OK!\n')
122 133 if gammalib_environ:
123 134 print(' - Have you set the GAMMALIB environment variable?')
124   - print(' - Did you source gammalib-init.sh?')
125 135 if ctools_environ:
126 136 print(' - Have you set the CTOOLS environment variable?')
127   - print(' - Did you source ctools-init.sh ?')
128 137 if gammalib_python or ctools_python:
  138 + print(' - Did you source gammalib-init.sh?')
  139 + print(' - Did you source ctools-init.sh ?')
129 140 print(' - Are you using the correct Python ?')
130 141  
  142 + print('')
  143 + print(' Gammalib: http://cta.irap.omp.eu/gammalib-devel/')
  144 + print(' ctools: http://cta.irap.omp.eu/ctools-devel/')
  145 +
131 146 print('')
132 147  
  148 +
133 149 def csinfo_setup_info():
134 150 print('\nGammalib / ctools setup info:\n')
135 151  
136   - log(' CTOOLS environment variable ... ', end='')
137   - try:
138   - ctools_env = os.environ['CTOOLS']
139   - print(' $CTOOLS = {0}'.format(ctools_env))
140   - except KeyError:
141   - print('WARNING: $CTOOLS environment variable not set. Did you source ctools-init.sh?')
142   - all_ok = False
  152 + gammalib_version = 'TODO' # a function needs to be added to the library to get the version
  153 + print(' Gammalib version ................. {0}'.format(gammalib_version))
  154 +
  155 + ctools_version = 'TODO' # a function needs to be added to the library to get the version
  156 + print(' ctools version ................. {0}'.format(ctools_version))
  157 +
  158 + gammalib_env = os.environ.get('GAMMALIB', 'Not found')
  159 + print(' $GAMMALIB environment variable ... {0}'.format(gammalib_env))
143 160  
  161 + ctools_env = os.environ.get('CTOOLS', 'Not found')
  162 + print(' $CTOOLS environment variable ... {0}'.format(ctools_env))
  163 +
  164 + print(' Python executable ................ {0}'.format(sys.executable))
  165 +
  166 + log(' gammalib Python package ......... ', end='')
144 167 try:
145 168 import gammalib
146   - print(' Python `gammalib` path: {0}'.format(gammalib.__path__[0]))
147   - except:
148   - print('WARNING: Python `gammalib` import failed. Are you using the right Python?')
  169 + print(gammalib.__path__[0])
  170 + except ImportError:
  171 + print('Not found')
149 172  
  173 + log(' ctools Python package ........ ', end='')
150 174 try:
151 175 import ctools
152   - print(' Python `ctools` path: {0}'.format(ctools.__path__[0]))
153   - except:
154   - print('WARNING: Python `ctools` import failed. Are you using the right Python?')
  176 + print(ctools.__path__[0])
  177 + except ImportError:
  178 + print('Not found')
  179 +
  180 + print(' GAMMALIB CFLAGS ................. {0}'.format(get_pkg_config_info('cflags', 'gammalib')))
  181 + print(' CTOOLS CFLAGS ................. {0}'.format(get_pkg_config_info('cflags', 'ctools')))
155 182  
156   - print(' GAMMALIB CFLAGS: {0}'.format(get_pkg_config_info('cflags', 'gammalib')))
157   - print(' CTOOLS CFLAGS: {0}'.format(get_pkg_config_info('cflags', 'ctools')))
  183 + print(' GAMMALIB LIBS ................. {0}'.format(get_pkg_config_info('libs', 'gammalib')))
  184 + print(' CTOOLS LIBS ................. {0}'.format(get_pkg_config_info('libs', 'ctools')))
158 185  
159 186 print('')
160 187  
161 188  
162 189 def get_pkg_config_info(info, library):
163 190 cmd = 'pkg-config --{0} {1}'.format(info, library)
164   - out = commands.getoutput(cmd)
  191 + out = get_command_output(cmd)
165 192 if 'was not found' in out:
166 193 return 'Not available'
167 194 else:
... ...