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 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): class CompileResult(enum.Enum):
ALREADY_EXISTS = 1 ALREADY_EXISTS = 1
@ -48,7 +33,7 @@ class documentclass(element):
} }
output_aux_folder = '.pyltk' output_aux_folder = '.pyltk'
auto_tex_folder = '{}/tex2pdf'.format(output_aux_folder) auto_tex_folder_name = 'tex2pdf'
def __init__(self, classname, childrens=[], options=[], def __init__(self, classname, childrens=[], options=[],
packages=[], preamble=[]): packages=[], preamble=[]):
@ -128,55 +113,60 @@ class documentclass(element):
res = CompileResult.ERROR 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'): # Remove extension if exists
outfile = outfile[:-4] if outfile.endswith('.pdf'):
outfile = outfile[:-4]
to_delete = False to_delete = False
if infile == 'auto': if infile == 'auto':
os.makedirs(self.auto_tex_folder, exist_ok=True) os.makedirs(auto_tex_folder, exist_ok=True)
infile = '{}/{}.tex'.format(self.auto_tex_folder, outfile) infile = '{}/{}.tex'.format(auto_tex_folder, outfile)
if infile is None: if infile is None:
to_delete = True to_delete = True
infile = '.pyltk-{}.tex'.format(outfile) infile = os.path.join(outdir, '.pyltk-{}.tex'.format(outfile))
to_create = True to_create = True
new_content = self.content() new_content = self.content()
# If the input file already exists... # If the input file already exists...
if os.path.exists(infile): if os.path.exists(infile):
with open(infile, 'r') as infd: with open(infile, 'r') as infd:
old_content = infd.read() old_content = infd.read()
# Content has not changed # Content has not changed
if old_content == new_content \ if old_content == new_content \
and os.path.exists('{}.pdf'.format(outfile)) \ and os.path.exists('{}.pdf'.format(outfile)) \
and os.path.getctime(infile) <= \ and os.path.getctime(infile) <= \
os.path.getctime('{}.pdf'.format(outfile)): os.path.getctime('{}.pdf'.format(outfile)):
to_create = False to_create = False
if to_create: if to_create:
with open(infile, 'w') as infd: with open(infile, 'w') as infd:
infd.write(self.content()) infd.write(self.content())
call = ['pdflatex', '-halt-on-error', call = ['pdflatex', '-halt-on-error',
'-output-directory', self.output_aux_folder, '-output-directory', output_aux_folder,
'-jobname', outfile, infile] '-jobname', outfile, infile]
if subprocess.call(call, stdout=outlog) == 0: if subprocess.call(call, stdout=outlog) == 0:
os.rename('{}/{}.pdf'.format( os.rename(
self.output_aux_folder, outfile), os.path.join(
'{}.pdf'.format(outfile)) output_aux_folder, outfile + '.pdf'),
res = CompileResult.SUCCESS os.path.join(outdir, outfile + '.pdf'))
else: res = CompileResult.SUCCESS
res = CompileResult.ERROR
else: else:
res = CompileResult.ALREADY_EXISTS res = CompileResult.ERROR
else:
res = CompileResult.ALREADY_EXISTS
if to_delete: if to_delete:
os.remove(infile) os.remove(infile)
return res return res