Generate files from the current directory instead of the output directory.

This commit is contained in:
Mikaël Capelle 2018-03-19 09:56:44 +01:00
parent 8a8cb4d177
commit 47944b3dd2

View File

@ -8,21 +8,6 @@ import os
import subprocess
class change_directory:
"""Context manager for changing the current working directory"""
def __init__(self, newPath):
self.newPath = os.path.expanduser(newPath)
def __enter__(self):
self.savedPath = os.getcwd()
os.chdir(self.newPath)
def __exit__(self, etype, value, traceback):
os.chdir(self.savedPath)
class CompileResult(enum.Enum):
ALREADY_EXISTS = 1
@ -48,7 +33,7 @@ class documentclass(element):
}
output_aux_folder = '.pyltk'
auto_tex_folder = '{}/tex2pdf'.format(output_aux_folder)
auto_tex_folder_name = 'tex2pdf'
def __init__(self, classname, childrens=[], options=[],
packages=[], preamble=[]):
@ -128,55 +113,60 @@ class documentclass(element):
res = CompileResult.ERROR
with change_directory(outdir):
output_aux_folder = os.path.join(outdir, self.output_aux_folder)
auto_tex_folder = os.path.join(output_aux_folder,
self.auto_tex_folder_name)
os.makedirs(self.output_aux_folder, exist_ok=True)
# Make aux directory
os.makedirs(output_aux_folder, exist_ok=True)
if outfile.endswith('.pdf'):
outfile = outfile[:-4]
# Remove extension if exists
if outfile.endswith('.pdf'):
outfile = outfile[:-4]
to_delete = False
to_delete = False
if infile == 'auto':
os.makedirs(self.auto_tex_folder, exist_ok=True)
infile = '{}/{}.tex'.format(self.auto_tex_folder, outfile)
if infile == 'auto':
os.makedirs(auto_tex_folder, exist_ok=True)
infile = '{}/{}.tex'.format(auto_tex_folder, outfile)
if infile is None:
to_delete = True
infile = '.pyltk-{}.tex'.format(outfile)
if infile is None:
to_delete = True
infile = os.path.join(outdir, '.pyltk-{}.tex'.format(outfile))
to_create = True
new_content = self.content()
to_create = True
new_content = self.content()
# If the input file already exists...
if os.path.exists(infile):
with open(infile, 'r') as infd:
old_content = infd.read()
# If the input file already exists...
if os.path.exists(infile):
with open(infile, 'r') as infd:
old_content = infd.read()
# Content has not changed
if old_content == new_content \
and os.path.exists('{}.pdf'.format(outfile)) \
and os.path.getctime(infile) <= \
os.path.getctime('{}.pdf'.format(outfile)):
to_create = False
# Content has not changed
if old_content == new_content \
and os.path.exists('{}.pdf'.format(outfile)) \
and os.path.getctime(infile) <= \
os.path.getctime('{}.pdf'.format(outfile)):
to_create = False
if to_create:
with open(infile, 'w') as infd:
infd.write(self.content())
call = ['pdflatex', '-halt-on-error',
'-output-directory', self.output_aux_folder,
'-jobname', outfile, infile]
if subprocess.call(call, stdout=outlog) == 0:
os.rename('{}/{}.pdf'.format(
self.output_aux_folder, outfile),
'{}.pdf'.format(outfile))
res = CompileResult.SUCCESS
else:
res = CompileResult.ERROR
if to_create:
with open(infile, 'w') as infd:
infd.write(self.content())
call = ['pdflatex', '-halt-on-error',
'-output-directory', output_aux_folder,
'-jobname', outfile, infile]
if subprocess.call(call, stdout=outlog) == 0:
os.rename(
os.path.join(
output_aux_folder, outfile + '.pdf'),
os.path.join(outdir, outfile + '.pdf'))
res = CompileResult.SUCCESS
else:
res = CompileResult.ALREADY_EXISTS
res = CompileResult.ERROR
else:
res = CompileResult.ALREADY_EXISTS
if to_delete:
os.remove(infile)
if to_delete:
os.remove(infile)
return res