Black formatting (in preparation for GitHub PR 44)

This commit is contained in:
Shahram Najm 2020-10-19 14:39:11 +01:00
parent 44d1f10876
commit 612f6078f2
1 changed files with 42 additions and 33 deletions

View File

@ -9,22 +9,22 @@ assert len(sys.argv) > 2
# Exclude experimental features e.g. GRIB3 and TAF # Exclude experimental features e.g. GRIB3 and TAF
# The BUFR codetables is not used in the engine # The BUFR codetables is not used in the engine
EXCLUDED = ['grib3', 'codetables', 'taf', 'stations'] EXCLUDED = ["grib3", "codetables", "taf", "stations"]
pos = 1 pos = 1
if sys.argv[1] == '-exclude': if sys.argv[1] == "-exclude":
product = sys.argv[2] product = sys.argv[2]
if product == 'bufr': if product == "bufr":
EXCLUDED.append(product) EXCLUDED.append(product)
elif product == 'grib': elif product == "grib":
EXCLUDED.extend(['grib1', 'grib2']) EXCLUDED.extend(["grib1", "grib2"])
else: else:
assert False, 'Invalid product %s' % product assert False, "Invalid product %s" % product
pos = 3 pos = 3
dirs = [os.path.realpath(x) for x in sys.argv[pos:-1]] dirs = [os.path.realpath(x) for x in sys.argv[pos:-1]]
print('Directories: ', dirs) print("Directories: ", dirs)
print('Excluding: ', EXCLUDED) print("Excluding: ", EXCLUDED)
FILES = {} FILES = {}
SIZES = {} SIZES = {}
@ -33,8 +33,8 @@ CHUNK = 5500 * 1000 # chunk size in bytes
# Binary to ASCII function. Different in Python 2 and 3 # Binary to ASCII function. Different in Python 2 and 3
try: try:
str(b'\x23\x20','ascii') str(b"\x23\x20", "ascii")
ascii = lambda x: str(x, 'ascii') # Python 3 ascii = lambda x: str(x, "ascii") # Python 3
except: except:
ascii = lambda x: str(x) # Python 2 ascii = lambda x: str(x) # Python 2
@ -48,7 +48,7 @@ output_file_base = sys.argv[-1]
totsize = 0 # amount written totsize = 0 # amount written
fcount = 0 fcount = 0
opath = get_outfile_name(output_file_base, fcount) opath = get_outfile_name(output_file_base, fcount)
print('MEMFS: Generating output: ', opath) print("MEMFS: Generating output: ", opath)
g = open(opath, "w") g = open(opath, "w")
for directory in dirs: for directory in dirs:
@ -65,11 +65,11 @@ for directory in dirs:
# Prune the walk by modifying the dirnames in-place # Prune the walk by modifying the dirnames in-place
dirnames[:] = [dirname for dirname in dirnames if dirname not in EXCLUDED] dirnames[:] = [dirname for dirname in dirnames if dirname not in EXCLUDED]
for name in files: for name in files:
full = '%s/%s' % (dirpath, name) full = "%s/%s" % (dirpath, name)
_, ext = os.path.splitext(full) _, ext = os.path.splitext(full)
if ext not in ['.def', '.table', '.tmpl', '.list', '.txt']: if ext not in [".def", ".table", ".tmpl", ".list", ".txt"]:
continue continue
if name == 'CMakeLists.txt': if name == "CMakeLists.txt":
continue continue
fsize = os.path.getsize(full) fsize = os.path.getsize(full)
@ -77,16 +77,16 @@ for directory in dirs:
full = full.replace("\\", "/") full = full.replace("\\", "/")
fname = full[full.find("/%s/" % (dname,)) :] fname = full[full.find("/%s/" % (dname,)) :]
# print("MEMFS: Add ", fname) # print("MEMFS: Add ", fname)
name = re.sub(r'\W', '_', fname) name = re.sub(r"\W", "_", fname)
assert name not in FILES assert name not in FILES
assert name not in SIZES assert name not in SIZES
FILES[name] = fname FILES[name] = fname
SIZES[name] = fsize SIZES[name] = fsize
print('const unsigned char %s[] = {' % (name,), file=g) print("const unsigned char %s[] = {" % (name,), file=g)
with open(full, 'rb') as f: with open(full, "rb") as f:
i = 0 i = 0
# Python 2 # Python 2
# contents_hex = f.read().encode("hex") # contents_hex = f.read().encode("hex")
@ -103,12 +103,12 @@ for directory in dirs:
if (i % 20) == 0: if (i % 20) == 0:
print("", file=g) print("", file=g)
print('};', file=g) print("};", file=g)
if totsize >= CHUNK: if totsize >= CHUNK:
g.close() g.close()
fcount += 1 fcount += 1
opath = get_outfile_name(output_file_base, fcount) opath = get_outfile_name(output_file_base, fcount)
print('MEMFS: Generating output: ', opath) print("MEMFS: Generating output: ", opath)
g = open(opath, "w") g = open(opath, "w")
totsize = 0 totsize = 0
@ -117,10 +117,11 @@ g.close()
# See memfs/CMakeLists.txt # See memfs/CMakeLists.txt
assert fcount == 3, fcount assert fcount == 3, fcount
opath = output_file_base + "_final.c" opath = output_file_base + "_final.c"
print('MEMFS: Generating output: ', opath) print("MEMFS: Generating output: ", opath)
g = open(opath, "w") g = open(opath, "w")
print(""" print(
"""
#include "eccodes_config.h" #include "eccodes_config.h"
#ifdef ECCODES_HAVE_FMEMOPEN #ifdef ECCODES_HAVE_FMEMOPEN
#define _GNU_SOURCE #define _GNU_SOURCE
@ -131,18 +132,23 @@ print("""
#include <errno.h> #include <errno.h>
#include <string.h> #include <string.h>
#include "eccodes_windef.h" #include "eccodes_windef.h"
""", file=g) """,
file=g,
)
# Write extern variables with sizes # Write extern variables with sizes
for k, v in SIZES.items(): for k, v in SIZES.items():
print('extern const unsigned char %s[%d];' % (k, v), file=g) print("extern const unsigned char %s[%d];" % (k, v), file=g)
print(""" print(
"""
struct entry { struct entry {
const char* path; const char* path;
const unsigned char* content; const unsigned char* content;
size_t length; size_t length;
} entries[] = { """, file=g) } entries[] = { """,
file=g,
)
items = [(v, k) for k, v in FILES.items()] items = [(v, k) for k, v in FILES.items()]
@ -150,7 +156,8 @@ for k, v in sorted(items):
print('{"/MEMFS%s", &%s[0], sizeof(%s) / sizeof(%s[0]) },' % (k, v, v, v), file=g) print('{"/MEMFS%s", &%s[0], sizeof(%s) / sizeof(%s[0]) },' % (k, v, v, v), file=g)
print("""}; print(
"""};
#if defined(ECCODES_HAVE_FUNOPEN) && !defined(ECCODES_HAVE_FMEMOPEN) #if defined(ECCODES_HAVE_FUNOPEN) && !defined(ECCODES_HAVE_FMEMOPEN)
@ -299,6 +306,8 @@ FILE* codes_memfs_open(const char* path) {
return fmemopen((void*)mem, size, "r"); return fmemopen((void*)mem, size, "r");
} }
""", file=g) """,
file=g,
)
print('Finished') print("Finished")