From 8a7ae0406278b301692505d1fe87a4c7d540adc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pavel=20=C5=A0imerda?= Date: Thu, 12 Jan 2012 16:07:47 +0100 Subject: [PATCH] Don't regenerate PDFs that are newer than source data. --- lib/invoice/cli.py | 52 +++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/invoice/cli.py b/lib/invoice/cli.py index 78916e7..bcb0b28 100644 --- a/lib/invoice/cli.py +++ b/lib/invoice/cli.py @@ -119,10 +119,6 @@ class Application: issuer = self.db.companies[self.my_company] customer = self.db.companies[invoice.company_name] - invoice_data = invoice.data() - issuer_data = issuer.data() - customer_data = customer.data() - tmp_path = self.tmp_path.format(year=self.year) output_path = self.output_path.format(year=self.year) log.debug("tmp_path={}".format(tmp_path)) @@ -132,26 +128,34 @@ class Application: tmp_pdf_file = os.path.join(tmp_path, "{}.pdf".format(invoice._name)) pdf_file = os.path.join(output_path, "{}.pdf".format(invoice._name)) - log.debug("Invoice: {}".format(invoice_data._data)) - log.debug("Issuer: {}".format(issuer_data._data)) - log.debug("Customer: {}".format(customer_data._data)) - - log.debug("Creating TeX invoice...") - self._check_path(self.tmp_path) - result = tempita.Template(open(tex_template).read()).substitute( - invoice=invoice_data, issuer=issuer_data, customer=customer_data) - open(tex_file, "w").write(str(result)) - assert(os.path.exists(tex_file)) - - log.debug("Creating PDF invoice...") - if subprocess.call((self.tex_program, "{}.tex".format(invoice._name)), cwd=tmp_path) != 0: - raise GenerationError("PDF generation failed.") - assert(os.path.exists(tmp_pdf_file)) - - log.debug("Moving PDF file to the output directory...") - self._check_path(output_path) - os.rename(tmp_pdf_file, pdf_file) - assert(os.path.exists(pdf_file)) + if (not os.path.exists(pdf_file) or + os.path.getmtime(invoice._path) > os.path.getmtime(pdf_file)): + invoice_data = invoice.data() + issuer_data = issuer.data() + customer_data = customer.data() + + log.debug("Invoice: {}".format(invoice_data._data)) + log.debug("Issuer: {}".format(issuer_data._data)) + log.debug("Customer: {}".format(customer_data._data)) + + log.debug("Creating TeX invoice...") + self._check_path(self.tmp_path) + result = tempita.Template(open(tex_template).read()).substitute( + invoice=invoice_data, issuer=issuer_data, customer=customer_data) + open(tex_file, "w").write(str(result)) + assert(os.path.exists(tex_file)) + + log.debug("Creating PDF invoice...") + if subprocess.call((self.tex_program, "{}.tex".format(invoice._name)), cwd=tmp_path) != 0: + raise GenerationError("PDF generation failed.") + assert(os.path.exists(tmp_pdf_file)) + + log.debug("Moving PDF file to the output directory...") + self._check_path(output_path) + os.rename(tmp_pdf_file, pdf_file) + assert(os.path.exists(pdf_file)) + else: + log.info("PDF file is up to date.") log.debug("Running PDF viewer...") subprocess.call((self.pdf_program, pdf_file))