Skip to content

Instantly share code, notes, and snippets.

@jopereira
Last active December 1, 2022 08:49
Show Gist options
  • Save jopereira/a78d06ee2eb72c4a823588a2333bb890 to your computer and use it in GitHub Desktop.
Save jopereira/a78d06ee2eb72c4a823588a2333bb890 to your computer and use it in GitHub Desktop.
Automatic texlive dependency downloader for rpm/dnf-based linux systems
#!/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