Beispiel #1
0
def initialize(webapp, root_dir, connection_url, settings):
    if os.name == "nt":
        default_shell = "powershell.exe"
    else:
        default_shell = which("sh")
    shell = settings.get("shell_command", [os.environ.get("SHELL") or default_shell])
    # Enable login mode - to automatically source the /etc/profile script
    if os.name != "nt":
        shell.append("-l")
    terminal_manager = webapp.settings["terminal_manager"] = NamedTermManager(
        shell_command=shell,
        extra_env={
            "JUPYTER_SERVER_ROOT": root_dir,
            "JUPYTER_SERVER_URL": connection_url,
        },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings["base_url"]
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (
            ujoin(base_url, r"/terminals/websocket/(\w+)"),
            TermSocket,
            {"term_manager": terminal_manager},
        ),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #2
0
def get_pandoc_version():
    """Gets the Pandoc version if Pandoc is installed.
    
    If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value.
    If the minimal version is met, it will return the cached version and stop probing Pandoc 
    (unless :func:`clean_cache()` is called).

    Raises
    ------
    PandocMissing
      If pandoc is unavailable.
    """
    global __version

    if __version is None:
        if not which('pandoc'):
            raise PandocMissing()

        out = subprocess.check_output(['pandoc', '-v'])
        out_lines = out.splitlines()
        version_pattern = re.compile(r"^\d+(\.\d+){1,}$")
        for tok in out_lines[0].decode('ascii', 'replace').split():
            if version_pattern.match(tok):
                __version = tok
                break
    return __version
Beispiel #3
0
    def _extract_package(self, source, tempdir):
        # npm pack the extension
        is_dir = osp.exists(source) and osp.isdir(source)
        if is_dir and not osp.exists(pjoin(source, 'node_modules')):
            self._run(['node', YARN_PATH, 'install'], cwd=source)

        info = dict(source=source, is_dir=is_dir)

        ret = self._run([which('npm'), 'pack', source], cwd=tempdir)
        if ret != 0:
            msg = '"%s" is not a valid npm package'
            raise ValueError(msg % source)

        path = glob.glob(pjoin(tempdir, '*.tgz'))[0]
        info['data'] = _read_package(path)
        if is_dir:
            info['sha'] = sha = _tarsum(path)
            target = path.replace('.tgz', '-%s.tgz' % sha)
            shutil.move(path, target)
            info['path'] = target
        else:
            info['path'] = path

        info['filename'] = osp.basename(info['path'])
        info['name'] = info['data']['name']
        info['version'] = info['data']['version']

        return info
Beispiel #4
0
def get_pandoc_version():
    """Gets the Pandoc version if Pandoc is installed.
    
    If the minimal version is not met, it will probe Pandoc for its version, cache it and return that value.
    If the minimal version is met, it will return the cached version and stop probing Pandoc 
    (unless :func:`clean_cache()` is called).

    Raises
    ------
    PandocMissing
      If pandoc is unavailable.
    """
    global __version

    if __version is None:
        if not which('pandoc'):
            raise PandocMissing()

        out = subprocess.check_output(['pandoc', '-v'],
                                      universal_newlines=True)
        out_lines = out.splitlines()
        version_pattern = re.compile(r"^\d+(\.\d+){1,}$")
        for tok in out_lines[0].split():
            if version_pattern.match(tok):
                __version = tok
                break
    return __version
def get_inkscape_executable_path():
    """
    Return the path of the system inkscape_ exectuable.

    .. _inkscape: https://inkscape.org/en

    """
    inkscape = which('inkscape')
    if inkscape is not None:
        return inkscape

    if sys.platform == 'darwin':
        default_darwin_inkscape_path = (
            '/Applications/Inkscape.app/Contents/Resources/bin/inkscape')
        if os.path.isfile(default_darwin_inkscape_path):
            return default_darwin_inkscape_path
    elif sys.platform == 'win32':
        try:
            import winreg  # py3 stdlib on windows
        except ImportError:
            import _winreg as winreg  # py2 stdlib on windows
        win32_inkscape_reg_key = "SOFTWARE\\Classes\\inkscape.svg\\DefaultIcon"
        wr_handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
        try:
            rkey = winreg.OpenKey(wr_handle, win32_inkscape_reg_key)
            inkscape = winreg.QueryValueEx(rkey, "")[0]
        except OSError as ex:
            if ex.errno == errno.ENOENT:
                return None
            raise
        return inkscape
    return None
Beispiel #6
0
def initialize(webapp, root_dir, connection_url, settings):
    if os.name == 'nt':
        default_shell = 'powershell.exe'
    else:
        default_shell = which('sh')
    shell = settings.get('shell_command',
                         [os.environ.get('SHELL') or default_shell])
    # Enable login mode - to automatically source the /etc/profile script
    if os.name != 'nt':
        shell.append('-l')
    terminal_manager = webapp.settings['terminal_manager'] = NamedTermManager(
        shell_command=shell,
        extra_env={
            'JUPYTER_SERVER_ROOT': root_dir,
            'JUPYTER_SERVER_URL': connection_url,
        },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket, {
            'term_manager': terminal_manager
        }),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url,
               r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
def get_inkscape_executable_path():
    """
    Return the path of the system inkscape_ exectuable.

    .. _inkscape: https://inkscape.org/en

    """
    inkscape = which('inkscape')
    if inkscape is not None:
        return inkscape

    if sys.platform == 'darwin':
        default_darwin_inkscape_path = (
            '/Applications/Inkscape.app/Contents/Resources/bin/inkscape')
        if os.path.isfile(default_darwin_inkscape_path):
            return default_darwin_inkscape_path
    elif sys.platform == 'win32':
        try:
            import winreg  # py3 stdlib on windows
        except ImportError:
            import _winreg as winreg  # py2 stdlib on windows
        win32_inkscape_reg_key = "SOFTWARE\\Classes\\inkscape.svg\\DefaultIcon"
        wr_handle = winreg.ConnectRegistry(None, winreg.HKEY_LOCAL_MACHINE)
        try:
            rkey = winreg.OpenKey(wr_handle, win32_inkscape_reg_key)
            inkscape = winreg.QueryValueEx(rkey, "")[0]
        except OSError as ex:
            if ex.errno == errno.ENOENT:
                return None
            raise
        return inkscape
    return None
Beispiel #8
0
def initialize(webapp, notebook_dir, connection_url, settings):
    if os.name == 'nt':
        default_shell = 'powershell.exe'
    else:
        default_shell = which('sh')
    shell = settings.get('shell_command',
        [os.environ.get('SHELL') or default_shell]
    )
    # Enable login mode - to automatically source the /etc/profile script
    if os.name != 'nt':
        shell.append('-l')
    terminal_manager = webapp.settings['terminal_manager'] = NamedTermManager(
        shell_command=shell,
        extra_env={'JUPYTER_SERVER_ROOT': notebook_dir,
                   'JUPYTER_SERVER_URL': connection_url,
                   },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket,
             {'term_manager': terminal_manager}),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #9
0
def initialize(webapp, notebook_dir, connection_url, settings):
    if os.name == 'nt':
        default_shell = 'powershell.exe'
    else:
        default_shell = which('sh')
    shell = settings.get('shell_command',
                         [os.environ.get('SHELL') or default_shell])
    terminal_manager = webapp.settings['terminal_manager'] = NamedTermManager(
        shell_command=shell,
        extra_env={
            'JUPYTER_SERVER_ROOT': notebook_dir,
            'JUPYTER_SERVER_URL': connection_url,
        },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket, {
            'term_manager': terminal_manager
        }),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url,
               r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #10
0
def initialize(webapp, root_dir, connection_url, settings):
    if os.name == 'nt':
        default_shell = 'powershell.exe'
    else:
        default_shell = which('sh')
    shell_override = settings.get('shell_command')
    shell = (
        [os.environ.get('SHELL') or default_shell]
        if shell_override is None
        else shell_override
    )
    # When the notebook server is not running in a terminal (e.g. when
    # it's launched by a JupyterHub spawner), it's likely that the user
    # environment hasn't been fully set up. In that case, run a login
    # shell to automatically source /etc/profile and the like, unless
    # the user has specifically set a preferred shell command.
    if os.name != 'nt' and shell_override is None and not sys.stdout.isatty():
        shell.append('-l')
    terminal_manager = webapp.settings['terminal_manager'] = TerminalManager(
        shell_command=shell,
        extra_env={'JUPYTER_SERVER_ROOT': root_dir,
                   'JUPYTER_SERVER_URL': connection_url,
                   },
        parent=webapp.settings['serverapp'],
    )
    terminal_manager.log = webapp.settings['serverapp'].log
    base_url = webapp.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket,
             {'term_manager': terminal_manager}),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #11
