Skip to content

Commit

Permalink
Merge branch 'feature-latex-packages'.
Browse files Browse the repository at this point in the history
  • Loading branch information
badshah400 committed Mar 15, 2024
2 parents f2713b5 + 8eb27be commit 718316e
Show file tree
Hide file tree
Showing 11 changed files with 285 additions and 170 deletions.
73 changes: 40 additions & 33 deletions .github/workflows/test_linux.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# This workflow will install Python dependencies, run tests and lint with a variety of Python versions
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python
# This workflow will install Python dependencies, run tests and lint with a
# variety of Python versions. For more information see:
# https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-python

---
name: Python package

on:
push:
branches: [ "main", "feature-output-dir", "feature-bash-completion" ]
branches: [
"main",
"feature-bash-completion",
"feature-latex-packages",
"feature-output-dir",
]
pull_request:
branches: [ "main" ]
branches: ["main"]

jobs:
build:
Expand All @@ -19,32 +26,32 @@ jobs:
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12"]

steps:
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install hatch
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install TeX Live
uses: zauguin/install-texlive@v3
with:
packages: >
latex-bin latexmk scheme-basic makeindex imakeidx xkeyval
cache_version: 1
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with hatch run pytest
run: |
# TEMPDIR is needed tor tex compilation based tests
export TMPDIR=$(mktemp -d -p .)
hatch run pytest -v
rm -fr ${TMPDIR}
- uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v5
with:
python-version: ${{ matrix.python-version }}
- name: Install dependencies
run: |
python -m pip install --upgrade pip
python -m pip install flake8 pytest
python -m pip install hatch
if [ -f requirements.txt ]; then pip install -r requirements.txt; fi
- name: Install TeX Live
uses: zauguin/install-texlive@v3
with:
packages: >
latex-bin latexmk scheme-basic makeindex imakeidx xkeyval float
cache_version: 1
- name: Lint with flake8
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with hatch run pytest
run: |
# TEMPDIR is needed tor tex compilation based tests
export TMPDIR=$(mktemp -d -p .)
hatch run pytest -v
rm -fr ${TMPDIR}
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## [Unreleased]

### Added

- Option "-p/--packages": Save a `TeXPackages.json` file, listing system and
local latex packages used, inside output tarball.
- Rich formatting for printed messages for completion options.

## [0.4.1] 2024-02-29

### Fixed
Expand Down
37 changes: 19 additions & 18 deletions src/tartex/_completion.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
Module to help users with completion syntax for tartex
"""

import contextlib
import os
from pathlib import Path
from shutil import copy2
import os
from tartex.__about__ import __appname__ as APPNAME

from rich import print as richprint

from tartex.__about__ import __appname__ as APPNAME # noqa

COMPFILE = {
"bash": Path(f"bash-completion/completions/{APPNAME}"),
Expand All @@ -19,35 +23,30 @@ class Completion:

"""Methods for helping users print or install shell completion"""

def __init__(self, shell, filename):
def __init__(self, shell_name, filename):
"""Initialise"""
self.shell = shell
self.shell = shell_name
self.completion_file = Path(__file__).parent.joinpath("data", filename)
self.data = self.completion_file.read_text(encoding="utf-8")

install_root = Path(
os.getenv("XDG_DATA_HOME") or Path.home().joinpath(".local", "share")
os.getenv("XDG_DATA_HOME")
or Path.home().joinpath(".local", "share")
)
self.install_dir = install_root.joinpath(COMPFILE[self.shell]).parent
self.install_filename = COMPFILE[self.shell].name

def print(self):
"""Print completion to stdout"""
print(self.data, end="")

def install(self, install_dir=None):
"""Install completion to path"""
path = Path(install_dir or self.install_dir)
try:
with contextlib.suppress(FileExistsError):
os.makedirs(path)
except FileExistsError:
pass
inst_path = Path(
copy2(self.completion_file, path.joinpath(self.install_filename))
)
print(
f"Completion file for {self.shell} shell installed to"
f" {inst_path.parent}"
richprint(
f"Completion file for [bold]{self.shell}[/] shell installed to"
f" [link={inst_path.parent.as_uri()}]{inst_path.parent}[/]"
)


Expand All @@ -58,7 +57,7 @@ class BashCompletion(Completion):
def __init__(self):
"""Initialise"""
Completion.__init__(
self, shell="bash", filename="tartex-completion.bash"
self, shell_name="bash", filename="tartex-completion.bash"
)


Expand All @@ -68,7 +67,9 @@ class ZshCompletion(Completion):

def __init__(self):
"""Initialise"""
Completion.__init__(self, shell="zsh", filename="tartex-completion.zsh")
Completion.__init__(
self, shell_name="zsh", filename="tartex-completion.zsh"
)


class FishCompletion(Completion):
Expand All @@ -78,5 +79,5 @@ class FishCompletion(Completion):
def __init__(self):
"""Initialise"""
Completion.__init__(
self, shell="fish", filename="tartex-completion.fish"
self, shell_name="fish", filename="tartex-completion.fish"
)
35 changes: 30 additions & 5 deletions src/tartex/_latex.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@

# Match only lines beginning with INPUT
INPUT_RE = re.compile(r"^INPUT")
INPUT_STY = re.compile(r"^INPUT\s.*.(cls|def|sty)")
INPUT_FONTS = re.compile(r"^INPUT\s.*.(pfb|tfm)")
FONT_PUBLIC = re.compile(r"/public/.*/")


def run_latexmk(filename, mode, compdir):
Expand Down Expand Up @@ -69,10 +72,11 @@ def run_latexmk(filename, mode, compdir):
return fls_path


def fls_input_files(f, lof_excl, skip_files):
def fls_input_files(fls_fileobj, lof_excl, skip_files, *, sty_files=False):
"""Helper function to return list on files marked as 'INPUT' in fls file"""
deps = []
for line in f:
deps = set()
pkgs = {"System": set(), "Local": set()}
for line in fls_fileobj:
if INPUT_RE.match(line):
p = Path(line.split()[-1])
if (
Expand All @@ -81,7 +85,28 @@ def fls_input_files(f, lof_excl, skip_files):
and (p.as_posix() not in lof_excl)
and (p.suffix not in skip_files)
):
deps.append(p.as_posix())
deps.add(p.as_posix())
log.info("Add file: %s", p.as_posix())

return deps
if sty_files:
if INPUT_STY.match(line):
p = Path(line.split()[-1])
if p.is_absolute():
# Base is not a (La)TeX package; it is installed with even
# the most basic TeXlive/MikTeX installation
if (pdir := p.parent.name) != "base":
pkgs["System"].add(pdir)
else:
pkgs["Local"].add(p.stem)
elif INPUT_FONTS.match(line):
p = Path(line.split()[-1])
if p.is_absolute():
try:
fontdir = (
FONT_PUBLIC.search(str(p)).group(0).split("/")[2]
)
except AttributeError:
fontdir = p.parent.name
pkgs["System"].add(fontdir)

return list(deps), pkgs
Loading

0 comments on commit 718316e

Please sign in to comment.