Skip to content

Commit e409e37

Browse files
authored
Merge pull request #13 from scemama/master
Simplified Python2/3 integration
2 parents 7c86c41 + bdeac71 commit e409e37

File tree

4 files changed

+150
-112
lines changed

4 files changed

+150
-112
lines changed

Makefile

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,23 @@ CFLAGS=-O3 -fPIC -Wall -pedantic -g
1212

1313
default: libf77zmq.so libf77zmq.a f77_zmq.h
1414

15+
$(ZMQ_H):
16+
$(error $(ZMQ_H) : file not found)
17+
18+
zmq.h: $(ZMQ_H)
19+
cp $(ZMQ_H) zmq.h
20+
1521
libf77zmq.so: f77_zmq.o
1622
$(CC) -shared $^ -o $@
1723

1824
libf77zmq.a: f77_zmq.o
1925
$(AR) cr $@ $^
2026

21-
zmq.h: $(ZMQ_H)
22-
cp $(ZMQ_H) .
23-
2427
f77_zmq.o: f77_zmq.c f77_zmq.h
2528
$(CC) $(CFLAGS) -c f77_zmq.c -o $@
2629

2730
f77_zmq.h: create_f77_zmq_h.py zmq.h f77_zmq.c
28-
python2 create_f77_zmq_h.py zmq.h || python3 create_f77_zmq_h_py3.py zmq.h
31+
python create_f77_zmq_h.py zmq.h
2932

3033
clean:
3134
$(RM) -f -- f77_zmq.o f77_zmq.h

create_f77_zmq_h.py

Lines changed: 6 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -24,106 +24,12 @@
2424
# Universite Paul Sabatier - Bat. 3R1b4, 118 route de Narbonne
2525
# 31062 Toulouse Cedex 09, France
2626

27-
28-
2927
import sys
30-
import ctypes
31-
32-
# The first argument is the location of the ZeroMQ source directory
33-
34-
if len(sys.argv) != 2:
35-
print "usage: %s zmq.h"%(sys.argv[0])
36-
sys.exit(1)
37-
38-
ZMQ_H = sys.argv[1]
39-
40-
def create_lines(f):
41-
result = f.read()
42-
result = result.replace('\\\n', '')
43-
result = result.split('\n')
44-
return result
45-
46-
def create_dict_of_defines(lines,file_out):
47-
"""lines is a list of lines coming from the zmq.h"""
48-
# Fetch all parameters in zmq.h
49-
d = {}
50-
for line in lines:
51-
if line.startswith("#define"):
52-
buffer = line.split()
53-
key = buffer[1]
54-
value = " ".join(buffer[2:])
55-
if key[0] == '_' or '(' in key or ',' in value:
56-
continue
57-
command = "%(key)s=%(value)s\nd['%(key)s']=%(key)s"%locals()
58-
exec command in locals()
59-
60-
# Add the version number:
61-
d['ZMQ_VERSION'] = ZMQ_VERSION_MAJOR*10000 + ZMQ_VERSION_MINOR*100 + ZMQ_VERSION_PATCH
62-
d['ZMQ_PTR'] = ctypes.sizeof(ctypes.c_voidp)
63-
print "==========================================="
64-
print "ZMQ_PTR set to %d (for %d-bit architectures)"%(d['ZMQ_PTR'],d['ZMQ_PTR']*8)
65-
print "==========================================="
66-
67-
# Print to file
68-
keys = list( d.keys() )
69-
keys.sort()
70-
for k in keys:
71-
print >>file_out, " integer %s"%(k)
72-
for k in keys:
73-
print >>file_out, " parameter ( %-20s = %s )"%(k, d[k])
74-
return None
75-
76-
def create_prototypes(lines,file_out):
77-
"""lines is a list of lines coming from the f77_zmq.c file"""
78-
typ_conv = {
79-
'int' : 'integer' ,
80-
'float' : 'real',
81-
'char*' : 'character*(64)',
82-
'double' : 'double precision',
83-
'void*' : 'integer*%d'%(ctypes.sizeof(ctypes.c_voidp)),
84-
'void' : None
85-
}
86-
# Get all the functions of the f77_zmq.c file
87-
d = {}
88-
for line in lines:
89-
if line == "":
90-
continue
91-
if line[0] in " #{}/":
92-
continue
93-
buffer = line.replace('_(','_ (').lower().split()
94-
typ = typ_conv[buffer[0]]
95-
if typ is None:
96-
continue
97-
name = buffer[1][:-1]
98-
d[name] = typ
99-
100-
# Print to file
101-
keys = list( d.keys() )
102-
keys.sort()
103-
for k in keys:
104-
print >>file_out, " %-20s %s"%(d[k],k)
105-
print >>file_out, " %-20s %s"%("external",k)
106-
return None
107-
108-
109-
110-
def main():
111-
file_out = open('f77_zmq.h','w')
112-
113-
file_in = open( ZMQ_H, 'r' )
114-
lines = create_lines(file_in)
115-
file_in.close()
116-
117-
create_dict_of_defines(lines,file_out)
118-
119-
file_in = open( 'f77_zmq.c', 'r' )
120-
lines = create_lines(file_in)
121-
file_in.close()
122-
123-
create_prototypes(lines,file_out)
124-
125-
file_out.close()
126-
12728

12829
if __name__ == '__main__':
129-
main()
30+
if sys.version_info >= (3, 0):
31+
import create_f77_zmq_h_py3 as x
32+
else:
33+
import create_f77_zmq_h_py2 as x
34+
x.main()
35+

create_f77_zmq_h_py2.py

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
#!/usr/bin/env python2
2+
#
3+
# f77_zmq : Fortran 77 bindings for the ZeroMQ library
4+
# Copyright (C) 2014 Anthony Scemama
5+
#
6+
#
7+
# This library is free software; you can redistribute it and/or
8+
# modify it under the terms of the GNU Lesser General Public
9+
# License as published by the Free Software Foundation; either
10+
# version 2.1 of the License, or (at your option) any later version.
11+
#
12+
# This library is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15+
# Lesser General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU Lesser General Public
18+
# License along with this library; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
20+
# USA
21+
#
22+
# Anthony Scemama <[email protected]>
23+
# Laboratoire de Chimie et Physique Quantiques - UMR5626
24+
# Universite Paul Sabatier - Bat. 3R1b4, 118 route de Narbonne
25+
# 31062 Toulouse Cedex 09, France
26+
27+
28+
29+
import sys
30+
import ctypes
31+
32+
def create_lines(f):
33+
result = f.read()
34+
result = result.replace('\\\n', '')
35+
result = result.split('\n')
36+
return result
37+
38+
def create_dict_of_defines(lines,file_out):
39+
"""lines is a list of lines coming from the zmq.h"""
40+
# Fetch all parameters in zmq.h
41+
d = {}
42+
for line in lines:
43+
if line.startswith("#define"):
44+
buffer = line.split()
45+
key = buffer[1]
46+
value = " ".join(buffer[2:])
47+
if key[0] == '_' or '(' in key or ',' in value:
48+
continue
49+
command = "%(key)s=%(value)s\nd['%(key)s']=%(key)s"%locals()
50+
exec command in locals()
51+
52+
# Add the version number:
53+
d['ZMQ_VERSION'] = ZMQ_VERSION_MAJOR*10000 + ZMQ_VERSION_MINOR*100 + ZMQ_VERSION_PATCH
54+
d['ZMQ_PTR'] = ctypes.sizeof(ctypes.c_voidp)
55+
print "==========================================="
56+
print "ZMQ_PTR set to %d (for %d-bit architectures)"%(d['ZMQ_PTR'],d['ZMQ_PTR']*8)
57+
print "==========================================="
58+
59+
# Print to file
60+
keys = list( d.keys() )
61+
keys.sort()
62+
for k in keys:
63+
print >>file_out, " integer %s"%(k)
64+
for k in keys:
65+
print >>file_out, " parameter ( %-20s = %s )"%(k, d[k])
66+
return None
67+
68+
def create_prototypes(lines,file_out):
69+
"""lines is a list of lines coming from the f77_zmq.c file"""
70+
typ_conv = {
71+
'int' : 'integer' ,
72+
'float' : 'real',
73+
'char*' : 'character*(64)',
74+
'double' : 'double precision',
75+
'void*' : 'integer*%d'%(ctypes.sizeof(ctypes.c_voidp)),
76+
'void' : None
77+
}
78+
# Get all the functions of the f77_zmq.c file
79+
d = {}
80+
for line in lines:
81+
if line == "":
82+
continue
83+
if line[0] in " #{}/":
84+
continue
85+
buffer = line.replace('_(','_ (').lower().split()
86+
typ = typ_conv[buffer[0]]
87+
if typ is None:
88+
continue
89+
name = buffer[1][:-1]
90+
d[name] = typ
91+
92+
# Print to file
93+
keys = list( d.keys() )
94+
keys.sort()
95+
for k in keys:
96+
print >>file_out, " %-20s %s"%(d[k],k)
97+
print >>file_out, " %-20s %s"%("external",k)
98+
return None
99+
100+
101+
102+
def main():
103+
# The first argument is the zmq.h file
104+
105+
if len(sys.argv) != 2:
106+
print "usage: %s zmq.h"%(sys.argv[0])
107+
sys.exit(1)
108+
109+
ZMQ_H = sys.argv[1]
110+
111+
file_out = open('f77_zmq.h','w')
112+
113+
file_in = open( ZMQ_H, 'r' )
114+
lines = create_lines(file_in)
115+
file_in.close()
116+
117+
create_dict_of_defines(lines,file_out)
118+
119+
file_in = open( 'f77_zmq.c', 'r' )
120+
lines = create_lines(file_in)
121+
file_in.close()
122+
123+
create_prototypes(lines,file_out)
124+
125+
file_out.close()
126+
127+
128+
if __name__ == '__main__':
129+
main()

create_f77_zmq_h_py3.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,6 @@
2929
import sys
3030
import ctypes
3131

32-
# The first argument is the location of the ZeroMQ source directory
33-
34-
if len(sys.argv) != 2:
35-
print("usage: %s zmq.h"%(sys.argv[0]))
36-
sys.exit(1)
37-
38-
ZMQ_H = sys.argv[1]
39-
4032
def create_lines(f):
4133
result = f.read()
4234
result = result.replace('\\\n', '')
@@ -109,6 +101,14 @@ def create_prototypes(lines,file_out):
109101

110102

111103
def main():
104+
# The first argument is the zmq.h file
105+
106+
if len(sys.argv) != 2:
107+
print("usage: %s zmq.h"%(sys.argv[0]))
108+
sys.exit(1)
109+
110+
ZMQ_H = sys.argv[1]
111+
112112
file_out = open('f77_zmq.h','w')
113113

114114
file_in = open( ZMQ_H, 'r' )

0 commit comments

Comments
 (0)