示例#1
0
文件: latex.py 项目: hex539/BAPCtools
def build_problem_pdf(problem):
    prepare_problem(problem)

    builddir = config.tmpdir / problem.id

    util.copy_and_substitute(
        config.tools_root / 'latex/problem.tex', builddir / 'problem.tex', {
            'problemlabel': problem.label,
            'problemyamlname': problem.config['name'],
            'problemauthor': problem.config.get('author'),
            'timelimit': get_tl(problem.config),
            'problemdir': builddir,
        })

    ensure_symlink(builddir / 'bapc.cls', config.tools_root / 'latex/bapc.cls')

    for i in range(3):
        ok, err, out = util.exec_command(
            PDFLATEX +
            ['-output-directory', builddir, builddir / 'problem.tex'],
            0,
            False,
            cwd=builddir,
            stdout=subprocess.PIPE)
        if ok is not True:
            print(f'{_c.red}Failure compiling pdf:{_c.reset}\n{out}')
            return False

    # link the output pdf
    output_pdf = problem.path / 'problem.pdf'
    ensure_symlink(output_pdf, builddir / 'problem.pdf', True)

    print(f'{_c.green}Pdf written to {output_pdf}{_c.reset}')
    return True
示例#2
0
def build_problem_pdf(problem, solutions=False):
    main_file = 'solution.tex' if solutions else 'problem.tex'
    prepare_problem(problem)

    builddir = problem.tmpdir

    util.copy_and_substitute(
        config.tools_root / 'latex' / main_file,
        builddir / main_file,
        {
            'problemlabel': problem.label,
            'problemyamlname': problem.settings.name.replace('_', ' '),
            'problemauthor': problem.settings.author,
            'timelimit': get_tl(problem),
            'problemdir': problem.path.absolute().as_posix(),
            'builddir': problem.tmpdir.as_posix(),
        },
    )

    return build_latex_pdf(builddir, builddir / main_file, problem.path)
示例#3
0
def build_problem_pdf(problem):
    prepare_problem(problem)

    builddir = problem.tmpdir

    util.copy_and_substitute(
        config.tools_root / 'latex/problem.tex', builddir / 'problem.tex', {
            'problemlabel': problem.label,
            'problemyamlname': problem.settings.name.replace('_', ' '),
            'problemauthor': problem.settings.author,
            'timelimit': problem.settings.timelimit,
            'problemdir': problem.path.absolute().as_posix(),
            'builddir': problem.tmpdir.as_posix(),
        })

    env = get_environment()
    for i in range(3):
        ret = util.exec_command(
            PDFLATEX + ['-output-directory', builddir,
                        builddir / 'problem.tex'],
            0,
            False,
            cwd=builddir,
            stdout=subprocess.PIPE,
            env=env,
            )
        if ret.ok is not True:
            print(f'{Fore.RED}Failure compiling pdf:{Style.RESET_ALL}\n{ret.out}')
            return False

    # link the output pdf
    output_pdf = problem.path / 'problem.pdf'
    ensure_symlink(output_pdf, builddir / 'problem.pdf', True)

    print(f'{Fore.GREEN}Pdf written to {output_pdf}{Style.RESET_ALL}')
    return True
示例#4
0
文件: latex.py 项目: hex539/BAPCtools
def build_contest_pdf(contest, problems, solutions=False, web=False):
    builddir = config.tmpdir / contest
    builddir.mkdir(parents=True, exist_ok=True)
    build_type = 'solution' if solutions else 'problem'

    main_file = 'solutions' if solutions else 'contest'
    main_file += '-web.tex' if web else '.tex'

    if solutions:
        ensure_symlink(builddir / 'solutions-base.tex',
                       config.tools_root / 'latex/solutions-base.tex')
    ensure_symlink(builddir / 'bapc.cls', config.tools_root / 'latex/bapc.cls')
    ensure_symlink(builddir / 'images', config.tools_root / 'latex/images')
    ensure_symlink(builddir / main_file,
                   config.tools_root / 'latex' / main_file)

    config_data = util.read_yaml(Path('contest.yaml'))
    config_data['testsession'] = '\\testsession' if config_data.get(
        'testsession') else ''

    util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex',
                             builddir / 'contest_data.tex', config_data)

    ensure_symlink(builddir / 'logo.pdf', find_logo())

    problems_data = ''

    # Link the solve stats directory if it exists.
    solve_stats = Path('solve_stats')
    if solve_stats.exists():
        ensure_symlink(builddir / 'solve_stats', solve_stats)

    # include a header slide in the solutions PDF
    headertex = Path('solution_header.tex')
    if headertex.exists():
        ensure_symlink(builddir / 'solution_header.tex', headertex)
    if solutions and headertex.exists():
        problems_data += f'\\input{{{headertex}}}\n'

    per_problem_data = (config.tools_root / 'latex' /
                        f'contest-{build_type}.tex').read_text()

    # Some logic to prevent duplicate problem IDs.
    for problem in problems:
        prepare_problem(problem)
        id_ok = True

        problems_data += util.substitute(
            per_problem_data, {
                'problemlabel': problem.label,
                'problemyamlname': problem.config['name'],
                'problemauthor': problem.config.get('author'),
                'timelimit': get_tl(problem.config),
                'problemdir': config.tmpdir / problem.id,
            })

    # include a statistics slide in the solutions PDF
    footer_tex = Path('solution_footer.tex')
    if footer_tex.exists():
        ensure_symlink(builddir / 'solution_footer.tex', footer_tex)
    if solutions and footer_tex.exists():
        problems_data += f'\\input{{{footer_tex}}}\n'

    (builddir / f'contest-{build_type}s.tex').write_text(problems_data)

    for i in range(3):
        ok, err, out = util.exec_command(PDFLATEX + [
            '-output-directory', builddir,
            (builddir / main_file).with_suffix('.tex')
        ],
                                         0,
                                         False,
                                         cwd=builddir,
                                         stdout=subprocess.PIPE)
        if ok is not True:
            print(f'{_c.red}Failure compiling pdf:{_c.reset}\n{out}')
            return False

    # link the output pdf
    output_pdf = Path(main_file).with_suffix('.pdf')
    ensure_symlink(output_pdf, builddir / output_pdf, True)

    print(f'{_c.green}Pdf written to {output_pdf}{_c.reset}')
    return True
示例#5
0
def build_contest_pdf(contest, problems, tmpdir, solutions=False, web=False):
    builddir = tmpdir / contest
    builddir.mkdir(parents=True, exist_ok=True)
    build_type = 'solution' if solutions else 'problem'

    main_file = 'solutions' if solutions else 'contest'
    main_file += '-web.tex' if web else '.tex'

    default_config_data = {
        'title': 'TITLE',
        'subtitle': '',
        'year': 'YEAR',
        'author': 'AUTHOR',
        'testsession': '',
        'blank_page_text': '',
    }
    config_data = contest_yaml()
    for x in default_config_data:
        if x not in config_data:
            config_data[x] = default_config_data[x]
    config_data['testsession'] = '\\testsession' if config_data.get(
        'testsession') else ''
    config_data['logofile'] = find_logo().as_posix()

    util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex',
                             builddir / 'contest_data.tex', config_data)

    problems_data = ''

    if solutions:
        # include a header slide in the solutions PDF
        headertex = Path('solution_header.tex')
        if headertex.exists():
            problems_data += f'\\input{{{headertex}}}\n'

    per_problem_data = (config.tools_root / 'latex' /
                        f'contest-{build_type}.tex').read_text()

    for problem in problems:
        if build_type == 'problem':
            prepare_problem(problem)

        if solutions:
            if not (problem.path / 'problem_statement/solution.tex').is_file():
                warn(f'solution.tex not found for problem {problem.name}')
                continue

        problems_data += util.substitute(
            per_problem_data,
            {
                'problemlabel': problem.label,
                'problemyamlname': problem.settings.name.replace('_', ' '),
                'problemauthor': problem.settings.author,
                'timelimit': get_tl(problem),
                'problemdir': problem.path.absolute().as_posix(),
                'builddir': problem.tmpdir.as_posix(),
            },
        )

    if solutions:
        # include a statistics slide in the solutions PDF
        footer_tex = Path('solution_footer.tex')
        if footer_tex.exists():
            problems_data += f'\\input{{{footer_tex}}}\n'

    (builddir / f'contest-{build_type}s.tex').write_text(problems_data)

    return build_latex_pdf(builddir, config.tools_root / 'latex' / main_file)
示例#6
0
def build_contest_pdf(contest, problems, tmpdir, solutions=False, web=False):
    builddir = tmpdir / contest
    builddir.mkdir(parents=True, exist_ok=True)
    build_type = 'solution' if solutions else 'problem'

    main_file = 'solutions' if solutions else 'contest'
    main_file += '-web.tex' if web else '.tex'

    config_data = util.read_yaml(Path('contest.yaml'))
    config_data['testsession'] = '\\testsession' if config_data.get('testsession') else ''
    config_data['logofile'] = find_logo().as_posix()

    util.copy_and_substitute(config.tools_root / 'latex/contest-data.tex',
                             builddir / 'contest_data.tex', config_data)

    problems_data = ''

    if solutions:
        # Link the solve stats directory if it exists.
        solve_stats = Path('solve_stats')

        # include a header slide in the solutions PDF
        headertex = Path('solution_header.tex')
        if headertex.exists():
            problems_data += f'\\input{{{headertex}}}\n'

    per_problem_data = (config.tools_root / 'latex' / f'contest-{build_type}.tex').read_text()

    # Some logic to prevent duplicate problem IDs.
    for problem in problems:
        if build_type == 'problem':
            prepare_problem(problem)

        problems_data += util.substitute(
            per_problem_data, {
                'problemlabel': problem.label,
                'problemyamlname': problem.settings.name.replace('_', ' '),
                'problemauthor': problem.settings.author,
                'timelimit': problem.settings.timelimit,
                'problemdir': problem.path.absolute().as_posix(),
                'builddir': problem.tmpdir.as_posix(),
            })

    if solutions:
        # include a statistics slide in the solutions PDF
        footer_tex = Path('solution_footer.tex')
        if footer_tex.exists():
            problems_data += f'\\input{{{footer_tex}}}\n'

    (builddir / f'contest-{build_type}s.tex').write_text(problems_data)

    env = get_environment()
    for i in range(3):
        ret = util.exec_command(
            PDFLATEX + ['-output-directory', builddir,
                        config.tools_root / 'latex' / main_file],
            0,
            False,
            cwd=builddir,
            stdout=subprocess.PIPE,
            env=env,
            )
        if ret.ok is not True:
            print(f'{Fore.RED}Failure compiling pdf:{Style.RESET_ALL}\n{ret.out}')
            return False

    # link the output pdf
    output_pdf = Path(main_file).with_suffix('.pdf')
    ensure_symlink(output_pdf, builddir / output_pdf, True)

    print(f'{Fore.GREEN}Pdf written to {output_pdf}{Style.RESET_ALL}')
    return True