0
    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
        cmd = which(command_list[0])
        if cmd is None:
            raise OSError("%s not found on PATH" % command_list[0])
        
        times = 'time' if count == 1 else 'times'
        self.log.info("Running %s %i %s: %s", command_list[0], count, times, command)
        
        shell = (sys.platform == 'win32')
        if shell:
            command = subprocess.list2cmdline(command)
        env = os.environ.copy()
        env['TEXINPUTS'] = os.pathsep.join([
            cast_bytes_py2(self.texinputs),
            env.get('TEXINPUTS', ''),
        ])
        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, shell=shell, env=env)
                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
Beispiel #12
0
def onlyif_cmds_exist(*commands):
    """
    Decorator to skip test when at least one of `commands` is not found.
    """
    for cmd in commands:
        if not which(cmd):
            return pytest.mark.skip("This test runs only if command '{0}' "
                        "is installed".format(cmd))
    return lambda f: f
Beispiel #13
0
    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
        cmd = which(command_list[0])
        if cmd is None:
            raise OSError("%s not found on PATH" % 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):
                shell = (sys.platform == 'win32')
                p = subprocess.Popen(
                    subprocess.list2cmdline(command) if shell else command,
                    stdout=stdout,
                    stdin=null,
                    shell=shell)
                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
Beispiel #14
0
def onlyif_cmds_exist(*commands):
    """
    Decorator to skip test when at least one of `commands` is not found.
    """
    for cmd in commands:
        if not which(cmd):
            return pytest.mark.skip("This test runs only if command '{0}' "
                                    "is installed".format(cmd))
    return lambda f: f
