Skip to content

Instantly share code, notes, and snippets.

@minrk
Created January 27, 2011 04:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save minrk/798081 to your computer and use it in GitHub Desktop.
Save minrk/798081 to your computer and use it in GitHub Desktop.
compare constants in zmq.h to czmq.pxd
#!/usr/bin/env python
"""A simply script to scrape zmq.h and zmq/core/czmq.pxd for constants,
and compare the two. This can be used to check for updates to zmq.h
that pyzmq may not yet match.
"""
from __future__ import with_statement
import os
import sys
import re
try:
from configparser import ConfigParser
except:
from ConfigParser import ConfigParser
pjoin = os.path.join
IGNORED = [] #['ZMQ_EXPORT','ZMQ_MAKE_VERSION']
def include_dirs_from_path():
"""Check the exec path for include dirs."""
include_dirs = []
for p in os.environ['PATH'].split(os.path.pathsep):
if p.endswith('/'):
p = p[:-1]
if p.endswith('bin'):
include_dirs.append(p[:-3]+'include')
return include_dirs
def default_include_dirs():
"""Default to just /usr/local/include:/usr/include"""
return ['/usr/local/include', '/usr/include']
def find_zmq_h():
"""check setup.cfg, then /usr/local/include, then /usr/include for zmq.h.
Then scrape zmq.h for the version tuple.
Returns
-------
((major,minor,patch), "/path/to/zmq.h")"""
include_dirs = []
if os.path.exists('setup.cfg'):
cfg = ConfigParser()
cfg.read('setup.cfg')
if 'build_ext' in cfg.sections():
items = cfg.items('build_ext')
for name,val in items:
if name == 'include_dirs':
include_dirs = val.split(os.path.pathsep)
if not include_dirs:
include_dirs = default_include_dirs()
for include in include_dirs:
zmq_h = pjoin(include, 'zmq.h')
if os.path.isfile(zmq_h):
return zmq_h
raise IOError("Couldn't find zmq.h")
def extract_zmq_constants(zmq_h):
constants = set()
with open(zmq_h) as f:
for line in f.readlines():
m = re.findall(r'#\W*define\W+[A-Z][A-Z_]+', line)
if m:
name = m[0].split()[-1]
if all([ re.match(ign, name) is None for ign in IGNORED ]):
constants.add(name)
else:
pass
# print 'skipping', line
return constants
def extract_pyx_constants():
constants = set()
with open(pjoin('zmq','core','czmq.pxd')) as f:
line = f.readline()
# strip until zmq.h scope:
while not re.match(r'cdef\W+extern\W+from\W+"zmq\.h"',line):
line = f.readline()
for line in f.readlines():
if re.match('\W', line) is None:
# exited scope:
return constants
if not line.strip().startswith('enum:'):
continue
m = re.findall(r'[A-Z][A-Z_]+', line)
if m:
name = m[-1]
constants.add(name)
return constants
def check_constants(zmq_h):
"""Check that zmq.h has an appropriate version."""
h = extract_zmq_constants(zmq_h)
x = extract_pyx_constants()
print ("%i constants in %s"%(len(h),zmq_h))
print ("%i constants in zmq.core.constants"%len(x))
print ("Only in zmq.h: %s"%list(h.difference(x)))
print ("Only in core.constants: %s"%list(x.difference(h)))
if __name__ == '__main__':
check_constants(find_zmq_h())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment