Exemplo n.º 1
0
def test_rmarkdown(clear_now_and_after):
    """Test action Rmarkdown"""
    if not R_library('rmarkdown1').target_exists():
        pytest.xfail('rmarkdown is not properly installed.')

    clear_now_and_after("myreport.html")
    execute_workflow(
        r"""
        [10]

        report:
        ## Some random figure

        Generated by matplotlib


        [100]
        # generate report
        output: 'myreport.html'
        Rmarkdown(output=_output[0])
        """,
        options={"report_output": "report.md"},
    )

    assert os.path.isfile("myreport.html")
Exemplo n.º 2
0
Arquivo: utils.py Projeto: lwa19/dsc
def install_package(lib, libtype, dryrun=False, autoinstall=False):
    groups = re.search('(.*?)\((.*?)\)', lib)
    if groups is not None:
        lib = groups.group(1).strip()
        versions = [x.strip() for x in groups.group(2).split(',')]
        # only allow for one version specification: in previous versions of DSC we support multiple versions
        # but now restricting it to just one version because I think it is more confusing than helpful otherwise
        version = versions[0]
    else:
        version = ""
    if version and not (version.startswith('=') or version.startswith(">") or
                        version.startswith("<") or version.startswith("!=")):
        version = f"=={version}"
    if not dryrun:
        logger.info(
            f"{'Checking / installing' if autoinstall else 'Checking'} {libtype.replace('_', ' ')} {lib}{(' version' + version) if version else ''} ..."
        )
        if libtype == 'R_library':
            from sos.targets_r import R_library
            return R_library(f"{lib}{version}",
                             autoinstall=autoinstall).target_exists()
        else:
            from sos.targets_python import Py_Module
            return Py_Module(f"{lib}{version}",
                             autoinstall=autoinstall).target_exists()
    else:
        return (lib, version)
Exemplo n.º 3
0
def test_rmarkdown_to_stdout():
    if not R_library('rmarkdown1').target_exists():
        pytest.xfail('rmarkdown is not properly installed.')
    execute_workflow(
        r"""
        # generate report
        Rmarkdown:
            # this is title
        """
    )
Exemplo n.º 4
0
def test_rmarkdown_with_input(clear_now_and_after):
    if not R_library('rmarkdown1').target_exists():
        pytest.xfail('rmarkdown is not properly installed.')

    clear_now_and_after("myreport.html")
    # Rmarkdown with specified input.
    execute_workflow(
        r"""
        [10]
        report: output='a.md'
        ## Some random figure

        Generated by matplotlib


        [100]
        # generate report
        output: 'myreport.html'
        Rmarkdown(input='a.md', output=_output[0])
        """
    )
    assert os.path.isfile("myreport.html")
Exemplo n.º 5
0
def test_rmarkdown_with_action_output(clear_now_and_after):
    if not R_library('rmarkdown1').target_exists():
        pytest.xfail('rmarkdown is not properly installed.')

    clear_now_and_after("default_10.md", "default_20.md", "output.html")
    execute_workflow(
        r"""
        [10]
        report: output='default_10.md'
        A_10

        [20]
        report: output='default_20.md'
        A_20

        [100]
        # generate report
        Rmarkdown(input=['default_10.md', 'default_20.md'], output='output.html')
        """,
        options={"report_output": "${step_name}.md"},
    )
    for f in ["default_10.md", "default_20.md", "output.html"]:
        assert file_target(f).exists()
