Use context manager in scripts.

pull/716/head
Elliott Sales de Andrade 9 years ago
parent aec1e88c21
commit 859c5e28df
  1. 4
      ghwt.py
  2. 107
      tools/ac_converter.py
  3. 55
      tools/cmake2meson.py

@ -52,8 +52,8 @@ def unpack(sproj, branch, outdir):
return 1
spdir = os.path.split(outdir)[0]
ofilename = os.path.join(spdir, config['wrap-file']['source_filename'])
ofile = open(ofilename, 'wb')
ofile.write(us)
with open(ofilename, 'wb') as ofile:
ofile.write(us)
if 'lead_directory_missing' in config['wrap-file']:
os.mkdir(outdir)
shutil.unpack_archive(ofilename, outdir)

@ -25,28 +25,6 @@ that are unrelated to configure checks.
import sys
print('''cc = meson.get_compiler('c')
cdata = configuration_data()''')
print('check_headers = [')
for line in open(sys.argv[1]):
line = line.strip()
if line.startswith('#mesondefine') and \
line.endswith('_H'):
token = line.split()[1]
tarr = token.split('_')[1:-1]
tarr = [x.lower() for x in tarr]
hname = '/'.join(tarr) + '.h'
print(" ['%s', '%s']," % (token, hname))
print(']\n')
print('''foreach h : check_headers
if cc.has_header(h.get(1))
cdata.set(h.get(0), 1)
endif
endforeach
''')
# Add stuff here as it is encountered.
function_data = \
@ -242,18 +220,71 @@ function_data = \
'HAVE_PTHREAD_SET_NAME_NP': ('pthread_set_name_np', 'pthread.h'),
}
print('check_functions = [')
headers = []
functions = []
sizes = []
with open(sys.argv[1]) as f:
for line in f:
line = line.strip()
arr = line.split()
# Check for headers.
if line.startswith('#mesondefine') and line.endswith('_H'):
token = line.split()[1]
tarr = token.split('_')[1:-1]
tarr = [x.lower() for x in tarr]
hname = '/'.join(tarr) + '.h'
headers.append((token, hname))
# Check for functions.
try:
token = arr[1]
if token in function_data:
fdata = function_data[token]
functions.append((token, fdata[0], fdata[1]))
elif token.startswith('HAVE_') and not token.endswith('_H'):
functions.append((token, ))
except Exception:
pass
# Check for sizeof tests.
if len(arr) != 2:
continue
elem = arr[1]
if elem.startswith('SIZEOF_'):
typename = elem.split('_', 1)[1] \
.replace('_P', '*') \
.replace('_', ' ') \
.lower() \
.replace('size t', 'size_t')
sizes.append((elem, typename))
for line in open(sys.argv[1]):
try:
token = line.split()[1]
if token in function_data:
fdata = function_data[token]
print(" ['%s', '%s', '#include<%s>']," % (token, fdata[0], fdata[1]))
elif token.startswith('HAVE_') and not token.endswith('_H'):
print('# check token', token)
except Exception:
pass
print('''cc = meson.get_compiler('c')
cdata = configuration_data()''')
# Convert header checks.
print('check_headers = [')
for token, hname in headers:
print(" ['%s', '%s']," % (token, hname))
print(']\n')
print('''foreach h : check_headers
if cc.has_header(h.get(1))
cdata.set(h.get(0), 1)
endif
endforeach
''')
# Convert function checks.
print('check_functions = [')
for token in functions:
if len(func) == 3:
token, fdata0, fdata1 = token
print(" ['%s', '%s', '#include<%s>']," % (token, fdata0, fdata1))
else:
print('# check token', token)
print(']\n')
print('''foreach f : check_functions
@ -265,14 +296,8 @@ endforeach
# Convert sizeof checks.
for line in open(sys.argv[1]):
arr = line.strip().split()
if len(arr) != 2:
continue
elem = arr[1]
if elem.startswith('SIZEOF_'):
typename = elem.split('_', 1)[1].replace('_P', '*').replace('_', ' ').lower().replace('size t', 'size_t')
print("cdata.set('%s', cc.sizeof('%s'))" % (elem, typename))
for elem, typename in size:
print("cdata.set('%s', cc.sizeof('%s'))" % (elem, typename))
print('''
configure_file(input : 'config.h.meson',

@ -252,39 +252,46 @@ class Converter:
subdir = self.cmake_root
cfile = os.path.join(subdir, 'CMakeLists.txt')
try:
cmakecode = open(cfile).read()
with open(cfile) as f:
cmakecode = f.read()
except FileNotFoundError:
print('\nWarning: No CMakeLists.txt in', subdir, '\n')
return
p = Parser(cmakecode)
outfile = open(os.path.join(subdir, 'meson.build'), 'w')
for t in p.parse():
if t.name == 'add_subdirectory':
#print('\nRecursing to subdir', os.path.join(self.cmake_root, t.args[0].value), '\n')
self.convert(os.path.join(subdir, t.args[0].value))
#print('\nReturning to', self.cmake_root, '\n')
self.write_entry(outfile, t)
with open(os.path.join(subdir, 'meson.build'), 'w') as outfile:
for t in p.parse():
if t.name == 'add_subdirectory':
# print('\nRecursing to subdir',
# os.path.join(self.cmake_root, t.args[0].value),
# '\n')
self.convert(os.path.join(subdir, t.args[0].value))
# print('\nReturning to', self.cmake_root, '\n')
self.write_entry(outfile, t)
if subdir == self.cmake_root and len(self.options) > 0:
self.write_options()
def write_options(self):
optfile = open(os.path.join(self.cmake_root, 'meson_options.txt'), 'w')
for o in self.options:
(optname, description, default) = o
if default is None:
defaultstr = ''
else:
if default == 'OFF':
typestr = ' type : boolean,'
default = 'false'
elif default == 'ON':
default = 'true'
typestr = ' type : boolean,'
filename = os.path.join(self.cmake_root, 'meson_options.txt')
with open(filename, 'w') as optfile:
for o in self.options:
(optname, description, default) = o
if default is None:
defaultstr = ''
else:
typestr = ' type : string,'
defaultstr = ' value : %s,' % default
line = "option(%s,%s%s description : '%s')\n" % (optname, typestr, defaultstr, description)
optfile.write(line)
if default == 'OFF':
typestr = ' type : boolean,'
default = 'false'
elif default == 'ON':
default = 'true'
typestr = ' type : boolean,'
else:
typestr = ' type : string,'
defaultstr = ' value : %s,' % default
line = "option(%s,%s%s description : '%s')\n" % (optname,
typestr,
defaultstr,
description)
optfile.write(line)
if __name__ == '__main__':
if len(sys.argv) != 2:

Loading…
Cancel
Save