SHARE
TWEET

Untitled

a guest Jul 19th, 2019 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import subprocess as sp
  2. import argparse
  3. import os
  4. import shutil
  5. import glob
  6.  
  7. ap = argparse.ArgumentParser()
  8. ap.add_argument("--format", required=True, help="Manuscript diff output format.")
  9. ap.add_argument("--first_commit", required=True, help="First commit for `diff`.")
  10. ap.add_argument("--second_commit", required=False, help="Second commit for `diff` (can be '').")
  11.  
  12. args = vars(ap.parse_args())
  13. fmt = args['format']
  14. first_commit = args["first_commit"]
  15. if args["second_commit"]:
  16.     second_commit = args["second_commit"]
  17. else:
  18.     second_commit = "now"
  19.  
  20. def prepare():
  21.     if not os.path.exists("diff/content/"):
  22.         os.makedirs("diff/content")
  23.  
  24. def checkout_commit(commit, file):
  25.     """
  26.     Find the line numbers in a file (in the second commit) that are changed relative to the first commit,
  27.     by checking out two copies of the same file on-the-fly, then calling `diff` and capturing the output.
  28.     This will work best with semantic line feeds.
  29.     """
  30.     base = os.path.splitext(file)[0]
  31.     command = f"git show {commit}:{file} > diff/{base}-{commit[0:7]}.md"
  32.     print(f"Runing {command}")
  33.     sp.call(command, shell=True)
  34.  
  35. def build_latex(commit):
  36.     """
  37.     Write a slightly modified build script (based off `build.sh`) that will create
  38.     `manuscript_diff.html` and `manuscript_diff.pdf` in the output directory. In the future,
  39.     this I imagine this can be a flag, like we currently have for `$BUILD_DOCX` to automatically
  40.     create a manuscript `diff` when desired.
  41.     """
  42.     for file in ["citation-tags.tsv", "manual-references.json", "metadata.yaml"]:
  43.         shutil.copy(f"content/{file}", "diff/content")
  44.     build_script = f"""
  45. set -o errexit
  46.  
  47. # Set timezone used by Python for setting the manuscript's date
  48. export TZ=Etc/UTC
  49. # Default Python to read/write text files using UTF-8 encoding
  50. export LC_ALL=en_US.UTF-8
  51.  
  52. # Generate reference information
  53. echo "Retrieving and processing reference metadata"
  54. manubot \
  55.   --content-directory=diff/content/ \
  56.   --output-directory=diff/output \
  57.   --cache-directory=ci/cache \
  58.   --log-level=INFO
  59.  
  60. mv diff/output/manuscript.md diff/output/manuscript-{commit[0:7]}.md
  61. CSL_PATH=build/assets/journal-of-chemical-theory-and-computation.csl
  62. BIBLIOGRAPHY_PATH=diff/output/references.json
  63. INPUT_PATH=diff/output/manuscript-{commit[0:7]}.md
  64.  
  65. # Make output directory
  66. mkdir -p output
  67. cp -r content/images diff/content/images
  68.  
  69. FONT="Helvetica"
  70. COLORLINKS="true"
  71. pandoc \
  72.     --from=markdown \
  73.     --filter=pandoc-eqnos \
  74.     --filter=pandoc-tablenos \
  75.     --filter=pandoc-img-glob \
  76.     --filter=pandoc-chemfig \
  77.     --filter=pandoc-fignos \
  78.     --bibliography=$BIBLIOGRAPHY_PATH \
  79.     --csl=$CSL_PATH \
  80.     --template=build/assets/nih4.tex \
  81.     --metadata link-citations=true \
  82.     --number-sections \
  83.     --resource-path=.:content:../content \
  84.     --pdf-engine=xelatex \
  85.     --variable mainfont="${{FONT}}" \
  86.     --variable sansfont="${{FONT}}" \
  87.     --variable colorlinks="${{COLORLINKS}}" \
  88.     --output=diff/output/manuscript-{commit[0:7]}.tex \
  89.     $INPUT_PATH
  90. echo "Build complete"
  91. rm diff/content/*.md
  92. """
  93.     with open("build/build_diff.sh", "w") as f:
  94.         f.write(build_script)
  95.     sp.call("bash build/build_diff.sh", cwd=".", shell=True)
  96.  
  97.  
  98. if __name__ == "__main__":
  99.     prepare()
  100.     paths = sorted(glob.glob("content/[0-9]*.md"))
  101.     for commit in [first_commit, second_commit]:
  102.         for path in paths:
  103.             if commit != "now":
  104.                 checkout_commit(first_commit, path)
  105.             else:
  106.                 sp.call(f"cp {path} diff/{path}", shell=True)
  107.         build_latex(commit)
  108.     sp.call(f"latexdiff diff/output/manuscript-{first_commit[0:7]} diff/output/manuscript-{second_commit[0:7]} > diff/output/diff.tex", shell=True)
  109.     sp.call("xelatex diff/output/diff.tex", shell=True)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top