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
1 changed files with 45 additions and 55 deletions

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,22 +113,26 @@ 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)
# Remove extension if exists
if outfile.endswith('.pdf'):
outfile = outfile[:-4]
to_delete = False
if infile == 'auto':
os.makedirs(self.auto_tex_folder, exist_ok=True)
infile = '{}/{}.tex'.format(self.auto_tex_folder, outfile)
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)
infile = os.path.join(outdir, '.pyltk-{}.tex'.format(outfile))
to_create = True
new_content = self.content()
@ -164,12 +153,13 @@ class documentclass(element):
with open(infile, 'w') as infd:
infd.write(self.content())
call = ['pdflatex', '-halt-on-error',
'-output-directory', self.output_aux_folder,
'-output-directory', 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))
os.rename(
os.path.join(
output_aux_folder, outfile + '.pdf'),
os.path.join(outdir, outfile + '.pdf'))
res = CompileResult.SUCCESS
else:
res = CompileResult.ERROR