-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_code.py
265 lines (216 loc) · 8.64 KB
/
generate_code.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python
################################################################################
# This script generates all templated code for qr_mumps.py
# It this the single one script to use before Cythonizing the qr_mumps.py
# library.
# This script is NOT automatically called by setup.py
#
# We use our internal library cygenja, using itself the Jinja2 template engine:
# http://jinja.pocoo.org/docs/dev/
################################################################################
from cygenja.generator import Generator
from jinja2 import Environment, FileSystemLoader
import os
import sys
import shutil
import argparse
import logging
import ConfigParser
################################################################################
# INIT
################################################################################
PATH = os.path.dirname(os.path.abspath(__file__))
def make_parser():
"""
Create a comment line argument parser.
Returns:
The command line parser.
"""
basename = os.path.basename(sys.argv[0])
parser = argparse.ArgumentParser(description='%s: a code generator' % basename)
parser.add_argument("-c", "--clean", help="Clean action files",
action='store_true', required=False)
parser.add_argument("-d", "--dry_run", help="Dry run: no action is taken",
action='store_true', required=False)
parser.add_argument("-f", "--force", help="Force generation no matter what",
action='store_true', required=False)
parser.add_argument("-u", "--untrack", help="Untrack files from git",
action='store_true', required=False)
parser.add_argument('dir_pattern', nargs='?', default='.',
help='Glob pattern')
parser.add_argument('file_pattern', nargs='?', default='*.*',
help='Fnmatch pattern')
return parser
# Conditionnal code generation
# ----------------------------
# type of platform? 32bits or 64bits?
is_64bits = sys.maxsize > 2**32
default_index_type_str = '32bits'
if is_64bits:
default_index_type_str = '64bits'
qr_mumps_config = ConfigParser.SafeConfigParser()
qr_mumps_config.read('site.cfg')
QR_MUMPS_INT = 'INT32'
INDEX_TYPES = [QR_MUMPS_INT]
COMPLEX_ELEMENT_TYPES = ['COMPLEX64', 'COMPLEX128']
REAL_ELEMENT_TYPES = ['FLOAT32', 'FLOAT64']
ELEMENT_TYPES = COMPLEX_ELEMENT_TYPES + REAL_ELEMENT_TYPES
GENERAL_CONTEXT = {'index_list': INDEX_TYPES,
'type_list': ELEMENT_TYPES,
'complex_list': COMPLEX_ELEMENT_TYPES}
# ACTION FUNCTION
def single_generation():
"""
Only generate one file without any suffix.
"""
yield '', GENERAL_CONTEXT
def generate_following_index_and_element():
"""
Generate files following the index and element types.
"""
for index in INDEX_TYPES:
GENERAL_CONTEXT['index'] = index
for type in ELEMENT_TYPES:
GENERAL_CONTEXT['type'] = type
yield '_%s_%s' % (index, type), GENERAL_CONTEXT
# JINJA2 FILTERS
def generic_to_qr_mumps_type(generic_type):
if generic_type in ['FLOAT32']:
return 's'
elif generic_type in ['FLOAT64']:
return 'd'
elif generic_type in ['COMPLEX64']:
return 'c'
elif generic_type in ['COMPLEX128']:
return 'z'
else:
raise TypeError("Not a recognized generic type")
def generic_to_c_type(generic_type):
if generic_type in ['INT32']:
return 'int'
elif generic_type in ['INT64']:
return 'long'
elif generic_type in ['FLOAT32']:
return 'float'
elif generic_type in ['FLOAT64']:
return 'double'
elif generic_type in ['COMPLEX64']:
return 'float complex'
elif generic_type in ['COMPLEX128']:
return 'double complex'
else:
raise TypeError("Not a recognized generic type")
def generic_to_single_double_type(generic_type):
if generic_type in ['FLOAT32', 'COMPLEX64']:
return 'float32'
elif generic_type in ['FLOAT64', 'COMPLEX128']:
return 'float64'
else:
raise TypeError("Not a recognized generic type")
def generic_to_c_single_double_type(generic_type):
if generic_type in ['FLOAT32', 'COMPLEX64']:
return 'float'
elif generic_type in ['FLOAT64', 'COMPLEX128']:
return 'double'
else:
raise TypeError("Not a recognized generic type")
# JINJA 2 environment
GENERAL_ENVIRONMENT = Environment(
autoescape=False,
loader=FileSystemLoader('/'), # we use absolute filenames
trim_blocks=True,
lstrip_blocks=True,
variable_start_string='@',
variable_end_string='@')
# LOGGER
LOG_LEVELS = {
'DEBUG': logging.DEBUG,
'INFO': logging.INFO,
'WARNING': logging.WARNING,
'ERROR': logging.ERROR,
'CRITICAL': logging.CRITICAL}
def create_logger(qr_mumps_config):
logger_name = qr_mumps_config.get('CODE_GENERATION', 'log_name')
if logger_name == '':
logger_name = 'qr_mumps_generator'
logger = logging.getLogger(logger_name)
# levels
log_level = LOG_LEVELS[qr_mumps_config.get('CODE_GENERATION',
'log_level')]
console_log_level = LOG_LEVELS[qr_mumps_config.get('CODE_GENERATION',
'console_log_level')]
file_log_level = LOG_LEVELS[qr_mumps_config.get('CODE_GENERATION',
'file_log_level')]
logger.setLevel(log_level)
# create console handler and set logging level
ch = logging.StreamHandler()
ch.setLevel(console_log_level)
# create file handler and set logging level
log_file_name = logger_name + '.log'
fh = logging.FileHandler(log_file_name)
fh.setLevel(file_log_level)
# create formatter
formatter = logging.Formatter('%(asctime)s - %(levelname)s - %(message)s')
# add formatter to ch and fh
ch.setFormatter(formatter)
fh.setFormatter(formatter)
# add ch and fh to logger
logger.addHandler(ch)
logger.addHandler(fh)
logger.info('*' * 100)
logger.info('*' * 100)
logger.info("Start some action(s)")
return logger
if __name__ == "__main__":
# command line arguments
parser = make_parser()
arg_options = parser.parse_args()
# create logger
logger = create_logger(qr_mumps_config)
# cygenja engine
current_directory = os.path.dirname(os.path.abspath(__file__))
cygenja_engine = Generator(current_directory,
GENERAL_ENVIRONMENT,
logger=logger)
# register filters
cygenja_engine.register_filter('generic_to_qr_mumps_type',
generic_to_qr_mumps_type)
cygenja_engine.register_filter('generic_to_c_type',
generic_to_c_type)
cygenja_engine.register_filter('generic_to_c_single_double_type',
generic_to_c_single_double_type)
cygenja_engine.register_filter('generic_to_single_double_type',
generic_to_single_double_type)
# register extensions
cygenja_engine.register_extension('.cpy', '.py')
cygenja_engine.register_extension('.cpx', '.pyx')
cygenja_engine.register_extension('.cpd', '.pxd')
cygenja_engine.register_extension('.cpi', '.pxi')
# register actions
cygenja_engine.register_action('config', '*.*', single_generation)
cygenja_engine.register_action('qr_mumps', '*.*',
single_generation)
cygenja_engine.register_action('qr_mumps/src', '*.*',
generate_following_index_and_element)
cygenja_engine.register_action('tests', '*.*', single_generation)
# Generation
if arg_options.dry_run:
cygenja_engine.generate(arg_options.dir_pattern,
arg_options.file_pattern,
action_ch='d',
recursively=True,
force=arg_options.force)
elif arg_options.clean:
cygenja_engine.generate(arg_options.dir_pattern,
arg_options.file_pattern,
action_ch='c',
recursively=True,
force=arg_options.force)
else:
cygenja_engine.generate(arg_options.dir_pattern,
arg_options.file_pattern,
action_ch='g',
recursively=True,
force=arg_options.force)
# special case for the setup.py file
shutil.copy2(os.path.join('config', 'setup.py'), '.')