Skip to content

Commit

Permalink
feat: Add force_hardlink_to() to utilities.py.
Browse files Browse the repository at this point in the history
  • Loading branch information
wxgeo committed Apr 1, 2024
1 parent b8f87eb commit ca1baae
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 9 deletions.
12 changes: 4 additions & 8 deletions ptyx/compilation.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ptyx.compilation_options import DEFAULT_OPTIONS, CompilationOptions
from ptyx.config import param
from ptyx.latex_generator import Compiler
from ptyx.utilities import force_hardlink_to

ANSI_RED = "\u001B[31m"
ANSI_REVERSE_RED = "\u001B[41m"
Expand Down Expand Up @@ -375,11 +376,6 @@ def make_files(
return all_compilation_info, compiler


def _force_hardlink_to(link: Path, target: Path) -> None:
link.unlink(missing_ok=True)
link.hardlink_to(target)


def _link_file_to_parent(
ext: str,
filenames: list[Path],
Expand All @@ -396,20 +392,20 @@ def _link_file_to_parent(
# There is only one file (only one document was generated,
# or they were several documents, but they were joined into a single document).
# shutil.copy(target, input_name.parent)
_force_hardlink_to(input_name.parent / target.name, target)
force_hardlink_to(input_name.parent / target.name, target)
elif options.names_list:
# Rename files according to the given names' list.
assert len(options.names_list) == len(filenames)
for filename, stem in zip(filenames, options.names_list):
new_name = filename.with_stem(stem).name
# shutil.copy(filename.with_suffix(ext), input_name.parent / new_name)
_force_hardlink_to(input_name.parent / new_name, filename.with_suffix(ext))
force_hardlink_to(input_name.parent / new_name, filename.with_suffix(ext))
else:
# Copy files without changing names.
for filename in filenames:
# shutil.copy(filename.with_suffix(ext), input_name.parent)
target = filename.with_suffix(ext)
_force_hardlink_to(input_name.parent / target.name, target)
force_hardlink_to(input_name.parent / target.name, target)


def generate_latex_file(
Expand Down
7 changes: 7 additions & 0 deletions ptyx/utilities.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import re
from math import ceil, floor, isnan, isinf
from pathlib import Path
from typing import Sequence, Any


Expand Down Expand Up @@ -267,3 +268,9 @@ def substitute(_: re.Match) -> str:
return substitutions.pop(0)

return re.sub(RE_VERBATIM_BLOCK, substitute, code, flags=re.DOTALL)


def force_hardlink_to(link: Path, target: Path) -> None:
"""Create a hardlink `link` to target `target`, even if `link` already exist."""
link.unlink(missing_ok=True)
link.hardlink_to(target)
2 changes: 1 addition & 1 deletion tests/test_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def test_PythonBlockError():

def test_PythonBlockError_full_pretty_report():
with pytest.raises(PythonBlockError) as exc_info:
# (Use case sensitive code, to prevent notably regressions
# (Use case-sensitive code, to prevent notably regressions
# for a previous bug concerning upper/lower case).
code = "\nl = []\nif len(L) > 1:\n print(l[1])"
try:
Expand Down

0 comments on commit ca1baae

Please sign in to comment.