Exemplo n.º 6
0
def prepare_bookdown(files, title, author, date, description, no_section_number,
                     split_by, url, url_edit, repo, pdf, output, pdf_args):

    if not os.path.exists(os.path.join(env.tmp_dir, env.time) + '.deps'):
        R_library('rstudio/bookdown')
        R_library('rstudio/DT')
        executable('pandoc')
        os.system('touch %s' % os.path.join(env.tmp_dir, env.time) + '.deps')
    #
    if title:
        idx['title'] = title
    if author:
        idx['author'] = author
    if date:
        idx['date'] = date
    if description:
        if os.path.isfile(description):
            idx['description'] = open(description).read()
        else:
            idx['description'] = description
    if url:
        idx['url'] = url
    else:
        del idx['url']
    if repo:
        idx['github-repo'] = repo
        cfg['repo'] = repo
    else:
        del idx['github-repo']
        del cfg['repo']
    idx['author'] = '&copy; ' + idx['author']
    out['bookdown::gitbook']['css'] =  'style.css'
    out['bookdown::html_chapters']['css'] = ['style.css', 'toc.css']
    out['bookdown::epub_book']['stylesheet'] = 'style.css'
    if pdf:
        out['bookdown::pdf_book']['includes']['in_header'] = os.path.join(env.tmp_dir, 'preamble.tex')
    else:
        del out['bookdown::pdf_book']
    workdir = os.getcwd()
    if output:
        output = os.path.abspath(os.path.expanduser(output))
        os.makedirs(output, exist_ok = True)
        cfg['output_dir'] = os.path.basename(output)
        workdir = os.path.dirname(output)
    else:
        os.makedirs(cfg['output_dir'], exist_ok = True)
    out['bookdown::gitbook']['config']['toc']['before'] = '{}'.format(
        out['bookdown::gitbook']['config']['toc']['before'].replace('VALUE', idx['title']))
    out['bookdown::gitbook']['config']['toc']['after'] = '{}'.format(
        out['bookdown::gitbook']['config']['toc']['after'].replace('VALUE',
                                                                   '{} {}'.format(env.year, idx['author'])))
    if split_by:
        out['bookdown::gitbook']['split_by'] = split_by
    if no_section_number:
        out['bookdown::gitbook']['number_sections'] = 0
    if pdf:
        pdf = (pdf, cfg['output_dir'], '{} {} {} --toc 3 --long_ref --font_size 12 {}'.\
               format('-a {}'.format(repr(author)) if author else '',
                      '-t {}'.format(repr(title)) if title else '',
                      '-d {}'.format(repr(date)) if date else '',
                      ' '.join(pdf_args)))
    # Move files around to resolve path problem for bookdown
    filenames = ['index.rmd' if idx == 0 else '{}_{}'.format(str(idx).zfill(4), os.path.basename(x))
                 for idx, x in enumerate(files)]
    filenames = [x if x.endswith('.rmd') else os.path.splitext(x)[0] + '.rmd' for x in filenames]
    for x, y in zip(files[1:], filenames[1:]):
        shutil.copy(x, os.path.join(workdir, y))
    with open(files[0]) as f:
        tmp = f.readlines()
        if len(tmp) == 0:
            tmp.append(' ')
        if not (tmp[0].startswith('# ')):
            name = os.path.splitext(os.path.basename(files[0]))[0].lstrip('0123456789.- ')
            tmp = ['# ' + re.sub(r'\-|_', ' ', name) + '\n'] \
                    + tmp
        tmp = ''.join(tmp)
    for i, fn in enumerate(files):
        if i == 0:
            continue
        with cd(workdir):
            with open(filenames[i]) as f:
                lines = f.readlines()
            if len(lines) == 0:
                lines.append(' ')
            if not (lines[0].startswith('# ')):
                name = os.path.splitext(os.path.basename(fn))[0].lstrip('0123456789.- ')
                lines = ['# ' + re.sub(r'\-|_', ' ', name) + '\n'] \
                        + lines
                with open(filenames[i], 'w') as f:
                    f.write(''.join(lines))
    # write resources
    with codecs.open(os.path.join(workdir, cfg['output_dir'], 'style.css'), 'w', encoding='UTF-8') as f:
            f.write(style)
    with codecs.open(os.path.join(workdir, cfg['output_dir'], 'toc.css'), 'w', encoding='UTF-8') as f:
            f.write(toc)
    with codecs.open(os.path.join(env.tmp_dir, 'preamble.tex'), 'w', encoding='UTF-8') as f:
            f.write(tex)
    error_msg = None
    try:
        with cd(workdir):
            with open(filenames[0], 'w') as f:
                f.write('---\n{}\n---\n{}'.format(dict2str(idx), tmp))
            cfg['rmd_files']['html'] = list(filenames)
            cfg['rmd_files']['latex'] = list(filenames)
            with open('_output.yml', 'w') as f:
                f.write(dict2str(out))
            with open('_bookdown.yml', 'w') as f:
                f.write(dict2str(cfg))
            sos_file = os.path.join(env.tmp_dir, 'bookdown.sos')
            with open(sos_file, 'w') as f:
                f.write(get_sos(filenames, pdf, workdir))
            os.system('sos run {}'.format(sos_file))
    except Exception as e:
        error_msg = e
    os.remove(sos_file)
    try:
        with cd(workdir):
            os.remove('_output.yml')
            os.remove('_bookdown.yml')
            for f in filenames:
                os.remove(f)
    except:
        pass
    if error_msg is not None:
        raise RuntimeError(error_msg)