Unless it is imperative that the script is run while LaTeX is running I would recommend just using make
to run LaTeX and you script.
I have used that approach to add word counting for articles and including statistics on bibliographic references.
Let your script generate a .tex
file and include that in you LaTeX source file.
Below is a snippet from one of my Makefiles:
TEX = /usr/texbin/pdflatex
PREVIEW = /usr/bin/open
REPORT = SimMon
REPORT_MASTER = $(REPORT).tex
TEX_OPTIONS = -halt-on-error
SimMon: $(REPORT_MASTER) countRefferedPages
$(TEX) $(TEX_OPTIONS) $(REPORT_MASTER)
@$(PREVIEW) $(REPORT).pdf
countRefferedPages: BibTeXPageCount
cat *.tex | support/BPC/build/Debug/BPC Castle.bib > litteraturTotal.tex