Beispiel #15
0
def initialize(webapp, notebook_dir, connection_url, settings):
    # remove existing terminal web handlers
    for host_rule in webapp.default_router.rules:
        if not hasattr(host_rule.matcher, "host_pattern"):
            continue
        if host_rule.matcher.host_pattern.pattern != ".*$":
            continue
        new_rules = []
        for path_rule in host_rule.target.rules:
            if not hasattr(path_rule.matcher, "regex"):
                continue
            pattern = path_rule.matcher.regex.pattern
            if pattern.find("/terminals/"
                            ) == -1 and not pattern.endswith("/terminals$"):
                new_rules.append(path_rule)
        host_rule.target.rules = new_rules

    if os.name == "nt":
        default_shell = "powershell.exe"
    else:
        default_shell = which("sh")
    shell = settings.get("shell_command",
                         [os.environ.get("SHELL") or default_shell])

    if os.name != "nt":
        shell.append("-l")
    terminal_manager = webapp.settings["terminal_manager"] = NamedTermManager(
        shell_command=shell,
        extra_env={
            "JUPYTER_SERVER_ROOT": notebook_dir,
            "JUPYTER_SERVER_URL": connection_url,
        },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings["base_url"]
    new_handlers = [
        (ujoin(base_url, r"/terminals/([^/]+)"), TerminalHandler),
        (ujoin(base_url,
               r"/terminals/websocket/([^/]+)"), term_handlers.TermSocket, {
                   "term_manager": terminal_manager
               }),
        (ujoin(base_url, r"/api/terminals"), APITerminalRootHandler),
        (ujoin(base_url, r"/api/terminals/([^/]+)"), APITerminalHandler),
        (ujoin(base_url, r"/terminal_extension"), TerminalExtensionHandler),
    ]
    webapp.add_handlers(".*$", new_handlers)
Beispiel #16
0
    def add_pdf(self, nb, resources):
        from ipython_genutils.py3compat import which

        template_file = 'notebook.tplx'

        exp = PDFExporter(config=self.config, template_file=template_file)

        if not which(exp.latex_command[0]):
            return

        (body, _) = exp.from_notebook_node(nb)

        resources['outputs'][self.base_name+'.pdf'] = body

        exp = LatexExporter(config=self.config, template_file=template_file)

        (body, _) = exp.from_notebook_node(nb)

        resources['outputs'][self.base_name+'.latex'] = body.encode('utf-8')
Beispiel #17
0
def initialize(webapp, root_dir, connection_url, settings):
    if os.name == "nt":
        default_shell = "powershell.exe"
    else:
        default_shell = which("sh")
    shell_override = settings.get("shell_command")
    shell = [os.environ.get("SHELL") or default_shell
             ] if shell_override is None else shell_override
    # When the notebook server is not running in a terminal (e.g. when
    # it's launched by a JupyterHub spawner), it's likely that the user
    # environment hasn't been fully set up. In that case, run a login
    # shell to automatically source /etc/profile and the like, unless
    # the user has specifically set a preferred shell command.
    if os.name != "nt" and shell_override is None and not sys.stdout.isatty():
        shell.append("-l")
    terminal_manager = webapp.settings["terminal_manager"] = TerminalManager(
        shell_command=shell,
        extra_env={
            "JUPYTER_SERVER_ROOT": root_dir,
            "JUPYTER_SERVER_URL": connection_url,
        },
        parent=webapp.settings["serverapp"],
    )
    terminal_manager.log = webapp.settings["serverapp"].log
    base_url = webapp.settings["base_url"]
    handlers = [
        (
            ujoin(base_url, r"/terminals/websocket/(\w+)"),
            TermSocket,
            {
                "term_manager": terminal_manager
            },
        ),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url,
               r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #18
0
def initialize(webapp, notebook_dir, connection_url, settings):
    default_shell = which('sh')
    if not default_shell and os.name == 'nt':
        default_shell = 'powershell.exe'
    shell = settings.get('shell_command',
        [os.environ.get('SHELL') or default_shell]
    )
    terminal_manager = webapp.settings['terminal_manager'] = NamedTermManager(
        shell_command=shell,
        extra_env={'JUPYTER_SERVER_ROOT': notebook_dir,
                   'JUPYTER_SERVER_URL': connection_url,
                   },
    )
    terminal_manager.log = app_log
    base_url = webapp.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket,
             {'term_manager': terminal_manager}),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url, r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    webapp.add_handlers(".*$", handlers)
Beispiel #19
0
def initialize(nb_app):
    if os.name == 'nt':
        default_shell = 'powershell.exe'
    else:
        default_shell = which('sh')
    shell = nb_app.terminado_settings.get(
        'shell_command', [os.environ.get('SHELL') or default_shell])
    # Enable login mode - to automatically source the /etc/profile
    # script, but only for non-nested shells; for nested shells, it's
    # superfluous and may even be harmful (e.g. on macOS, where login
    # shells invoke /usr/libexec/path_helper to add entries from
    # /etc/paths{,.d} to the PATH, reordering it in the process and
    # potentially overriding virtualenvs and other PATH modifications)
    if os.name != 'nt' and int(os.environ.get("SHLVL", 0)) < 1:
        shell.append('-l')
    terminal_manager = nb_app.web_app.settings[
        'terminal_manager'] = TerminalManager(
            shell_command=shell,
            extra_env={
                'JUPYTER_SERVER_ROOT': nb_app.notebook_dir,
                'JUPYTER_SERVER_URL': nb_app.connection_url,
            },
            parent=nb_app,
        )
    terminal_manager.log = nb_app.log
    base_url = nb_app.web_app.settings['base_url']
    handlers = [
        (ujoin(base_url, r"/terminals/(\w+)"), TerminalHandler),
        (ujoin(base_url, r"/terminals/websocket/(\w+)"), TermSocket, {
            'term_manager': terminal_manager
        }),
        (ujoin(base_url, r"/api/terminals"), api_handlers.TerminalRootHandler),
        (ujoin(base_url,
               r"/api/terminals/(\w+)"), api_handlers.TerminalHandler),
    ]
    nb_app.web_app.add_handlers(".*$", handlers)
Beispiel #20
0
except ImportError:
    class TimeoutExpired(Exception):
        pass
    def popen_wait(p, timeout):
        """backport of Popen.wait from Python 3"""
        for i in range(int(10 * timeout)):
            if p.poll() is not None:
                return
            time.sleep(0.1)
        if p.poll() is None:
            raise TimeoutExpired

NOTEBOOK_SHUTDOWN_TIMEOUT = 10

have = {}
have['casperjs'] = bool(which('casperjs'))
have['phantomjs'] = bool(which('phantomjs'))
have['slimerjs'] = bool(which('slimerjs'))

class StreamCapturer(Thread):
    daemon = True  # Don't hang if main thread crashes
    started = False
    def __init__(self, echo=False):
        super(StreamCapturer, self).__init__()
        self.echo = echo
        self.streams = []
        self.buffer = BytesIO()
        self.readfd, self.writefd = os.pipe()
        self.buffer_lock = Lock()
        self.stop = Event()
    cc1 = new_code_cell(source=u'print(2*6)')
    cc1.outputs.append(new_output(output_type="stream", text=u'12'))
    cc1.outputs.append(
        new_output(
            output_type="execute_result",
            data={'image/png': png_green_pixel},
            execution_count=1,
        ))
    nb.cells.append(cc1)

    # Write file to tmp dir.
    nbfile = subdir / 'testnb.ipynb'
    nbfile.write_text(writes(nb, version=4), encoding='utf-8')


pytestmark = pytest.mark.skipif(not which('pandoc'),
                                reason="Command 'pandoc' is not available")


async def test_from_file(jp_fetch, notebook):
    r = await jp_fetch('nbconvert',
                       'html',
                       'foo',
                       'testnb.ipynb',
                       method='GET',
                       params={'download': False})

    assert r.code == 200
    assert 'text/html' in r.headers['Content-Type']
    assert 'Created by test' in r.body.decode()
    assert 'print' in r.body.decode()
Beispiel #22
0
    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
        cmd = which(command_list[0])
        if cmd is None:
            link = "https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex"
            raise OSError("{formatter} not found on PATH, if you have not installed "
                          "{formatter} you may need to do so. Find further instructions "
                          "at {link}.".format(formatter=command_list[0], link=link))
        
        times = 'time' if count == 1 else 'times'
        self.log.info("Running %s %i %s: %s", command_list[0], count, times, command)
        
        shell = (sys.platform == 'win32')
        if shell:
            command = subprocess.list2cmdline(command)
        env = os.environ.copy()
        prepend_to_env_search_path('TEXINPUTS', self.texinputs, env)
        prepend_to_env_search_path('BIBINPUTS', self.texinputs, env)
        prepend_to_env_search_path('BSTINPUTS', self.texinputs, env)

        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, stderr=subprocess.STDOUT,
                        stdin=null, shell=shell, env=env)
                out, _ = 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)
                    self._captured_output.append(out)
                    return False # failure
        return True # success
