#!/usr/bin/env python # Usage: ./build.py INPUT_DIR OUTPUT_FILE # import re import os import sys import subprocess def gx_usage(): """Show usage""" print(f"Usage: {sys.argv[0]} METADATA_FILE INPUT_FILE OUTPUT_FILE") print() def run_process_with_params(command_params): """ Runs a process with parameters provided in the command_params list. Args: - command_params (list): List of parameters to run the process. The first item in the list should be the command (executable). """ try: # Run the process using subprocess.run (Python 3.5+) result = subprocess.run(command_params, check=True, text=True, capture_output=True) # If the process ran successfully, print the output print("Process output:", result.stdout) print("Process error output (if any):", result.stderr) except subprocess.CalledProcessError as e: print(f"An error occurred while running the process: {e}") print("Error Output:", e.stderr) except Exception as e: print(f"Unexpected error: {e}") # Example usage if __name__ == "__main__": # Define the parameters for the command # Example: Running `ls -l /home` on a Unix-like system if len(sys.argv) != 4: gx_usage() metadata_file = sys.argv[1] if not os.path.exists(metadata_file): print(f"Metadata file not found: {metadata_file}") sys.exit(1) input_file = sys.argv[2] if not os.path.exists(input_file): print(f"Input file not found: {input_file}") sys.exit(1) # Get second output_file = sys.argv[3] if os.path.exists(output_file): print(f"Output file already exists: {output_file}") sys.exit(1) # Build the pandoc options as a string pandoc_cmd = [ "pandoc", "--verbose", "--toc", "--number-sections", "--include-in-header", "utils/docs/main.tex", "--metadata-file", metadata_file, # "-V", "linkcolor:blue", # "-V", "geometry:a4paper", # "-V", "geometry:margin=1.8cm", "-V", "mainfont=DejaVu Serif", "-V", "monofont=Noto Sans Mono", "--pdf-engine=xelatex", "--resource-path=utils/docs", "--filter=./utils/docs/filter-nobg.hs", "-f", "markdown", "-t", "pdf", "-o", output_file, input_file ] print(f"Metadata: {metadata_file}") print(f"Input: {input_file}") print(f"Output: {output_file}") # Call the function to run the process run_process_with_params(pandoc_cmd) #