asn1compile: enabled use of load_mod.txt and improve utf-8 handling with Python 2
This commit is contained in:
parent
9ed5917876
commit
c13581a97c
|
@ -39,6 +39,7 @@ from pycrate_asn1c.proc import compile_text, compile_spec, compile_all, \
|
||||||
# inputs:
|
# inputs:
|
||||||
# compile any single file
|
# compile any single file
|
||||||
# compile all .asn or .asn1 files into a directory
|
# compile all .asn or .asn1 files into a directory
|
||||||
|
# -> take load_mod.txt into account
|
||||||
# compile a given spec (by shortname)
|
# compile a given spec (by shortname)
|
||||||
# compile all specs from asndir
|
# compile all specs from asndir
|
||||||
|
|
||||||
|
@ -49,11 +50,36 @@ from pycrate_asn1c.proc import compile_text, compile_spec, compile_all, \
|
||||||
# output:
|
# output:
|
||||||
# destination file or directory
|
# destination file or directory
|
||||||
|
|
||||||
|
|
||||||
|
python_version = sys.version_info[0]
|
||||||
|
|
||||||
|
|
||||||
def print_specnames():
|
def print_specnames():
|
||||||
print('%s, valid specification names:' % sys.argv[0])
|
print('%s, valid specification names:' % sys.argv[0])
|
||||||
for k, v in ASN_SPECS.items():
|
for k, v in ASN_SPECS.items():
|
||||||
print(' %s (%s)' % (k, v))
|
print(' %s (%s)' % (k, v))
|
||||||
|
|
||||||
|
|
||||||
|
def get_mod_wl(fn):
|
||||||
|
ret = []
|
||||||
|
try:
|
||||||
|
fd = open(fn)
|
||||||
|
except:
|
||||||
|
print('unable to read %s, ignoring it')
|
||||||
|
return ret
|
||||||
|
else:
|
||||||
|
try:
|
||||||
|
for l in fd.readlines():
|
||||||
|
if len(l) > 1 and l[0] != '#':
|
||||||
|
ret.append(l[:-1].strip())
|
||||||
|
except:
|
||||||
|
print('unable to read %s, ignoring it')
|
||||||
|
fd.close()
|
||||||
|
return ret
|
||||||
|
else:
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
|
||||||
parser = argparse.ArgumentParser(description='compile ASN.1 input file(s) for the pycrate ASN.1 runtime')
|
parser = argparse.ArgumentParser(description='compile ASN.1 input file(s) for the pycrate ASN.1 runtime')
|
||||||
|
@ -99,25 +125,34 @@ def main():
|
||||||
# compile_spec(shortname=args.spec, **ckw)
|
# compile_spec(shortname=args.spec, **ckw)
|
||||||
#
|
#
|
||||||
if args.input:
|
if args.input:
|
||||||
fn = []
|
files = []
|
||||||
for i in args.input:
|
for i in args.input:
|
||||||
if os.path.isdir(i):
|
if os.path.isdir(i):
|
||||||
|
fn, wl = [], []
|
||||||
# get all potential .asn / .asn1 / .ASN / .ASN1 files from the dir
|
# get all potential .asn / .asn1 / .ASN / .ASN1 files from the dir
|
||||||
for f in os.listdir(i):
|
for f in os.listdir(i):
|
||||||
if f.split('.')[-1] in ('asn', 'asn1', 'ASN', 'ASN1'):
|
if f.split('.')[-1] in ('asn', 'asn1', 'ASN', 'ASN1'):
|
||||||
fn.append('%s/%s' % (i, f))
|
fn.append(f)
|
||||||
|
elif f == 'load_mod.txt':
|
||||||
|
wl = get_mod_wl('%s/%s' % (i, f))
|
||||||
|
# keep only asn files specified in the load_mod.txt file
|
||||||
|
if wl:
|
||||||
|
files.extend(['%s%s' % (i, f) for f in fn if f in wl])
|
||||||
|
else:
|
||||||
|
files.extend(['%s%s' % (i, f) for f in fn])
|
||||||
elif os.path.isfile(i):
|
elif os.path.isfile(i):
|
||||||
fn.append(i)
|
files.append(i)
|
||||||
else:
|
else:
|
||||||
print('%s, args warning: invalid input %s' % (sys.argv[0], i))
|
print('%s, args warning: invalid input %s' % (sys.argv[0], i))
|
||||||
if not fn:
|
if not files:
|
||||||
print('%s, args error: no ASN.1 inputs found')
|
print('%s, args error: no ASN.1 inputs found')
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
ckw['filenames'] = list(fn)
|
print(files)
|
||||||
|
ckw['filenames'] = list(files)
|
||||||
# read all file content into a single buffer
|
# read all file content into a single buffer
|
||||||
txt = []
|
txt = []
|
||||||
for f in fn:
|
for f in files:
|
||||||
try:
|
try:
|
||||||
fd = open(f)
|
fd = open(f)
|
||||||
except:
|
except:
|
||||||
|
@ -125,6 +160,9 @@ def main():
|
||||||
return 0
|
return 0
|
||||||
else:
|
else:
|
||||||
try:
|
try:
|
||||||
|
if python_version < 3:
|
||||||
|
txt.append( fd.read().decode('utf-8') )
|
||||||
|
else:
|
||||||
txt.append( fd.read() )
|
txt.append( fd.read() )
|
||||||
except:
|
except:
|
||||||
print('%s, args error: unable to read input file %s' % (sys.argv[0], f))
|
print('%s, args error: unable to read input file %s' % (sys.argv[0], f))
|
||||||
|
|
Loading…
Reference in New Issue