예제 #1
0
def delete_kernel(kernel_name):
    """
    Delete the kernel by removing the kernel.json and directory.

    Parameters
    ----------
    kernel_name : str
        The name of the kernel to delete

    Raises
    ------
    KeyError
        If the kernel is not found.
    """
    spec = ks.get_kernel_spec(kernel_name)
    os.remove(path.join(spec.resource_dir, "kernel.json"))
    try:
        os.rmdir(spec.resource_dir)
    except OSError:
        # Non empty directory, just leave it
        pass
예제 #2
0
def show_kernel(kernel_name):
    """
    Print the contents of the kernel.json to the terminal, plus some extra
    information.

    Parameters
    ----------
    kernel_name : str
        The name of the kernel to show the information for.
    """
    # Load the raw json, since we store some unexpected data in there too
    spec = ks.get_kernel_spec(kernel_name)
    with open(path.join(spec.resource_dir, "kernel.json")) as kernel_file:
        kernel_json = json.load(kernel_file)

    # Manually format the json to put each key: value on a single line
    print("  * Kernel found in: {0}".format(spec.resource_dir))
    print("  * Name: {0}".format(spec.display_name))
    print("  * Kernel command: {0}".format(list2cmdline(spec.argv)))
    print("  * remote_ikernel command: {0}".format(list2cmdline(kernel_json["remote_ikernel_argv"])))
    print("  * Raw json: {0}".format(json.dumps(kernel_json, indent=2)))
예제 #3
0
def delete_kernel(kernel_name):
    """
    Delete the kernel by removing the kernel.json and directory.

    Parameters
    ----------
    kernel_name : str
        The name of the kernel to delete

    Raises
    ------
    KeyError
        If the kernel is not found.
    """
    spec = ks.get_kernel_spec(kernel_name)
    os.remove(path.join(spec.resource_dir, "kernel.json"))
    try:
        os.rmdir(spec.resource_dir)
    except OSError:
        # Non empty directory, just leave it
        pass
예제 #4
0
def show_kernel(kernel_name):
    """
    Print the contents of the kernel.json to the terminal, plus some extra
    information.

    Parameters
    ----------
    kernel_name : str
        The name of the kernel to show the information for.
    """
    # Load the raw json, since we store some unexpected data in there too
    spec = ks.get_kernel_spec(kernel_name)
    with open(path.join(spec.resource_dir, 'kernel.json')) as kernel_file:
        kernel_json = json.load(kernel_file)

    # Manually format the json to put each key: value on a single line
    print("  * Kernel found in: {0}".format(spec.resource_dir))
    print("  * Name: {0}".format(spec.display_name))
    print("  * Kernel command: {0}".format(list2cmdline(spec.argv)))
    print("  * remote_ikernel command: {0}".format(
        list2cmdline(kernel_json['remote_ikernel_argv'])))
    print("  * Raw json: {0}".format(json.dumps(kernel_json, indent=2)))
예제 #5
0
def manage():
    """
    Manage the available remote_ikernels.

    All the options are pulled from arguments so we take no
    arguments here.
    """

    description = ["Remote IKernel management utility", "", "Currently installed kernels:"]
    existing_kernels = {}

    # Sort so they are always in the same order
    for kernel_name in sorted(ks.find_kernel_specs()):
        if kernel_name.startswith(RIK_PREFIX):
            spec = ks.get_kernel_spec(kernel_name)
            display = "  ['{kernel_name}']: {desc}".format(kernel_name=kernel_name, desc=spec.display_name)
            existing_kernels[kernel_name] = spec
            description.append(display)

    # The raw formatter stops lines wrapping
    parser = argparse.ArgumentParser(
        prog="%prog manage", description="\n".join(description), formatter_class=argparse.RawTextHelpFormatter
    )
    parser.add_argument("--show", "-s", help="Print the contents of the " "kernel.")
    parser.add_argument(
        "--add", "-a", action="store_true", help="Add a new " "kernel according to other commandline options."
    )
    parser.add_argument("--delete", "-d", help="Remove the kernel and delete " "the associated kernel.json.")
    parser.add_argument("--kernel_cmd", "-k", help="Kernel command " "to install.")
    parser.add_argument("--name", "-n", help="Name to identify the kernel," "e.g. 'Python 2.7'.")
    parser.add_argument("--language", "-l", help="Explicitly specify the " "language of the kernel.")
    parser.add_argument(
        "--cpus", "-c", type=int, help="Launch the kernel " "as a multi-core job with this many cores if > 1."
    )
    parser.add_argument("--pe", help="Parallel environment to use on when" "running on gridengine.")
    parser.add_argument(
        "--host",
        "-x",
        help="The hostname or ip address "
        "running through an SSH connection. For non standard "
        "ports use host:port.",
    )
    parser.add_argument(
        "--interface",
        "-i",
        choices=["local", "ssh", "pbs", "sge", "slurm"],
        help="Specify how the remote kernel is launched.",
    )
    parser.add_argument(
        "--system",
        help="Install the kernel into the system "
        "directory so that it is available for all users. "
        "Might need admin privileges.",
        action="store_true",
    )
    parser.add_argument(
        "--workdir",
        help="Directory in which to start the "
        "kernel. If not specified it will use the current "
        "directory. This is important if the local and remote "
        "filesystems differ.",
    )
    parser.add_argument(
        "--remote-precmd",
        help="Command to execute on the "
        "remote host before launching the kernel, but after "
        "changing to the working directory.",
    )
    parser.add_argument(
        "--remote-launch-args",
        help="Arguments to add to the "
        "command that launches the remote session, i.e. the "
        "ssh or qlogin command, such as '-l h_rt=24:00:00' to "
        "limit job time on GridEngine jobs.",
    )
    parser.add_argument(
        "--tunnel-hosts",
        "-t",
        nargs="+",
        help="Tunnel the "
        "connection through the given ssh hosts before "
        "starting the endpoint interface. Works with any "
        "interface. For non standard ports use host:port.",
    )
    parser.add_argument(
        "--verbose", "-v", action="store_true", help="Running " "kernel will produce verbose debugging on the console."
    )

    # Temporarily remove 'manage' from the arguments
    raw_args = sys.argv[:]
    sys.argv.remove("manage")
    args = parser.parse_args()
    sys.argv = raw_args

    if args.add:
        kernel_name = add_kernel(
            args.interface,
            args.name,
            args.kernel_cmd,
            args.cpus,
            args.pe,
            args.language,
            args.system,
            args.workdir,
            args.host,
            args.remote_precmd,
            args.remote_launch_args,
            args.tunnel_hosts,
            args.verbose,
        )
        print("Installed kernel {0}.".format(kernel_name))
    elif args.delete:
        if args.delete in existing_kernels:
            delete_kernel(args.delete)
        else:
            print("Can't delete {0}".format(args.delete))
            print("\n".join(description[2:]))
    elif args.show:
        if args.show in existing_kernels:
            show_kernel(args.show)
        else:
            print("Kernel {0} doesn't exist".format(args.show))
            print("\n".join(description[2:]))
    else:
        parser.print_help()
예제 #6
0
def manage():
    """
    Manage the available remote_ikernels.

    All the options are pulled from arguments so we take no
    arguments here.
    """

    description = [
        "Remote IKernel management utility",
        "",
        "Currently installed kernels:",
    ]
    existing_kernels = {}

    # Sort so they are always in the same order
    for kernel_name in sorted(ks.find_kernel_specs()):
        if kernel_name.startswith(RIK_PREFIX):
            spec = ks.get_kernel_spec(kernel_name)
            display = "  ['{kernel_name}']: {desc}".format(
                kernel_name=kernel_name, desc=spec.display_name)
            existing_kernels[kernel_name] = spec
            description.append(display)

    # The raw formatter stops lines wrapping
    parser = argparse.ArgumentParser(
        prog="%prog manage",
        description="\n".join(description),
        formatter_class=argparse.RawTextHelpFormatter,
    )
    parser.add_argument(
        "--show",
        "-s",
        nargs="*",
        help="Print the contents of the kernel. "
        "Specify a list of kernels, or leave blank to list all.",
    )
    parser.add_argument(
        "--add",
        "-a",
        action="store_true",
        help="Add a new kernel according to other commandline options.",
    )
    parser.add_argument(
        "--delete",
        "-d",
        nargs="+",
        help="Remove the kernel and delete the associated kernel.json.",
    )
    parser.add_argument("--kernel_cmd",
                        "-k",
                        help="Kernel command "
                        "to install.")
    parser.add_argument("--name",
                        "-n",
                        help="Name to identify the kernel, e.g. 'Python 2.7'.")
    parser.add_argument("--language",
                        "-l",
                        help="Explicitly specify the language of the kernel.")
    parser.add_argument(
        "--cpus",
        "-c",
        type=int,
        help=
        "Launch the kernel as a multi-core job with this many cores if > 1.",
    )
    parser.add_argument(
        "--pe",
        help="Parallel environment to use on when running on gridengine.")
    parser.add_argument(
        "--host",
        "-x",
        help="The hostname or ip address running through an SSH connection. "
        "For non standard ports use host:port.",
    )
    parser.add_argument(
        "--interface",
        "-i",
        choices=["local", "ssh", "pbs", "sge", "sge_qrsh", "slurm", "lsf"],
        help="Specify how the remote kernel is launched.",
    )
    parser.add_argument(
        "--system",
        help=
        "Install the kernel into the system directory so that it is available "
        "for all users. Might need admin privileges.",
        action="store_true",
    )
    parser.add_argument(
        "--workdir",
        help="Directory in which to start the kernel. If not specified it "
        "will use the current directory. This is important if the local "
        "and remote filesystems differ.",
    )
    parser.add_argument(
        "--remote-precmd",
        help=
        "Command to execute on the remote host before launching the kernel, "
        "but after changing to the working directory.",
    )
    parser.add_argument(
        "--launch-cmd",
        help="Override the command used to launch the remote session "
        "(e.g. 'qrsh' to replace 'qlogin') or provide the full path for the "
        "executable if it is not in $PATH.",
    )
    parser.add_argument(
        "--remote-launch-args",
        help="Arguments to add to the command that launches the remote session, "
        "i.e. the ssh or qlogin command, such as '-l h_rt=24:00:00' to "
        "limit job time on GridEngine jobs.",
    )
    parser.add_argument(
        "--tunnel-hosts",
        "-t",
        nargs="+",
        help="Tunnel the connection through the given ssh hosts before "
        "starting the endpoint interface. Works with any "
        "interface. For non standard ports use host:port.",
    )
    parser.add_argument(
        "--verbose",
        "-v",
        action="store_true",
        help="Running kernel will produce verbose debugging on the console.",
    )
    parser.add_argument(
        "--version",
        "-V",
        action="version",
        version="Remote Jupyter kernel manager (version {0}).".format(
            __version__),
    )

    # Work on a copy so we don't mangle sys.argv when it is copied into
    # the kernel json
    raw_args = sys.argv[1:]
    # give argparse something unicode to deal with for PY2
    # otherwise, ignore if there is nothing to 'decode'
    try:
        raw_args = [x.decode("UTF-8") for x in sys.argv[1:]]
    except AttributeError:
        pass
    # Remove 'manage' to parse manage specific options
    raw_args.remove("manage")
    args = parser.parse_args(raw_args)

    if args.add:
        kernel_name, display_name = add_kernel(
            args.interface,
            args.name,
            args.kernel_cmd,
            args.cpus,
            args.pe,
            args.language,
            args.system,
            args.workdir,
            args.host,
            args.remote_precmd,
            args.remote_launch_args,
            args.tunnel_hosts,
            args.verbose,
            args.launch_cmd,
        )
        print("Added kernel ['{0}']: {1}.".format(kernel_name, display_name))
    elif args.delete:
        undeleted = []
        for to_delete in args.delete:
            if to_delete in existing_kernels:
                delete_kernel(to_delete)
                print("Removed kernel ['{0}']: {1}.".format(
                    to_delete, existing_kernels[to_delete].display_name))
            else:
                undeleted.append(to_delete)
        if undeleted:
            print("Can't delete: {0}.".format(", ".join(undeleted)))
            print("\n".join(description[2:]))
            raise SystemExit(1)
    elif args.show is not None:
        unshowable = []
        # Show all if none are specified
        for to_show in args.show or existing_kernels:
            if to_show in existing_kernels:
                show_kernel(to_show)
            else:
                unshowable.append(to_show)
        if unshowable:
            print("Could not find: {0}.".format(", ".join(unshowable)))
            print("\n".join(description[2:]))
            raise SystemExit(1)
    else:
        parser.print_help()
