Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tickets/DM-5018: Modernize Python code #1

Merged
merged 1 commit into from
Feb 9, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
50 changes: 28 additions & 22 deletions scripts/check_python_module
Original file line number Diff line number Diff line change
Expand Up @@ -5,48 +5,54 @@
# usage: check_python_module <module> <min_version>
#

import sys, os.path, importlib, argparse
from __future__ import print_function
import sys
import importlib
import argparse

parser = argparse.ArgumentParser(description="Check presence and version of a Python module")
parser.add_argument("module", help="python module to check")
parser.add_argument("min_version", help="minimum version")
parser.add_argument("-v", "--verbose", action="store_true")
args = parser.parse_args()

modname=args.module
version=args.min_version
modname = args.module
version = args.min_version

#
# See if numpy is installed in the first place
#
try:
module = importlib.import_module(modname)
module = importlib.import_module(modname)
except ImportError:
print >>sys.stderr, "error: %(module)s is not installed. please install %(module)s v%(needver)s or later and try again." % {
'module' : modname,
'needver' : version
}
exit(-1)
print("error: %(module)s is not installed."
" Please install %(module)s v%(needver)s or later and try again." % {
'module': modname,
'needver': version
},
file=sys.stderr)
exit(-1)

#
# Check version compatibility
#
needver = map(int, version.split('.'))
havever = map(int, module.__version__.split('.')) + [ 0 ] * len(needver)
needver = list(map(int, version.split('.')))
havever = list(map(int, module.__version__.split('.'))) + [0] * len(needver)
for a, b in zip(needver, havever):
if a > b:
print >>sys.stderr, "error: %(module)s version too old (v%(havever)s). please install %(module)s v%(needver)s or later and try again." % {
'module' : modname,
'havever' : module.__version__,
'needver' : version
}
exit(-1)
elif a < b:
break
if a > b:
print("error: %(module)s version too old (v%(havever)s)."
" Please install %(module)s v%(needver)s or later and try again." % {
'module': modname,
'havever': module.__version__,
'needver': version
},
file=sys.stderr)
exit(-1)
elif a < b:
break

if args.verbose:
print "Using externally provided %s v%s." % (modname, module.__version__)
# print "Located in '%s'." % os.path.dirname(module.__file__)
print("Using externally provided %s v%s." % (modname, module.__version__))

# OK.
exit(0)