def run(arg): """Run command. """ filename = _get_filename(arg) if arg.binary: error('Warning: Not yet done...\n') else: var, defs = TextParser(open(filename, 'r').read()).parse_info() if arg.val: varv = TextParser(open(filename, 'r').read()).parse() print 'Variables Stored in "%s":\n' % arg.inputfile if arg.val: for name, info in var.iteritems(): print ' %s: %s = ' % (name, str(info)) print ' %s\n' % repr(varv[name]) else: for name, info in var.iteritems(): print ' %s: %s' % (name, str(info)) print '' if arg.defs: print 'Definitions Stored in "%s":\n' % arg.inputfile for key in defs: print " %s: " % key for att, info in defs[key]: print ' %s: %s' % (att, str(info))
def _compile_cpp_file(in_fname, func, cfg): """Helper function """ out_fname = '%s/%s_mex' % (cfg['matlab']['pkg'], func.name) mexbin, _, _ = exec_cmd('mexext') mexbin = '%s.%s' % (out_fname, mexbin.strip()) force = cfg['matlab']['force'] epilog = cfg['matlab']['epilog'] verbose = cfg['matlab']['verbose'] make_new = True if isinstance(force, list) and (len(force) == 0 or func.num in force): pass elif force in ['True', 'true']: pass elif os.path.exists(mexbin): date_in = datetime.fromtimestamp(os.path.getmtime(in_fname)) date_out = datetime.fromtimestamp(os.path.getmtime(mexbin)) if date_in < date_out: make_new = False trace(' - skipping compilation\n') if make_new: cmd = gen_cmd(cfg, 'matlab', int(cfg['matlab']['debug'])) cmd = '%s%s -output %s %s' % (cmd, in_fname, out_fname, epilog) trace(' - compiling %s ... ' % in_fname) if verbose is True or verbose in ['true', 'True']: cmd += ' -v' trace('\n * command:\n %s\n * ' % str(cmd)) exec_cmd(cmd, True) else: _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def check_inputs(xcfile, cfg, lang): """Make an executable with all the inputs the xcpp file uses and make sure that all of them are valid. Return a dictionary with the info on all the inputs. """ fname = cfg['xcpp']['filename'] fname, fvar = gen_input_file(xcfile, fname) cmd = '%s%s ' % (gen_cmd(cfg, lang, 1), fname) trace(' - compiling %s ... ' % fname) out, err, _ = exec_cmd(cmd) if err != '': msg = "ERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n') trace(' - running executable ... ') out, err, _ = exec_cmd('./a.out') if err != '': msg = "\nERROR: The command `%s` returned the following " \ "error:\n%s" % ('./a.out', str(err)) error(msg) trace('done\n') var, defs = TextParser(out).parse_info() info = _make_map(var, fvar) os.remove('a.out') os.remove(fname) return info, defs
def _compile(fname, mma, in_fname, cfg): """Helper function. """ cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) cmd = '%s%s -c -I%s -o %s.o' % (cmd, in_fname, mma, fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def read_config(arg): """Read the configuration file xcpp.config""" path = arg.cfg if path == '.' and not os.path.exists('xcpp.config'): if 'XCPP_CONFIG_PATH' in os.environ: tmp_path = os.environ['XCPP_CONFIG_PATH'] if os.path.exists('%s/xcpp.config' % tmp_path): path = tmp_path elif not os.path.exists('%s/xcpp.config' % path): error("ERROR: %s/xcpp.config does not exist\n" % path) arg.cfg = path config = configparser.ConfigParser(allow_no_value=True) config.read('%s/xcpp.config' % path) return config
def run(arg): """Run command. """ name = arg.name if name in get_lang_names(): mod = import_mod('excentury.lang.%s' % name) else: try: mod = import_mod('excentury.command.%s' % name) except ImportError: error('ERROR: invalid command or language: %r\n' % name) if hasattr(mod, 'DEFAULTS'): for key, val in mod.DEFAULTS.iteritems(): print '%s = %r' % (key, val) else: print 'NO DEFAULTS'
def _link(fname, mma, out_fname, cfg): """Helper function. """ cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) files = '%s.o tmp-%s.o' % (fname, fname) extras = '-lMLi3 -lstdc++ -framework Foundation' cmd = '%s%s -I%s -L%s %s -o %s' % (cmd, files, mma, mma, extras, out_fname) trace(' - preparing library ... ') _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) os.remove('%s.o' % fname) os.remove('tmp-%s.o' % fname) trace('done\n')
def read_config(arg): """Read the configuration file xcpp.config""" path = arg.cfg if path == '.' and not os.path.exists('xcpp.config'): if 'XCPP_CONFIG_PATH' in os.environ: tmp_path = os.environ['XCPP_CONFIG_PATH'] if os.path.exists('%s/xcpp.config' % tmp_path): trace("Configured with: '%s/xcpp.config'\n" % tmp_path) path = tmp_path elif not os.path.exists('%s/xcpp.config' % path): error("ERROR: %s/xcpp.config does not exist\n" % path) arg.cfg = path try: config = _read_config('%s/xcpp.config' % path, arg) except IOError: config = OrderedDict() return config
def _compile_cpp_file(in_fname, cfg): """Helper function. """ out_fname = _get_exec_name(cfg) epilog = cfg['python']['epilog'] verbose = cfg['python']['verbose'] cmd = gen_cmd(cfg, 'python', int(cfg['python']['debug'])) cmd = '%s --shared -fPIC %s -o %s %s' % (cmd, in_fname, out_fname, epilog) trace(' - compiling %s ... ' % in_fname) if verbose is True or verbose in ['true', 'True']: trace('\n * command:\n %s\n * ' % str(cmd)) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def process_function(xcf, cfg, func): """Given a Function object from .xcpp it will create the contents of a valid cpp file that can be compiled for a valid executable. """ in_fname = cfg['xcpp']['filename'] epilog = func.epilog if epilog != '': epilog += '\n' contents = TEMPLATE.format(date=date(), pre_xc=xcf.pre_xc, preamble=xcf.preamble, help=_format_help(func, in_fname), param=_format_input(func.param), doc=func.doc, body=func.body, load=cfg['cpp']['load'].capitalize(), dump=cfg['cpp']['dump'].capitalize(), input=format_input(func.param), output=format_return(func.ret), out=func.out, funcpre=func.preamble, funcepi=epilog, epilog=xcf.epilog) in_fname = _write_file(contents, func, cfg) out_fname = _get_exec_name(func, cfg) force = cfg['cpp']['force'] make_new = True if isinstance(force, list) and (len(force) == 0 or func.num in force): pass elif force in ['True', 'true']: pass elif os.path.exists(out_fname): date_in = datetime.fromtimestamp(os.path.getmtime(in_fname)) date_out = datetime.fromtimestamp(os.path.getmtime(out_fname)) if date_in < date_out: make_new = False trace(' - skipping compilation\n') if make_new: cmd = gen_cmd(cfg, 'cpp', int(cfg['cpp']['debug'])) cmd = '%s%s -o %s' % (cmd, in_fname, out_fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n')
def run(arg): """Run command. """ config = read_config(arg) if arg.v: disp('path to xcpp.config: "%s"\n' % arg.cfg) if arg.var is None: for sec in config: disp('[%s]\n' % sec) for key in config[sec]: disp(' %s = %s\n' % (key, config[sec][key])) disp('\n') return try: command, var = arg.var.split('.', 1) except ValueError: error("ERROR: '%s' is not of the form sec.key\n" % arg.var) try: disp(config[command][var]+'\n') except KeyError: pass return
def run(arg): """Run command. """ config = read_config(arg) try: command, var = arg.var.split('.', 1) except ValueError: error("ERROR: '%s' is not of the form sec.key\n" % arg.var) if arg.v: print 'path to xcpp.config: "%s"' % arg.cfg if arg.value is None: try: print config[command][var] except KeyError: pass return try: config[command][var] = arg.value except KeyError: config.add_section(command) config[command][var] = arg.value write_config(config, arg)
def _get_filename(arg): """Helper function. """ if arg.inputfile == '_': error("ERROR: Missing inputfile. Use -h to see usage.\n") cfg = get_cfg(arg, 'xcpp') root = cfg['xcpp']['root'] paths = cfg['xcpp']['path'].split(':') is_file = False for path in paths: if path == '.' or path[0] in ['/']: abs_path = '%s/%s' % (path, arg.inputfile) else: abs_path = '%s/%s/%s' % (root, path, arg.inputfile) if os.path.exists(abs_path): is_file = True break if not is_file: fname = '`%s` in\n %s' % (arg.inputfile, '\n '.join(paths)) error("ERROR: Unable to find %s\n" % fname) return abs_path
def write_tm_file(funcs, cfg, in_map): """Write the tm file and compile it with mprep. """ fname = cfg['xcpp']['filename'] contents = ':Evaluate: Begin["`Private`"]\n\n' for func in funcs: contents += TM_FUNC.format(name=m_name(func.name), args=_inputs(func.param, in_map, True), inputs=_inputs(func.param, in_map)) contents += ':Evaluate: End[]\n' in_fname = 'tmp-%s.tm' % fname trace('+ writing temporary file %s ... ' % in_fname) with open(in_fname, 'w') as tmp: tmp.write(contents) trace('done\n') mma = cfg['mathematica']['mlink'] # MPREP cmd = '%s/mprep %s -o %s' % (mma, in_fname, 'tmp-%s.cpp' % fname) trace(' - preparing %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) trace('done\n') # COMPILE_TM cmd = gen_cmd(cfg, 'mathematica', int(cfg['mathematica']['debug'])) in_fname = 'tmp-%s.cpp' % fname out_fname = 'tmp-%s.o' % fname cmd = '%s%s -c -I%s -o %s' % (cmd, in_fname, mma, out_fname) trace(' - compiling %s ... ' % in_fname) _, err, _ = exec_cmd(cmd) if err != '': msg = "\nERROR: The command\n%s\n\nreturned the following " \ "error:\n%s" % (str(cmd), str(err)) error(msg) # CLEAN UP os.remove('tmp-%s.cpp' % fname) os.remove('tmp-%s.tm' % fname) trace('done\n')
def set_project(name): """Sets a project. """ rc_path = pth.expandvars('$HOME/.excentury/excenturyrc') if name == '0': with open(rc_path, 'w') as rcfile: rcfile.write(excenturyrc_str()) set_current('') disp("Restart bash to clear previous project settings.\n") return data = get_entries() found = False for num, entry in enumerate(data): if name in [entry[0], str(num+1)]: set_current(entry[1]) with open(rc_path, 'w') as rcfile: rcfile.write(excenturyrc_str()) rcfile.write('source %s/.xcpprc\n' % entry[1]) disp('Project %r has been set. Restart bash.\n' % name) found = True break if not found: error("ERROR: not a valid entry\n")
def update_entries(path): """Updates the projects entries. Checks to see if the `xcpp.config` and `.xcpprc` files exists. """ data = get_entries() path = pth.abspath(path) try: config = _read_config('%s/xcpp.config' % path) except IOError: error("ERROR: `xcpp.config` not found in %r\n" % path) if not pth.exists('%s/.xcpprc' % path): error("ERROR: `xcpprc` not found in %r\n" % path) if 'xcpp' not in config: error("ERROR: Missing `xcpp` section in `xcpp.config`\n") if 'name' not in config['xcpp']: error("ERROR: Missing `xcpp.name` value in `xcpp.config`\n") if not append_entry(data, config['xcpp']['name'], path): trace("%r has been previously added\n" % path) set_entries(data)