def test_asn1c(self): print('[<>] testing pycrate_asn1c') # create an "asn" dir for storing compiled specifications if 'test_asn_todelete' not in os.listdir('.'): os.mkdir('test_asn_todelete') # compile and generate the Hardcore ASN.1 module fd = open('./test/res/Hardcore.asn', 'r') asntext = fd.read() fd.close() fd_init = open('./test_asn_todelete/__init__.py', 'w') fd_init.write('__all__ = [') compile_text(asntext) generate_modules(PycrateGenerator, './test_asn_todelete/Hardcore.py') GLOBAL.clear() fd_init.write('\'Hardcore\', ') if TEST_ASN1C_ALL: # compile and generate all specifications from the asndir for sn in ASN_SPECS: compile_spec(shortname=sn) generate_modules(PycrateGenerator, './test_asn_todelete/%s.py' % sn) GLOBAL.clear() fd_init.write('\'%s\',' % sn) fd_init.write(']\n') fd_init.close() print('[<>] all ASN.1 modules generated to ./test_asn_todelete/') # load all specification print('[<>] loading all compiled module') importlib.import_module('test_asn_todelete.Hardcore') del sys.modules['test_asn_todelete.Hardcore'] if TEST_ASN1C_ALL: for sn in ASN_SPECS: importlib.import_module('test_asn_todelete.%s' % sn) del sys.modules['test_asn_todelete.%s' % sn] print('[<>] all ASN.1 modules loaded successfully from ./test_asn_delete/') GLOBAL.clear()
def main(asnfile, outpath): # txt = [] fd = open(asnfile) txt.append(fd.read()) fd.close() # ckw = {} ckw['filenames'] = list([asnfile]) compile_text(txt, **ckw) # if not os.path.exists(outpath): os.mkdir(outpath) # check_dup() gen_msg(asnfile, outpath, 'val')
def execute(): ckw = {} try: ofd = open('out.py', 'w') except: print("args error: unable to create output file") return 0 else: ofd.close() files = [] files.append("test.asn1") txt = [] for f in files: try: fd = open(f) except: print('%s, args error: unable to open input file %s' % (sys.argv[0], f)) return 0 else: try: if python_version < 3: txt.append(fd.read().decode('utf-8')) else: txt.append(fd.read()) except: print('%s, args error: unable to read input file %s' % (sys.argv[0], f)) fd.close() return 0 else: fd.close() compile_text(txt, **ckw) generate_modules(PycrateGenerator, 'out.py') return 0
def main(): parser = argparse.ArgumentParser( description='compile ASN.1 input file(s) for the pycrate ASN.1 runtime' ) # parser.add_argument( '-s', dest='spec', type=str, help='provide a specification shortname, instead of ASN.1 input file(s)' ) parser.add_argument('-i', dest='input', type=str, nargs='+', help='ASN.1 input file(s) or directory') parser.add_argument( '-o', dest='output', type=str, default='out', help='compiled output Python (and json) source file(s)') parser.add_argument( '-j', dest='json', action='store_true', help='output a json file with information on ASN.1 objects dependency') parser.add_argument('-fautotags', action='store_true', help='force AUTOMATIC TAGS for all ASN.1 modules') parser.add_argument( '-fextimpl', action='store_true', help='force EXTENSIBILITY IMPLIED for all ASN.1 modules') parser.add_argument( '-fverifwarn', action='store_true', help='force warning instead of raising during the verification stage') # args = parser.parse_args() # ckw = {} if args.fautotags: ckw['autotags'] = True if args.fextimpl: ckw['extimpl'] = True if args.fverifwarn: ckw['verifwarn'] = True # try: ofd = open(args.output + '.py', 'w') except: print('%s, args error: unable to create output file %s' % (sys.argv[0], args.output)) return 0 else: ofd.close() if args.spec: if args.spec not in ASN_SPECS: print('%s, args error: invalid specification name %s' % (sys.argv[0], args.spec)) print_specnames() return 0 # get spec name and potential flags specname = ASN_SPECS[args.spec] if isinstance(specname, (tuple, list)): for kw in specname[1:]: if kw not in ckw: ckw[kw] = True specname = specname[0] specdir = os.path.abspath(get_spec_dir(specname)) # compile the spec GLOBAL.clear() compile_spec(name=specname, **ckw) # generate .txt files objname = specdir + os.path.sep + 'load_obj.txt' modname = specdir + os.path.sep + 'load_mod.txt' if not os.path.exists(modname): with open(modname, 'w') as fd: for m in GLOBAL.MOD: if m[0] != '_': fd.write('%s.asn\n' % m) print('%s file created' % modname) if not os.path.exists(objname): with open(objname, 'w') as fd: for (m, n) in GLOBAL.COMP['DONE']: fd.write('%s.%s\n' % (m, n)) print('%s file created' % objname) # generate python and json files destname = os.path.abspath(specdir + os.path.sep + '..') + os.path.sep + args.spec generate_modules(PycrateGenerator, destname + '.py') print('%s file created' % (destname + '.py', )) generate_modules(JSONDepGraphGenerator, destname + '.json') print('%s file created' % (destname + '.json', )) GLOBAL.clear() # elif args.input: files = [] for i in args.input: if os.path.isdir(i): fn, wl = [], [] # get all potential .asn / .asn1 / .ASN / .ASN1 files from the dir for f in os.listdir(i): if f.split('.')[-1] in ('asn', 'asn1', 'ASN', 'ASN1'): 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): files.append(i) else: print('%s, args warning: invalid input %s' % (sys.argv[0], i)) if not files: print('%s, args error: no ASN.1 inputs found') return 0 else: #print(files) ckw['filenames'] = list(files) # read all file content into a single buffer txt = [] for f in files: try: fd = open(f) except: print('%s, args error: unable to open input file %s' % (sys.argv[0], f)) return 0 else: try: if python_version < 3: txt.append(fd.read().decode('utf-8')) else: txt.append(fd.read()) except: print('%s, args error: unable to read input file %s' % (sys.argv[0], f)) fd.close() return 0 else: fd.close() compile_text(txt, **ckw) # generate_modules(PycrateGenerator, args.output + '.py') if args.json: generate_modules(JSONDepGraphGenerator, args.output + '.json') # else: print('%s, args error: missing ASN.1 input(s) or specification name' % sys.argv[0]) # return 0
def main(): parser = argparse.ArgumentParser( description='compile ASN.1 input file(s) for the pycrate ASN.1 runtime' ) # parser.add_argument('-i', dest='input', type=str, nargs='+', help='ASN.1 input file(s) or directory') #parser.add_argument('-s', dest='spec', type=str, # help='provide a specification shortname, instead of ASN.1 input file(s)') parser.add_argument( '-o', dest='output', type=str, default='out', help='compiled output Python or Go (and json) source file(s)') parser.add_argument( '-j', dest='json', action='store_true', help='output a json file with information on ASN.1 objects dependency') parser.add_argument('-go', dest='go', action='store_true', help='output a Go language') parser.add_argument('-fautotags', action='store_true', help='force AUTOMATIC TAGS for all ASN.1 modules') parser.add_argument( '-fextimpl', action='store_true', help='force EXTENSIBILITY IMPLIED for all ASN.1 modules') parser.add_argument( '-fverifwarn', action='store_true', help='force warning instead of raising during the verification stage') # args = parser.parse_args() # ckw = {} if args.fautotags: ckw['autotags'] = True if args.fextimpl: ckw['extimpl'] = True if args.fverifwarn: ckw['verifwarn'] = True # if not args.go: try: ofd = open(args.output + '.py', 'w') except: print('%s, args error: unable to create output file %s' % (sys.argv[0], args.output)) return 0 else: ofd.close() #if args.spec: # if args.spec not in ASN_SPECS: # print('%s, args error: invalid specification name %s' % (sys.argv[0], args.spec)) # print_specnames() # return 0 # compile_spec(shortname=args.spec, **ckw) # if args.input: files = [] for i in args.input: if os.path.isdir(i): fn, wl = [], [] # get all potential .asn / .asn1 / .ASN / .ASN1 files from the dir for f in os.listdir(i): if f.split('.')[-1] in ('asn', 'asn1', 'ASN', 'ASN1'): 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): files.append(i) else: print('%s, args warning: invalid input %s' % (sys.argv[0], i)) if not files: print('%s, args error: no ASN.1 inputs found') return 0 else: #print(files) ckw['filenames'] = list(files) # read all file content into a single buffer txt = [] for f in files: try: fd = open(f) except: print('%s, args error: unable to open input file %s' % (sys.argv[0], f)) return 0 else: try: if python_version < 3: txt.append(fd.read().decode('utf-8')) else: txt.append(fd.read()) except: print('%s, args error: unable to read input file %s' % (sys.argv[0], f)) fd.close() return 0 else: fd.close() compile_text(txt, **ckw) # else: print('%s, args error: missing ASN.1 input(s) or specification name' % sys.argv[0]) return 0 if args.go: generate_modules(GoGenerator, "gocode/src/" + args.output) else: generate_modules(PycrateGenerator, args.output + '.py') if args.json: generate_modules(JSONDepGraphGenerator, args.output + '.json') return 0