Commit 958610d8 authored by Konstantinos Chatzilygeroudis's avatar Konstantinos Chatzilygeroudis
Browse files

Call extract params from function

parent 7a3fd32d
#!/usr/bin/env python
import fnmatch,re
import os
import sys
from collections import defaultdict
def make_dirlist(folder, extensions):
matches = []
for root, dirnames, filenames in os.walk(folder):
for ext in extensions:
for filename in fnmatch.filter(filenames, '*'+ext):
matches.append(os.path.join(root, filename))
return matches
def extract_namespace(level):
namespace = []
struct = ''
for i in level:
if 'namespace' in i:
d = re.findall('namespace [a-zA-Z_]+', i)
n = d[0].split(' ')[1]
namespace += [n]
if 'struct' in i:
d = re.findall('struct [a-zA-Z_]+', i)
n = d[0].split(' ')[1]
struct = n
return namespace, struct
def extract_param(line):
i = line.replace(' ', '').replace('\t', '')
k = re.split('(,|\(|\))', i)
type = k[2]
name = k[4]
value = k[6]
return type, name, value
def extract_params(fname):
params = []
f = open(fname)
level = []
defaults = []
for line in f:
if '{' in line:
level += [line];
if '}' in line:
level.pop(-1)
if 'defaults::' in line:
d = re.findall('defaults::\w+', line)[0]
dd = d.split('::')[1]
defaults += [dd]
if 'BO_PARAM(' in line and not "#define" in line:
namespace, struct = extract_namespace(level)
type, name, value = extract_param(line)
p = Param(namespace, struct, type, name, value, fname)
params += [p]
return params, defaults
class Param:
def __init__(self, namespace, struct, type, name, value, fname):
self.namespace = namespace
self.struct = struct
self.type = type
self.name = name
self.value = value
self.fname = fname
def to_str(self):
s = ''
for k in self.namespace:
s += k + ' :: '
s += self.struct
s += " -> " + self.type + ' ' + self.name + ' = ' + self.value + ' [from ' + self.fname + ']'
print s
def underline(k):
print k
s = ''
for i in range(0, len(k)):
s += '='
print s
if __name__ == "__main__":
# find the default params
dirs = make_dirlist('src/', ['.hpp'])
params = []
for fname in dirs:
p, d = extract_params(fname)
params += p
defaults = defaultdict(dict)
for i in params:
if 'defaults' in i.namespace:
defaults[i.struct][i.name] = i
# if we have a filename
if (len(sys.argv) > 1):
# find the params in the current file
p, d = extract_params(sys.argv[1])
struct_set = set()
plist = defaultdict(dict)
for i in p:
struct_set.add(i.struct)
plist[i.struct][i.name] = i
for i in d:
struct_set.add(i)
for k in struct_set:
underline(k)
for kk in defaults[k].keys():
if kk in plist[k]:
print '-', plist[k][kk].type, plist[k][kk].name, '=', plist[k][kk].value, '[defined in ' + plist[k][kk].fname + ']'
else:
print '-', defaults[k][kk].type, defaults[k][kk].name, '=', defaults[k][kk].value, '[default value, from ' + defaults[k][kk].fname + ']'
print ''
else: # no filename, print the defaults in rst (for the doc)
print "Default values"
print '------------------------'
print ''
print '.. highlight:: c++'
print ''
for k in sorted(defaults.keys()):
underline(k)
print ''
for kk in sorted(defaults[k].keys()):
print '- ``' + defaults[k][kk].type + ' ' + defaults[k][kk].name + ' = ' + defaults[k][kk].value + '`` [default value, from ' + defaults[k][kk].fname + ']'
print '\n'
......@@ -3,6 +3,7 @@ import stat
import subprocess
import time
import threading
import params
from waflib.Tools import waf_unit_test
json_ok = True
......@@ -230,21 +231,13 @@ exec @exec
retcode = subprocess.call(s, shell=True, env=None)
print "oarsub returned:" + str(retcode)
def run_script_on_folder(script, folder):
def output_params(folder):
files = [each for each in os.listdir(folder) if each.endswith('.cpp')]
output = ''
for file in files:
cmd = script + ' ' + folder + '/' + file
process = subprocess.Popen(cmd, shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
# wait for the process to terminate
out, err = process.communicate()
errcode = process.returncode
output += 'FILE: ' + folder + '/' + file + '\n\n'
output += out
output += '========================================='
output += params.get_output(folder + '/' + file)
output += '=========================================\n'
text_file = open("params.txt", "w")
text_file.write(output)
......
#!/usr/bin/env python
import fnmatch,re
import os
import sys
from collections import defaultdict
def make_dirlist(folder, extensions):
matches = []
for root, dirnames, filenames in os.walk(folder):
for ext in extensions:
for filename in fnmatch.filter(filenames, '*'+ext):
matches.append(os.path.join(root, filename))
return matches
def extract_namespace(level):
namespace = []
struct = ''
for i in level:
if 'namespace' in i:
d = re.findall('namespace [a-zA-Z_]+', i)
n = d[0].split(' ')[1]
namespace += [n]
if 'struct' in i:
d = re.findall('struct [a-zA-Z_]+', i)
n = d[0].split(' ')[1]
struct = n
return namespace, struct
def extract_param(line):
i = line.replace(' ', '').replace('\t', '')
k = re.split('(,|\(|\))', i)
type = k[2]
name = k[4]
value = k[6]
return type, name, value
def extract_params(fname):
params = []
f = open(fname)
level = []
defaults = []
for line in f:
if '{' in line:
level += [line];
if '}' in line:
level.pop(-1)
if 'defaults::' in line:
d = re.findall('defaults::\w+', line)[0]
dd = d.split('::')[1]
defaults += [dd]
if 'BO_PARAM(' in line and not "#define" in line:
namespace, struct = extract_namespace(level)
type, name, value = extract_param(line)
p = Param(namespace, struct, type, name, value, fname)
params += [p]
return params, defaults
class Param:
def __init__(self, namespace, struct, type, name, value, fname):
self.namespace = namespace
self.struct = struct
self.type = type
self.name = name
self.value = value
self.fname = fname
def to_str(self):
s = ''
for k in self.namespace:
s += k + ' :: '
s += self.struct
s += " -> " + self.type + ' ' + self.name + ' = ' + self.value + ' [from ' + self.fname + ']'
return s
def underline(k):
out = k + '\n'
s = ''
for i in range(0, len(k)):
s += '='
out += s + '\n'
return out
def get_output(file):
output = ""
# find the default params
dirs = make_dirlist('src/', ['.hpp'])
params = []
for fname in dirs:
p, d = extract_params(fname)
params += p
defaults = defaultdict(dict)
for i in params:
if 'defaults' in i.namespace:
defaults[i.struct][i.name] = i
# find the params in the current file
p, d = extract_params(file)
struct_set = set()
plist = defaultdict(dict)
for i in p:
struct_set.add(i.struct)
plist[i.struct][i.name] = i
for i in d:
struct_set.add(i)
for k in struct_set:
output += underline(k)
for kk in defaults[k].keys():
if kk in plist[k]:
output += '- ' + str(plist[k][kk].type) + ' ' + str(plist[k][kk].name) + ' = ' + str(plist[k][kk].value) + ' [defined in ' + plist[k][kk].fname + ']\n'
else:
output += '- ' + str(defaults[k][kk].type) + ' ' + str(defaults[k][kk].name) + ' = ' + str(defaults[k][kk].value) + ' [default value, from ' + defaults[k][kk].fname + ']\n'
output += '\n'
return output
......@@ -96,7 +96,7 @@ def build(bld):
for i in bld.options.exp.split(','):
print 'Building exp: ' + i
bld.recurse('exp/' + i)
limbo.run_script_on_folder('./tools/extract_params.py', 'exp/'+i)
limbo.output_params('exp/'+i)
bld.add_post_fun(limbo.summary)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment