Last active
December 1, 2022 08:49
-
-
Save jopereira/a78d06ee2eb72c4a823588a2333bb890 to your computer and use it in GitHub Desktop.
Automatic texlive dependency downloader for rpm/dnf-based linux systems
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
# automatic texlive dependency downloader for rpm/dnf-based linux systems | |
from sys import argv | |
import re | |
import dnf | |
def map2Pkg(*files): | |
pkgs = [] | |
print("Updating repository metadata... (this can take a while)") | |
base = dnf.Base() | |
base.read_all_repos() | |
base.fill_sack() | |
for f in files: | |
q = base.sack.query() | |
r = q.filter(file__glob='*/'+f) | |
print("Searching:",f) | |
if not r: | |
print(" not found") | |
else: | |
for pkg in r: | |
if pkg.installed: | |
print(" installed in",pkg) | |
break | |
else: | |
sel = r[0] | |
for pkg in r: | |
print(" found in", pkg) | |
if str(pkg).startswith('texlive-') and not str(sel).startswith('texlive-'): | |
sel = pkg | |
print(" installing from", sel) | |
pkgs.append(str(sel)) | |
return pkgs | |
e1 = r"\\usepackage(?:\[[^\]]*\])?\{([^\}]*)\}" | |
e2 = r"([a-zA-Z0-9]+)" | |
def findSty(*files): | |
stys = [] | |
for fn in files: | |
try: | |
f = open(fn) | |
for l in f.readlines(): | |
for use in re.findall(e3, l): | |
for sty in re.findall(e2, use): | |
stys.append(sty+'.cls') | |
for use in re.findall(e1, l): | |
for sty in re.findall(e2, use): | |
stys.append(sty+'.sty') | |
except IOError as e: | |
print(e) | |
return stys | |
e3 = r"\\documentclass(?:\[[^\]]*\])?\{([^\}]*)\}" | |
if len(argv)==1: | |
print("Usage: texdep texfiles...") | |
else: | |
texs = findSty(*argv[1:]) | |
if texs: | |
pkgs = map2Pkg(*texs) | |
if pkgs: | |
print("Execute this:\n") | |
print("sudo dnf install %s"%(' '.join(pkgs))) | |
else: | |
print("Nothing to do!") | |
else: | |
print("No styles used") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment