예제 #1
0
def parse_source_notebook(path: str,
                          encoding: str,
                          debugging: bool = False) -> Notebook:
    """
    Parse a Databricks source notebook into a Notebook object.

    :param path:      the path to the notebook
    :param encoding:  the encoding to use when reading the file
    :param debugging: True to enable debug messages, False otherwise

    :returns: a parsed Notebook object

    :raises NotebookParseError: if the notebook cannot be parsed
    :raises NotebookError:      other errors (e.g., invalid file type)
    """
    prev_debug = debug_is_enabled()
    try:
        set_debug(debugging)
        return _parse_source_notebook(path, encoding)
    finally:
        set_debug(prev_debug)
예제 #2
0
def main():
    if os.environ.get('COURSE_DEBUG', 'false') == 'true':
        set_debug(True)

    try:
        # Load the configuration and then run it through update_config() to
        # ensure that course name-related settings are updated, if necessary.
        cfg = update_config(load_config(CONFIG_PATH, show_warnings=True))

        # Update the environment, for subprocesses we need to invoke.

        os.environ['EDITOR'] = cfg['EDITOR']
        os.environ['PAGER'] = cfg['PAGER']

        # Loop over the argument list, since we need to support chaining some
        # commands (e.g., "course download build"). This logic emulates
        # what was in the original shell script version, and it's not easily
        # handled by Python's argparse or docopt. So, ugly as it is, we go
        # with manual parsing.

        if len(sys.argv) == 1:
            args = ["help"]
        else:
            args = sys.argv[1:]

        i = 0
        while i < len(args):
            cmd = args[i]

            if cmd in ('--version', '-V'):
                print(VERSION)
                break

            if cmd in ('toolversions', 'tool-versions'):
                print_tool_versions()
                break

            if cmd in ('-n', '--name'):
                try:
                    i += 1
                    # Changing the name of the course has to reset the
                    # build.yaml name.
                    del cfg['COURSE_YAML']
                    cfg['COURSE_NAME'] = args[i]
                    cfg = update_config(cfg)
                except IndexError:
                    die("Saw -n or --name without subsequent course name.")

            elif cmd in ('-f', '--build-file'):
                try:
                    i += 1
                    cfg['COURSE_YAML'] = args[i]
                    cfg = update_config(cfg)
                except IndexError:
                    die("Saw -f or --build-file without subsequent file name.")

            elif cmd in ('-h', '--help', 'help', 'usage'):
                help(cfg)
                break

            elif cmd in ('work-on', 'workon'):
                try:
                    i += 1
                    cfg = work_on(cfg, args[i], CONFIG_PATH)
                except IndexError:
                    die('Expected course name after "work-on".')

            elif cmd == 'tag':
                git_tag(cfg)

            elif cmd == 'which':
                which(cfg)

            elif cmd in ('install-tools', 'installtools'):
                install_tools()

            elif cmd == 'download':
                download(cfg)

            elif cmd == 'upload':
                upload(cfg)

            elif cmd in ('upload-built', 'uploadbuilt'):
                upload_build(cfg)

            elif cmd == 'build':
                build_and_upload(cfg)

            elif cmd in ('build-local', 'buildlocal'):
                build_local(cfg)

            elif cmd == 'clean':
                clean(cfg)

            elif cmd in ('clean-source', 'cleansource'):
                clean_source(cfg)

            elif cmd in ('deploy-images', 'deployimages'):
                deploy_images(cfg)

            elif cmd == 'status':
                git_status(cfg)

            elif cmd == 'diff':
                git_diff(cfg)

            elif cmd == 'difftool':
                git_difftool(cfg)

            elif cmd == 'home':
                browse_directory(cfg, cfg['COURSE_HOME'], 'home')

            elif cmd == 'modules':
                browse_directory(cfg, cfg['COURSE_MODULES'], 'modules')

            elif cmd == 'repo':
                browse_directory(cfg, cfg['COURSE_REPO'], 'repo')

            elif cmd == 'config':
                cfg = edit_config(cfg)

            elif cmd == 'yaml':
                edit_file(cfg, build_file_path(cfg), 'yaml')

            elif cmd == 'guide':
                edit_file(
                    cfg, os.path.join(cfg['COURSE_HOME'], 'Teaching-Guide.md'),
                    'guide')

            elif cmd == ('deploy-images', 'deployimages'):
                deploy_images(cfg)

            elif cmd == 'grep':
                try:
                    i += 1
                    pattern = args[i]
                    if pattern == '-i':
                        case_blind = True
                        i += 1
                        pattern = args[i]
                    else:
                        case_blind = False

                    grep(cfg, pattern, case_blind)
                except IndexError:
                    die('Missing grep argument(s).')

            elif cmd == 'sed':
                try:
                    i += 1
                    sed(cfg, args[i])
                except IndexError:
                    die('Missing sed argument.')

            elif cmd == 'xargs':
                # All the remaining arguments go to the command.
                try:
                    i += 1
                    command = args[i]
                    if i < len(args):
                        i += 1
                        command_args = args[i:]
                    else:
                        command_args = []

                    run_command_on_notebooks(cfg, command, command_args)
                    break
                except IndexError:
                    die('Missing command to run.')

            elif cmd == 'set':
                try:
                    i += 1
                    setting = args[i]
                    fields = setting.split('=')
                    if len(fields) != 2:
                        die('Argument to "set" must be of the form CONF=VAL.')
                    key, value = fields
                    value = value.replace('"', '')
                    cfg = configure(cfg, CONFIG_PATH, key, value)
                except IndexError:
                    die('Missing CONF=VAL argument to "set".')

            elif cmd == "showconfig":
                hdr = "Current configuration"
                print('-' * len(hdr))
                print(hdr)
                print('-' * len(hdr))
                for key in sorted(cfg.keys()):
                    print(f'{key}="{cfg[key]}"')

            else:
                die(f'"{cmd}" is not a valid "course" subcommand.')

            i += 1

    except CourseError as e:
        error(str(e))

    except bdc.BDCError as e:
        error(str(e))

    except KeyboardInterrupt:
        error('\n*** Interrupted.')
예제 #3
0
def main():
    if os.environ.get("COURSE_DEBUG", "false") == "true":
        set_debug(True)

    try:
        # Load the configuration and then run it through update_config() to
        # ensure that course name-related settings are updated, if necessary.
        cfg = update_config(load_config(CONFIG_PATH, show_warnings=True))

        # Update the environment, for subprocesses we need to invoke.

        os.environ["EDITOR"] = cfg["EDITOR"]
        os.environ["PAGER"] = cfg["PAGER"]

        # Loop over the argument list, since we need to support chaining some
        # commands (e.g., "course download build"). This logic emulates
        # what was in the original shell script version, and it's not easily
        # handled by Python's argparse or docopt. So, ugly as it is, we go
        # with manual parsing.

        if len(sys.argv) == 1:
            args = ["help"]
        else:
            args = sys.argv[1:]

        i = 0
        while i < len(args):
            cmd = args[i]

            if cmd in ("--version", "-V"):
                print(VERSION)
                break

            if cmd in ("toolversions", "tool-versions"):
                print_tool_versions()
                break

            if cmd in ("-n", "--name"):
                try:
                    i += 1
                    # Changing the name of the course has to reset the
                    # build.yaml name.
                    del cfg["COURSE_YAML"]
                    cfg["COURSE_NAME"] = args[i]
                    cfg = update_config(cfg)
                except IndexError:
                    die("Saw -n or --name without subsequent course name.")

            elif cmd in ("-f", "--build-file"):
                try:
                    i += 1
                    cfg["COURSE_YAML"] = args[i]
                    cfg = update_config(cfg)
                except IndexError:
                    die("Saw -f or --build-file without subsequent file name.")

            elif cmd in ("-h", "--help", "help", "usage"):
                help(cfg)
                break

            elif cmd in ("work-on", "workon"):
                try:
                    i += 1
                    cfg = work_on(cfg, args[i], CONFIG_PATH)
                except IndexError:
                    die('Expected course name after "work-on".')

            elif cmd == "tag":
                git_tag(cfg)

            elif cmd == "which":
                which(cfg)

            elif cmd in ("install-tools", "installtools"):
                install_tools()

            elif cmd == "download":
                download(cfg)

            elif cmd == "upload":
                upload(cfg)

            elif cmd in ("upload-built", "uploadbuilt"):
                upload_build(cfg)

            elif cmd == "build":
                build_and_upload(cfg)

            elif cmd in ("build-local", "buildlocal"):
                build_local(cfg)

            elif cmd == "clean":
                clean(cfg)

            elif cmd in ("clean-source", "cleansource"):
                clean_source(cfg)

            elif cmd in ("deploy-images", "deployimages"):
                deploy_images(cfg)

            elif cmd == "status":
                git_status(cfg)

            elif cmd == "diff":
                git_diff(cfg)

            elif cmd == "difftool":
                git_difftool(cfg)

            elif cmd == "home":
                browse_directory(cfg, cfg["COURSE_HOME"], "home")

            elif cmd == "modules":
                browse_directory(cfg, cfg["COURSE_MODULES"], "modules")

            elif cmd == "repo":
                browse_directory(cfg, cfg["COURSE_REPO"], "repo")

            elif cmd == "config":
                cfg = edit_config(cfg)

            elif cmd == "yaml":
                edit_file(cfg, build_file_path(cfg), "yaml")

            elif cmd == "guide":
                edit_file(
                    cfg, os.path.join(cfg["COURSE_HOME"], "Teaching-Guide.md"),
                    "guide")

            elif cmd == ("deploy-images", "deployimages"):
                deploy_images(cfg)

            elif cmd == "grep":
                try:
                    i += 1
                    pattern = args[i]
                    if pattern == "-i":
                        case_blind = True
                        i += 1
                        pattern = args[i]
                    else:
                        case_blind = False

                    grep(cfg, pattern, case_blind)
                except IndexError:
                    die("Missing grep argument(s).")

            elif cmd == "sed":
                try:
                    i += 1
                    sed(cfg, args[i])
                except IndexError:
                    die("Missing sed argument.")

            elif cmd == "xargs":
                # All the remaining arguments go to the command.
                try:
                    i += 1
                    command = args[i]
                    if i < len(args):
                        i += 1
                        command_args = args[i:]
                    else:
                        command_args = []

                    run_command_on_notebooks(cfg, command, command_args)
                    break
                except IndexError:
                    die("Missing command to run.")

            elif cmd == "set":
                try:
                    i += 1
                    setting = args[i]
                    fields = setting.split("=")
                    if len(fields) != 2:
                        die('Argument to "set" must be of the form CONF=VAL.')
                    key, value = fields
                    value = value.replace('"', "")
                    cfg = configure(cfg, CONFIG_PATH, key, value)
                except IndexError:
                    die('Missing CONF=VAL argument to "set".')

            elif cmd == "showconfig":
                hdr = "Current configuration"
                print("-" * len(hdr))
                print(hdr)
                print("-" * len(hdr))
                for key in sorted(cfg.keys()):
                    print(f'{key}="{cfg[key]}"')

            else:
                die(f'"{cmd}" is not a valid "course" subcommand.')

            i += 1

    except CourseError as e:
        error(str(e))

    except bdc.BDCError as e:
        error(str(e))

    except KeyboardInterrupt:
        error("\n*** Interrupted.")