Esempio n. 1
0
def _get_diff_entry_stream(path, blob, ref_name, repo_dir):
    """Get a stream to the notebook, for a given diff entry's path and blob

    Returns None if path is not a Notebook file, and EXPLICIT_MISSING_FILE
    if path is missing."""
    if path:
        if not path.endswith('.ipynb'):
            return None
        if blob is None:
            # Diffing against working copy, use file on disk!
            with pushd(repo_dir):
                # Ensure we filter if appropriate:
                if ref_name is None:
                    # We are diffing against working dir, so remote is candidate
                    ret = apply_possible_filter(path)
                    # ret == path means no filter was applied
                    if ret != path:
                        return ret
                try:
                    return io.open(path, encoding='utf-8')
                except IOError:
                    return EXPLICIT_MISSING_FILE
        else:
            # There were strange issues with passing blob data_streams around,
            # so we solve this by reading into a StringIO buffer.
            # The penalty should be low as long as changed_notebooks are used
            # properly as an iterator.
            f = BlobWrapper(blob.data_stream.read().decode('utf-8'))
            f.name = '%s (%s)' % (path, ref_name)
            return f
    return EXPLICIT_MISSING_FILE
Esempio n. 2
0
def _get_diff_entry_stream(path, blob, ref_name, repo_dir):
    """Get a stream to the notebook, for a given diff entry's path and blob

    Returns None if path is not a Notebook file, and EXPLICIT_MISSING_FILE
    if path is missing."""
    if path:
        if not path.endswith('.ipynb'):
            return None
        if blob is None:
            # Diffing against working copy, use file on disk!
            with pushd(repo_dir):
                # Ensure we filter if appropriate:
                if ref_name is None:
                    # We are diffing against working dir, so remote is candidate
                    ret = apply_possible_filter(path)
                    # ret == path means no filter was applied
                    if ret != path:
                        return ret
                try:
                    return io.open(path)
                except IOError:
                    return EXPLICIT_MISSING_FILE
        else:
            # There were strange issues with passing blob data_streams around,
            # so we solve this by reading into a StringIO buffer.
            # The penalty should be low as long as changed_notebooks are used
            # properly as an iterator.
            f = BlobWrapper(blob.data_stream.read().decode('utf-8'))
            f.name = '%s (%s)' % (path, ref_name)
            return f
    return EXPLICIT_MISSING_FILE
def test_apply_filter_valid_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    gitattr = locate_gitattributes()
    with io.open(gitattr, 'a', encoding="utf8") as f:
        f.write(u'\n*.ipynb\tfilter=myfilter\n')
    call('git config --local --add filter.myfilter.clean "cat"')
    f = apply_possible_filter(path)
    assert isinstance(f, StringIO)
    # Read validates notebook:
    nbformat.validate(nbformat.read(f, as_version=4))
def test_apply_filter_valid_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    gitattr = locate_gitattributes()
    with io.open(gitattr, 'a', encoding="utf8") as f:
        f.write(u'\n*.ipynb\tfilter=myfilter\n')
    call('git config --local --add filter.myfilter.clean "cat"')
    f = apply_possible_filter(path)
    assert isinstance(f, StringIO)
    # Read validates notebook:
    nbformat.validate(nbformat.read(f, as_version=4))
def test_apply_filter_valid_filter(git_repo):
    try:
        call('cat --help')
        filter_cmd = 'cat'
    except (CalledProcessError, FileNotFoundError):
        filter_cmd = 'findstr x*'
    path = pjoin(git_repo, 'diff.ipynb')
    gitattr = locate_gitattributes()
    with io.open(gitattr, 'a', encoding="utf8") as f:
        f.write(u'\n*.ipynb\tfilter=myfilter\n')
    call('git config --local --add filter.myfilter.clean "%s"' % filter_cmd)
    f = apply_possible_filter(path)
    assert isinstance(f, StringIO)
    # Read validates notebook:
    nbformat.validate(nbformat.read(f, as_version=4))
Esempio n. 6
0
def _get_diff_entry_stream(path, blob, ref_name, repo_dir):
    """Get a stream to the notebook, for a given diff entry's path and blob

    Returns None if path is not a Notebook file, and EXPLICIT_MISSING_FILE
    if path is missing, or the blob is None (unless diffing against working
    tree).
    """
    if path:
        if not path.endswith('.ipynb'):
            return None
        if ref_name is GitRefWorkingTree:
            # Diffing against working copy, use file on disk!
            with pushd(repo_dir):
                # We are diffing against working dir, so ensure we apply
                # any git filters before comparing:
                ret = apply_possible_filter(path)
                # ret == path means no filter was applied
                if ret != path:
                    return ret
                try:
                    return io.open(path, encoding='utf-8')
                except IOError:
                    return EXPLICIT_MISSING_FILE
        elif blob is None:
            # GitPython uses a None blob to indicate if a file was deleted or
            # added. Workaround for GitPython issue #749.
            return EXPLICIT_MISSING_FILE
        else:
            # There were strange issues with passing blob data_streams around,
            # so we solve this by reading into a StringIO buffer.
            # The penalty should be low as long as changed_notebooks are used
            # properly as an iterator.
            f = BlobWrapper(blob.data_stream.read().decode('utf-8'))
            f.name = '%s (%s)' % (
                path,
                ref_name if ref_name != GitRefIndex else '<INDEX>'
            )
            return f
    return EXPLICIT_MISSING_FILE
def test_apply_filter_invalid_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    gitattr = locate_gitattributes()
    with io.open(gitattr, 'a', encoding="utf8") as f:
        f.write(u'\n*.ipynb\tfilter=myfilter\n')
    assert apply_possible_filter(path) == path
def test_apply_filter_no_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    assert apply_possible_filter(path) == path
def test_apply_filter_no_repo(filespath):
    path = pjoin(filespath, 'foo--1.ipynb')
    assert apply_possible_filter(path) == path
def test_apply_filter_invalid_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    gitattr = locate_gitattributes()
    with io.open(gitattr, 'a', encoding="utf8") as f:
        f.write(u'\n*.ipynb\tfilter=myfilter\n')
    assert apply_possible_filter(path) == path
def test_apply_filter_no_filter(git_repo):
    path = pjoin(git_repo, 'diff.ipynb')
    assert apply_possible_filter(path) == path
def test_apply_filter_no_repo(filespath):
    path = pjoin(filespath, 'foo--1.ipynb')
    assert apply_possible_filter(path) == path