Skip to content

Instantly share code, notes, and snippets.

@typhoon1986
Created July 18, 2013 03:51
Show Gist options
  • Save typhoon1986/6026597 to your computer and use it in GitHub Desktop.
Save typhoon1986/6026597 to your computer and use it in GitHub Desktop.
check python libs installed and version satisfied
import pkg_resources
from distutils.version import LooseVersion, StrictVersion
#LooseVersion("2.3.1") < LooseVersion("10.1.2")
import re
'''
test whether python libs are istalled with the required version
'''
libs = {
#import name, package name, required version
"Ice":("Ice", ">=3.3.1"),
"redis":("redis", ">=2.6"),
"MySQLdb":("MySQLdb", ""),
"pylibmc":("pylibmc", ">=1.2.2"),
"twisted":("twisted", ""),
"pysolr":("pysolr", ""),
"simplejson":("simplejson", ""),
"fabric":("fabric", ""),
"PIL":("PIL", ""),
"zookeeper":("zkpython", ""),
"django":("Django", "==1.4.2"),
"PyBfdCache":("", ""),
"flask":("flask", ""),
"urlgrabber":("urlgrabber", ""),
"zmq":("zmq", ""),
"protobuf":("protobuf", ""),
}
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
def disable(self):
self.HEADER = ''
self.OKBLUE = ''
self.OKGREEN = ''
self.WARNING = ''
self.FAIL = ''
self.ENDC = ''
for l in libs:
try:
__import__(l)
except Exception, e:
print bcolors.FAIL + "Not Found: %s, erro info: %s"%(l, e) + bcolors.ENDC
continue
if libs[l][0]:
try:
version = pkg_resources.get_distribution(libs[l][0]).version
except:
version = ""
else:
version = ""
version_satisfied = False
if version == "":
if libs[l][1]:
version_satisfied = False
else:
version_satisfied = True
else:
if libs[l][1]:
match = re.match(r"(?P<op>[>=<]{0,2})(?P<reqver>[0-9\.]*)", libs[l][1])
if match:
op = match.group("op")
reqver = match.group("reqver")
else:
op = ""
reqver = ""
if op:
cmd = "LooseVersion(\"%s\") %s LooseVersion(\"%s\")"%(version, op, reqver)
if eval(cmd):
version_satisfied = True
else:
version_satisfied = False
else:
version_satisfied = False
else:
version_satisfied = True
print bcolors.OKGREEN + "found: %s, version: %s, version ok: %s"%(l, version, version_satisfied) + bcolors.ENDC
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment