Skip to content

Commit 719cd48

Browse files
authored
Keep only scanned dependencies if they already exist and release v0.0.10. (#16)
1 parent 013108a commit 719cd48

File tree

14 files changed

+111
-23
lines changed

14 files changed

+111
-23
lines changed

CHANGES.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@ all releases are available on `Anaconda.org
77
<https://anaconda.org/pytask/pytask-latex>`_.
88

99

10+
0.0.10 - 2021-01-16
11+
-------------------
12+
13+
- :gh:`16` fixes the scanner by keeping only scanned dependencies which exist. Convert
14+
args to strings.
15+
16+
1017
0.0.9 - 2020-12-28
1118
------------------
1219

README.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
.. image:: https://codecov.io/gh/pytask-dev/pytask-latex/branch/main/graph/badge.svg
1111
:target: https://codecov.io/gh/pytask-dev/pytask-latex
1212

13+
.. image:: https://results.pre-commit.ci/badge/github/pytask-dev/pytask-latex/main.svg
14+
:target: https://results.pre-commit.ci/latest/github/pytask-dev/pytask-latex/main
15+
:alt: pre-commit.ci status
16+
1317
.. image:: https://img.shields.io/badge/code%20style-black-000000.svg
1418
:target: https://github.com/psf/black
1519

@@ -134,7 +138,7 @@ For example, to compile your document with XeLaTeX, use
134138
def task_compile_latex_document():
135139
pass
136140
137-
The options ``jobname``, ``output-directory`` and the ``.tex`` file which will be
141+
The options ``--jobname``, ``--output-directory`` and the ``.tex`` file which will be
138142
compiled are automatically handled and inferred from the ``@pytask.mark.depends_on`` and
139143
``@pytask.mark.produces`` markers.
140144

@@ -180,11 +184,11 @@ to include the latex decorator in the parametrization just like with
180184
[
181185
(
182186
"document.pdf",
183-
(["--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd"],),
187+
("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd"),
184188
),
185189
(
186190
"document.dvi",
187-
(["--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd"],),
191+
("--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd"),
188192
),
189193
],
190194
)
@@ -216,12 +220,14 @@ infer_latex_dependencies
216220
`latex-dependency-scanner <https://github.com/pytask-dev/latex-dependency-scanner>`_
217221
if the following configuration value is true which is also the default.
218222

223+
.. code-block:: ini
224+
219225
infer_latex_dependencies = true
220226
221227
Since the package is in its early development phase and LaTeX provides a myriad of
222228
ways to include files as well as providing shortcuts for paths (e.g.,
223229
``\graphicspath``), there are definitely some rough edges left. File an issue here
224-
or in the other project to make us aware of the problem.
230+
or in the other project in case of a problem.
225231

226232

227233
Changes

environment.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,4 @@ dependencies:
2525
- pre-commit
2626
- pytest-cov
2727
- pytest-xdist
28+
- virtualenv=20.0.33

setup.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[bumpversion]
2-
current_version = 0.0.9
2+
current_version = 0.0.10
33
parse = (?P<major>\d+)\.(?P<minor>\d+)(\.(?P<patch>\d+))(\-?((dev)?(?P<dev>\d+))?)
44
serialize =
55
{major}.{minor}.{patch}dev{dev}

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
setup(
55
name="pytask-latex",
6-
version="0.0.9",
6+
version="0.0.10",
77
packages=find_packages(where="src"),
88
package_dir={"": "src"},
99
entry_points={"pytask": ["pytask_latex = pytask_latex.plugin"]},

src/pytask_latex/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "0.0.9"
1+
__version__ = "0.0.10"

src/pytask_latex/collect.py

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Iterable
88
from typing import Optional
9+
from typing import Sequence
910
from typing import Union
1011

1112
from _pytask.config import hookimpl
@@ -33,14 +34,15 @@ def latex(options: Optional[Union[str, Iterable[str]]] = None):
3334
"""
3435
if options is None:
3536
options = DEFAULT_OPTIONS.copy()
36-
elif isinstance(options, str):
37-
options = [options]
38-
37+
else:
38+
options = _to_list(options)
39+
options = [str(i) for i in options]
3940
return options
4041

4142

4243
def compile_latex_document(latex):
4344
"""Replaces the dummy function provided by the user."""
45+
print("Executing " + " ".join(latex) + ".") # noqa: T001
4446
subprocess.run(latex, check=True)
4547

4648

@@ -101,9 +103,7 @@ def pytask_collect_task_teardown(session, task):
101103

102104

103105
def _get_node_from_dictionary(obj, key, fallback=0):
104-
if isinstance(obj, Path):
105-
pass
106-
elif isinstance(obj, dict):
106+
if isinstance(obj, dict):
107107
obj = obj.get(key) or obj.get(fallback)
108108
return obj
109109

@@ -135,18 +135,22 @@ def _add_latex_dependencies_retroactively(task, session):
135135
# Scan the LaTeX document for included files.
136136
latex_dependencies = set(scan(source.path))
137137

138-
# Remove duplicated dependencies which have already been added by the user.
138+
# Remove duplicated dependencies which have already been added by the user and those
139+
# which do not exist.
139140
existing_paths = {
140141
i.path for i in task.depends_on.values() if isinstance(i, FilePathNode)
141142
}
142-
new_dependencies = latex_dependencies - existing_paths
143+
new_deps = latex_dependencies - existing_paths
144+
new_existing_deps = {i for i in new_deps if i.exists()}
143145

146+
# Put scanned dependencies in a dictionary with incrementing keys.
144147
used_integer_keys = [i for i in task.depends_on if isinstance(i, int)]
145148
max_int = max(used_integer_keys) if used_integer_keys else 0
149+
new_existing_deps = dict(enumerate(new_existing_deps, max_int + 1))
146150

147-
new_dependencies = dict(enumerate(new_dependencies, max_int + 1))
151+
# Collect new dependencies and add them to the task.
148152
collected_dependencies = _collect_nodes(
149-
session, task.path, task.name, new_dependencies
153+
session, task.path, task.name, new_existing_deps
150154
)
151155
task.depends_on = {**task.depends_on, **collected_dependencies}
152156

@@ -211,3 +215,29 @@ def _prepare_cmd_options(session, task, args):
211215
latex_document.as_posix(),
212216
]
213217
)
218+
219+
220+
def _to_list(scalar_or_iter):
221+
"""Convert scalars and iterables to list.
222+
223+
Parameters
224+
----------
225+
scalar_or_iter : str or list
226+
227+
Returns
228+
-------
229+
list
230+
231+
Examples
232+
--------
233+
>>> _to_list("a")
234+
['a']
235+
>>> _to_list(["b"])
236+
['b']
237+
238+
"""
239+
return (
240+
[scalar_or_iter]
241+
if isinstance(scalar_or_iter, str) or not isinstance(scalar_or_iter, Sequence)
242+
else list(scalar_or_iter)
243+
)

src/pytask_latex/parametrize.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ def pytask_parametrize_kwarg_to_marker(obj, kwargs):
88
"""Register kwargs as latex marker."""
99
if callable(obj):
1010
if "latex" in kwargs:
11-
mark.latex(*kwargs.pop("latex"))(obj)
11+
mark.latex(kwargs.pop("latex"))(obj)

tests/conftest.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,15 @@
22
import os
33
import shutil
44
import sys
5+
from pathlib import Path
56

67
import pytest
78
from click.testing import CliRunner
89

10+
11+
TEST_RESOURCES = Path(__file__).parent / "resources"
12+
13+
914
needs_latexmk = pytest.mark.skipif(
1015
shutil.which("latexmk") is None, reason="latexmk needs to be installed."
1116
)

tests/resources/image.png

205 Bytes
Loading

tests/test_execute.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from _pytask.mark import Mark
77
from conftest import needs_latexmk
88
from conftest import skip_on_github_actions_with_win
9+
from conftest import TEST_RESOURCES
910
from pytask import cli
1011
from pytask import main
1112
from pytask_latex.execute import pytask_execute_task_setup
@@ -384,7 +385,7 @@ def task_compile_document():
384385
@pytest.mark.end_to_end
385386
def test_compile_document_w_wrong_flag(tmp_path):
386387
"""Test that wrong flags raise errors."""
387-
tmp_path.joinpath("sub", "resources").mkdir(parents=True)
388+
tmp_path.joinpath("sub").mkdir(parents=True)
388389

389390
task_source = """
390391
import pytask
@@ -411,3 +412,40 @@ def task_compile_document():
411412
assert session.exit_code == 1
412413
assert len(session.tasks) == 1
413414
assert isinstance(session.execution_reports[0].exc_info[1], CalledProcessError)
415+
416+
417+
@needs_latexmk
418+
@pytest.mark.end_to_end
419+
def test_compile_document_w_image(runner, tmp_path):
420+
task_source = f"""
421+
import pytask
422+
import shutil
423+
424+
@pytask.mark.produces("image.png")
425+
def task_create_image():
426+
shutil.copy(
427+
"{TEST_RESOURCES.joinpath("image.png").as_posix()}",
428+
"{tmp_path.joinpath("image.png").as_posix()}"
429+
)
430+
431+
@pytask.mark.latex
432+
@pytask.mark.depends_on("document.tex")
433+
@pytask.mark.produces("document.pdf")
434+
def task_compile_document():
435+
pass
436+
437+
"""
438+
tmp_path.joinpath("task_dummy.py").write_text(textwrap.dedent(task_source))
439+
440+
latex_source = r"""
441+
\documentclass{report}
442+
\usepackage{graphicx}
443+
\begin{document}
444+
\includegraphics{image}
445+
\end{document}
446+
"""
447+
tmp_path.joinpath("document.tex").write_text(textwrap.dedent(latex_source))
448+
449+
result = runner.invoke(cli, [tmp_path.as_posix()])
450+
451+
assert result.exit_code == 0

tests/test_parallel.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,11 @@ def test_parallel_parametrization_over_source_file(runner, tmp_path):
8484
[
8585
(
8686
"document.pdf",
87-
(["--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd"],)
87+
("--pdf", "--interaction=nonstopmode", "--synctex=1", "--cd")
8888
),
8989
(
9090
"document.dvi",
91-
(["--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd"],)
91+
("--dvi", "--interaction=nonstopmode", "--synctex=1", "--cd")
9292
),
9393
],
9494
)

tests/test_parametrize.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ def test_parametrizing_latex_options(tmp_path):
7676
(
7777
"document.tex",
7878
"document.pdf",
79-
(["--interaction=nonstopmode", "--pdf", "--cd"],)
79+
("--interaction=nonstopmode", "--pdf", "--cd")
8080
),
8181
(
8282
"document.tex",
8383
"document.dvi",
84-
(["--interaction=nonstopmode", "--dvi", "--cd"],)
84+
("--interaction=nonstopmode", "--dvi", "--cd")
8585
),
8686
])
8787
def task_compile_latex_document():

tox.ini

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ addopts = --doctest-modules
4747
filterwarnings =
4848
ignore: the imp module is deprecated in favour of importlib
4949
ignore: Using or importing the ABCs from 'collections'
50+
ignore: The (parser|symbol) module is deprecated and will be removed in future
5051
junit_family = xunit2
5152
markers =
5253
wip: Tests that are work-in-progress.

0 commit comments

Comments
 (0)