Skip to content

Instantly share code, notes, and snippets.

@osmatsuda
Last active April 20, 2020 15:44
Show Gist options
  • Save osmatsuda/8baa0e000625ef002a4ebee5857ee001 to your computer and use it in GitHub Desktop.
Save osmatsuda/8baa0e000625ef002a4ebee5857ee001 to your computer and use it in GitHub Desktop.
import sys
import re, io
import subprocess
import concurrent.futures
from pathlib import Path
from hashlib import md5
_source_enc = 'euc-jp'
_cdbmake = '/opt/local/bin/cdbmake'
def _genCdbFormat(target):
re_commentline = re.compile("^;")
re_keyvalue = re.compile("^([^ ]+) +(.+)$")
fmt = "+{key_len},{val_len}:{key}->{val}"
def _gen():
try:
while True:
line = yield
if not re_commentline.match(line):
matched = re_keyvalue.match(line)
if matched:
key = matched.group(1)
val = matched.group(2)
print(fmt.format(key=key, key_len=len(key.encode()),
val=val, val_len=len(val.encode())),
file=target)
finally:
print('', file=target)
gen = _gen()
next(gen)
return gen
def _runCdbMake(filename, buff):
src_p = Path(filename)
tar_p = src_p.parent / (src_p.name + '.cdb')
tmp_name = md5(bytes(src_p.name, 'utf-8')).hexdigest()
tmp_p = src_p.parent / tmp_name
if tar_p.exists():
return ('[Error: '+str(tar_p)+' already exists]', 1)
done = subprocess.run([_cdbmake, str(tar_p), str(tmp_p)],
input=buff.getvalue(), text=True)
return (str(tar_p), done.returncode)
def make(filename):
with open(filename, "r", encoding=_source_enc) as source:
with io.StringIO() as target:
gen = _genCdbFormat(target)
for line in source:
gen.send(line)
gen.close()
return _runCdbMake(filename, target)
if __name__ == '__main__':
with concurrent.futures.ProcessPoolExecutor() as exe:
futures = {exe.submit(make, arg): arg for arg in sys.argv[1:]}
for future in concurrent.futures.as_completed(futures):
src = futures[future]
print('%s -> %s, exit %d' % (src, *future.result()))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment