diff --git a/build_frontmatter.py b/build_frontmatter.py index f36eee9..de2a4d6 100644 --- a/build_frontmatter.py +++ b/build_frontmatter.py @@ -1,7 +1,7 @@ #!/usr/bin/python3 # -*- coding: utf-8; mode: python -*- -__version__="0.1" -__date__="20170206" +__version__="1.0" +__date__="20170208" __author__ = "kthoden@mpiwg-berlin.mpg.de" __doc__ = """Generates LaTeX code for informative frontmatters. These will be attached to the chapter PDFs of EOA publications we offer for @@ -17,6 +17,7 @@ # using https://wiki.postgresql.org/wiki/Psycopg2_Tutorial PUBLICATION_PATTERN = re.compile("(?Pstudies|sources|proceedings|textbooks)(?P[1-9]?[0-9]{1})$") +MEDIA_DIR = "/home/editionopenaccess/eoa/website/website/media/" def print_error(message): """Print error message to stderr """ @@ -86,7 +87,7 @@ def get_publication_info(eoa_pub_id, eoa_cursor): eoa_cursor.execute(query_string) rows = eoa_cursor.fetchall() - + return(rows) # def get_publication_info ends here @@ -108,7 +109,7 @@ def get_chapters(eoa_pub_id, eoa_cursor): if len(row[7]) == 0: print_error("There seems to be no file attached to %s. Removing this chapter." % row[1]) - rows[:] = [row for row in rows if len(row[7]) > 0] + rows[:] = [row for row in rows if len(row[7]) > 0] return(rows) # get_chapters ends here @@ -134,12 +135,12 @@ def format_authors(result_list, start_range, end_range): elif len(authors) > 2: authors_as_string = """%s""" % authors[0] for author in range(1, len(authors) - 1): - authors_as_string += ", " + authors[author] + authors_as_string += ", " + authors[author] authors_as_string += " and %s" % (authors[-1]) return(authors_as_string) # def format_authors ends here - + def format_title(title_string): """Convert html tags to their LaTeX counterpart.""" @@ -230,6 +231,8 @@ def choose_geometry(eoa_series): def copy_command(thingy): """Create a shell script for copying the media files back""" + home/editionopenaccess/eoa/website/website/media + # write a command to copy the files back to e.g. /var/www-mpdrl/media/proceedings/4/9/Proc4ch8.pdf pass # def copy_command ends here @@ -269,7 +272,7 @@ def download_cover_image(image_url): """Download image from website. Code from - https://stackoverflow.com/questions/8286352/how-to-save-an-image-locally-using-python-whose-url-address-i-already-know + https://stackoverflow.com/questions/8286352 """ import urllib.request @@ -314,7 +317,7 @@ def main(eoa_publication): eoa_pub_id = get_publication_id(eoa_publication, eoa_cursor) eoa_publication_info = get_publication_info(eoa_pub_id, eoa_cursor) - print(eoa_publication_info) + # print(eoa_publication_info) base_url, publisher_string = which_publisher(eoa_publication_info[0][8]) @@ -334,15 +337,21 @@ def main(eoa_publication): os.chdir("./generated_files/") command_file = open(eoa_publication + "_copycommand.sh", "w") - for chapter in chapter_files: + command_file.write("#!/bin/bash\n") + list_of_auxfiles = [] + + for chapter in chapter_files: # get the original pdf file chapter_url = "%s/media/%s" % (base_url, chapter[7]) - command_file.write(chapter_url + "\n") + pdf_filename = chapter_url.split("/")[-1] pdffilename_front, pdffilename_ext = os.path.splitext(pdf_filename) - pdf_destination = pdffilename_front + "-orig" + pdffilename_ext - download_chapter_pdf(chapter_url, pdf_destination) + original_pdf_file = pdffilename_front + "-orig" + pdffilename_ext + + list_of_auxfiles.append(original_pdf_file) + + download_chapter_pdf(chapter_url, original_pdf_file) tex_filename = eoa_publication + "ch" + str(chapter[0]) + ".tex" @@ -369,16 +378,35 @@ def main(eoa_publication): ISBN_CODE = item_for_template[7], PUB_DATE = item_for_template[8], PUBLICATION_URL = item_for_template[9], - ORIGINAL_PDF = pdf_destination) + ORIGINAL_PDF = original_pdf_file) outfile.write(frontmatter_replacement) outfile.close() # generate PDF file - latex_command = "xelatex -jobname=%s %s" % (pdffilename_front, tex_filename) + print("Attaching a frontmatter to chapter '%s'" % formatted_chapter_title) + + latex_command = "xelatex --interaction=batchmode -jobname=%s %s" % (pdffilename_front, tex_filename) run_latex(latex_command) + command_file.write("cp %s%s %s%s.bak\n" % (MEDIA_DIR, chapter[7], MEDIA_DIR, chapter[7])) + command_file.write("cp %s %s%s\n" % (pdf_filename, MEDIA_DIR, chapter[7])) + # back to normal + command_file.close() + + print("Removing aux files.") + + for auxfile in os.listdir("."): + if re.search(r'.*\.(aux|log|tex)', auxfile): + list_of_auxfiles.append(auxfile) + + for file_for_deletion in list_of_auxfiles: + os.unlink(file_for_deletion) + + print("Removing other files.") + os.unlink("Coverimage.jpg") + os.chdir("..") # def main ends here