diff --git a/memfs.py b/memfs.py index 642534d2e..f3864a992 100755 --- a/memfs.py +++ b/memfs.py @@ -9,22 +9,22 @@ assert len(sys.argv) > 2 # Exclude experimental features e.g. GRIB3 and TAF # The BUFR codetables is not used in the engine -EXCLUDED = ['grib3', 'codetables', 'taf', 'stations'] +EXCLUDED = ["grib3", "codetables", "taf", "stations"] pos = 1 -if sys.argv[1] == '-exclude': +if sys.argv[1] == "-exclude": product = sys.argv[2] - if product == 'bufr': + if product == "bufr": EXCLUDED.append(product) - elif product == 'grib': - EXCLUDED.extend(['grib1', 'grib2']) + elif product == "grib": + EXCLUDED.extend(["grib1", "grib2"]) else: - assert False, 'Invalid product %s' % product + assert False, "Invalid product %s" % product pos = 3 dirs = [os.path.realpath(x) for x in sys.argv[pos:-1]] -print('Directories: ', dirs) -print('Excluding: ', EXCLUDED) +print("Directories: ", dirs) +print("Excluding: ", EXCLUDED) FILES = {} SIZES = {} @@ -33,10 +33,10 @@ CHUNK = 5500 * 1000 # chunk size in bytes # Binary to ASCII function. Different in Python 2 and 3 try: - str(b'\x23\x20','ascii') - ascii = lambda x: str(x, 'ascii') # Python 3 + str(b"\x23\x20", "ascii") + ascii = lambda x: str(x, "ascii") # Python 3 except: - ascii = lambda x: str(x) # Python 2 + ascii = lambda x: str(x) # Python 2 def get_outfile_name(base, count): @@ -48,7 +48,7 @@ output_file_base = sys.argv[-1] totsize = 0 # amount written fcount = 0 opath = get_outfile_name(output_file_base, fcount) -print('MEMFS: Generating output: ', opath) +print("MEMFS: Generating output: ", opath) g = open(opath, "w") for directory in dirs: @@ -65,31 +65,31 @@ for directory in dirs: # Prune the walk by modifying the dirnames in-place dirnames[:] = [dirname for dirname in dirnames if dirname not in EXCLUDED] for name in files: - full = '%s/%s' % (dirpath, name) + full = "%s/%s" % (dirpath, name) _, ext = os.path.splitext(full) - if ext not in ['.def', '.table', '.tmpl', '.list', '.txt']: + if ext not in [".def", ".table", ".tmpl", ".list", ".txt"]: continue - if name == 'CMakeLists.txt': + if name == "CMakeLists.txt": continue fsize = os.path.getsize(full) totsize += fsize full = full.replace("\\", "/") - fname = full[full.find("/%s/" % (dname,)):] - #print("MEMFS: Add ", fname) - name = re.sub(r'\W', '_', fname) + fname = full[full.find("/%s/" % (dname,)) :] + # print("MEMFS: Add ", fname) + name = re.sub(r"\W", "_", fname) assert name not in FILES assert name not in SIZES FILES[name] = fname 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 # Python 2 - #contents_hex = f.read().encode("hex") + # contents_hex = f.read().encode("hex") # Python 2 and 3 contents_hex = binascii.hexlify(f.read()) @@ -97,18 +97,18 @@ for directory in dirs: # Read two characters at a time and convert to C hex # e.g. 23 -> 0x23 for n in range(0, len(contents_hex), 2): - twoChars = ascii(contents_hex[n:n + 2]) + twoChars = ascii(contents_hex[n : n + 2]) print("0x%s," % (twoChars,), end="", file=g) i += 1 if (i % 20) == 0: print("", file=g) - print('};', file=g) + print("};", file=g) if totsize >= CHUNK: g.close() fcount += 1 opath = get_outfile_name(output_file_base, fcount) - print('MEMFS: Generating output: ', opath) + print("MEMFS: Generating output: ", opath) g = open(opath, "w") totsize = 0 @@ -117,10 +117,11 @@ g.close() # See memfs/CMakeLists.txt assert fcount == 3, fcount opath = output_file_base + "_final.c" -print('MEMFS: Generating output: ', opath) +print("MEMFS: Generating output: ", opath) g = open(opath, "w") -print(""" +print( + """ #include "eccodes_config.h" #ifdef ECCODES_HAVE_FMEMOPEN #define _GNU_SOURCE @@ -131,18 +132,23 @@ print(""" #include #include #include "eccodes_windef.h" -""", file=g) +""", + file=g, +) # Write extern variables with sizes 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 { const char* path; const unsigned char* content; size_t length; -} entries[] = { """, file=g) +} entries[] = { """, + file=g, +) 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("""}; +print( + """}; #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"); } -""", file=g) +""", + file=g, +) -print('Finished') +print("Finished")