Beispiel #23
0
        pass

    def popen_wait(p, timeout):
        """backport of Popen.wait from Python 3"""
        for i in range(int(10 * timeout)):
            if p.poll() is not None:
                return
            time.sleep(0.1)
        if p.poll() is None:
            raise TimeoutExpired


NOTEBOOK_SHUTDOWN_TIMEOUT = 10

have = {}
have['casperjs'] = bool(which('casperjs'))
have['phantomjs'] = bool(which('phantomjs'))
have['slimerjs'] = bool(which('slimerjs'))


class StreamCapturer(Thread):
    daemon = True  # Don't hang if main thread crashes
    started = False

    def __init__(self, echo=False):
        super(StreamCapturer, self).__init__()
        self.echo = echo
        self.streams = []
        self.buffer = BytesIO()
        self.readfd, self.writefd = os.pipe()
        self.buffer_lock = Lock()
Beispiel #24
0
    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
        cmd = which(command_list[0])
        if cmd is None:
            link = "https://nbconvert.readthedocs.io/en/latest/install.html#installing-tex"
            raise OSError(
                "{formatter} not found on PATH, if you have not installed "
                "{formatter} you may need to do so. Find further instructions "
                "at {link}.".format(formatter=command_list[0], link=link))

        times = 'time' if count == 1 else 'times'
        self.log.info("Running %s %i %s: %s", command_list[0], count, times,
                      command)

        shell = (sys.platform == 'win32')
        if shell:
            command = subprocess.list2cmdline(command)
        env = os.environ.copy()
        env['TEXINPUTS'] = os.pathsep.join([
            cast_bytes_py2(self.texinputs),
            env.get('TEXINPUTS', ''),
        ])
        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,
                                     stderr=subprocess.STDOUT,
                                     stdin=null,
                                     shell=shell,
                                     env=env)
                out, _ = 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)
                    self._captured_output.append(out)
                    return False  # failure
        return True  # success