Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
executable file 178 lines (164 sloc) 5.22 KB
#!/usr/bin/env python3
from utils.load_config import load_config, check_executable, exec_command, copy_dir_overwrite, copy_dir_keepaux
import utils.libeoaconvert as libeoaconvert
import argparse
from pathlib import Path
import logging
import os
import sys
import shutil
import pathlib
BASE_DIR = Path( __file__ ).resolve().parent
SCRIPT_PATH = Path( __file__ )
SCRIPT_NAME = SCRIPT_PATH.stem
DEFAULT_INPUT_DIR = \
Path(os.environ['INPUT_DIR'] if 'INPUT_DIR' in os.environ else './input')
DEFAULT_OUTPUT_DIR = \
Path(os.environ['OUTPUT_DIR'] if 'OUTPUT_DIR' in os.environ else './output')
def main(
input_file,
output_dir,
nobiber,
include
):
check_executable( "xelatex" )
if( not os.path.exists( output_dir ) ):
os.makedirs( output_dir )
input_file = Path( input_file )
input_dir = input_file.resolve().parent
input_path = Path( input_file )
if input_path.suffix == '':
input_path = input_path.with_suffix( ".tex" )
elif input_path.suffix != ".tex":
raise( Exception( "input file matching '*.tex' expected" ) )
output_dir = Path( output_dir )
fixed_file_path = output_dir / input_file.with_suffix( ".tex" ).name
libeoaconvert.enable_preamble(
input_file = input_path,
output_file = fixed_file_path,
pdf_or_xml = "pdf"
)
if include:
copy_dir_keepaux(
input_dir / "texfiles",
output_dir / "texfiles"
)
else:
copy_dir_overwrite(
input_dir / "texfiles",
output_dir / "texfiles"
)
copy_dir_overwrite(
input_dir / "preambel",
output_dir / "preambel"
)
if (input_dir / "inline").exists():
copy_dir_overwrite(
input_dir / "inline",
output_dir / "inline"
)
if (input_dir / "images").exists():
copy_dir_overwrite(
input_dir / "images",
output_dir / "images"
)
if (input_dir / "facsim").exists():
copy_dir_overwrite(
input_dir / "facsim",
output_dir / "facsim"
)
# copy bib file in current working dir, if any:
def copy_bib_file():
# find bib file
logging.debug( f"input_dir {input_dir}" )
logging.debug( f"output_dir {output_dir}" )
for f in (input_dir . glob( '*.bib' )):
shutil.copy(
f,
output_dir / f.name
)
copy_bib_file()
output_dir = output_dir.resolve()
cwd = Path.cwd()
os.chdir( output_dir )
logging.info( "cd {}".format( output_dir ) )
if nobiber:
logging.info("Skipping biber and running only two passes of XeLaTeX.")
pass
else:
exec_command(
f"xelatex --halt-on-error {input_file.name}",
)
exec_command(
"biber {}".format( input_file.stem ),
)
exec_command(
f"xelatex --halt-on-error {input_file.name}",
)
exec_command(
f"xelatex --halt-on-error {input_file.name}",
)
logging.info( "cd {}".format( cwd ) )
os.chdir( cwd )
if __name__ == '__main__':
parser = argparse.ArgumentParser(
formatter_class=argparse.ArgumentDefaultsHelpFormatter
)
parser.add_argument(
"-f", "--filename",
default = Path("*.tex"),
type = Path,
help = "xml file inside INPUT_DIR, or absolute path. Patterns like '*.xml' are also acceptable"
)
parser.add_argument(
"-o", "--output-dir",
type = Path,
help = f"output directory. default: {DEFAULT_OUTPUT_DIR}/INPUT_DIR/pdf"
)
parser.add_argument(
"-c", "--config",
default = BASE_DIR / "config" / "eoaconvert.cfg",
help="Name of config file"
)
parser.add_argument(
"--log-level",
default = "INFO",
help="log level: choose between DEBUG, INFO, WARNING, ERROR, CRITICAL"
)
parser.add_argument(
"--no-biber",
action="store_true",
default = False,
help="Run only two passes of XeLaTeX and no biber."
)
parser.add_argument(
"--include",
action="store_true",
default = False,
help="Use include rather than input and keep aux files in output directory."
)
parser.add_argument(
"INPUT_DIR",
help = "directory containing the publication (including resources like pictures, etc.)",
type = Path,
)
args = parser.parse_args()
input_dir = args.INPUT_DIR
input_file = \
args.filename if args.filename . is_absolute() else list(input_dir . glob( str(args.filename) ))[0]
# output_dir = args.output_dir
output_dir = \
args.output_dir if args.output_dir is not None else (DEFAULT_OUTPUT_DIR / input_dir.resolve().stem) / "pdf"
log_dir = output_dir / "log"
log_file = (log_dir / SCRIPT_NAME) . with_suffix( ".log" )
load_config(
args.config,
args.log_level,
log_file
)
main(
input_file = input_file,
output_dir = output_dir,
nobiber = args.no_biber,
include = args.include
)