예제 #7
0
def manage():
    """
    Manage the available remote_ikernels.

    All the options are pulled from arguments so we take no
    arguments here.
    """

    description = [
        "Remote IKernel management utility", "", "Currently installed kernels:"
    ]
    existing_kernels = {}

    # Sort so they are always in the same order
    for kernel_name in sorted(ks.find_kernel_specs()):
        if kernel_name.startswith(RIK_PREFIX):
            spec = ks.get_kernel_spec(kernel_name)
            display = "  ['{kernel_name}']: {desc}".format(
                kernel_name=kernel_name, desc=spec.display_name)
            existing_kernels[kernel_name] = spec
            description.append(display)

    # The raw formatter stops lines wrapping
    parser = argparse.ArgumentParser(
        prog='%prog manage',
        description="\n".join(description),
        formatter_class=argparse.RawTextHelpFormatter)
    parser.add_argument('--show',
                        '-s',
                        help="Print the contents of the "
                        "kernel.")
    parser.add_argument('--add',
                        '-a',
                        action="store_true",
                        help="Add a new "
                        "kernel according to other commandline options.")
    parser.add_argument('--delete',
                        '-d',
                        help="Remove the kernel and delete "
                        "the associated kernel.json.")
    parser.add_argument('--kernel_cmd',
                        '-k',
                        help="Kernel command "
                        "to install.")
    parser.add_argument('--name',
                        '-n',
                        help="Name to identify the kernel,"
                        "e.g. 'Python 2.7'.")
    parser.add_argument('--language',
                        '-l',
                        help="Explicitly specify the "
                        "language of the kernel.")
    parser.add_argument('--cpus',
                        '-c',
                        type=int,
                        help="Launch the kernel "
                        "as a multi-core job with this many cores if > 1.")
    parser.add_argument('--pe',
                        help="Parallel environment to use on when"
                        "running on gridengine.")
    parser.add_argument('--host',
                        '-x',
                        help="The hostname or ip address "
                        "running through an SSH connection. For non standard "
                        "ports use host:port.")
    parser.add_argument('--interface',
                        '-i',
                        choices=['local', 'ssh', 'pbs', 'sge', 'slurm'],
                        help="Specify how the remote kernel is launched.")
    parser.add_argument('--system',
                        help="Install the kernel into the system "
                        "directory so that it is available for all users. "
                        "Might need admin privileges.",
                        action='store_true')
    parser.add_argument('--workdir',
                        help="Directory in which to start the "
                        "kernel. If not specified it will use the current "
                        "directory. This is important if the local and remote "
                        "filesystems differ.")
    parser.add_argument('--remote-precmd',
                        help="Command to execute on the "
                        "remote host before launching the kernel, but after "
                        "changing to the working directory.")
    parser.add_argument('--remote-launch-args',
                        help="Arguments to add to the "
                        "command that launches the remote session, i.e. the "
                        "ssh or qlogin command, such as '-l h_rt=24:00:00' to "
                        "limit job time on GridEngine jobs.")
    parser.add_argument('--tunnel-hosts',
                        '-t',
                        nargs='+',
                        help="Tunnel the "
                        "connection through the given ssh hosts before "
                        "starting the endpoint interface. Works with any "
                        "interface. For non standard ports use host:port.")
    parser.add_argument(
        '--verbose',
        '-v',
        action='store_true',
        help="Running "
        "kernel will produce verbose debugging on the console.")

    # Temporarily remove 'manage' from the arguments
    raw_args = sys.argv[:]
    sys.argv.remove('manage')
    args = parser.parse_args()
    sys.argv = raw_args

    if args.add:
        kernel_name = add_kernel(args.interface, args.name, args.kernel_cmd,
                                 args.cpus, args.pe, args.language,
                                 args.system, args.workdir, args.host,
                                 args.remote_precmd, args.remote_launch_args,
                                 args.tunnel_hosts, args.verbose)
        print("Installed kernel {0}.".format(kernel_name))
    elif args.delete:
        if args.delete in existing_kernels:
            delete_kernel(args.delete)
        else:
            print("Can't delete {0}".format(args.delete))
            print("\n".join(description[2:]))
    elif args.show:
        if args.show in existing_kernels:
            show_kernel(args.show)
        else:
            print("Kernel {0} doesn't exist".format(args.show))
            print("\n".join(description[2:]))
    else:
        parser.print_help()