forked from ecmwf/eccodes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
memfs.py
executable file
·301 lines (235 loc) · 7.25 KB
/
memfs.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env python
from __future__ import print_function
import os
import re
import sys
import binascii
assert len(sys.argv) > 2
# For now exclude GRIB3 as it is still experimental
# The BUFR codetables is not used in the engine
# METAR and TAF are also experimental
EXCLUDED = ['grib3', 'codetables', 'taf', 'metar']
pos = 1
if sys.argv[1] == '-exclude':
product = sys.argv[2]
if product == 'bufr':
EXCLUDED.append(product)
elif product == 'grib':
EXCLUDED.extend(['grib1', 'grib2'])
else:
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)
FILES = {}
SIZES = {}
NAMES = []
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
except:
ascii = lambda x: str(x) # Python 2
def get_outfile_name(base, count):
return base + "_" + str(count).zfill(3) + ".c"
# The last argument is the base name of the generated C file(s)
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)
g = open(opath, "w")
for directory in dirs:
# print("MEMFS: directory=", directory)
dname = os.path.basename(directory)
NAMES.append(dname)
for dirpath, dirnames, files in os.walk(directory, followlinks=True):
# for ex in EXCLUDED:
# if ex in dirnames:
# print('Note: eccodes memfs.py script: %s/%s will not be included.' % (dirpath,ex))
# 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)
_, ext = os.path.splitext(full)
if ext not in ['.def', '.table', '.tmpl']:
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)
assert name not in FILES
assert name not in SIZES
FILES[name] = fname
SIZES[name] = fsize
print('const unsigned char %s[] = {' % (name,), file=g)
with open(full, 'rb') as f:
i = 0
# Python 2
#contents_hex = f.read().encode("hex")
# Python 2 and 3
contents_hex = binascii.hexlify(f.read())
# 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])
print("0x%s," % (twoChars,), end="", file=g)
i += 1
if (i % 20) == 0:
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)
g = open(opath, "w")
totsize = 0
g.close()
assert fcount == 3
opath = output_file_base + "_final.c"
print('MEMFS: Generating output: ', opath)
g = open(opath, "w")
print("""
#include "eccodes_config.h"
#ifdef ECCODES_HAVE_FMEMOPEN
#define _GNU_SOURCE
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include "eccodes_windef.h"
""", 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("""
struct entry {
const char* path;
const unsigned char* content;
size_t length;
} entries[] = { """, file=g)
items = [(v, k) for k, v in FILES.items()]
for k, v in sorted(items):
print('{"/MEMFS%s", &%s[0], sizeof(%s) / sizeof(%s[0]) },' % (k, v, v, v), file=g)
print("""};
#if defined(ECCODES_HAVE_FUNOPEN) && !defined(ECCODES_HAVE_FMEMOPEN)
typedef struct mem_file {
const char* buffer;
size_t size;
size_t pos;
} mem_file;
static int read_mem(void *data, char * buf, int len) {
mem_file* f = (mem_file*)data;
int n = len;
if(f->pos + n > f->size) {
n = f->size - f->pos;
}
memcpy(buf, f->buffer + f->pos, n);
f->pos += n;
return n;
}
static int write_mem(void* data, const char* buf, int len) {
mem_file* f = (mem_file*)data;
return -1;
}
static fpos_t seek_mem(void *data, fpos_t pos, int whence) {
mem_file* f = (mem_file*)data;
long newpos = 0;
switch (whence) {
case SEEK_SET:
newpos = (long)pos;
break;
case SEEK_CUR:
newpos = (long)f->pos + (long)pos;
break;
case SEEK_END:
newpos = (long)f->size - (long)pos;
break;
default:
return -1;
break;
}
if(newpos < 0) { newpos = 0; }
if(newpos > f->size) { newpos = f->size; }
f->pos = newpos;
return newpos;
}
static int close_mem(void *data) {
mem_file* f = (mem_file*)data;
free(f);
return 0;
}
static FILE* fmemopen(const char* buffer, size_t size, const char* mode){
mem_file* f = (mem_file*)calloc(sizeof(mem_file), 1);
if(!f) return NULL;
f->buffer = buffer;
f->size = size;
return funopen(f, &read_mem, &write_mem, &seek_mem, &close_mem);
}
#elif defined(ECCODES_ON_WINDOWS)
#include <io.h>
#include <fcntl.h>
#include <windows.h>
static FILE *fmemopen(void* buffer, size_t size, const char* mode) {
char path[MAX_PATH - 13];
if (!GetTempPath(sizeof(path), path))
return NULL;
char filename[MAX_PATH + 1];
if (!GetTempFileName(path, "eccodes", 0, filename))
return NULL;
HANDLE h = CreateFile(filename,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE,
NULL);
if (h == INVALID_HANDLE_VALUE)
return NULL;
int fd = _open_osfhandle((intptr_t)h, _O_RDWR);
if (fd < 0) {
CloseHandle(h);
return NULL;
}
FILE* f = _fdopen(fd, "w+");
if (!f) {
_close(fd);
return NULL;
}
fwrite(buffer, size, 1, f);
rewind(f);
return f;
}
#endif
static size_t entries_count = sizeof(entries)/sizeof(entries[0]);
static const unsigned char* find(const char* path, size_t* length) {
size_t i;
for(i = 0; i < entries_count; i++) {
if(strcmp(path, entries[i].path) == 0) {
/*printf("Found in MEMFS %s\\n", path);*/
*length = entries[i].length;
return entries[i].content;
}
}
return NULL;
}
int codes_memfs_exists(const char* path) {
size_t dummy;
return find(path, &dummy) != NULL;
}
FILE* codes_memfs_open(const char* path) {
size_t size;
const unsigned char* mem = find(path, &size);
if(!mem) {
return NULL;
}
return fmemopen((void*)mem, size, "r");
}
""", file=g)
print('Finished')