def latex_to_png_dvipng(s, wrap): try: find_cmd('latex') find_cmd('dvipng') except FindCmdError: return None try: workdir = tempfile.mkdtemp() tmpfile = os.path.join(workdir, "tmp.tex") dvifile = os.path.join(workdir, "tmp.dvi") outfile = os.path.join(workdir, "tmp.png") with open(tmpfile, "w", encoding='utf8') as f: f.writelines(genelatex(s, wrap)) with open(os.devnull, 'wb') as devnull: subprocess.check_call( ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], cwd=workdir, stdout=devnull, stderr=devnull) subprocess.check_call( ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, stdout=devnull, stderr=devnull) with open(outfile, "rb") as f: return f.read() except subprocess.CalledProcessError: return None finally: shutil.rmtree(workdir)
def _script_magics_default(self): """default to a common list of programs if we find them""" defaults = [] to_try = [] if os.name == 'nt': defaults.append('cmd') to_try.append('powershell') to_try.extend([ 'sh', 'bash', 'perl', 'ruby', 'python3', 'pypy', ]) for cmd in to_try: if cmd in self.script_paths: defaults.append(cmd) else: try: find_cmd(cmd) except FindCmdError: # command not found, ignore it pass except ImportError: # Windows without pywin32, find_cmd doesn't work pass else: defaults.append(cmd) return defaults
def latex_to_png_dvipng(s, wrap): try: find_cmd('latex') find_cmd('dvipng') except FindCmdError: return None try: workdir = tempfile.mkdtemp() tmpfile = os.path.join(workdir, "tmp.tex") dvifile = os.path.join(workdir, "tmp.dvi") outfile = os.path.join(workdir, "tmp.png") with open(tmpfile, "w", encoding='utf8') as f: f.writelines(genelatex(s, wrap)) with open(os.devnull, 'wb') as devnull: subprocess.check_call([ "latex", "-halt-on-error", "-interaction", "batchmode", tmpfile ], cwd=workdir, stdout=devnull, stderr=devnull) subprocess.check_call([ "dvipng", "-T", "tight", "-x", "1500", "-z", "9", "-bg", "transparent", "-o", outfile, dvifile ], cwd=workdir, stdout=devnull, stderr=devnull) with open(outfile, "rb") as f: return f.read() finally: shutil.rmtree(workdir)
def latex_to_png_dvipng(s): try: find_cmd('latex') find_cmd('dvipng') except FindCmdError: return None try: workdir = tempfile.mkdtemp() tmpfile = os.path.join(workdir, "tmp.tex") dvifile = os.path.join(workdir, "tmp.dvi") outfile = os.path.join(workdir, "tmp.png") with open(tmpfile, "w") as f: f.write(_latex_header) f.write(s) f.write(_latex_footer) subprocess.check_call( ["latex", "-halt-on-errror", tmpfile], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.check_call( ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) with open(outfile) as f: bin_data = f.read() finally: shutil.rmtree(workdir) return bin_data
def latex_to_png_dvipng(s, wrap, color='Black', scale=1.0): try: find_cmd('latex') find_cmd('dvipng') except FindCmdError: return None try: workdir = Path(tempfile.mkdtemp()) tmpfile = workdir.joinpath("tmp.tex") dvifile = workdir.joinpath("tmp.dvi") outfile = workdir.joinpath("tmp.png") with tmpfile.open("w", encoding="utf8") as f: f.writelines(genelatex(s, wrap)) with open(os.devnull, 'wb') as devnull: subprocess.check_call( ["latex", "-halt-on-error", "-interaction", "batchmode", tmpfile], cwd=workdir, stdout=devnull, stderr=devnull) resolution = round(150*scale) subprocess.check_call( ["dvipng", "-T", "tight", "-D", str(resolution), "-z", "9", "-bg", "transparent", "-o", outfile, dvifile, "-fg", color], cwd=workdir, stdout=devnull, stderr=devnull) with outfile.open("rb") as f: return f.read() except subprocess.CalledProcessError: return None finally: shutil.rmtree(workdir)
def latex_to_png_dvipng(s, wrap): try: find_cmd('latex') find_cmd('dvipng') except FindCmdError: return None try: workdir = tempfile.mkdtemp() tmpfile = os.path.join(workdir, "tmp.tex") dvifile = os.path.join(workdir, "tmp.dvi") outfile = os.path.join(workdir, "tmp.png") with open(tmpfile, "w") as f: f.writelines(genelatex(s, wrap)) subprocess.check_call( ["latex", "-halt-on-errror", tmpfile], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) subprocess.check_call( ["dvipng", "-T", "tight", "-x", "1500", "-z", "9", "-bg", "transparent", "-o", outfile, dvifile], cwd=workdir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) with open(outfile, "rb") as f: bin_data = f.read() finally: shutil.rmtree(workdir) return bin_data
def test_script_defaults(): ip = get_ipython() for cmd in ['sh', 'bash', 'perl', 'ruby']: try: find_cmd(cmd) except Exception: pass else: nt.assert_in(cmd, ip.magics_manager.magics['cell'])
def test_script_defaults(): ip = get_ipython() for cmd in ["sh", "bash", "perl", "ruby"]: try: find_cmd(cmd) except Exception: pass else: nt.assert_in(cmd, ip.magics_manager.magics["cell"])
def kpsewhich(filename): """Invoke kpsewhich command with an argument `filename`.""" try: find_cmd("kpsewhich") proc = subprocess.Popen(["kpsewhich", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = proc.communicate() return stdout.strip() except FindCmdError: pass
def kpsewhich(filename): """Invoke kpsewhich command with an argument `filename`.""" try: find_cmd("kpsewhich") proc = subprocess.Popen(["kpsewhich", filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) (stdout, stderr) = proc.communicate() return stdout.strip().decode('utf8', 'replace') except FindCmdError: pass
def run_command(self, command_list, filename, count, log_function): """Run command_list count times. Parameters ---------- command_list : list A list of args to provide to Popen. Each element of this list will be interpolated with the filename to convert. filename : unicode The name of the file to convert. count : int How many times to run the command. Returns ------- success : bool A boolean indicating if the command was successful (True) or failed (False). """ command = [c.format(filename=filename) for c in command_list] # On windows with python 2.x there is a bug in subprocess.Popen and # unicode commands are not supported if sys.platform == 'win32' and sys.version_info < (3, 0): #We must use cp1252 encoding for calling subprocess.Popen #Note that sys.stdin.encoding and encoding.DEFAULT_ENCODING # could be different (cp437 in case of dos console) command = [c.encode('cp1252') for c in command] # This will throw a clearer error if the command is not found find_cmd(command_list[0]) times = 'time' if count == 1 else 'times' self.log.info("Running %s %i %s: %s", command_list[0], count, times, command) with open(os.devnull, 'rb') as null: stdout = subprocess.PIPE if not self.verbose else None for index in range(count): p = subprocess.Popen(command, stdout=stdout, stdin=null) out, err = p.communicate() if p.returncode: if self.verbose: # verbose means I didn't capture stdout with PIPE, # so it's already been displayed and `out` is None. out = u'' else: out = out.decode('utf-8', 'replace') log_function(command, out) return False # failure return True # success
def run_command(self, command_list, filename, count, log_function): """Run command_list count times. Parameters ---------- command_list : list A list of args to provide to Popen. Each element of this list will be interpolated with the filename to convert. filename : unicode The name of the file to convert. count : int How many times to run the command. Returns ------- success : bool A boolean indicating if the command was successful (True) or failed (False). """ command = [c.format(filename=filename) for c in command_list] # On windows with python 2.x there is a bug in subprocess.Popen and # unicode commands are not supported if sys.platform == "win32" and sys.version_info < (3, 0): # We must use cp1252 encoding for calling subprocess.Popen # Note that sys.stdin.encoding and encoding.DEFAULT_ENCODING # could be different (cp437 in case of dos console) command = [c.encode("cp1252") for c in command] # This will throw a clearer error if the command is not found find_cmd(command_list[0]) times = "time" if count == 1 else "times" self.log.info("Running %s %i %s: %s", command_list[0], count, times, command) with open(os.devnull, "rb") as null: stdout = subprocess.PIPE if not self.verbose else None for index in range(count): p = subprocess.Popen(command, stdout=stdout, stdin=null) out, err = p.communicate() if p.returncode: if self.verbose: # verbose means I didn't capture stdout with PIPE, # so it's already been displayed and `out` is None. out = u"" else: out = out.decode("utf-8", "replace") log_function(command, out) return False # failure return True # success
def test_console_starts(): """test that `ipython console` starts a terminal""" from IPython.external import pexpect # weird IOErrors prevent this from firing sometimes: ipython_cmd = None for i in range(5): try: ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') except IOError: time.sleep(0.1) else: break if ipython_cmd is None: raise SkipTest("Could not determine ipython command") p = pexpect.spawn(ipython_cmd, args=['console', '--colors=NoColor']) idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=15) nt.assert_equals(idx, 0, "expected in prompt") p.sendline('5') idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=5) nt.assert_equals(idx, 0, "expected out prompt") idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=5) nt.assert_equals(idx, 0, "expected second in prompt") # send ctrl-D;ctrl-D to exit p.sendeof() p.sendeof() p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=5) if p.isalive(): p.terminate()
def test_console_starts(): """test that `ipython console` starts a terminal""" from IPython.external import pexpect # weird IOErrors prevent this from firing sometimes: ipython_cmd = None for i in range(5): try: ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') except IOError: time.sleep(0.1) else: break if ipython_cmd is None: raise SkipTest("Could not determine ipython command") p = pexpect.spawn(ipython_cmd, args=['console', '--colors=NoColor']) idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=15) nt.assert_equal(idx, 0, "expected in prompt") p.sendline('5') idx = p.expect([r'Out\[\d+\]: 5', pexpect.EOF], timeout=5) nt.assert_equal(idx, 0, "expected out prompt") idx = p.expect([r'In \[\d+\]', pexpect.EOF], timeout=5) nt.assert_equal(idx, 0, "expected second in prompt") # send ctrl-D;ctrl-D to exit p.sendeof() p.sendeof() p.expect([pexpect.EOF, pexpect.TIMEOUT], timeout=5) if p.isalive(): p.terminate()
def find_job_cmd(): if os.name == 'nt': try: return find_cmd('job') except FindCmdError: return 'job' else: return 'job'
def find_job_cmd(): if os.name=='nt': try: return find_cmd('job') except FindCmdError: return 'job' else: return 'job'
def find_job_cmd(): if WINDOWS: try: return find_cmd('job') except (FindCmdError, ImportError): # ImportError will be raised if win32api is not installed return 'job' else: return 'job'
def test_exit_code_signal_csh(self): SHELL = os.environ.get('SHELL', None) os.environ['SHELL'] = find_cmd("csh") try: self.test_exit_code_signal() finally: if SHELL is not None: os.environ['SHELL'] = SHELL else: del os.environ['SHELL']
def test_exit_code_signal_csh(self): # pragma: no cover SHELL = os.environ.get("SHELL", None) os.environ["SHELL"] = find_cmd("csh") try: self.test_exit_code_signal() finally: if SHELL is not None: os.environ['SHELL'] = SHELL else: del os.environ['SHELL']
def ipexec(fname, options=None): """Utility to call 'ipython filename'. Starts IPython witha minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. Returns ------- (stdout, stderr) of ipython subprocess. """ if options is None: options = [] # For these subprocess calls, eliminate all prompt printing so we only see # output from script execution prompt_opts = [ '--InteractiveShell.prompt_in1=""', '--InteractiveShell.prompt_in2=""', '--InteractiveShell.prompt_out=""', ] cmdargs = " ".join(default_argv() + prompt_opts + options) _ip = get_ipython() test_dir = os.path.dirname(__file__) ipython_cmd = find_cmd("ipython") # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = "%s %s %s" % (ipython_cmd, cmdargs, full_fname) # print >> sys.stderr, 'FULL CMD:', full_cmd # dbg out = getoutputerror(full_cmd) # `import readline` causes 'ESC[?1034h' to be the first output sometimes, # so strip that off the front of the first line if it is found if out: first = out[0] m = re.match(r"\x1b\[[^h]+h", first) if m: # strip initial readline escape out = list(out) out[0] = first[len(m.group()) :] out = tuple(out) return out
def pandocify(src, lang): """use pandoc to convert various things to rst""" with tempfile.NamedTemporaryFile(delete=False) as f: f.write(src) fname = f.name pandoc = find_cmd('pandoc') cmd = "%s -r %s -w rst '%s'" % (pandoc, lang, fname) try: rst = getoutput(cmd) return rst finally: os.unlink(fname)
def ipexec(fname, options=None): """Utility to call 'ipython filename'. Starts IPython witha minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. Returns ------- (stdout, stderr) of ipython subprocess. """ if options is None: options = [] # For these subprocess calls, eliminate all prompt printing so we only see # output from script execution prompt_opts = [ '--PromptManager.in_template=""', '--PromptManager.in2_template=""', '--PromptManager.out_template=""' ] cmdargs = ' '.join(default_argv() + prompt_opts + options) _ip = get_ipython() test_dir = os.path.dirname(__file__) ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg out = getoutputerror(full_cmd) # `import readline` causes 'ESC[?1034h' to be the first output sometimes, # so strip that off the front of the first line if it is found if out: first = out[0] m = re.match(r'\x1b\[[^h]+h', first) if m: # strip initial readline escape out = list(out) out[0] = first[len(m.group()):] out = tuple(out) return out
def ipexec(fname, options=None): """Utility to call 'ipython filename'. Starts IPython witha minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. Returns ------- (stdout, stderr) of ipython subprocess. """ if options is None: options = [] # For these subprocess calls, eliminate all prompt printing so we only see # output from script execution prompt_opts = [ '--PromptManager.in_template=""', '--PromptManager.in2_template=""', '--PromptManager.out_template=""' ] cmdargs = ' '.join(default_argv() + prompt_opts + options) _ip = get_ipython() test_dir = os.path.dirname(__file__) ipython_cmd = find_cmd('ipython3' if py3compat.PY3 else 'ipython') # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg out, err = getoutputerror(full_cmd) # `import readline` causes 'ESC[?1034h' to be output sometimes, # so strip that out before doing comparisons if out: out = re.sub(r'\x1b\[[^h]+h', '', out) return out, err
def ipexec(fname, options=None): """Utility to call 'ipython filename'. Starts IPython witha minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. Returns ------- (stdout, stderr) of ipython subprocess. """ if options is None: options = [] # For these subprocess calls, eliminate all prompt printing so we only see # output from script execution prompt_opts = [ 'InteractiveShell.prompt_in1=""', 'InteractiveShell.prompt_in2=""', 'InteractiveShell.prompt_out=""' ] cmdargs = ' '.join(default_argv() + prompt_opts + options) _ip = get_ipython() test_dir = os.path.dirname(__file__) ipython_cmd = find_cmd('ipython') # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg return getoutputerror(full_cmd)
def ipexec(fname, options=None): """Utility to call 'ipython filename'. Starts IPython witha minimal and safe configuration to make startup as fast as possible. Note that this starts IPython in a subprocess! Parameters ---------- fname : str Name of file to be executed (should have .py or .ipy extension). options : optional, list Extra command-line flags to be passed to IPython. Returns ------- (stdout, stderr) of ipython subprocess. """ if options is None: options = [] # For these subprocess calls, eliminate all prompt printing so we only see # output from script execution prompt_opts = [ 'InteractiveShell.prompt_in1=""', 'InteractiveShell.prompt_in2=""', 'InteractiveShell.prompt_out=""' ] cmdargs = ' '.join(default_argv() + prompt_opts + options) _ip = get_ipython() test_dir = os.path.dirname(__file__) ipython_cmd = find_cmd('ipython3') # Absolute path for filename full_fname = os.path.join(test_dir, fname) full_cmd = '%s %s %s' % (ipython_cmd, cmdargs, full_fname) #print >> sys.stderr, 'FULL CMD:', full_cmd # dbg return getoutputerror(full_cmd)
def __init__(self, runner='iptest', params=None): """Create new test runner.""" p = os.path if runner == 'iptest': iptest_app = get_ipython_module_path('IPython.testing.iptest') self.runner = pycmd2argv(iptest_app) + sys.argv[1:] elif runner == 'trial': # For trial, it needs to be installed system-wide self.runner = pycmd2argv(p.abspath(find_cmd('trial'))) else: raise Exception('Not a valid test runner: %s' % repr(runner)) if params is None: params = [] if isinstance(params, str): params = [params] self.params = params # Assemble call self.call_args = self.runner+self.params # Store pids of anything we start to clean up on deletion, if possible # (on posix only, since win32 has no os.kill) self.pids = []
def test_find_cmd_python(): """Make sure we find sys.exectable for python.""" nt.assert_equals(find_cmd('python'), sys.executable)
def test_find_cmd_ls(): """Make sure we can find the full path to ls.""" path = find_cmd('ls') nt.assert_true(path.endswith('ls'))
This function will raise an error if pandoc is not installed. Any error messages generated by pandoc are printed to stderr. Parameters ---------- source : string Input string, assumed to be valid markdown. Returns ------- out : string Output as returned by pandoc. """ return pandoc(source, 'markdown', 'rst') # prefer md2html via marked if node.js is available # node is called nodejs on debian, so try that first node_cmd = 'nodejs' try: find_cmd(node_cmd) except FindCmdError: node_cmd = 'node' try: find_cmd(node_cmd) except FindCmdError: markdown2html = markdown2html_pandoc else: markdown2html = markdown2html_marked else: markdown2html = markdown2html_marked
def test_find_cmd_pythonw(): """Try to find pythonw on Windows.""" path = find_cmd('pythonw') nt.assert_true(path.endswith('pythonw.exe'))
def test_find_cmd_pythonw(): """Try to find pythonw on Windows.""" path = find_cmd('pythonw') assert path.lower().endswith('pythonw.exe'), path
Any error messages generated by pandoc are printed to stderr. Parameters ---------- source : string Input string, assumed to be valid markdown. Returns ------- out : string Output as returned by pandoc. """ return pandoc(source, 'markdown', 'rst') # prefer md2html via marked if node.js is available # node is called nodejs on debian, so try that first node_cmd = 'nodejs' try: find_cmd(node_cmd) except FindCmdError: node_cmd = 'node' try: find_cmd(node_cmd) except FindCmdError: markdown2html = markdown2html_pandoc else: markdown2html = markdown2html_marked else: markdown2html = markdown2html_marked
"Please check that Node.js is installed." ) out, _ = p.communicate(cast_bytes(source, encoding)) out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() return out.rstrip('\n') def markdown2rst(source): """Convert a markdown string to LaTeX via pandoc. This function will raise an error if pandoc is not installed. Any error messages generated by pandoc are printed to stderr. Parameters ---------- source : string Input string, assumed to be valid markdown. Returns ------- out : string Output as returned by pandoc. """ return pandoc(source, 'markdown', 'rst') try: find_cmd('node') except FindCmdError: markdown2html = markdown2html_pandoc else: markdown2html = markdown2html_marked
def test_find_cmd_ls(): """Make sure we can find the full path to ls.""" path = find_cmd("ls") assert path.endswith("ls")
out, _ = p.communicate(cast_bytes(source, encoding)) out = TextIOWrapper(BytesIO(out), encoding, 'replace').read() return out.rstrip('\n') def markdown2rst(source): """Convert a markdown string to LaTeX via pandoc. This function will raise an error if pandoc is not installed. Any error messages generated by pandoc are printed to stderr. Parameters ---------- source : string Input string, assumed to be valid markdown. Returns ------- out : string Output as returned by pandoc. """ return pandoc(source, 'markdown', 'rst') try: find_cmd('node') except FindCmdError: markdown2html = markdown2html_pandoc else: markdown2html = markdown2html_marked