def test_asn1c(self): print('[<>] testing pycrate_asn1c') # create an "asn" dir for storing compiled specifications if 'test_asn' not in os.listdir('.'): os.mkdir('test_asn') # 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/__init__.py', 'w') fd_init.write('__all__ = [') compile_text(asntext) generate_modules(PycrateGenerator, './test_asn/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/%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/') # load all specification print('[<>] loading all compiled module') importlib.import_module('test_asn.Hardcore') del sys.modules['test_asn.Hardcore'] if TEST_ASN1C_ALL: for sn in ASN_SPECS: importlib.import_module('test_asn.%s' % sn) del sys.modules['test_asn.%s' % sn] print('[<>] all ASN.1 modules loaded successfully from ./test_asn/')
def compile(self, data): try: self.asn_mgmt = AsnCodeMgmt(data) ckw = {'autotags': True, 'extimpl': True, 'verifwarn': True} from pycrate_asn1c.proc import compile_text, generate_modules, PycrateGenerator compile_text(data, **ckw) generate_modules(PycrateGenerator, self.py_file) except AsnCodeError as e: return False, str(e), [] except Exception as e: return False, str(e), [] self.msgs_in_modules = _get_supported_messages_in_modules(self.py_file) msgs = [] for module in self.msgs_in_modules: msgs.extend(self.msgs_in_modules[module]) msgs.sort() return True, "Compile Success!", msgs
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 (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 # compile_spec(shortname=args.spec, **ckw) # if args.input: fn = [] for i in args.input: if os.path.isdir(i): # 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('%s/%s' % (i, f)) elif os.path.isfile(i): fn.append(i) else: print('%s, args warning: invalid input %s' % (sys.argv[0], i)) if not fn: print('%s, args error: no ASN.1 inputs found') return 0 else: ckw['filenames'] = list(fn) # read all file content into a single buffer txt = [] for f in fn: try: fd = open(f) except: print('%s, args error: unable to open input file %s' % (sys.argv[0], f)) return 0 else: try: 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 generate_modules(PycrateGenerator, args.output + '.py') if args.json: generate_modules(JSONDepGraphGenerator, args.output + '.json') return 0