Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add doc command #111

Merged
merged 8 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/Arch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Prepare system
run: |
sysctl kernel.perf_event_paranoid=-1
pacman -Syu --noconfirm diffutils time gcc dpkg
pacman -Syu --noconfirm diffutils time gcc dpkg ghostscript texlive-latexextra
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/Ubuntu.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ jobs:
- /home/actions/oiejq:/github/home/.local/bin
env:
DEB_PYTHON_INSTALL_LAYOUT: deb
DEBIAN_FRONTEND: noninteractive
TZ: Europe/Warsaw
options:
--privileged
steps:
Expand All @@ -22,7 +24,7 @@ jobs:
- name: Prepare system
run: |
apt update
apt install -y sqlite3 sqlite3-doc build-essential dpkg
apt install -y sqlite3 sqlite3-doc build-essential dpkg texlive-latex-extra ghostscript
sysctl kernel.perf_event_paranoid=-1
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macOS.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
run: |
rm -f /usr/local/bin/2to3* /usr/local/bin/python3* /usr/local/bin/idle3* \
/usr/local/bin/pydoc3* # Homebrew will fail if these exist
brew install gnu-time coreutils diffutils dpkg
brew install gnu-time coreutils diffutils dpkg ghostscript texlive
- name: Install Python dependencies
run: |
python3 -m pip install .[tests]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ You can also specify your ingen source file which will be used. Run `sinol-make
- `sinol-make inwer` -- Verifies whether input files are correct using your "inwer.cpp" program. You can specify what inwer
program to use, what tests to check and how many CPUs to use. Run `sinol-make inwer --help` to see available flags.
- `sinol-make export` -- Creates archive ready to upload to sio2 or szkopul. Run `sinol-make export --help` to see all available flags.
- `sinol-make doc` -- Compiles all LaTeX files in doc/ directory to PDF. Run `sinol-make doc --help` to see all available flags.

### Reporting bugs and contributing code

Expand Down
71 changes: 71 additions & 0 deletions src/sinol_make/commands/doc/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import os
import glob
import argparse
import subprocess

from sinol_make import util
from sinol_make.interfaces.BaseCommand import BaseCommand


class Command(BaseCommand):
"""
Class for `doc` command.
"""

def get_name(self):
return "doc"

def compile_file(self, file_path):
print(util.info(f'Compiling {os.path.basename(file_path)}...'))
os.chdir(os.path.dirname(file_path))
subprocess.run(['latex', file_path])
dvi_file = os.path.splitext(file_path)[0] + '.dvi'
dvi_file_path = os.path.join(os.path.dirname(file_path), dvi_file)
if not os.path.exists(dvi_file_path):
print(util.error('Compilation failed.'))
return False

process = subprocess.run(['dvipdf', dvi_file_path])
if process.returncode != 0:
print(util.error('Compilation failed.'))
return False
print(util.info(f'Compilation successful for file {os.path.basename(file_path)}.'))
return True

def configure_subparser(self, subparser: argparse.ArgumentParser):
parser = subparser.add_parser(
self.get_name(),
help='Compile latex files to pdf',
description='Compiles latex files to pdf. By default compiles all files in the `doc` directory.\n'
'You can also specify files to compile.')
parser.add_argument('files', type=str, nargs='*', help='files to compile')

def run(self, args: argparse.Namespace):
util.exit_if_not_package()

if args.files == []:
self.files = glob.glob(os.path.join(os.getcwd(), 'doc', '*.tex'))
else:
self.files = []
for file in args.files:
if not os.path.exists(file):
print(util.warning(f'File {file} does not exist.'))
else:
self.files.append(os.path.abspath(file))
if self.files == []:
print(util.warning('No files to compile.'))
return

original_cwd = os.getcwd()
failed = []
for file in self.files:
if not self.compile_file(file):
failed.append(file)
os.chdir(original_cwd)

if failed:
for failed_file in failed:
print(util.error(f'Failed to compile {failed_file}'))
util.exit_with_error('Compilation failed.')
else:
print(util.info('Compilation was successful for all files.'))
11 changes: 11 additions & 0 deletions src/sinol_make/commands/export/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ def copy_package_required_files(self, target_dir: str):
print(util.warning(f'Coping {os.path.basename(test)}...'))
shutil.copy(test, os.path.join(target_dir, os.path.splitext(os.path.basename(test))[1]))

def clear_files(self, target_dir: str):
"""
Clears unnecessary files from target directory.
:param target_dir: Directory to clear files from.
"""
files_to_remove = ['doc/*~', 'doc/*.aux', 'doc/*.log', 'doc/*.dvi', 'doc/*.err', 'doc/*.inf']
for pattern in files_to_remove:
for f in glob.glob(os.path.join(target_dir, pattern)):
os.remove(f)

def create_makefile_in(self, target_dir: str, config: dict):
"""
Creates required `makefile.in` file.
Expand Down Expand Up @@ -143,6 +153,7 @@ def run(self, args: argparse.Namespace):

util.change_stack_size_to_unlimited()
self.copy_package_required_files(export_package_path)
self.clear_files(export_package_path)
self.create_makefile_in(export_package_path, config)
archive = self.compress(export_package_path)

Expand Down
32 changes: 32 additions & 0 deletions tests/commands/doc/test_integration.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import pytest

from sinol_make import configure_parsers
from sinol_make.commands.doc import Command
from tests.fixtures import create_package
from tests import util


@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True)
def test_simple(capsys, create_package):
"""
Test `doc` command with no parameters.
"""
parser = configure_parsers()
args = parser.parse_args(["doc"])
command = Command()
command.run(args)
out = capsys.readouterr().out
assert "Compilation was successful for all files." in out


@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True)
def test_argument(capsys, create_package):
"""
Test `doc` command with specified file.
"""
parser = configure_parsers()
args = parser.parse_args(["doc", "doc/doczad.tex"])
command = Command()
command.run(args)
out = capsys.readouterr().out
assert "Compilation was successful for all files." in out
12 changes: 12 additions & 0 deletions tests/commands/doc/test_unit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import os
import pytest

from sinol_make.commands.doc import Command
from tests.fixtures import create_package
from tests import util


@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True)
def test_compile_file(create_package):
command = Command()
assert command.compile_file(os.path.abspath(os.path.join(os.getcwd(), "doc/doczad.tex"))) is True
24 changes: 24 additions & 0 deletions tests/commands/export/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import tempfile

from sinol_make import configure_parsers
from sinol_make.commands.doc import Command as DocCommand
from tests import util
from tests.fixtures import create_package
from .util import *
Expand Down Expand Up @@ -56,3 +57,26 @@ def test_simple(create_package, capsys):
task_id = package_util.get_task_id()
out = capsys.readouterr().out
_test_archive(package_path, out, f'{task_id}.tgz')


@pytest.mark.parametrize("create_package", [util.get_doc_package_path()], indirect=True)
def test_doc_cleared(create_package):
"""
Test if files in `doc` directory are cleared.
"""
parser = configure_parsers()
args = parser.parse_args(["doc"])
command = DocCommand()
command.run(args)
args = parser.parse_args(["export"])
command = Command()
command.run(args)

with tempfile.TemporaryDirectory() as tmpdir:
with tarfile.open(f'{package_util.get_task_id()}.tgz', "r") as tar:
tar.extractall(tmpdir)

extracted = os.path.join(tmpdir, package_util.get_task_id())
assert os.path.exists(extracted)
for pattern in ['doc/*~', 'doc/*.aux', 'doc/*.log', 'doc/*.dvi', 'doc/*.err', 'doc/*.inf']:
assert glob.glob(os.path.join(extracted, pattern)) == []
3 changes: 3 additions & 0 deletions tests/packages/doc/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
title: Package for testing `doc` command
time_limit: 1000
memory_limit: 1024
4 changes: 4 additions & 0 deletions tests/packages/doc/doc/doczad.tex
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
\documentclass{article}
\begin{document}
Hello World!
\end{document}
Empty file added tests/packages/doc/in/.gitkeep
Empty file.
Empty file added tests/packages/doc/out/.gitkeep
Empty file.
7 changes: 7 additions & 0 deletions tests/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,13 @@ def get_override_limits_package_path():
return os.path.join(os.path.dirname(__file__), "packages", "ovl")


def get_doc_package_path():
"""
Get path to package for testing `doc` command (/test/packages/doc)
"""
return os.path.join(os.path.dirname(__file__), "packages", "doc")


def get_long_name_package_path():
"""
Get path to package with long name (/test/packages/long_package_name)
Expand Down