Esempio n. 1
0
def _md5sum_is_current(src_file, md5_file):
    if not os.path.exists(md5_file):
        return False
    src_md5 = genrst.get_md5sum(src_file)
    with open(md5_file, "r") as file:
        ref_md5 = file.read()
    return src_md5 == ref_md5
Esempio n. 2
0
def _create_tutorial_section(fname, src_dir, target_dir):
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    src_file = os.path.normpath(os.path.join(src_dir, fname))
    # Check if the same tutorial script has been already run
    md5_file = os.path.join(target_dir, f"{fname}.md5")
    if _md5sum_is_current(src_file, md5_file):
        return

    file_conf, script_blocks = parser.split_code_and_text_blocks(src_file)

    # Remove *.py suffix
    base_image_name = os.path.splitext(fname)[0]
    image_path_template = os.path.join(target_dir,
                                       base_image_name + "_{0:02}.png")

    script_vars = {
        "execute_script": True,
        "image_path_iterator": scrapers.ImagePathIterator(image_path_template),
        "src_file": src_file,
        "memory_delta": [],
    }
    tutorial_globals = {"__doc__": "", "__name__": "__main__"}
    compiler = codeop.Compile()

    content_rst = ""
    for block_label, block_content, line_no in script_blocks:
        if block_label == "code":
            # Run code and save output images
            code_output = genrst.execute_code_block(
                compiler=compiler,
                block=(block_label, block_content, line_no),
                example_globals=tutorial_globals,
                script_vars=script_vars,
                gallery_conf={
                    "abort_on_example_error": True,
                    "src_dir": ".",
                    "execute_script": True,
                    "show_memory": False,
                    "image_scrapers": (scrapers.matplotlib_scraper, ),
                })
            content_rst += genrst.codestr2rst(block_content,
                                              lineno=None) + "\n"
            content_rst += code_output

        else:
            content_rst += block_content + "\n\n"

    with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file:
        file.write(content_rst)

    # Write checksum of file to avoid unnecessary rerun
    with open(md5_file, "w") as file:
        file.write(genrst.get_md5sum(src_file))
Esempio n. 3
0
def _create_tutorial_section(fname, src_dir, target_dir):
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    src_file = os.path.normpath(os.path.join(src_dir, fname))
    # Check if the same tutorial script has been already run
    md5_file = os.path.join(target_dir, f"{fname}.md5")
    if _md5sum_is_current(src_file, md5_file):
        return

    file_conf, script_blocks = parser.split_code_and_text_blocks(src_file)

    # Remove *.py suffix
    base_image_name = os.path.splitext(fname)[0]
    image_path_template = os.path.join(target_dir,
                                       base_image_name+"_{0:02}.png")

    block_vars = {'execute_script': True, 'fig_count': 0,
                  'image_path': image_path_template, 'src_file': src_file}
    tutorial_globals = {
        "__doc__": "",
        "__name__": "__main__"
    }
    compiler = codeop.Compile()

    content_rst = ""
    for block_label, block_content, line_no in script_blocks:
        if block_label == 'code':
            # Run code and save output images
            code_output, rtime = genrst.execute_code_block(
                compiler=compiler, src_file=src_file, code_block=block_content,
                lineno=line_no, example_globals=tutorial_globals,
                block_vars=block_vars,
                gallery_conf = {"abort_on_example_error": True, "src_dir":"."}
            )
            content_rst += genrst.codestr2rst(
                block_content, lineno=None
            ) + "\n"
            content_rst += code_output

        else:
            content_rst += block_content + "\n\n"

    with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file:
        file.write(content_rst)
    
    # Write checksum of file to avoid unnecessary rerun
    with open(md5_file, "w") as file:
        file.write(genrst.get_md5sum(src_file))
Esempio n. 4
0
def test_md5sums():
    """Test md5sum check functions work on know file content"""

    with tempfile.NamedTemporaryFile('w') as f:
        f.write('Local test\n')
        f.flush()
        file_md5 = sg.get_md5sum(f.name)
        # verify correct md5sum
        assert_equal('ea8a570e9f3afc0a7c3f2a17a48b8047', file_md5)
        # True because is a new file
        assert_true(sg.check_md5sum_change(f.name))
        # False because file has not changed since last check
        assert_false(sg.check_md5sum_change(f.name))

    os.remove(f.name + '.md5')
Esempio n. 5
0
def test_md5sums():
    """Test md5sum check functions work on know file content"""

    with tempfile.NamedTemporaryFile('w') as f:
        f.write('Local test\n')
        f.flush()
        file_md5 = sg.get_md5sum(f.name)
        # verify correct md5sum
        assert_equal('ea8a570e9f3afc0a7c3f2a17a48b8047', file_md5)
        # True because is a new file
        assert_true(sg.check_md5sum_change(f.name))
        # False because file has not changed since last check
        assert_false(sg.check_md5sum_change(f.name))

    os.remove(f.name + '.md5')
Esempio n. 6
0
def test_md5sums():
    """Test md5sum check functions work on know file content"""

    with tempfile.NamedTemporaryFile('w') as f:
        f.write('Local test\n')
        f.flush()
        file_md5 = sg.get_md5sum(f.name)
        # verify correct md5sum
        assert_equal('ea8a570e9f3afc0a7c3f2a17a48b8047', file_md5)
        # False because is a new file
        assert_false(sg.md5sum_is_current(f.name))
        # Write md5sum to file to check is current
        with open(f.name + '.md5', 'w') as file_checksum:
            file_checksum.write(file_md5)
        assert_true(sg.md5sum_is_current(f.name))
    os.remove(f.name + '.md5')
Esempio n. 7
0
def test_md5sums():
    """Test md5sum check functions work on know file content."""
    with tempfile.NamedTemporaryFile('wb', delete=False) as f:
        f.write(b'Local test\n')
    try:
        file_md5 = sg.get_md5sum(f.name)
        # verify correct md5sum
        assert 'ea8a570e9f3afc0a7c3f2a17a48b8047' == file_md5
        # False because is a new file
        assert not sg.md5sum_is_current(f.name)
        # Write md5sum to file to check is current
        with open(f.name + '.md5', 'w') as file_checksum:
            file_checksum.write(file_md5)
        try:
            assert sg.md5sum_is_current(f.name)
        finally:
            os.remove(f.name + '.md5')
    finally:
        os.remove(f.name)
Esempio n. 8
0
def test_md5sums():
    """Test md5sum check functions work on know file content"""

    with tempfile.NamedTemporaryFile('wb', delete=False) as f:
        f.write(b'Local test\n')
    try:
        file_md5 = sg.get_md5sum(f.name)
        # verify correct md5sum
        assert 'ea8a570e9f3afc0a7c3f2a17a48b8047' == file_md5
        # False because is a new file
        assert not sg.md5sum_is_current(f.name)
        # Write md5sum to file to check is current
        with open(f.name + '.md5', 'w') as file_checksum:
            file_checksum.write(file_md5)
        try:
            assert sg.md5sum_is_current(f.name)
        finally:
            os.remove(f.name + '.md5')
    finally:
        os.remove(f.name)
Esempio n. 9
0
def test_md5sums(mode, expected_md5):
    """Test md5sum check functions work on know file content."""
    file_content = b'Local test\r\n'
    with tempfile.NamedTemporaryFile('wb', delete=False) as f:
        f.write(file_content)
    try:
        file_md5 = sg.get_md5sum(f.name, mode)
        # verify correct md5sum
        assert file_md5 == expected_md5
        # False because is a new file
        assert not sg.md5sum_is_current(f.name)
        # Write md5sum to file to check is current
        with open(f.name + '.md5', 'w') as file_checksum:
            file_checksum.write(file_md5)
        try:
            assert sg.md5sum_is_current(f.name, mode)
        finally:
            os.remove(f.name + '.md5')
    finally:
        os.remove(f.name)
Esempio n. 10
0
def _create_tutorial_section(fname, src_dir, target_dir):
    if not os.path.exists(target_dir):
        os.makedirs(target_dir)

    src_file = os.path.normpath(os.path.join(src_dir, fname))
    # Check if the same tutorial script has been already run
    md5_file = os.path.join(target_dir, f"{fname}.md5")
    if _md5sum_is_current(src_file, md5_file):
        return

    file_conf, script_blocks = parser.split_code_and_text_blocks(src_file)

    # Remove *.py suffix
    base_image_name = os.path.splitext(fname)[0]
    # Locate file in tutorial target directory
    abs_base_image_name = os.path.join(os.getcwd(), "tutorial", "target",
                                       base_image_name)
    image_path_template = abs_base_image_name + "_{0:02}.png"

    fake_main = module_from_spec(spec_from_loader('__main__', None))
    script_vars = {
        "execute_script": True,
        "image_path_iterator": scrapers.ImagePathIterator(image_path_template),
        "src_file": src_file,
        "memory_delta": [],
        "fake_main": fake_main
    }
    tutorial_globals = fake_main.__dict__
    tutorial_globals.update({
        "__doc__": "",
    })
    gallery_conf = copy.deepcopy(DEFAULT_GALLERY_CONF)
    gallery_conf.update({
        "abort_on_example_error": True,
        "src_dir": os.getcwd(),
        "execute_script": True,
        "inspect_global_variables": False,
        "call_memory": (lambda func: (0., func())),
        "image_scrapers": (scrapers.matplotlib_scraper, ),
    })
    compiler = codeop.Compile()

    content_rst = ""
    for block_label, block_content, line_no in script_blocks:
        if block_label == "code":
            # Run code and save output images
            code_output = genrst.execute_code_block(
                compiler=compiler,
                block=(block_label, block_content, line_no),
                example_globals=tutorial_globals,
                script_vars=script_vars,
                gallery_conf=gallery_conf)
            content_rst += genrst.codestr2rst(block_content,
                                              lineno=None) + "\n"
            content_rst += code_output

        else:
            content_rst += block_content + "\n\n"

    with open(os.path.join(target_dir, f"{base_image_name}.rst"), "w") as file:
        file.write(content_rst)

    # Write checksum of file to avoid unnecessary rerun
    with open(md5_file, "w") as file:
        file.write(genrst.get_md5sum(src_file))