Combined several scripts into generate_headers.py

Signed-off-by: Ian Bell <ian.h.bell@gmail.com>
This commit is contained in:
Ian Bell
2014-06-26 01:00:26 +02:00
parent 1ee68e7a28
commit cd9009e91f

View File

@@ -10,6 +10,10 @@ import os
import sys
import json
import hashlib
import struct
import glob
json_options = {'indent' : 2, 'sort_keys' : True}
def get_hash(data):
return hashlib.sha224(data).hexdigest()
@@ -23,6 +27,68 @@ if os.path.exists(hashes_fname):
else:
hashes = dict()
# 0: Input file path relative to dev folder
# 1: Output file path relative to include folder
# 2: Name of variable
values = [
('all_fluids.json','all_fluids_JSON.h','all_fluids_JSON'),
('all_incompressibles.json','all_incompressibles_JSON.h','all_incompressibles_JSON'),
('mixtures/mixture_excess_term.json', 'mixture_excess_term_JSON.h', 'mixture_excess_term_JSON'),
('mixtures/mixture_reducing_parameters.json', 'mixture_reducing_parameters_JSON.h', 'mixture_reducing_parameters_JSON')
]
def TO_CPP(root_dir, hashes):
def to_chunks(l, n):
if n<1:
n=1
return [l[i:i+n] for i in range(0, len(l), n)]
# Normalise path name
root_dir = os.path.normpath(root_dir)
# First we package up the JSON files
combine_json(root_dir)
for infile,outfile,variable in values:
json = open(os.path.join(root_dir,'dev',infile),'r').read()
# convert each character to hex and add a terminating NULL character to end the
# string, join into a comma separated string
if sys.version_info[0] == 2:
h = [hex(struct.unpack("b",b)[0]) for b in json] + ['0x00']
else:
# Encode as ASCII characters
json = json.encode('ascii')
h = [str(hex(b)) for b in json] + [str('0x00')]
# Break up the file into lines of 16 hex characters
chunks = to_chunks(h, 16)
# Put the lines back together again
# The chunks are joined together with commas, and then EOL are used to join the rest
hex_string = ',\n'.join([', '.join(chunk) for chunk in chunks])
# Check if hash is up to date based on using variable as key
if variable not in hashes or (variable in hashes and hashes[variable] != get_hash(hex_string)):
# Generate the output string
output = '// File generated by the script dev/JSON_to_CPP.py on '+ str(datetime.now()) + '\n\n'
output += '// JSON file encoded in binary form\n'
output += 'const unsigned char '+variable+'_binary[] = {\n' + hex_string + '\n};'+'\n\n'
output += '// Combined into a single std::string \n'
output += 'std::string {v:s}({v:s}_binary, {v:s}_binary + sizeof({v:s}_binary)/sizeof({v:s}_binary[0]));'.format(v = variable)
# Write it to file
f = open(os.path.join(root_dir,'include',outfile), 'w')
f.write(output)
f.close()
# Store the hash of the data that was written to file (not including the header)
hashes[variable] = get_hash(hex_string)
else:
print(outfile + ' is up to date')
def version_to_file(root_dir):
# Get the version from the version.txt file
@@ -72,11 +138,15 @@ def gitrev_to_file(root_dir):
include_dir = os.path.join(root_dir,'include')
if p.returncode != 0:
print('tried to update git revision, but it failed for some reason (building from zip file?)')
gitrev = '???'
print('tried to get git revision from git, but could not (building from zip file?)')
gitrevision_path = os.path.join(root_dir, 'dev', 'gitrevision.txt')
if os.path.exists(gitrevision_path):
gitrev = open(gitrevision_path, 'r').read().strip()
else:
print('tried to get git revision from '+gitrevision_path+', but could not')
gitrev = '???'
else:
gitrev = stdout.strip()
print('git revision is', gitrev)
try:
is_hash = gitrev.find(' ') == -1 # python 2.x
@@ -86,6 +156,8 @@ def gitrev_to_file(root_dir):
if not is_hash:
raise ValueError('No hash returned from call to git, got '+rev+' instead')
print('git revision is', gitrev)
if 'gitrevision' not in hashes or ('gitrevision' in hashes and hashes['gitrevision'] != get_hash(gitrev)):
print('*** Generating gitrevision.h ***')
gitstring = '//Generated by the generate_headers.py script on {t:s}\n\nstd::string gitrevision = \"{rev:s}\";'.format(t = str(datetime.now()), rev = gitrev)
@@ -100,6 +172,50 @@ def gitrev_to_file(root_dir):
except (subprocess.CalledProcessError,OSError):
pass
def combine_json(root_dir):
master = []
print('*** Combining fluid JSON files in JSON format in dev folder...')
for file in glob.glob(os.path.join(root_dir,'dev','fluids','*.json')):
path, file_name = os.path.split(file)
fluid_name = file_name.split('.')[0]
# Load the fluid file
fluid = json.load(open(file, 'r'))
master += [fluid]
fp = open(os.path.join(root_dir,'dev','all_fluids_verbose.json'),'w')
fp.write(json.dumps(master, **json_options))
fp.close()
fp = open(os.path.join(root_dir,'dev','all_fluids.json'),'w')
fp.write(json.dumps(master))
fp.close()
master = []
print('*** Combining incompressible JSON files in JSON format in dev folder...')
for file in glob.glob(os.path.join(root_dir,'dev','IncompressibleLiquids','*.json')):
path, file_name = os.path.split(file)
fluid_name = file_name.split('.')[0]
# Load the fluid file
fluid = json.load(open(file, 'r'))
master += [fluid]
fp = open(os.path.join(root_dir,'dev','all_incompressibles_verbose.json'),'w')
fp.write(json.dumps(master, **json_options))
fp.close()
fp = open(os.path.join(root_dir,'dev','all_incompressibles.json'),'w')
fp.write(json.dumps(master))
fp.close()
if __name__=='__main__':
@@ -109,8 +225,7 @@ if __name__=='__main__':
version_to_file(root_dir = repo_root_path)
gitrev_to_file(root_dir = repo_root_path)
import JSON_to_CPP
JSON_to_CPP.TO_CPP(root_dir = repo_root_path, hashes = hashes)
TO_CPP(root_dir = repo_root_path, hashes = hashes)
# Write the hashes to a hashes JSON file
if hashes: