mirror of
https://github.com/CoolProp/CoolProp.git
synced 2026-04-23 03:00:17 -04:00
Implemented the hash checking of generate_headers.py to avoid rebuilding unnecessarily (https://github.com/CoolProp/CoolProp/issues/34)
Signed-off-by: Ian bell <ian.h.bell@gmail.com>
This commit is contained in:
@@ -6,6 +6,7 @@ import struct
|
||||
import os
|
||||
import argparse, textwrap
|
||||
import sys
|
||||
import generate_headers
|
||||
|
||||
# 0: Input file path relative to dev folder
|
||||
# 1: Output file path relative to include folder
|
||||
@@ -16,7 +17,7 @@ values = [
|
||||
('mixtures/mixture_reducing_parameters.json', 'mixture_reducing_parameters_JSON.h', 'mixture_reducing_parameters_JSON')
|
||||
]
|
||||
|
||||
def TO_CPP(root_dir):
|
||||
def TO_CPP(root_dir, hashes):
|
||||
def to_chunks(l, n):
|
||||
if n<1:
|
||||
n=1
|
||||
@@ -48,17 +49,26 @@ def TO_CPP(root_dir):
|
||||
# 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] != generate_headers.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)
|
||||
|
||||
f = open(os.path.join(root_dir,'include',outfile), 'w')
|
||||
f.write(output)
|
||||
f.close()
|
||||
# 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] = generate_headers.get_hash(hex_string)
|
||||
else:
|
||||
print(outfile + 'is up to date')
|
||||
|
||||
if __name__=='__main__':
|
||||
parser = argparse.ArgumentParser(
|
||||
|
||||
@@ -8,33 +8,50 @@ from datetime import datetime
|
||||
import subprocess
|
||||
import os
|
||||
import sys
|
||||
import json
|
||||
import hashlib
|
||||
|
||||
def get_hash(data):
|
||||
return hashlib.sha224("Nobody inspects the spammish repetition").hexdigest()
|
||||
|
||||
# Load up the hashes of the data that will be written to each file
|
||||
if os.path.exists('hashes.json'):
|
||||
hashes = json.load(open('hashes.json','r'))
|
||||
else:
|
||||
hashes = dict()
|
||||
|
||||
def version_to_file(root_dir):
|
||||
print('*** Generating cpversion.h ***')
|
||||
|
||||
# Get the version from the version.txt file
|
||||
version = open(os.path.join(root_dir,'version.txt'),'r').read().strip()
|
||||
|
||||
# Format the string to be written
|
||||
string_for_file = '//Generated by the generate_headers.py script on {t:s}\n\nstatic char version [] ="{v:s}";'.format(t = str(datetime.now()),v = version)
|
||||
|
||||
# Include path relative to the root
|
||||
include_dir = os.path.join(root_dir,'include')
|
||||
|
||||
# The name of the file to be written into
|
||||
file_name = os.path.join(include_dir,'cpversion.h')
|
||||
|
||||
# Write to file
|
||||
f = open(file_name,'w')
|
||||
f.write(string_for_file)
|
||||
f.close()
|
||||
# Get the hash of the version
|
||||
if 'version' not in hashes or ('version' in hashes and hashes['version'] != get_hash(version)):
|
||||
print('*** Generating cpversion.h ***')
|
||||
hashes['version'] = get_hash(version)
|
||||
|
||||
# Format the string to be written
|
||||
string_for_file = '//Generated by the generate_headers.py script on {t:s}\n\nstatic char version [] ="{v:s}";'.format(t = str(datetime.now()),v = version)
|
||||
|
||||
# Include path relative to the root
|
||||
include_dir = os.path.join(root_dir,'include')
|
||||
|
||||
# The name of the file to be written into
|
||||
file_name = os.path.join(include_dir,'cpversion.h')
|
||||
|
||||
# Write to file
|
||||
f = open(file_name,'w')
|
||||
f.write(string_for_file)
|
||||
f.close()
|
||||
else:
|
||||
print('cpversion.h is up to date')
|
||||
|
||||
def gitrev_to_file(root_dir):
|
||||
"""
|
||||
If a git repo, use git to update the gitrevision. If not a git repo, read
|
||||
the gitrevision from the gitrevision.txt file. Otherwise, fail.
|
||||
"""
|
||||
print('*** Generating gitrevision.h ***')
|
||||
|
||||
|
||||
try:
|
||||
try:
|
||||
subprocess.check_call('git --version', shell=True)
|
||||
@@ -53,28 +70,31 @@ def gitrev_to_file(root_dir):
|
||||
|
||||
if p.returncode != 0:
|
||||
print('tried to update git revision, but it failed for some reason (building from zip file?)')
|
||||
gitstring = '//Generated by the generate_headers.py script on {t:s}\n\n std::string gitrevision = \"???????\";'
|
||||
f = open(os.path.join(include_dir,'gitrevision.h'),'w')
|
||||
f.write(gitstring)
|
||||
f.close()
|
||||
return
|
||||
gitrev = '???'
|
||||
else:
|
||||
rev = stdout.strip()
|
||||
print('git revision is', rev)
|
||||
|
||||
gitstring = '//Generated by the generate_headers.py script on {t:s}\n\nstd::string gitrevision = \"{rev:s}\";'.format(t = str(datetime.now()), rev = rev)
|
||||
gitrev = stdout.strip()
|
||||
print('git revision is', gitrev)
|
||||
|
||||
try:
|
||||
is_hash = rev.find(' ') == -1 # python 2.x
|
||||
is_hash = gitrev.find(' ') == -1 # python 2.x
|
||||
except TypeError:
|
||||
is_hash = ' ' not in str(rev) # python 3.x
|
||||
is_hash = ' ' not in str(gitrev) # python 3.x
|
||||
|
||||
if not is_hash:
|
||||
raise ValueError('No hash returned from call to git, got '+rev+' instead')
|
||||
|
||||
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)
|
||||
|
||||
f = open(os.path.join(include_dir,'gitrevision.h'),'w')
|
||||
f.write(gitstring)
|
||||
f.close()
|
||||
|
||||
hashes['gitrevision'] = get_hash(gitrev)
|
||||
else:
|
||||
print('gitrevision.h is up to date')
|
||||
|
||||
|
||||
except (subprocess.CalledProcessError,OSError):
|
||||
pass
|
||||
@@ -83,14 +103,15 @@ if __name__=='__main__':
|
||||
path = os.path.abspath(__file__)
|
||||
path = os.path.dirname(path)
|
||||
path = os.path.dirname(path)
|
||||
|
||||
# if os.path.exists(os.path.join(path, ".JSON_done")):
|
||||
# sys.exit()
|
||||
|
||||
version_to_file(root_dir = path)
|
||||
gitrev_to_file(root_dir = path)
|
||||
import JSON_to_CPP
|
||||
JSON_to_CPP.TO_CPP(root_dir = path)
|
||||
JSON_to_CPP.TO_CPP(root_dir = path, hashes = hashes)
|
||||
|
||||
open(os.path.join(path, ".JSON_done"), "w").close()
|
||||
# Write the hashes to a hashes JSON file
|
||||
if hashes:
|
||||
fp = open('hashes.json','w')
|
||||
fp.write(json.dumps(hashes))
|
||||
fp.close()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user