def test_get_output_error_code(self): quiet_exit = '%s -c "import sys; sys.exit(1)"' % python out, err, code = get_output_error_code(quiet_exit) self.assertEqual(out, "") self.assertEqual(err, "") self.assertEqual(code, 1) out, err, code = get_output_error_code('%s "%s"' % (python, self.fname)) self.assertEqual(out, "on stdout") self.assertEqual(err, "on stderr") self.assertEqual(code, 0)
def test_get_output_error_code(self): quiet_exit = '%s -c "import sys; sys.exit(1)"' % python out, err, code = get_output_error_code(quiet_exit) self.assertEqual(out, '') self.assertEqual(err, '') self.assertEqual(code, 1) out, err, code = get_output_error_code(f'{python} "{self.fname}"') self.assertEqual(out, 'on stdout') self.assertEqual(err, 'on stderr') self.assertEqual(code, 0)
def test_get_output_error_code(self): quiet_exit = '%s -c "import sys; sys.exit(1)"' % python out, err, code = get_output_error_code(quiet_exit) self.assertEqual(out, '') self.assertEqual(err, '') self.assertEqual(code, 1) out, err, code = get_output_error_code('%s "%s"' % (python, self.fname)) self.assertEqual(out, 'on stdout') self.assertEqual(err, 'on stderr') self.assertEqual(code, 0)
def help_output_test(subcommand=""): """test that `ipython [subcommand] -h` works""" cmd = " ".join(get_ipython_cmd() + [subcommand, "-h"]) out, err, rc = get_output_error_code(cmd) nt.assert_equal(rc, 0, err) nt.assert_not_in("Traceback", err) nt.assert_in("Options", out) nt.assert_in("--help-all", out) return out, err
def help_all_output_test(subcommand=''): """test that `ipython [subcommand] --help-all` works""" cmd = get_ipython_cmd() + [subcommand, '--help-all'] out, err, rc = get_output_error_code(cmd) nt.assert_equal(rc, 0, err) nt.assert_not_in("Traceback", err) nt.assert_in("Options", out) nt.assert_in("Class parameters", out) return out, err
def help_output_test(subcommand=''): """test that `ipython [subcommand] -h` works""" cmd = ' '.join(get_ipython_cmd() + [subcommand, '-h']) out, err, rc = get_output_error_code(cmd) nt.assert_equal(rc, 0, err) nt.assert_not_in("Traceback", err) nt.assert_in("Options", out) nt.assert_in("--help-all", out) return out, err
def help_all_output_test(subcommand=''): """test that `ipython [subcommand] --help-all` works""" cmd = get_ipython_cmd() + [subcommand, '--help-all'] out, err, rc = get_output_error_code(cmd) assert rc == 0, err assert "Traceback" not in err assert "Options" in out assert "Class" in out return out, err
def help_all_output_test(subcommand=''): """test that `ipython [subcommand] --help-all` works""" cmd = get_ipython_cmd() + [subcommand, '--help-all'] out, err, rc = get_output_error_code(cmd) nt.assert_equal(rc, 0, err) nt.assert_not_in("Traceback", err) nt.assert_in("Options", out) nt.assert_in("Class", out) return out, err
def _load_ips_ifconfig(): """load ip addresses from `ifconfig` output (posix)""" out, err, rc = get_output_error_code('ifconfig') if rc: # no ifconfig, it's usually in /sbin and /sbin is not on everyone's PATH out, err, rc = get_output_error_code('/sbin/ifconfig') if rc: raise IOError("no ifconfig: %s" % err) lines = out.splitlines() addrs = [] for line in lines: blocks = line.lower().split() if (len(blocks) >= 2) and (blocks[0] == 'inet'): if blocks[1].startswith("addr:"): addrs.append(blocks[1].split(":")[1]) else: addrs.append(blocks[1]) _populate_from_list(addrs)
def create_notebooks(notebooks): for filename, name in notebooks: head, tail = os.path.split(filename) print("\tgenerate {0}.rst".format(name)) #command = ipy_cmd +'nbconvert --to rst --execute {0} --output {1}.rst --template myrst.tpl'.format(filename, SRC_DIR + name) command = ipy_cmd +'nbconvert --to rst {0} --output-dir {1} --template myrst.tpl'.format(filename, head) out, err, return_code = get_output_error_code(command) if return_code != 0: print(err) clean() sys.exit()
def _load_ips_ip(): """load ip addresses from `ip addr` output (Linux)""" out, err, rc = get_output_error_code('ip addr') if rc: raise IOError("no ip: %s" % err) lines = out.splitlines() addrs = [] for line in lines: blocks = line.lower().split() if (len(blocks) >= 2) and (blocks[0] == 'inet'): addrs.append(blocks[1].split('/')[0]) _populate_from_list(addrs)
def _load_ips_ipconfig(): """load ip addresses from `ipconfig` output (Windows)""" out, err, rc = get_output_error_code('ipconfig') if rc: raise IOError("no ipconfig: %s" % err) lines = out.splitlines() addrs = [] for line in lines: m = _ipconfig_ipv4_pat.match(line.strip()) if m: addrs.append(m.group(1)) _populate_from_list(addrs)
def call(self, parameters, ignore_return_code=False): """ Execute a, IPython shell command, listening for both Errors and non-zero return codes. Parameters ---------- parameters : str List of parameters to pass to IPython. ignore_return_code : optional bool (default False) Throw an OSError if the return code """ stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters) if not (retcode == 0 or ignore_return_code): raise OSError(stderr) return stdout, stderr
def call(self, parameters, ignore_return_code=False): """ Execute a, IPython shell command, listening for both Errors and non-zero return codes. PARAMETERS: ----------- parameters : str List of parameters to pass to IPython. ignore_return_code : optional bool (default False) Throw an OSError if the return code """ stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters) if not (retcode == 0 or ignore_return_code): raise OSError(stderr) return stdout, stderr
def _verify_node(cmd): """Verify that the node command exists and is at least the minimum supported version of node. Parameters ---------- cmd : string Node command to verify (i.e 'node').""" try: out, err, return_code = get_output_error_code([cmd, '--version']) except OSError: # Command not found return False if return_code: # Command error return False return check_version(out.lstrip('v'), '0.9.12')
def call(self, parameters, raise_on_error=True): stdout, stderr, retcode = get_output_error_code(ipy_cmd + parameters) if retcode != 0 and raise_on_error: raise OSError(stderr) return stdout, stderr
def fortran(self, line, cell): """Compile and run a Fortran code cell. """ path = mkdtemp(prefix="fortranmagic_") try: #print("work dir: %s" % path) filename = os.path.join(path, "a.f90") if cell.startswith("program") or cell.startswith("module"): # The whole program is supplied, don't touch it: source = cell else: # Fortran lines are supplied directly, make it a valid # Fortran code: if cell.startswith("use"): # put "implicit none" below all "use" lines: source = [] c = cell.split("\n") line = c[0]; del c[0] while line.startswith("use"): source.append(line) line = c[0]; del c[0] source.append("implicit none") source.append(line) source.extend(c) source = "\n".join(source) + "\nend" else: source = "implicit none\n" + cell + "\nend" implicit_modules = """\ module types implicit none private public dp integer, parameter :: dp=kind(0.d0) ! double precision end module module constants use types, only: dp implicit none private public pi, e_, i_ ! Constants contain more digits than double precision, so that ! they are rounded correctly. Single letter constants contain underscore so ! that they do not clash with user variables ("e" and "i" are frequently used as ! loop variables) real(dp), parameter :: pi = 3.1415926535897932384626433832795_dp real(dp), parameter :: e_ = 2.7182818284590452353602874713527_dp complex(dp), parameter :: i_ = (0, 1) end module """ source = implicit_modules + source with open(filename, "w") as f: f.write(source) # Compile out, err, code = get_output_error_code('cd %s; gfortran -Wall -Wextra -Wimplicit-interface -fPIC -g -fcheck=all -fbacktrace a.f90' % \ path) if out != "": print("out:") print(out) if err != "": print("err:") print(err) if code != 0: print("*** FAILED TO COMPILE ***") return # Run out, err, code = get_output_error_code('cd %s; ./a.out' % path) if out != "": print(out) if err != "": print("err:") print(err) if code != 0: print("Non-zero exit code: %d" % code) finally: rmtree(path)